packinfoseed2server.cpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. //docs: http://doc.vanlai.net:3001/web/#/1?page_id=64
  24. }
  25. }
  26. void PackInfoSeed2Server::result(int code, const QJsonObject & body)
  27. {
  28. if(code != 200 || body.isEmpty()){
  29. _current.clear();
  30. QTimer::singleShot(500,this,&PackInfoSeed2Server::doSend);
  31. _netErrorCount ++;
  32. return;
  33. }
  34. auto error = body.value("code").toInt();
  35. auto message = body.value("message").toString();
  36. if(error == 200 || message.indexOf("已存在") >= 0){ // TODO: fix error
  37. _queue.dequeue();
  38. _manger->setPackInfoSync(_current->codeSingle(),_current->time(),_autoSync);
  39. _current.clear();
  40. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  41. _netErrorCount = 0;
  42. _oneSyncError = 0;
  43. } else {
  44. _oneSyncError ++;
  45. if(_oneSyncError > 3){
  46. pqWarning(log) << "sync error,and to skip: data : \n" << QJsonDocument(_current->toObject()).toJson() <<
  47. "\n error: \n " << QJsonDocument(body).toJson();
  48. _oneSyncError = 0;
  49. _queue.dequeue();
  50. }
  51. _current.clear();
  52. QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
  53. }
  54. }