remove peer from torrent on termination. Always run reconnect timer

This commit is contained in:
orignal committed 2026-09-13 11:33:03 -04:00
1 parent aee00c1984
commit d72ae7a3dc
4 files changed
+46 -12

No files matched your search

+29 -10
View File
@@ -899,13 +899,21 @@ namespace torrents
std::unordered_set<i2p::data::IdentHash> Torrent::GetNonConnectedPeers ()
{
std::unordered_set<i2p::data::IdentHash> ret;
for (const auto& it: m_TrackerStats)
for (size_t i = 0; i < m_TrackerStats.size (); i++)
ret.merge (GetNonConnectedPeers (i));
return ret;
}
std::unordered_set<i2p::data::IdentHash> Torrent::GetNonConnectedPeers (size_t trackerID)
{
std::unordered_set<i2p::data::IdentHash> ret;
if (trackerID < m_TrackerStats.size ())
{
const auto& peers = std::get<0>(it);
for (const auto& it1: peers)
const auto& peers = std::get<0>(m_TrackerStats[trackerID]);
for (const auto& it: peers)
{
if (!IsConnectedToPeer (it1))
ret.emplace (it1);
if (!IsConnectedToPeer (it))
ret.emplace (it);
}
}
return ret;
@@ -951,6 +959,14 @@ namespace torrents
return true;
}
void Torrent::RemoveConnection (std::shared_ptr<PeerConnection> conn)
{
if (!conn) return;
auto remoteIdentHash = conn->GetRemoteIdentHash ();
if (!remoteIdentHash) return;
m_Connections.erase (*remoteIdentHash);
}
std::list<std::shared_ptr<PeerConnection> > Torrent::GetConnections ()
{
std::list<std::shared_ptr<PeerConnection> > ret;
@@ -1006,11 +1022,15 @@ namespace torrents
void PeerConnection::Terminate ()
{
if (Kill()) return;
if (m_Torrent && m_LastRequestedPieceIndex >= 0) // pending requests by us
if (m_Torrent)
{
auto& piece = m_Torrent->GetPiece (m_LastRequestedPieceIndex);
if (piece.IsRequested ())
piece.ClearAllRequests (); // piece can be requested by other connections
if (m_LastRequestedPieceIndex >= 0) // pending requests by us
{
auto& piece = m_Torrent->GetPiece (m_LastRequestedPieceIndex);
if (piece.IsRequested ())
piece.ClearAllRequests (); // piece can be requested by other connections
}
m_Torrent->RemoveConnection (shared_from_this ());
}
if (m_Stream)
{
@@ -1022,7 +1042,6 @@ namespace torrents
m_HandshakeReceiveTimer->cancel ();
m_HandshakeReceiveTimer = nullptr;
}
Done(shared_from_this ());
}
void PeerConnection::ResetStats ()
+2
View File
@@ -213,6 +213,7 @@ namespace torrents
std::pair<std::vector<uint8_t>, boost::logic::tribool> CreateBitfield () const; // (bitfield, true - all false - none)
bool ApplyBitfield (const std::vector<uint8_t>& bitfield); // return true if complete
std::unordered_set<i2p::data::IdentHash> GetNonConnectedPeers ();
std::unordered_set<i2p::data::IdentHash> GetNonConnectedPeers (size_t trackerID);
RequestedBlock GetNextBlockToRequest (std::shared_ptr<PeerConnection> conn, bool skipRequested = true);
std::vector<PieceFileFragment> GetPieceFileFragments (int index) const;
std::vector<size_t> GetFilesCompleted () const; // completed size per file
@@ -220,6 +221,7 @@ namespace torrents
uint64_t GetNextUpdateStatusTime () { return m_NextUpdateStatusTime; }
void SetNextUpdateStatusTime (uint64_t nextUpdateStatusTime) { m_NextUpdateStatusTime = nextUpdateStatusTime; }
bool AddConnection (std::shared_ptr<PeerConnection> conn);
void RemoveConnection (std::shared_ptr<PeerConnection> conn);
std::list<std::shared_ptr<PeerConnection> > GetConnections ();
bool IsConnectedToPeer (const i2p::data::IdentHash& peer);
+14 -2
View File
@@ -66,6 +66,7 @@ namespace torrents
}
ScheduleTrackerRequestsCheck ();
ScheduleReconnectCheck ();
ScheduleKeepAliveCheck ();
ScheduleStatusUpdate ();
}
@@ -651,8 +652,7 @@ namespace torrents
if (res->result () == boost::beast::http::status::ok)
{
torrent->ParseTrackerResponse (trackerID, res->body ());
ConnectToPeers (torrent);
ScheduleReconnectCheck ();
ConnectToPeers (torrent, trackerID);
}
else
LogPrint (eLogWarning, "TorrentsTunnel: Tracker ", trackerID, " response code ", res->result_int());
@@ -696,6 +696,18 @@ namespace torrents
return peersToConnect.size ();
}
size_t TorrentsTunnel::ConnectToPeers (std::shared_ptr<Torrent> torrent, size_t trackerID)
{
if (!torrent) return 0;
auto peersToConnect = torrent->GetNonConnectedPeers (trackerID);
if (!peersToConnect.empty ())
{
for (const auto& it: peersToConnect)
ConnectToPeer (torrent, it);
}
return peersToConnect.size ();
}
void TorrentsTunnel::ConnectToNewPeers (std::shared_ptr<Torrent> torrent, std::unordered_set<i2p::data::IdentHash>& newPeers)
{
if (!torrent) return;
+1
View File
@@ -143,6 +143,7 @@ namespace torrents
void ConnectToPeer (std::shared_ptr<Torrent> torrent, const i2p::data::IdentHash& peer);
size_t ConnectToPeers (std::shared_ptr<Torrent> torrent);
size_t ConnectToPeers (std::shared_ptr<Torrent> torrent, size_t trackerID);
void UpdatePeersPerPiece (std::shared_ptr<Torrent> torrent);
void UpdateStats ();