handle torrent-stop, torrent-start, torrent-reannounce for all torrents

This commit is contained in:
orignal committed 2026-10-14 16:38:46 -04:00
1 parent 5cfd8d67cc
commit 1df62b0bbe
3 files changed
+71 -8

No files matched your search

+17 -8
View File
@@ -274,10 +274,8 @@ namespace torrents
deleteFiles = arguments.at ("delete-local-data").as_bool ();
auto torrentIds = GetTorrentIds (arguments);
if (!torrentIds.empty ())
{
for (auto id: torrentIds)
m_Tunnel->RemoveTorrent (id, deleteFiles);
}
else
m_Tunnel->RemoveAllTorrents (deleteFiles);
boost::json::object response; // always empty
@@ -288,8 +286,11 @@ namespace torrents
{
auto arguments = jsonRequest.at ("arguments").as_object ();
auto torrentIds = GetTorrentIds (arguments);
for (auto id: torrentIds)
m_Tunnel->ReannounceTorrent (id);
if (!torrentIds.empty ())
for (auto id: torrentIds)
m_Tunnel->ReannounceTorrent (id);
else
m_Tunnel->ReannounceAllTorrents ();
boost::json::object response; // always empty
return SuccessResponse (GetTag (jsonRequest), std::move (response));
}
@@ -725,8 +726,12 @@ namespace torrents
std::string JSONRPCHandler::HandleTorrentStop (boost::json::object&& jsonRequest)
{
auto arguments = jsonRequest.at ("arguments").as_object ();
for (auto id: GetTorrentIds (arguments))
m_Tunnel->StopTorrent (id);
auto torrentIds = GetTorrentIds (arguments);
if (!torrentIds.empty ())
for (auto id: torrentIds)
m_Tunnel->StopTorrent (id);
else
m_Tunnel->StopAllTorrents ();
boost::json::object response; // always empty
return SuccessResponse (GetTag (jsonRequest), std::move (response));
}
@@ -734,8 +739,12 @@ namespace torrents
std::string JSONRPCHandler::HandleTorrentStart (boost::json::object&& jsonRequest)
{
auto arguments = jsonRequest.at ("arguments").as_object ();
for (auto id: GetTorrentIds (arguments))
m_Tunnel->StartTorrent (id);
auto torrentIds = GetTorrentIds (arguments);
if (!torrentIds.empty ())
for (auto id: torrentIds)
m_Tunnel->StartTorrent (id);
else
m_Tunnel->StartAllTorrents ();
boost::json::object response; // always empty
return SuccessResponse (GetTag (jsonRequest), std::move (response));
}
+51
View File
@@ -551,6 +551,21 @@ namespace torrents
it->Close ();
}
void TorrentsTunnel::StopAllTorrents ()
{
std::list<std::shared_ptr<Torrent> > torrentsToStop;
{
std::lock_guard<std::mutex> l(m_TorrentsMutex);
for (const auto& it: m_Torrents)
torrentsToStop.push_back (it.second);
}
boost::asio::post (GetService (), [this, torrentsToStop = std::move (torrentsToStop)]()
{
for (auto it: torrentsToStop) // TODO: announce with random intervals
StopTorrent (it);
});
}
bool TorrentsTunnel::StartTorrent (int id)
{
std::shared_ptr<Torrent> torrent;
@@ -570,6 +585,24 @@ namespace torrents
return true;
}
void TorrentsTunnel::StartAllTorrents ()
{
std::list<std::shared_ptr<Torrent> > torrentsToStart;
{
std::lock_guard<std::mutex> l(m_TorrentsMutex);
for (const auto& it: m_Torrents)
torrentsToStart.push_back (it.second);
}
boost::asio::post (GetService (), [this, torrentsToStart = std::move (torrentsToStart)]()
{
for (auto it: torrentsToStart) // TODO: announce with random intervals
{
it->SetStopped (false);
RequestTorrentTrackers (it, eTrackerAnnounceEventStarted);
}
});
}
bool TorrentsTunnel::ReannounceTorrent (int id)
{
std::shared_ptr<Torrent> torrent;
@@ -588,6 +621,24 @@ namespace torrents
return true;
}
void TorrentsTunnel::ReannounceAllTorrents ()
{
std::list<std::shared_ptr<Torrent> > torrentsToReannounce;
{
std::lock_guard<std::mutex> l(m_TorrentsMutex);
for (const auto& it: m_Torrents)
torrentsToReannounce.push_back (it.second);
}
boost::asio::post (GetService (), [this, torrentsToReannounce = std::move (torrentsToReannounce)]()
{
for (auto it: torrentsToReannounce) // TODO: announce with random intervals
{
RequestTorrentTrackers (it, eTrackerAnnounceEventNone);
if (m_DHT) m_DHT->GetPeersAndAnnounce (it);
}
});
}
void TorrentsTunnel::UpdateTorrentInfo (std::shared_ptr<Torrent> torrent, std::string_view info)
{
if (!torrent || torrent->IsStopped ()) return;
+3
View File
@@ -112,8 +112,11 @@ namespace torrents
bool RemoveTorrent (int id, bool deleteFiles);
void RemoveAllTorrents (bool deleteFiles);
bool StopTorrent (int id);
void StopAllTorrents ();
bool StartTorrent (int id);
void StartAllTorrents ();
bool ReannounceTorrent (int id);
void ReannounceAllTorrents ();
void ConnectToNewPeers (std::shared_ptr<Torrent> torrent, std::unordered_set<i2p::data::IdentHash>& newPeers, PeerConnectionOrigin origin);
void SendDHTPingQuery (const i2p::data::IdentHash& toIdent, uint16_t toPort);
std::pair<uint16_t, uint16_t> GetDHTPorts () const { return { m_DHT ? m_DHT->GetPort () : 0, m_DHT ? m_DHT->GetRPort () : 0 }; }