mirror of
https://github.com/PurpleI2P/i2pd.git
synced 2026-08-29 01:08:53 +00:00
try to request block from piece requested by another connection if no other pieces available
This commit is contained in:
+24
-11
@@ -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;
|
||||
|
||||
@@ -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);
|
||||
|
||||
Reference in New Issue
Block a user