Handling network timeout in Qt



  • When dealing with QNetworkReply, it is "prescribed":http://qt-project.org/forums/viewthread/11763 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":http://www.qtcentre.org/threads/37530-QNetworkAccessManager-only-6-requests-are-executed-in-parallel-for-one-host, 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



  • This has been an open question for a while now given this "bug report":https://bugreports.qt-project.org/browse/QTBUG-3443. It does not seem to be a priority.



  • Just an idea, but maybe subclass QNetworkAccessManager, reimplement createRequest function and start the timer there?
    There is also downloadProgress signal in QNetworkReply, but it will be only called if connection is already established to the server, so it's useless in your situation...


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.