From ca897a8a3577dbe25d0c14264f342577b47ab693 Mon Sep 17 00:00:00 2001 From: Vladimir Golovnev Date: Thu, 1 Sep 2022 06:54:51 +0300 Subject: [PATCH] Cache download/upload limit values PR #17640. --- src/base/bittorrent/torrentimpl.cpp | 25 +++++++++++++++++++------ src/base/bittorrent/torrentimpl.h | 3 +++ 2 files changed, 22 insertions(+), 6 deletions(-) diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index fc8afc9c4..3c97e96c6 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -223,6 +223,13 @@ namespace outVector.resize(size, defaultValue); return outVector; } + + // This is an imitation of limit normalization performed by libtorrent itself. + // We need perform it to keep cached values in line with the ones used by libtorrent. + int cleanLimitValue(const int value) + { + return ((value < 0) || (value == std::numeric_limits::max())) ? 0 : value; + } } // TorrentImpl @@ -252,6 +259,8 @@ TorrentImpl::TorrentImpl(SessionImpl *session, lt::session *nativeSession , m_useAutoTMM(params.useAutoTMM) , m_isStopped(params.stopped) , m_ltAddTorrentParams(params.ltAddTorrentParams) + , m_downloadLimit(cleanLimitValue(m_ltAddTorrentParams.download_limit)) + , m_uploadLimit(cleanLimitValue(m_ltAddTorrentParams.upload_limit)) { if (m_ltAddTorrentParams.ti) { @@ -1208,12 +1217,12 @@ qlonglong TorrentImpl::timeSinceActivity() const int TorrentImpl::downloadLimit() const { - return m_nativeHandle.download_limit(); + return m_downloadLimit;; } int TorrentImpl::uploadLimit() const { - return m_nativeHandle.upload_limit(); + return m_uploadLimit; } bool TorrentImpl::superSeeding() const @@ -2206,19 +2215,23 @@ void TorrentImpl::setSeedingTimeLimit(int limit) void TorrentImpl::setUploadLimit(const int limit) { - if (limit == uploadLimit()) + const int cleanValue = cleanLimitValue(limit); + if (cleanValue == uploadLimit()) return; - m_nativeHandle.set_upload_limit(limit); + m_uploadLimit = cleanValue; + m_nativeHandle.set_upload_limit(m_uploadLimit); m_session->handleTorrentNeedSaveResumeData(this); } void TorrentImpl::setDownloadLimit(const int limit) { - if (limit == downloadLimit()) + const int cleanValue = cleanLimitValue(limit); + if (cleanValue == downloadLimit()) return; - m_nativeHandle.set_download_limit(limit); + m_downloadLimit = cleanValue; + m_nativeHandle.set_download_limit(m_downloadLimit); m_session->handleTorrentNeedSaveResumeData(this); } diff --git a/src/base/bittorrent/torrentimpl.h b/src/base/bittorrent/torrentimpl.h index 8cde42a99..7a8e69539 100644 --- a/src/base/bittorrent/torrentimpl.h +++ b/src/base/bittorrent/torrentimpl.h @@ -337,6 +337,9 @@ namespace BitTorrent lt::add_torrent_params m_ltAddTorrentParams; + int m_downloadLimit = 0; + int m_uploadLimit = 0; + mutable QBitArray m_pieces; }; }