try to request block from piece requested by another connection if no other pieces available

This commit is contained in:
orignal
2026-08-13 21:29:48 -04:00
parent 866b526ccc
commit e2a1b6d7e5
2 changed files with 26 additions and 12 deletions
+24 -11
View File
@@ -507,7 +507,7 @@ namespace torrents
return complete;
}
std::tuple<uint32_t, uint32_t, uint32_t> Torrent::GetNextBlockToRequest (std::shared_ptr<PeerConnection> conn)
std::tuple<uint32_t, uint32_t, uint32_t> Torrent::GetNextBlockToRequest (std::shared_ptr<PeerConnection> conn, bool skipRequested)
{
if (conn)
{
@@ -530,7 +530,7 @@ namespace torrents
uint32_t ind = 0;
for (auto& it: m_Pieces)
{
if (!it.IsComplete () && conn->IsPieceAvailable (ind) && !it.IsRequested ())
if (!it.IsComplete () && conn->IsPieceAvailable (ind) && (!skipRequested || !it.IsRequested ()))
sortedByNumPeers.emplace (ind, it.GetNumPeers ());
ind++;
}
@@ -1279,21 +1279,34 @@ namespace torrents
bool PeerConnection::RequestNextBlock ()
{
if (!m_Torrent) return false;
auto [index, offset, len] = m_Torrent->GetNextBlockToRequest (shared_from_this ());
auto [index, offset, len] = m_Torrent->GetNextBlockToRequest (shared_from_this (), true); // skip already requested pieces
if (len > 0)
RequestNextBlock (index, offset, len);
else
{
m_LastRequestedPieceIndex = index;
SendRequestMsg (index, offset, len);
m_NumRequests++;
}
else if (m_LastRequestedPieceIndex >= 0)
{
m_LastRequestedPieceIndex = -1; // no request
SendNotinterestedMsg ();
// try to get block from requested by another connection piece
auto [index1, offset1, len1] = m_Torrent->GetNextBlockToRequest (shared_from_this (), false);
if (len1 > 0)
{
len = len1;
RequestNextBlock (index1, offset1, len1);
}
else if (m_LastRequestedPieceIndex >= 0)
{
m_LastRequestedPieceIndex = -1; // no request
SendNotinterestedMsg ();
}
}
return len > 0;
}
void PeerConnection::RequestNextBlock (uint32_t index, uint32_t offset, uint32_t len)
{
m_LastRequestedPieceIndex = index;
SendRequestMsg (index, offset, len);
m_NumRequests++;
}
void PeerConnection::RequestNextBlocks ()
{
if (m_IsChoked || !m_Torrent || m_Torrent->IsComplete ()) return;
+2 -1
View File
@@ -168,7 +168,7 @@ namespace torrents
std::pair<std::vector<uint8_t>, bool> CreateBitfield () const; // (bitfield, empty)
bool ApplyBitfield (const std::vector<uint8_t>& bitfield); // return true if complete
const std::unordered_set<i2p::data::IdentHash>& GetPeers () const { return m_Peers; }
std::tuple<uint32_t, uint32_t, uint32_t> GetNextBlockToRequest (std::shared_ptr<PeerConnection> conn); // return (index, offset, len)
std::tuple<uint32_t, uint32_t, uint32_t> GetNextBlockToRequest (std::shared_ptr<PeerConnection> conn, bool skipRequested = true); // return (index, offset, len)
std::vector<PieceFileFragment> GetPieceFileFragments (int index) const;
bool UpdateStatus (uint64_t ts); // return true if complete
@@ -265,6 +265,7 @@ namespace torrents
void HandleChokeMsg ();
bool RequestNextBlock ();
void RequestNextBlock (uint32_t index, uint32_t offset, uint32_t len);
void RequestNextBlocks ();
bool SendRequestedBlock (const RequestedBlock& requestedBlock);