diff --git a/libi2pd_client/Torrents.cpp b/libi2pd_client/Torrents.cpp index 2f01b3eb..eb7ca6b2 100644 --- a/libi2pd_client/Torrents.cpp +++ b/libi2pd_client/Torrents.cpp @@ -469,13 +469,6 @@ namespace torrents return { 0, 0, 0 }; } - void Torrent::ClearAllRequests () - { - for (auto& it: m_Pieces) - if (!it.IsComplete ()) - it.ClearAllRequests (); - } - void Torrent::UpdateStatus (uint64_t ts) { //bool complete = true; @@ -691,8 +684,7 @@ namespace torrents HandlePieceMsg (m_ReceiveBuffer + offset + 1, msgLen - 1); break; case eMessageTypeChoke: - m_IsChoked = true; - if (m_Torrent) m_Torrent->ClearAllRequests (); + HandleChokeMsg (); break; case eMessageTypeUnchoke: m_IsChoked = false; @@ -1017,6 +1009,15 @@ namespace torrents WriteToStream (buf, UNCHOKE_MSG_LENGTH); } + void PeerConnection::HandleChokeMsg () + { + m_IsChoked = true; + m_NumRequests = 0; + if (m_Torrent && m_LastRequestedPieceIndex >= 0) + m_Torrent->GetPiece (m_LastRequestedPieceIndex).ClearAllRequests (); + m_LastRequestedPieceIndex = -1; + } + bool PeerConnection::RequestNextBlock () { if (!m_Torrent) return false; @@ -1027,6 +1028,8 @@ namespace torrents SendRequestMsg (index, offset, len); m_NumRequests++; } + else + m_LastRequestedPieceIndex = -1; // no request return len > 0; } diff --git a/libi2pd_client/Torrents.h b/libi2pd_client/Torrents.h index 9d561f25..1838669e 100644 --- a/libi2pd_client/Torrents.h +++ b/libi2pd_client/Torrents.h @@ -143,7 +143,6 @@ namespace torrents void ApplyBitfield (const std::vector& bitfield); const std::unordered_set& GetPeers () const { return m_Peers; } std::tuple GetNextBlockToRequest (std::shared_ptr conn); // return (index, offset, len) - void ClearAllRequests (); void UpdateStatus (uint64_t ts); uint64_t GetNextTrackerRequestTime () const { return m_NextTrackerRequestTime; } @@ -222,6 +221,7 @@ namespace torrents void SendRequestMsg (uint32_t index, uint32_t offset, uint32_t len); void SendInterestedMsg (); void SendUnchokeMsg (); + void HandleChokeMsg (); bool RequestNextBlock (); void RequestNextBlocks ();