From c3f8c762cc9fbefbdaca428e96e80a3b53e0733a Mon Sep 17 00:00:00 2001 From: orignal Date: Fri, 14 Aug 2026 09:23:42 -0400 Subject: [PATCH] try to request blocks again for stalled connection --- libi2pd_client/Torrents.cpp | 30 ++++++++++++++++++++++-------- libi2pd_client/Torrents.h | 2 +- 2 files changed, 23 insertions(+), 9 deletions(-) diff --git a/libi2pd_client/Torrents.cpp b/libi2pd_client/Torrents.cpp index b1cb4e7a..da1f8850 100644 --- a/libi2pd_client/Torrents.cpp +++ b/libi2pd_client/Torrents.cpp @@ -202,7 +202,12 @@ namespace torrents } } else - LogPrint (eLogWarning, "Torrents: Duplicated, late or unsolicited piece block ", blockIndex); + { + if ((*m_Blocks)[blockIndex] == BlockStatus::Available) + LogPrint (eLogWarning, "Torrents: Duplicated piece block ", blockIndex); + else + LogPrint (eLogWarning, "Torrents: Late or unsolicited piece block ", blockIndex); + } } void Piece::Dump (PieceFileFragment&& fragment) @@ -759,10 +764,14 @@ namespace torrents } else if (ts > m_LastSendTime + PEER_KEEP_SEND_INTERVAL) { - // send keep-alive - uint32_t len = 0; - WriteToStream ((const uint8_t *)&len, 4); - m_LastSendTime = ts; + m_NumRequests = 0; // if we need to send keep-alive, all pending requests are invalid now + if (m_Torrent->IsComplete () || !RequestNextBlocks ()) // try to request if we still have blocks to request + { + // send keep-alive + uint32_t len = 0; + WriteToStream ((const uint8_t *)&len, 4); + m_LastSendTime = ts; + } } } } @@ -1307,13 +1316,18 @@ namespace torrents m_NumRequests++; } - void PeerConnection::RequestNextBlocks () + bool PeerConnection::RequestNextBlocks () { - if (m_IsChoked || !m_Torrent || m_Torrent->IsComplete ()) return; + if (m_IsChoked || !m_Torrent || m_Torrent->IsComplete ()) return false; + bool sent = false; while (m_NumRequests < MAX_NUM_REQUESTS) { - if (!RequestNextBlock ()) break; + if (RequestNextBlock ()) + sent = true; + else + break; } + return sent; } TorrentsTunnel::TorrentsTunnel (std::string_view name, std::shared_ptr localDestination, diff --git a/libi2pd_client/Torrents.h b/libi2pd_client/Torrents.h index c9bf1e90..421ab7c7 100644 --- a/libi2pd_client/Torrents.h +++ b/libi2pd_client/Torrents.h @@ -266,7 +266,7 @@ namespace torrents bool RequestNextBlock (); void RequestNextBlock (uint32_t index, uint32_t offset, uint32_t len); - void RequestNextBlocks (); + bool RequestNextBlocks (); bool SendRequestedBlock (const RequestedBlock& requestedBlock);