packinfoseed2server.cpp 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "packinfoseed2server.h"
  2. #include <QTimer>
  3. #include <QJsonDocument>
  4. PackInfoSeed2Server::PackInfoSeed2Server(PackInfoManger * manger, QObject *parent, bool autoSync) : QObject(parent),
  5. _manger(manger),_autoSync(autoSync)
  6. {
  7. connect(&_requst,&HttpRequest::request,this,&PackInfoSeed2Server::_requst);
  8. _netErrorCount = 0;
  9. _oneSyncError = 0;
  10. log = PQ::PQLogManger::this_()->getLog("PackInfoSeed2Server");
  11. }
  12. void PackInfoSeed2Server::send(QSharedPointer<PackInfo> & info)
  13. {
  14. if(_manger == nullptr) return;
  15. _queue.append(info);
  16. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  17. }
  18. void PackInfoSeed2Server::doSend()
  19. {
  20. if(_current.isNull() && !_queue.isEmpty()){
  21. _current = _queue.first();
  22. // TODO:小宋, 发送 _current 到服务器
  23. }
  24. }
  25. void PackInfoSeed2Server::result(int code, const QJsonObject & body)
  26. {
  27. if(code != 200 || body.isEmpty()){
  28. _current.clear();
  29. QTimer::singleShot(500,this,&PackInfoSeed2Server::doSend);
  30. _netErrorCount ++;
  31. return;
  32. }
  33. auto error = body.value("code").toInt();
  34. auto message = body.value("message").toString();
  35. if(error == 200 || message.indexOf("已存在") >= 0){ // TODO: fix error
  36. _queue.dequeue();
  37. _manger->setPackInfoSync(_current->codeSingle(),_current->time(),_autoSync);
  38. _current.clear();
  39. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  40. _netErrorCount = 0;
  41. _oneSyncError = 0;
  42. } else {
  43. _oneSyncError ++;
  44. if(_oneSyncError > 3){
  45. pqWarning(log) << "sync error,and to skip: data : \n" << QJsonDocument(_current->toObject()).toJson() <<
  46. "\n error: \n " << QJsonDocument(body).toJson();
  47. _oneSyncError = 0;
  48. _queue.dequeue();
  49. }
  50. _current.clear();
  51. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  52. }
  53. }