diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 9124c2c36..cdcbbf0f1 100644 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -778,6 +778,10 @@ namespace libtorrent // the piece requests std::vector m_requests_in_buffer; + // the block we're currently receiving. Or + // (-1, -1) if we're not receiving one + piece_block m_receiving_block; + // the number of bytes that the other // end has to send us in order to respond // to all outstanding piece requests we diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 712d4c57e..5a9796966 100644 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -98,6 +98,7 @@ namespace libtorrent , m_socket(s) , m_remote(endp) , m_torrent(tor) + , m_receiving_block(-1, -1) , m_outstanding_bytes(0) , m_queued_time_critical(0) , m_num_pieces(0) @@ -218,6 +219,7 @@ namespace libtorrent , m_disk_recv_buffer(ses, 0) , m_socket(s) , m_remote(endp) + , m_receiving_block(-1, -1) , m_outstanding_bytes(0) , m_queued_time_critical(0) , m_num_pieces(0) @@ -1738,6 +1740,7 @@ namespace libtorrent boost::shared_ptr t = associated_torrent().lock(); TORRENT_ASSERT(t); piece_block b(r.piece, r.start / t->block_size()); + m_receiving_block = b; if (!verify_piece(r)) { @@ -1836,6 +1839,10 @@ namespace libtorrent TORRENT_ASSERT(!m_disk_recv_buffer); TORRENT_ASSERT(m_disk_recv_buffer_size == 0); + // we're not receiving any block right now + m_receiving_block.piece_index = -1; + m_receiving_block.block_index = -1; + #ifdef TORRENT_CORRUPT_DATA // corrupt all pieces from certain peers if (m_remote.address().is_v4() @@ -2477,9 +2484,7 @@ namespace libtorrent TORRENT_ASSERT(block_size <= t->block_size()); // we can't cancel the piece if we've started receiving it - // if m_outstsanding_bytes is less than the block size, - // it means we have received parts of it already - if (m_outstanding_bytes < block_size) break; + if (m_receiving_block == b) continue; peer_request r; r.piece = b.piece_index;