From 466e99fa55d9b4e9674a8a1b0bf96b359ef7def6 Mon Sep 17 00:00:00 2001 From: orignal Date: Sat, 3 Oct 2026 21:36:16 -0400 Subject: [PATCH] torrent-reannounce RPC method --- libi2pd_client/TorrentsRPC.cpp | 14 +++++++++++++- libi2pd_client/TorrentsTunnel.cpp | 28 ++++++++++++++++++++++++++-- libi2pd_client/TorrentsTunnel.h | 1 + 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/libi2pd_client/TorrentsRPC.cpp b/libi2pd_client/TorrentsRPC.cpp index 78f3ff8e..4e3a76d2 100644 --- a/libi2pd_client/TorrentsRPC.cpp +++ b/libi2pd_client/TorrentsRPC.cpp @@ -66,6 +66,7 @@ namespace torrents std::string HandleSessionGet (boost::json::object&& jsonRequest); // for transmission-rpc library std::string HandleSessionStats (boost::json::object&& jsonRequest); std::string HandleTorrentSet (boost::json::object&& jsonRequest); + std::string HandleTorrentReannounce (boost::json::object&& jsonRequest); private: @@ -123,7 +124,8 @@ namespace torrents { "torrent-start", &JSONRPCHandler::HandleTorrentStart }, { "session-get", &JSONRPCHandler::HandleSessionGet }, { "session-stats", &JSONRPCHandler::HandleSessionStats }, - { "torrent-set", &JSONRPCHandler::HandleTorrentSet } + { "torrent-set", &JSONRPCHandler::HandleTorrentSet }, + { "torrent-reannounce", &JSONRPCHandler::HandleTorrentReannounce } }; try @@ -213,6 +215,16 @@ namespace torrents return SuccessResponse (GetTag (jsonRequest), std::move (response)); } + std::string JSONRPCHandler::HandleTorrentReannounce (boost::json::object&& jsonRequest) + { + auto arguments = jsonRequest.at ("arguments").as_object (); + auto torrentIds = GetTorrentIds (arguments); + for (auto id: torrentIds) + m_Tunnel->ReannounceTorrent (id); + boost::json::object response; // always empty + return SuccessResponse (GetTag (jsonRequest), std::move (response)); + } + std::string JSONRPCHandler::HandleTorrentGet (boost::json::object&& jsonRequest) { auto arguments = jsonRequest.at ("arguments").as_object (); diff --git a/libi2pd_client/TorrentsTunnel.cpp b/libi2pd_client/TorrentsTunnel.cpp index cd65ec70..2528e0b5 100644 --- a/libi2pd_client/TorrentsTunnel.cpp +++ b/libi2pd_client/TorrentsTunnel.cpp @@ -453,12 +453,18 @@ namespace torrents void TorrentsTunnel::RemoveAllTorrents (bool deleteFiles) { - boost::asio::post (GetService (), [this, deleteFiles]() + std::list > torrentsToRemove; { + std::lock_guard l(m_TorrentsMutex); for (const auto& it: m_Torrents) - RemoveTorrent (it.second, deleteFiles); + torrentsToRemove.push_back (it.second); m_Torrents.clear (); m_TorrentsByID.clear (); + } + boost::asio::post (GetService (), [this, torrentsToRemove = std::move (torrentsToRemove), deleteFiles]() + { + for (auto it: torrentsToRemove) + RemoveTorrent (it, deleteFiles); }); } @@ -564,6 +570,24 @@ namespace torrents return true; } + bool TorrentsTunnel::ReannounceTorrent (int id) + { + std::shared_ptr torrent; + { + std::lock_guard l(m_TorrentsMutex); + auto it = m_TorrentsByID.find (id); + if (it == m_TorrentsByID.end ()) return false; + torrent = it->second.lock (); + } + if (!torrent) return false; + boost::asio::post (GetService (), [this, torrent]() + { + RequestTorrentTrackers (torrent, eTrackerAnnounceEventNone); + if (m_DHT) m_DHT->GetPeersAndAnnounce (torrent); + }); + return true; + } + void TorrentsTunnel::UpdateTorrentInfo (std::shared_ptr torrent, std::string_view info) { if (!torrent || torrent->IsStopped ()) return; diff --git a/libi2pd_client/TorrentsTunnel.h b/libi2pd_client/TorrentsTunnel.h index 4fb8e35a..0fd1bf9a 100644 --- a/libi2pd_client/TorrentsTunnel.h +++ b/libi2pd_client/TorrentsTunnel.h @@ -113,6 +113,7 @@ namespace torrents void RemoveAllTorrents (bool deleteFiles); bool StopTorrent (int id); bool StartTorrent (int id); + bool ReannounceTorrent (int id); void ConnectToNewPeers (std::shared_ptr torrent, std::unordered_set& newPeers, PeerConnectionOrigin origin); void SendDHTPingQuery (const i2p::data::IdentHash& toIdent, uint16_t toPort); std::pair GetDHTPorts () const { return { m_DHT ? m_DHT->GetPort () : 0, m_DHT ? m_DHT->GetRPort () : 0 }; }