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