diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index 2143fec14..fee77216f 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -2832,21 +2832,8 @@ bool SessionImpl::downloadMetadata(const MagnetUri &magnetUri) #endif // Adding torrent to libtorrent session - lt::error_code ec; - lt::torrent_handle torrentHandle = m_nativeSession->add_torrent(p, ec); - if (ec) - return false; - - // waiting for metadata... - 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, torrentHandle); - } - ++m_extraLimit; - adjustLimits(); + m_nativeSession->async_add_torrent(p); + m_downloadedMetadata.insert(id, {}); return true; } @@ -5066,18 +5053,36 @@ void SessionImpl::handleAddTorrentAlerts(const std::vector &alerts) #else const InfoHash infoHash {(hasMetadata ? params.ti->info_hash() : params.info_hash)}; #endif - m_loadingTorrents.remove(TorrentID::fromInfoHash(infoHash)); + if (const auto loadingTorrentsIter = m_loadingTorrents.find(TorrentID::fromInfoHash(infoHash)) + ; loadingTorrentsIter != m_loadingTorrents.end()) + { + m_loadingTorrents.erase(loadingTorrentsIter); + } + else if (const auto downloadedMetadataIter = m_downloadedMetadata.find(TorrentID::fromInfoHash(infoHash)) + ; downloadedMetadataIter != m_downloadedMetadata.end()) + { + m_downloadedMetadata.erase(downloadedMetadataIter); + if (infoHash.isHybrid()) + { + // index hybrid magnet links by both v1 and v2 info hashes + const auto altID = TorrentID::fromSHA1Hash(infoHash.v1()); + m_downloadedMetadata.remove(altID); + } + } return; } + #ifdef QBT_USES_LIBTORRENT2 - const auto torrentID = TorrentID::fromInfoHash(alert->handle.info_hashes()); + const InfoHash infoHash {alert->handle.info_hashes()}; #else - const auto torrentID = TorrentID::fromInfoHash(alert->handle.info_hash()); + const InfoHash infoHash {alert->handle.info_hash()}; #endif - const auto loadingTorrentsIter = m_loadingTorrents.find(torrentID); - if (loadingTorrentsIter != m_loadingTorrents.end()) + const auto torrentID = TorrentID::fromInfoHash(infoHash); + + if (const auto loadingTorrentsIter = m_loadingTorrents.find(torrentID) + ; loadingTorrentsIter != m_loadingTorrents.end()) { LoadTorrentParams params = loadingTorrentsIter.value(); m_loadingTorrents.erase(loadingTorrentsIter); @@ -5085,6 +5090,20 @@ void SessionImpl::handleAddTorrentAlerts(const std::vector &alerts) Torrent *torrent = createTorrent(alert->handle, params); loadedTorrents.append(torrent); } + else if (const auto downloadedMetadataIter = m_downloadedMetadata.find(torrentID) + ; downloadedMetadataIter != m_downloadedMetadata.end()) + { + ++m_extraLimit; + adjustLimits(); + + downloadedMetadataIter.value() = alert->handle; + if (infoHash.isHybrid()) + { + // index hybrid magnet links by both v1 and v2 info hashes + const auto altID = TorrentID::fromSHA1Hash(infoHash.v1()); + m_downloadedMetadata[altID] = alert->handle; + } + } } if (!loadedTorrents.isEmpty())