Skip to content

Commit

Permalink
Preserve last announce info when torrent is stopped
Browse files Browse the repository at this point in the history
  • Loading branch information
glassez committed Sep 23, 2023
1 parent bfe4318 commit 16786bb
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 24 deletions.
2 changes: 0 additions & 2 deletions src/base/bittorrent/sessionimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4855,8 +4855,6 @@ void SessionImpl::handleTorrentMetadataReceived(TorrentImpl *const torrent)

void SessionImpl::handleTorrentPaused(TorrentImpl *const torrent)
{
torrent->resetTrackerEntries();

const auto &trackerEntries = torrent->trackers();
QHash<QString, TrackerEntry> updatedTrackerEntries;
updatedTrackerEntries.reserve(trackerEntries.size());
Expand Down
66 changes: 45 additions & 21 deletions src/base/bittorrent/torrentimpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,10 @@ namespace

#ifdef QBT_USES_LIBTORRENT2
void updateTrackerEntry(TrackerEntry &trackerEntry, const lt::announce_entry &nativeEntry
, const lt::info_hash_t &hashes, const QHash<lt::tcp::endpoint, QMap<int, int>> &updateInfo)
, const lt::info_hash_t &hashes, const QHash<lt::tcp::endpoint, QMap<int, int>> &updateInfo, const bool ignoreAnnounceTimes)
#else
void updateTrackerEntry(TrackerEntry &trackerEntry, const lt::announce_entry &nativeEntry
, const QHash<lt::tcp::endpoint, QMap<int, int>> &updateInfo)
, const QHash<lt::tcp::endpoint, QMap<int, int>> &updateInfo, const bool ignoreAnnounceTimes)
#endif
{
Q_ASSERT(trackerEntry.url == QString::fromStdString(nativeEntry.url));
Expand Down Expand Up @@ -149,8 +149,17 @@ namespace
trackerEndpointEntry.numSeeds = ltAnnounceInfo.scrape_complete;
trackerEndpointEntry.numLeeches = ltAnnounceInfo.scrape_incomplete;
trackerEndpointEntry.numDownloaded = ltAnnounceInfo.scrape_downloaded;
trackerEndpointEntry.nextAnnounceTime = fromLTTimePoint32(ltAnnounceInfo.next_announce);
trackerEndpointEntry.minAnnounceTime = fromLTTimePoint32(ltAnnounceInfo.min_announce);

if (!ignoreAnnounceTimes)
{
trackerEndpointEntry.nextAnnounceTime = fromLTTimePoint32(ltAnnounceInfo.next_announce);
trackerEndpointEntry.minAnnounceTime = fromLTTimePoint32(ltAnnounceInfo.min_announce);
}
else
{
trackerEndpointEntry.nextAnnounceTime = QDateTime();
trackerEndpointEntry.minAnnounceTime = QDateTime();
}

if (ltAnnounceInfo.updating)
{
Expand Down Expand Up @@ -241,18 +250,21 @@ namespace

if (endpointEntry.status == trackerEntry.status)
{
if (!trackerEntry.nextAnnounceTime.isValid() || (trackerEntry.nextAnnounceTime > endpointEntry.nextAnnounceTime))
if (!ignoreAnnounceTimes)
{
trackerEntry.nextAnnounceTime = endpointEntry.nextAnnounceTime;
trackerEntry.minAnnounceTime = endpointEntry.minAnnounceTime;
if ((endpointEntry.status != BitTorrent::TrackerEntry::Status::Working)
|| !endpointEntry.message.isEmpty())
if (!trackerEntry.nextAnnounceTime.isValid() || (trackerEntry.nextAnnounceTime > endpointEntry.nextAnnounceTime))
{
trackerEntry.message = endpointEntry.message;
trackerEntry.nextAnnounceTime = endpointEntry.nextAnnounceTime;
trackerEntry.minAnnounceTime = endpointEntry.minAnnounceTime;
if ((endpointEntry.status != BitTorrent::TrackerEntry::Status::Working)
|| !endpointEntry.message.isEmpty())
{
trackerEntry.message = endpointEntry.message;
}
}
}

if (endpointEntry.status == BitTorrent::TrackerEntry::Status::Working)
if ((endpointEntry.status == BitTorrent::TrackerEntry::Status::Working) || ignoreAnnounceTimes)
{
if (trackerEntry.message.isEmpty())
trackerEntry.message = endpointEntry.message;
Expand Down Expand Up @@ -1668,19 +1680,13 @@ TrackerEntry TorrentImpl::updateTrackerEntry(const lt::announce_entry &announceE
return {};

#ifdef QBT_USES_LIBTORRENT2
::updateTrackerEntry(*it, announceEntry, nativeHandle().info_hashes(), updateInfo);
::updateTrackerEntry(*it, announceEntry, nativeHandle().info_hashes(), updateInfo, isPaused());
#else
::updateTrackerEntry(*it, announceEntry, updateInfo);
::updateTrackerEntry(*it, announceEntry, updateInfo, isPaused());
#endif
return *it;
}

void TorrentImpl::resetTrackerEntries()
{
for (auto &trackerEntry : m_trackerEntries)
trackerEntry = {trackerEntry.url, trackerEntry.tier};
}

std::shared_ptr<const libtorrent::torrent_info> TorrentImpl::nativeTorrentInfo() const
{
if (m_nativeStatus.torrent_file.expired())
Expand Down Expand Up @@ -1738,7 +1744,7 @@ void TorrentImpl::endReceivedMetadataHandling(const Path &savePath, const PathLi
p.flags |= lt::torrent_flags::paused;
p.flags &= ~lt::torrent_flags::auto_managed;

m_session->handleTorrentPaused(this);
onPaused();
}

reload();
Expand Down Expand Up @@ -1811,7 +1817,8 @@ void TorrentImpl::pause()
m_stopCondition = StopCondition::None;
m_isStopped = true;
m_session->handleTorrentNeedSaveResumeData(this);
m_session->handleTorrentPaused(this);

onPaused();
}

if (m_maintenanceJob == MaintenanceJob::None)
Expand Down Expand Up @@ -2005,6 +2012,23 @@ void TorrentImpl::handleTorrentResumedAlert([[maybe_unused]] const lt::torrent_r
{
}

void TorrentImpl::onPaused()
{
for (TrackerEntry &trackerEntry : m_trackerEntries)
{
trackerEntry.nextAnnounceTime = QDateTime();
trackerEntry.minAnnounceTime = QDateTime();

for (TrackerEntry::EndpointEntry &endpointEntry : trackerEntry.endpointEntries)
{
endpointEntry.nextAnnounceTime = QDateTime();
endpointEntry.minAnnounceTime = QDateTime();
}
}

m_session->handleTorrentPaused(this);
}

void TorrentImpl::handleSaveResumeDataAlert(const lt::save_resume_data_alert *p)
{
if (m_ltAddTorrentParams.url_seeds != p->params.url_seeds)
Expand Down
3 changes: 2 additions & 1 deletion src/base/bittorrent/torrentimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ namespace BitTorrent
void handleMoveStorageJobFinished(const Path &path, MoveStorageContext context, bool hasOutstandingJob);
void fileSearchFinished(const Path &savePath, const PathList &fileNames);
TrackerEntry updateTrackerEntry(const lt::announce_entry &announceEntry, const QHash<lt::tcp::endpoint, QMap<int, int>> &updateInfo);
void resetTrackerEntries();

private:
using EventTrigger = std::function<void ()>;
Expand Down Expand Up @@ -295,6 +294,8 @@ namespace BitTorrent
void handleTorrentPausedAlert(const lt::torrent_paused_alert *p);
void handleTorrentResumedAlert(const lt::torrent_resumed_alert *p);

void onPaused();

bool isMoveInProgress() const;

void setAutoManaged(bool enable);
Expand Down

0 comments on commit 16786bb

Please sign in to comment.