This commit is contained in:
zhuzichu 2023-11-30 10:59:09 +08:00
parent 1b3344e9f9
commit 29749a5761

View File

@ -182,6 +182,7 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){
QNetworkAccessManager manager; QNetworkAccessManager manager;
manager.setTransferTimeout(params->getTimeout()); manager.setTransferTimeout(params->getTimeout());
QEventLoop loop; QEventLoop loop;
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();});
for (int i = 0; i < params->getRetry(); ++i) { for (int i = 0; i < params->getRetry(); ++i) {
QUrl url(params->_url); QUrl url(params->_url);
addQueryParam(&url,params->_queryMap); addQueryParam(&url,params->_queryMap);
@ -194,10 +195,25 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){
reply = nullptr; reply = nullptr;
return; return;
} }
QEventLoop loop; auto abortCallable = [&loop,reply,&i,params]{
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();}); if(reply){
connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop,reply](){reply->abort(),loop.quit();}); i = params->getRetry();
reply->abort();
}
};
QMetaObject::Connection conn_destoryed = {};
QMetaObject::Connection conn_quit = {};
if(params->_target){
conn_destoryed = connect(params->_target,&QObject::destroyed,&manager,abortCallable);
}
conn_quit = connect(qApp,&QGuiApplication::aboutToQuit,&manager, abortCallable);
loop.exec(); loop.exec();
if(conn_destoryed){
disconnect(conn_destoryed);
}
if(conn_quit){
disconnect(conn_quit);
}
QString response; QString response;
if(reply->isOpen()){ if(reply->isOpen()){
response = QString::fromUtf8(reply->readAll()); response = QString::fromUtf8(reply->readAll());
@ -234,15 +250,15 @@ void FluNetwork::handle(NetworkParams* params,NetworkCallable* c){
} }
void FluNetwork::handleDownload(NetworkParams* params,NetworkCallable* c){ void FluNetwork::handleDownload(NetworkParams* params,NetworkCallable* c){
QString cacheKey = params->buildCacheKey();
QPointer<NetworkCallable> callable(c); QPointer<NetworkCallable> callable(c);
QThreadPool::globalInstance()->start([=](){
if(!callable.isNull()){ if(!callable.isNull()){
callable->start(); callable->start();
} }
QString cacheKey = params->buildCacheKey();
QUrl url(params->_url); QUrl url(params->_url);
QNetworkAccessManager* manager = new QNetworkAccessManager(); QNetworkAccessManager manager;
QNetworkReply *reply = nullptr; manager.setTransferTimeout(params->getTimeout());
manager->setTransferTimeout(params->getTimeout());
addQueryParam(&url,params->_queryMap); addQueryParam(&url,params->_queryMap);
QNetworkRequest request(url); QNetworkRequest request(url);
addHeaders(&request,params->_headerMap); addHeaders(&request,params->_headerMap);
@ -291,16 +307,23 @@ void FluNetwork::handleDownload(NetworkParams* params,NetworkCallable* c){
return; return;
} }
} }
reply = manager->get(request); QEventLoop loop;
QNetworkReply *reply = manager.get(request);
destFile->setParent(reply); destFile->setParent(reply);
cacheFile->setParent(reply); cacheFile->setParent(reply);
if(params->_target){ auto abortCallable = [&loop,reply,params]{
connect(params->_target,&QObject::destroyed,this,[reply]{
if(reply){ if(reply){
reply->abort(); reply->abort();
} }
}); };
connect(&manager,&QNetworkAccessManager::finished,&manager,[&loop](QNetworkReply *reply){loop.quit();});
connect(qApp,&QGuiApplication::aboutToQuit,&manager, [&loop,reply](){reply->abort(),loop.quit();});
QMetaObject::Connection conn_destoryed = {};
QMetaObject::Connection conn_quit = {};
if(params->_target){
conn_destoryed = connect(params->_target,&QObject::destroyed,&manager,abortCallable);
} }
conn_quit = connect(qApp,&QGuiApplication::aboutToQuit,&manager, abortCallable);
connect(reply,&QNetworkReply::readyRead,reply,[reply,seek,destFile,cacheFile,callable]{ connect(reply,&QNetworkReply::readyRead,reply,[reply,seek,destFile,cacheFile,callable]{
if (!reply || !destFile || reply->error() != QNetworkReply::NoError) if (!reply || !destFile || reply->error() != QNetworkReply::NoError)
{ {
@ -323,10 +346,15 @@ void FluNetwork::handleDownload(NetworkParams* params,NetworkCallable* c){
callable->downloadProgress(destFile->size(),contentLength); callable->downloadProgress(destFile->size(),contentLength);
} }
}); });
connect(manager,&QNetworkAccessManager::finished,this,[params,manager,callable](QNetworkReply *reply){ loop.exec();
if(conn_destoryed){
disconnect(conn_destoryed);
}
if(conn_quit){
disconnect(conn_quit);
}
params->deleteLater(); params->deleteLater();
reply->deleteLater(); reply->deleteLater();
manager->deleteLater();
if(!callable.isNull()){ if(!callable.isNull()){
callable->finish(); callable->finish();
} }
@ -464,7 +492,7 @@ void FluNetwork::addQueryParam(QUrl* url,const QMap<QString, QVariant>& params){
FluNetwork::FluNetwork(QObject *parent): QObject{parent} FluNetwork::FluNetwork(QObject *parent): QObject{parent}
{ {
timeout(15000); timeout(5000);
retry(3); retry(3);
cacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation).append(QDir::separator()).append("network")); cacheDir(QStandardPaths::writableLocation(QStandardPaths::CacheLocation).append(QDir::separator()).append("network"));
} }