Handling network timeout in Qt
Asked Answered
C

1

4

When dealing with QNetworkReply, it is prescribed to use timers to abort the connection.

Here is my current code:

void ImageDownloader::download(QString imgUrl){    
  this->timeoutTimer = new QTimer(this);
  this->timeoutTimer->setSingleShot(true);
  this->timeoutTimer->setInterval(15000);
  connect(this->timeoutTimer, SIGNAL(timeout()), this, SLOT(timeout()));

  QUrl requestUrl(imgUrl);
  QNetworkRequest nwRequest(requestUrl);
  this->imageNwReply = this->nam->get(nwRequest);
  connect(imageNwReply,SIGNAL(finished()),this,SLOT(imgDownloaded()));
  connect(imageNwReply, SIGNAL(downloadProgress(qint64,qint64)), this->timeoutTimer, SLOT(start()));
  this->timeoutTimer->start();
}

void ImageDownloader::timeout(){
  qDebug()<<__FUNCTION__<<" Forced timeout!";    
  this->imageNwReply->abort();
}

The confusion I am facing is when should I start the timer? At times I have to make around 50 concurrent Get requests from QNetworkAccessManager but since there is throttling for maximum concurrent connections, at times it happens that some of the requests get timed out even before they have been processed.

Is there a signal to know exactly when the processing for a request is started by QNeworkAccessManager so that I can start the corresponding timer only then?

One possible solution might be to implement a Queue of requests and have only the maximum possible connections to process but I am looking for a cleaner solution

Castrate answered 17/8, 2012 at 8:45 Comment(0)
C
5

There is an open bug/enhancement request for the issue. I got to know about this bug from Qt forum

Castrate answered 21/8, 2012 at 8:52 Comment(1)
Third year passing for such a basic functionality :| I feel huge urge to grumble about that. That's a sign that I should implement a patch and post it..Westleigh

© 2022 - 2024 — McMap. All rights reserved.