| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859 |
- #include "packinfoseed2server.h"
- #include <QTimer>
- #include <QJsonDocument>
- PackInfoSeed2Server::PackInfoSeed2Server(PackInfoManger * manger, QObject *parent, bool autoSync) : QObject(parent),
- _manger(manger),_autoSync(autoSync)
- {
- connect(&_requst,&HttpRequest::request,this,&PackInfoSeed2Server::_requst);
- _netErrorCount = 0;
- _oneSyncError = 0;
- log = PQ::PQLogManger::this_()->getLog("PackInfoSeed2Server");
- }
- void PackInfoSeed2Server::send(QSharedPointer<PackInfo> & info)
- {
- if(_manger == nullptr) return;
- _queue.append(info);
- QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
- }
- void PackInfoSeed2Server::doSend()
- {
- if(_current.isNull() && !_queue.isEmpty()){
- _current = _queue.first();
- // TODO:小宋, 发送 _current 到服务器
- }
- }
- void PackInfoSeed2Server::result(int code, const QJsonObject & body)
- {
- if(code != 200 || body.isEmpty()){
- _current.clear();
- QTimer::singleShot(500,this,&PackInfoSeed2Server::doSend);
- _netErrorCount ++;
- return;
- }
- auto error = body.value("code").toInt();
- auto message = body.value("message").toString();
- if(error == 200 || message.indexOf("已存在") >= 0){ // TODO: fix error
- _queue.dequeue();
- _manger->setPackInfoSync(_current->codeSingle(),_current->time(),_autoSync);
- _current.clear();
- QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
- _netErrorCount = 0;
- _oneSyncError = 0;
- } else {
- _oneSyncError ++;
- if(_oneSyncError > 3){
- pqWarning(log) << "sync error,and to skip: data : \n" << QJsonDocument(_current->toObject()).toJson() <<
- "\n error: \n " << QJsonDocument(body).toJson();
- _oneSyncError = 0;
- _queue.dequeue();
- }
- _current.clear();
- QTimer::singleShot(20,this,&PackInfoSeed2Server::doSend);
- }
- }
|