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() {
// 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;
}
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 ) {}
QNetworkReply* NetworkAccess::manualGet(QNetworkRequest request, int operation) {
QNetworkAccessManager *manager = The::networkAccessManager();
+ // manager->setCookieJar(new QNetworkCookieJar());
QNetworkReply *networkReply;
switch (operation) {