Add torrent for preloading magnet asynchronously

This commit is contained in:
Vladimir Golovnev (Glassez) 2022-11-03 14:19:17 +03:00
parent 0b70ccf9e9
commit 6aee7f95b7
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7

View file

@ -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<lt::alert *> &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<lt::alert *> &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())