From 3b551ac27221dad4e4078261817e1f3cf22f83f6 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 17 Dec 2003 19:03:23 +0000 Subject: [PATCH] *** empty log message *** --- include/libtorrent/peer_connection.hpp | 3 --- include/libtorrent/session.hpp | 2 +- src/peer_connection.cpp | 23 +++++++++++++++++-- src/piece_picker.cpp | 31 +++++++++++++++----------- 4 files changed, 40 insertions(+), 19 deletions(-) diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 721c2fd5b..f2bd6cafb 100755 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -68,9 +68,6 @@ POSSIBILITY OF SUCH DAMAGE. // only should be given to those we are not interested // in? -// TODO: the interested flag has to be updated when we -// get pieces. - namespace libtorrent { class torrent; diff --git a/include/libtorrent/session.hpp b/include/libtorrent/session.hpp index 6c940583b..fd348b498 100755 --- a/include/libtorrent/session.hpp +++ b/include/libtorrent/session.hpp @@ -60,7 +60,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/debug.hpp" -// TODO: if we're not interested and the peer isn't interested, close the connections +// TODO: if we're a seed and the peer is a seed, close the connections namespace libtorrent { diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 10d3cd08f..74d8c27d5 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -377,6 +377,7 @@ bool libtorrent::peer_connection::dispatch_message(int received) else { m_have_piece[index] = true; + // TODO: maybe this if-statement should be moved into the policy if (!m_torrent->peer_has(index) && !is_interesting()) m_torrent->get_policy().peer_is_interesting(*this); } @@ -397,15 +398,17 @@ bool libtorrent::peer_connection::dispatch_message(int received) #ifndef NDEBUG (*m_logger) << m_socket->sender().as_string() << " <== BITFIELD\n"; #endif - bool interesting = false; bool is_seed = true; + + // build a vector of all pieces + std::vector piece_list; for (std::size_t i = 0; i < m_have_piece.size(); ++i) { bool have = m_recv_buffer[1 + (i>>3)] & (1 << (7 - (i&7))); if (have && !m_have_piece[i]) { m_have_piece[i] = true; - if (m_torrent->peer_has(i)) interesting = true; + piece_list.push_back(i); } else if (!have && m_have_piece[i]) { @@ -414,6 +417,22 @@ bool libtorrent::peer_connection::dispatch_message(int received) } if (!have) is_seed = false; } + + // shuffle the piece list + std::random_shuffle(piece_list.begin(), piece_list.end()); + + // let the torrent know which pieces the + // peer has, in a shuffled order + bool interesting = false; + for (std::vector::iterator i = piece_list.begin(); + i != piece_list.end(); + ++i) + { + int index = *i; + if (m_torrent->peer_has(index)) + interesting = true; + } + if (is_seed) { #ifndef NDEBUG diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index aa98a1bbb..83c96231d 100755 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include "libtorrent/piece_picker.hpp" @@ -73,13 +74,30 @@ namespace libtorrent void piece_picker::files_checked(const std::vector& pieces) { + // build a vector of all the pieces we don't have + std::vector piece_list; + piece_list.reserve( + pieces.size() + - std::accumulate(pieces.begin(), pieces.end(), 0)); + for (std::vector::const_iterator i = pieces.begin(); i != pieces.end(); ++i) { if (*i) continue; int index = i - pieces.begin(); + piece_list.push_back(index); + } + + // random shuffle the list + std::random_shuffle(piece_list.begin(), piece_list.end()); + // add the pieces to the piece_picker + for (std::vector::iterator i = piece_list.begin(); + i != piece_list.end(); + ++i) + { + int index = *i; assert(index < m_piece_map.size()); assert(m_piece_map[index].index == 0xffffff); @@ -93,7 +111,6 @@ namespace libtorrent #ifndef NDEBUG // integrity_check(); #endif - // TODO: random_shuffle } #ifndef NDEBUG @@ -309,18 +326,6 @@ namespace libtorrent #endif } - // TODO: since inc_refcount is called - // with sequential indices when peers - // connect, the pieces will be sorted. - // that is not good. one solution is - // to insert the element at a random - // index when moving it to another - // vector. - // one solution might be to create a - // vector of all piece indices that - // are to have their ref_count increased - // and then random_shuffle that vector - // before processing them. bool piece_picker::inc_refcount(int i) { assert(i >= 0);