From 0b70ccf9e90af5bd979deafd20e3c8ea1dbfa7d8 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Thu, 3 Nov 2022 13:29:48 +0300 Subject: [PATCH] Cache torrent handles of preloading magnets --- src/base/bittorrent/sessionimpl.cpp | 21 +++++++++++---------- src/base/bittorrent/sessionimpl.h | 2 +- 2 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index 6328760c7..2143fec14 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -2293,7 +2293,7 @@ bool SessionImpl::cancelDownloadMetadata(const TorrentID &id) if (downloadedMetadataIter == m_downloadedMetadata.end()) return false; - const lt::torrent_handle nativeHandle = m_nativeSession->find_torrent(id); + const lt::torrent_handle nativeHandle = downloadedMetadataIter.value(); #ifdef QBT_USES_LIBTORRENT2 const InfoHash infoHash {nativeHandle.info_hashes()}; if (infoHash.isHybrid()) @@ -2301,7 +2301,7 @@ bool SessionImpl::cancelDownloadMetadata(const TorrentID &id) // if magnet link was hybrid initially then it is indexed also by v1 info hash // so we need to remove both entries const auto altID = TorrentID::fromSHA1Hash(infoHash.v1()); - m_downloadedMetadata.remove((altID == *downloadedMetadataIter) ? id : altID); + m_downloadedMetadata.remove((altID == downloadedMetadataIter.key()) ? id : altID); } #endif m_downloadedMetadata.erase(downloadedMetadataIter); @@ -2360,8 +2360,8 @@ void SessionImpl::decreaseTorrentsQueuePos(const QVector &ids) torrentQueue.pop(); } - for (auto i = m_downloadedMetadata.cbegin(); i != m_downloadedMetadata.cend(); ++i) - torrentQueuePositionBottom(m_nativeSession->find_torrent(*i)); + for (const lt::torrent_handle &torrentHandle : asConst(m_downloadedMetadata)) + torrentQueuePositionBottom(torrentHandle); saveTorrentsQueue(); } @@ -2415,8 +2415,8 @@ void SessionImpl::bottomTorrentsQueuePos(const QVector &ids) torrentQueue.pop(); } - for (auto i = m_downloadedMetadata.cbegin(); i != m_downloadedMetadata.cend(); ++i) - torrentQueuePositionBottom(m_nativeSession->find_torrent(*i)); + for (const lt::torrent_handle &torrentHandle : asConst(m_downloadedMetadata)) + torrentQueuePositionBottom(torrentHandle); saveTorrentsQueue(); } @@ -2833,16 +2833,17 @@ bool SessionImpl::downloadMetadata(const MagnetUri &magnetUri) // Adding torrent to libtorrent session lt::error_code ec; - lt::torrent_handle h = m_nativeSession->add_torrent(p, ec); - if (ec) return false; + lt::torrent_handle torrentHandle = m_nativeSession->add_torrent(p, ec); + if (ec) + return false; // waiting for metadata... - m_downloadedMetadata.insert(id); + m_downloadedMetadata.insert(id, torrentHandle); if (infoHash.isHybrid()) { // index hybrid magnet links by both v1 and v2 info hashes const auto altID = TorrentID::fromSHA1Hash(infoHash.v1()); - m_downloadedMetadata.insert(altID); + m_downloadedMetadata.insert(altID, torrentHandle); } ++m_extraLimit; adjustLimits(); diff --git a/src/base/bittorrent/sessionimpl.h b/src/base/bittorrent/sessionimpl.h index d7a747f61..6dc42b42c 100644 --- a/src/base/bittorrent/sessionimpl.h +++ b/src/base/bittorrent/sessionimpl.h @@ -694,7 +694,7 @@ namespace BitTorrent ResumeDataStorage *m_resumeDataStorage = nullptr; FileSearcher *m_fileSearcher = nullptr; - QSet m_downloadedMetadata; + QHash m_downloadedMetadata; QHash m_torrents; QHash m_hybridTorrentsByAltID;