diff --git a/libtransmission/announcer.cc b/libtransmission/announcer.cc index 18479ad15..1c9d60f61 100644 --- a/libtransmission/announcer.cc +++ b/libtransmission/announcer.cc @@ -622,7 +622,7 @@ struct tr_torrent_announcer std::vector tiers; - tr_tracker_callback callback = nullptr; + tr_tracker_callback callback; private: [[nodiscard]] static tr_announce_list getAnnounceList(tr_torrent const* tor) @@ -660,7 +660,7 @@ void publishMessage(tr_tier* tier, std::string_view msg, tr_tracker_event::Type event.announce_url = current_tracker->announce_url; } - (*ta->callback)(tier->tor, &event); + ta->callback(*tier->tor, &event); } } @@ -689,7 +689,7 @@ void publishPeerCounts(tr_tier* tier, int seeders, int leechers) e.leechers = leechers; tr_logAddDebugTier(tier, fmt::format("peer counts: {} seeders, {} leechers.", seeders, leechers)); - (*tier->tor->torrent_announcer->callback)(tier->tor, &e); + tier->tor->torrent_announcer->callback(*tier->tor, &e); } } @@ -710,7 +710,7 @@ void publishPeersPex(tr_tier* tier, int seeders, int leechers, std::vectortor->torrent_announcer->callback)(tier->tor, &e); + tier->tor->torrent_announcer->callback(*tier->tor, &e); } } } // namespace publish_helpers diff --git a/libtransmission/announcer.h b/libtransmission/announcer.h index 8e50b8a5e..668001b88 100644 --- a/libtransmission/announcer.h +++ b/libtransmission/announcer.h @@ -59,7 +59,7 @@ struct tr_tracker_event int seeders; }; -using tr_tracker_callback = void (*)(tr_torrent* tor, tr_tracker_event const* event); +using tr_tracker_callback = std::function; class tr_announcer { diff --git a/libtransmission/torrent.cc b/libtransmission/torrent.cc index 8534030c1..345e5e9e3 100644 --- a/libtransmission/torrent.cc +++ b/libtransmission/torrent.cc @@ -1025,46 +1025,6 @@ bool isNewTorrentASeed(tr_torrent* tor) return tor->ensurePieceIsChecked(0); } -void onTrackerResponse(tr_torrent* tor, tr_tracker_event const* event) -{ - switch (event->type) - { - case tr_tracker_event::Type::Peers: - tr_logAddTraceTor(tor, fmt::format("Got {} peers from tracker", std::size(event->pex))); - tr_peerMgrAddPex(tor, TR_PEER_FROM_TRACKER, std::data(event->pex), std::size(event->pex)); - break; - - case tr_tracker_event::Type::Counts: - if (tor->isPrivate() && (event->leechers == 0)) - { - tr_peerMgrSetSwarmIsAllSeeds(tor); - } - - break; - - case tr_tracker_event::Type::Warning: - tr_logAddWarnTor(tor, fmt::format(_("Tracker warning: '{warning}'"), fmt::arg("warning", event->text))); - tor->error = TR_STAT_TRACKER_WARNING; - tor->error_announce_url = event->announce_url; - tor->error_string = event->text; - break; - - case tr_tracker_event::Type::Error: - tor->error = TR_STAT_TRACKER_ERROR; - tor->error_announce_url = event->announce_url; - tor->error_string = event->text; - break; - - case tr_tracker_event::Type::ErrorClear: - if (tor->error != TR_STAT_LOCAL_ERROR) - { - tr_torrentClearError(tor); - } - - break; - } -} - void torrentInitFromInfoDict(tr_torrent* tor) { tor->completion = tr_completion{ tor, &tor->blockInfo() }; @@ -1212,7 +1172,7 @@ void torrentInit(tr_torrent* tor, tr_ctor const* ctor) } } - tor->torrent_announcer = session->announcer_->addTorrent(tor, onTrackerResponse); + tor->torrent_announcer = session->announcer_->addTorrent(tor, &tr_torrent::onTrackerResponse); if (is_new_torrent) { @@ -2235,6 +2195,46 @@ bool tr_torrent::setTrackerList(std::string_view text) return true; } +void tr_torrent::onTrackerResponse(tr_tracker_event const* event) +{ + switch (event->type) + { + case tr_tracker_event::Type::Peers: + tr_logAddTraceTor(this, fmt::format("Got {} peers from tracker", std::size(event->pex))); + tr_peerMgrAddPex(this, TR_PEER_FROM_TRACKER, std::data(event->pex), std::size(event->pex)); + break; + + case tr_tracker_event::Type::Counts: + if (isPrivate() && (event->leechers == 0)) + { + tr_peerMgrSetSwarmIsAllSeeds(this); + } + + break; + + case tr_tracker_event::Type::Warning: + tr_logAddWarnTor(this, fmt::format(_("Tracker warning: '{warning}'"), fmt::arg("warning", event->text))); + error = TR_STAT_TRACKER_WARNING; + error_announce_url = event->announce_url; + error_string = event->text; + break; + + case tr_tracker_event::Type::Error: + error = TR_STAT_TRACKER_ERROR; + error_announce_url = event->announce_url; + error_string = event->text; + break; + + case tr_tracker_event::Type::ErrorClear: + if (error != TR_STAT_LOCAL_ERROR) + { + tr_torrentClearError(this); + } + + break; + } +} + bool tr_torrentSetTrackerList(tr_torrent* tor, char const* text) { return text != nullptr && tor->setTrackerList(text); diff --git a/libtransmission/torrent.h b/libtransmission/torrent.h index 79a7a60c9..e2e599f73 100644 --- a/libtransmission/torrent.h +++ b/libtransmission/torrent.h @@ -432,6 +432,8 @@ public: bool setTrackerList(std::string_view text); + void onTrackerResponse(tr_tracker_event const* event); + /// METAINFO - WEBSEEDS [[nodiscard]] TR_CONSTEXPR20 auto webseedCount() const noexcept