From ca436a9927d673183cae7d227c90d53ca29b1ba6 Mon Sep 17 00:00:00 2001 From: Sude Date: Fri, 1 Jul 2016 18:37:23 +0300 Subject: [PATCH] Add average download rate to progressInfo struct --- include/downloadinfo.h | 1 + src/downloader.cpp | 26 ++++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/include/downloadinfo.h b/include/downloadinfo.h index 7acbfac..6015f89 100644 --- a/include/downloadinfo.h +++ b/include/downloadinfo.h @@ -20,6 +20,7 @@ struct progressInfo curl_off_t dlnow; curl_off_t dltotal; double rate; + double rate_avg; }; class DownloadInfo diff --git a/src/downloader.cpp b/src/downloader.cpp index e4b573a..4696a23 100644 --- a/src/downloader.cpp +++ b/src/downloader.cpp @@ -3112,7 +3112,25 @@ void Downloader::processDownloadQueue(Config conf, const unsigned int& tid) curl_easy_getinfo(dlhandle, CURLINFO_RESPONSE_CODE, &response_code); } if (result == CURLE_OK || result == CURLE_RANGE_ERROR || (result == CURLE_HTTP_RETURNED_ERROR && response_code == 416)) - msgQueue.push(msg_prefix + "Finished download: " + filepath.filename().string()); + { + // Average download speed + std::ostringstream dlrate_avg; + std::string rate_unit; + progressInfo progress_info = vDownloadInfo[tid].getProgressInfo(); + if (progress_info.rate_avg > 1048576) // 1 MB + { + progress_info.rate_avg /= 1048576; + rate_unit = "MB/s"; + } + else + { + progress_info.rate_avg /= 1024; + rate_unit = "kB/s"; + } + dlrate_avg << std::setprecision(2) << std::fixed << progress_info.rate_avg << rate_unit; + + msgQueue.push(msg_prefix + "Finished download: " + filepath.filename().string() + " (@ " + dlrate_avg.str() + ")"); + } else { msgQueue.push(msg_prefix + "Finished download (" + static_cast(curl_easy_strerror(result)) + "): " + filepath.filename().string()); @@ -3166,7 +3184,7 @@ int Downloader::progressCallbackForThread(void *clientp, curl_off_t dltotal, cur // trying to get rate and setting to NaN if it fails if (CURLE_OK != curl_easy_getinfo(xferinfo->curlhandle, CURLINFO_SPEED_DOWNLOAD, &info.rate)) - info.rate = std::numeric_limits::quiet_NaN(); + info.rate_avg = std::numeric_limits::quiet_NaN(); // setting full dlwnow and dltotal if (xferinfo->offset > 0) @@ -3187,6 +3205,10 @@ int Downloader::progressCallbackForThread(void *clientp, curl_off_t dltotal, cur uintmax_t size_last = xferinfo->TimeAndSize.back().second; info.rate = (size_last - size_first) / static_cast((time_last - time_first)); } + else + { + info.rate = info.rate_avg; + } vDownloadInfo[xferinfo->tid].setProgressInfo(info); vDownloadInfo[xferinfo->tid].setStatus(DLSTATUS_RUNNING);