qint64 QNetworkReplyProto::size() const { QNetworkReply *item = qscriptvalue_cast<QNetworkReply*>(thisObject()); if (item) return item->size(); return 0; }
int Updater::checkManifest() { QUrl baseUrl(MANIFEST_URL); QNetworkAccessManager manager; QNetworkRequest request = getNetworkRequest(baseUrl); QNetworkReply *rep = manager.get(request); while(rep->error() == QNetworkReply::NoError && !rep->isFinished()) QCoreApplication::processEvents(); while(true) { while(!rep->isFinished()) QCoreApplication::processEvents(); if(rep->error() != QNetworkReply::NoError) return RES_CHECK_FAILED; QVariant redirect = rep->attribute(QNetworkRequest::RedirectionTargetAttribute); if(redirect.type() != QVariant::Url) break; // redirect baseUrl = baseUrl.resolved(redirect.toUrl()); request = getNetworkRequest(baseUrl); rep = manager.get(request); } if(rep->isFinished() && rep->size() != 0) { QString s; QString ver(VERSION); while(!rep->atEnd()) { s = rep->readLine(); QStringList parts = s.split(' ', QString::SkipEmptyParts); if(parts.size() < 3 || !ver.contains(parts[0])) continue; if(REVISION < parts[1].toInt()) return RES_UPDATE_AVAILABLE; else return RES_NO_UPDATE; } } return RES_NO_UPDATE; }
void tst_qhttpnetworkconnection::bigRemoteFile() { QNetworkAccessManager manager; qint64 size; QTime t; QNetworkRequest request(QUrl("http://nds1.nokia.com/files/support/global/phones/software/Nokia_Ovi_Suite_webinstaller.exe")); QNetworkReply* reply = manager.get(request); connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection); qDebug() << "Starting download"; t.start(); QTestEventLoop::instance().enterLoop(50); QVERIFY(!QTestEventLoop::instance().timeout()); size = reply->size(); delete reply; qDebug() << "Finished!" << endl; qDebug() << "Time:" << t.elapsed() << "msec"; qDebug() << "Bytes:" << size; qDebug() << "Speed:" << (size / 1024) / (t.elapsed() / 1000) << "KB/sec"; }
void tst_qhttpnetworkconnection::bigRemoteFile() { QNetworkAccessManager manager; qint64 size; QTime t; QNetworkRequest request(QUrl(QString::fromLatin1(urlC))); QNetworkReply* reply = manager.get(request); connect(reply, SIGNAL(finished()), &QTestEventLoop::instance(), SLOT(exitLoop()), Qt::QueuedConnection); qDebug() << "Starting download" << urlC; t.start(); QTestEventLoop::instance().enterLoop(50); QVERIFY(!QTestEventLoop::instance().timeout()); size = reply->size(); delete reply; qDebug() << "Finished!" << endl; qDebug() << "Time:" << t.elapsed() << "msec"; qDebug() << "Bytes:" << size; qDebug() << "Speed:" << (size / qreal(1024)) / (t.elapsed() / qreal(1000)) << "KB/sec"; }
void DownloadTask::onDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) { qDebug() << QString(__FUNCTION__) << " bytesReceived:" << bytesReceived << " , bytesTotal:" << bytesTotal; QNetworkReply *reply = reinterpret_cast<QNetworkReply*>(sender()); if (reply->error() != QNetworkReply::NoError) { qDebug() << QString(__FUNCTION__) << " " << __LINE__ << " error : " << reply->error(); return; } qint64 bytesWritten = 0; if (infoFile_.size() == 0) { size_ = reply->header(QNetworkRequest::ContentLengthHeader).toLongLong(); taskFile_.setFileName(taskFileFullName()); //if inf file doesnt exist, task file has to be truncated. if (!taskFile_.open(QFile::ReadWrite | QFile::Truncate)) { qDebug() << QString(__FUNCTION__) << " " << __LINE__ << " error : " << taskFile_.error(); return; } if (!taskFile_.resize(size_)) { qDebug() << QString(__FUNCTION__) << " " << __LINE__ << " error : " << taskFile_.error(); return; } bytesWritten = infoFile_.write((char*)&size_, sizeof(size_)); if (bytesWritten != sizeof(size_)) { qDebug() << QString(__FUNCTION__) << " " << __LINE__ << " error : " << infoFile_.error(); return; } bytesWritten = infoFile_.write((char*)&progress_, sizeof(progress_)); if (bytesWritten != sizeof(progress_)) { qDebug() << QString(__FUNCTION__) << " " << __LINE__ << " error : " << infoFile_.error(); return; } auto urlBytes = url_.toUtf8(); bytesWritten = infoFile_.write(urlBytes); if (bytesWritten != urlBytes.size()) { qDebug() << QString(__FUNCTION__) << " " << __LINE__ << " error : " << infoFile_.error(); return; } } if (!taskFile_.isOpen()) { taskFile_.setFileName(taskFileFullName()); if (!taskFile_.open(QFile::ReadWrite)) { qDebug() << QString(__FUNCTION__) << " " << __LINE__ << " error : " << taskFile_.error(); return; } } if (!taskFile_.seek(progress_)) { qDebug() << QString(__FUNCTION__) << " " << __LINE__ << " error : " << taskFile_.error(); return; } qint64 size = reply->size(); bytesWritten = taskFile_.write(reply->readAll()); if (bytesWritten != size) { qDebug() << QString(__FUNCTION__) << " " << __LINE__ << " error : " << taskFile_.error(); return; } progress_ += size; if (!infoFile_.seek(sizeof(size_))) { qDebug() << QString(__FUNCTION__) << " " << __LINE__ << " error : " << infoFile_.error(); return; } bytesWritten = infoFile_.write((char*)&progress_, sizeof(progress_)); if (bytesWritten != sizeof(progress_)) { qDebug() << QString(__FUNCTION__) << " " << __LINE__ << " error : " << infoFile_.error(); return; } speedTest_.downloadBytes_ += size; qint64 currentMsecs = QDateTime::currentMSecsSinceEpoch(); qint64 timeSpan = currentMsecs - speedTest_.lastMSecsSinceEpoch_; qint64 bytesPerSecond = 0; if (timeSpan > 50) //每50ms通知一次UI { bytesPerSecond = speedTest_.bytesPerSecond(currentMsecs); emit downloadProgress(progress_, size_, bytesPerSecond); qDebug() << "kbytes per second : " << bytesPerSecond / 1024.0f << " timespan : " << timeSpan; } if (state_ == Pause || state_ == Cancel) { reply->deleteLater(); if (state_ == Cancel) { infoFile_.remove(); taskFile_.remove(); } } }