diff --git a/libi2pd_client/Torrents.cpp b/libi2pd_client/Torrents.cpp index 55871835..e29687cb 100644 --- a/libi2pd_client/Torrents.cpp +++ b/libi2pd_client/Torrents.cpp @@ -500,7 +500,7 @@ namespace torrents PeerConnection::PeerConnection (i2p::client::I2PService * owner, std::shared_ptr stream): i2p::client::I2PServiceHandler (owner), m_Stream (stream), m_ReceiveBufferOffset (0), m_IsHandshakeSent (false), m_IsEstablished (false), m_IsChoked (true), - m_LastReceiveTime (0), m_LastSendTime (0), m_NumRequests (0), m_IsSendingPieceMsg (false), + m_LastReceiveTime (0), m_LastSendTime (0), m_NumRequests (0), m_NumPieces (0), m_LastRequestedPieceIndex (-1) { } @@ -833,8 +833,6 @@ namespace torrents } if (isInterested) SendInterestedMsg (); - else if (m_Torrent->IsComplete ()) - SendNotinterestedMsg (); } void PeerConnection::SendBitfieldMsg (const uint8_t * bitfield, size_t bitfieldLen) @@ -910,14 +908,14 @@ namespace torrents htobe32buf (sendBuffer.data () + 9, offset); memcpy (sendBuffer.data () + 13, data, len); LogPrint (eLogDebug, "Torrents: Sending piece index ", index, " offset ", offset, " length ", len); - m_IsSendingPieceMsg = true; + m_NumPieces++; m_Stream->AsyncSend (sendBuffer.data (), sendBuffer.size (), [s = shared_from_this ()](const boost::system::error_code& ecode, size_t bytes_transferred) { - s->m_IsSendingPieceMsg = false; + if (s->m_NumPieces > 0) s->m_NumPieces--; if (!ecode) { - if (!s->m_IncomingRequestsQueue.empty ()) + while (!s->m_IncomingRequestsQueue.empty () && s->m_NumPieces < MAX_NUM_PIECES) { s->SendRequestedBlock (s->m_IncomingRequestsQueue.front ()); s->m_IncomingRequestsQueue.pop_front (); @@ -951,7 +949,7 @@ namespace torrents Piece& piece = m_Torrent->GetPiece (index); if (piece.HasBlock (offset)) { - if (m_IsSendingPieceMsg) + if (m_NumPieces >= MAX_NUM_PIECES) m_IncomingRequestsQueue.emplace_back (index, offset, length); else if (!SendRequestedBlock ({index, offset, length})) // block was not sent { @@ -966,7 +964,7 @@ namespace torrents boost::asio::post (s->GetTorrentsTunnel ()->GetService (), [requestedBlock = RequestedBlock{index, offset, length}, s]() { - if (!s->m_IsSendingPieceMsg) + if (s->m_NumPieces < MAX_NUM_PIECES) s->SendRequestedBlock (requestedBlock); else s->m_IncomingRequestsQueue.emplace_back (std::move (requestedBlock)); diff --git a/libi2pd_client/Torrents.h b/libi2pd_client/Torrents.h index 17c406a9..efde83bf 100644 --- a/libi2pd_client/Torrents.h +++ b/libi2pd_client/Torrents.h @@ -47,6 +47,7 @@ namespace torrents constexpr int PEER_KEEP_SEND_INTERVAL = 95; // in seconds constexpr int PEER_KEEP_ALIVE_CHECK_INTERVAL = 15; // in seconds constexpr size_t MAX_NUM_REQUESTS = 8; + constexpr size_t MAX_NUM_PIECES = 4; constexpr int PIECE_INACTIVITY_TIMEOUT = 60; // in seconds constexpr int TORRENTS_STATUS_UPDATE_INTERVAL = 25; // in seconds @@ -240,9 +241,8 @@ namespace torrents boost::dynamic_bitset<> m_RemoteBitfield; bool m_IsHandshakeSent, m_IsEstablished, m_IsChoked; uint64_t m_LastReceiveTime, m_LastSendTime; // monotonic seconds - size_t m_NumRequests; // outgoing + size_t m_NumRequests, m_NumPieces; // outgoing std::list m_IncomingRequestsQueue; - bool m_IsSendingPieceMsg; int m_LastRequestedPieceIndex; };