]> git.sur5r.net Git - minitube/commitdiff
Read timeout in NetworkAccess
authorFlavio <flavio@odisseo.local>
Sat, 29 Jan 2011 21:08:36 +0000 (22:08 +0100)
committerFlavio <flavio@odisseo.local>
Sat, 29 Jan 2011 21:08:36 +0000 (22:08 +0100)
src/networkaccess.cpp
src/networkaccess.h

index 36d4ae63570cec9af12add0b58c13e7c682b8808..259bd38153db77c9565ad7892af1ce50ed371d24 100644 (file)
@@ -16,6 +16,16 @@ const QString USER_AGENT = "Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en
 
 NetworkReply::NetworkReply(QNetworkReply *networkReply) : QObject(networkReply) {
     this->networkReply = networkReply;
+
+    // monitor downloadProgress to impl timeout
+    connect(networkReply, SIGNAL(downloadProgress(qint64,qint64)),
+            SLOT(downloadProgress(qint64,qint64)), Qt::AutoConnection);
+
+    readTimeoutTimer = new QTimer(this);
+    readTimeoutTimer->setInterval(5000);
+    readTimeoutTimer->setSingleShot(true);
+    connect(readTimeoutTimer, SIGNAL(timeout()), SLOT(readTimeout()));
+    readTimeoutTimer->start();
 }
 
 void NetworkReply::finished() {
@@ -34,6 +44,15 @@ void NetworkReply::finished() {
         // when the request is finished we'll invoke the target method
         connect(networkReply, SIGNAL(finished()), this, SLOT(finished()), Qt::AutoConnection);
 
+        // monitor downloadProgress to impl timeout
+        connect(networkReply, SIGNAL(downloadProgress(qint64,qint64)),
+                SLOT(downloadProgress(qint64,qint64)), Qt::AutoConnection);
+        readTimeoutTimer->start();
+
+        // error signal
+        connect(networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
+                SLOT(requestError(QNetworkReply::NetworkError)));
+
         return;
     }
 
@@ -50,10 +69,45 @@ void NetworkReply::finished() {
     networkReply->deleteLater();
 }
 
-void NetworkReply::requestError(QNetworkReply::NetworkError code) {
+void NetworkReply::requestError(QNetworkReply::NetworkError /* code */) {
     emit error(networkReply);
 }
 
+void NetworkReply::downloadProgress(qint64 bytesReceived, qint64 /* bytesTotal */) {
+    // qDebug() << "Downloading" << bytesReceived << bytesTotal;
+    if (bytesReceived > 0) {
+        readTimeoutTimer->stop();
+        disconnect(networkReply, SIGNAL(downloadProgress(qint64,qint64)),
+                   this, SLOT(downloadProgress(qint64,qint64)));
+    }
+}
+
+void NetworkReply::readTimeout() {
+    // qDebug() << "HTTP read timeout" << networkReply->url();
+    networkReply->disconnect();
+    networkReply->abort();
+
+    QNetworkReply *retryReply = The::http()->simpleGet(networkReply->url(), networkReply->operation());
+
+    setParent(retryReply);
+    networkReply->deleteLater();
+    networkReply = retryReply;
+
+    // when the request is finished we'll invoke the target method
+    connect(networkReply, SIGNAL(finished()), this, SLOT(finished()), Qt::AutoConnection);
+
+    // monitor downloadProgress to impl timeout
+    connect(networkReply, SIGNAL(downloadProgress(qint64,qint64)),
+            SLOT(downloadProgress(qint64,qint64)), Qt::AutoConnection);
+    readTimeoutTimer->start();
+
+    // error signal
+    connect(networkReply, SIGNAL(error(QNetworkReply::NetworkError)),
+            SLOT(requestError(QNetworkReply::NetworkError)));
+
+    // emit error(networkReply);
+}
+
 /* --- NetworkAccess --- */
 
 NetworkAccess::NetworkAccess( QObject* parent) : QObject( parent ) {}
@@ -61,6 +115,7 @@ NetworkAccess::NetworkAccess( QObject* parent) : QObject( parent ) {}
 QNetworkReply* NetworkAccess::manualGet(QNetworkRequest request, int operation) {
 
     QNetworkAccessManager *manager = The::networkAccessManager();
+    // manager->setCookieJar(new QNetworkCookieJar());
 
     QNetworkReply *networkReply;
     switch (operation) {
index 58e55e3b1caccb9d15130506d24bac3147340d89..0509ae6296c3711d29b2e99237ee0d37534c5a00 100644 (file)
@@ -18,6 +18,8 @@ public:
 public slots:
     void finished();
     void requestError(QNetworkReply::NetworkError);
+    void downloadProgress(qint64 bytesReceived, qint64 bytesTotal);
+    void readTimeout();
 
 signals:
     void data(QByteArray);
@@ -26,6 +28,7 @@ signals:
 
 private:
     QNetworkReply *networkReply;
+    QTimer *readTimeoutTimer;
 
 };