diff --git a/include/libtorrent/piece_picker.hpp b/include/libtorrent/piece_picker.hpp index 8aa4cf90b..dcff791f9 100755 --- a/include/libtorrent/piece_picker.hpp +++ b/include/libtorrent/piece_picker.hpp @@ -110,9 +110,10 @@ namespace libtorrent // increases the peer count for the given piece // (is used when a HAVE or BITFIELD message is received) - // returns true if this piece was interesting - bool inc_refcount(int index); + void inc_refcount(int index); + // decreases the peer count for the given piece + // (used when a peer disconnects) void dec_refcount(int index); // This indicates that we just received this piece diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index 7f32fd93e..d6f7c20de 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -197,17 +197,12 @@ namespace libtorrent // when we get a have- or bitfield- messages, this is called for every // piece a peer has gained. - // returns true if this piece is interesting (i.e. if we would like to download it) - bool peer_has(int index) - { - return m_picker.inc_refcount(index); - } + void peer_has(int index) + { m_picker.inc_refcount(index); } // when peer disconnects, this is called for every piece it had void peer_lost(int index) - { - m_picker.dec_refcount(index); - } + { m_picker.dec_refcount(index); } int block_size() const { return m_block_size; } diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 25e208356..00331265d 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -380,8 +380,10 @@ 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->peer_has(index); + if (!m_torrent->have_piece(index) && !is_interesting()) m_torrent->get_policy().peer_is_interesting(*this); } break; @@ -401,8 +403,6 @@ bool libtorrent::peer_connection::dispatch_message(int received) #ifndef NDEBUG (*m_logger) << m_socket->sender().as_string() << " <== BITFIELD\n"; #endif - 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) @@ -418,7 +418,6 @@ bool libtorrent::peer_connection::dispatch_message(int received) m_have_piece[i] = false; m_torrent->peer_lost(i); } - if (!have) is_seed = false; } // shuffle the piece list @@ -432,11 +431,12 @@ bool libtorrent::peer_connection::dispatch_message(int received) ++i) { int index = *i; - if (m_torrent->peer_has(index)) + m_torrent->peer_has(index); + if (!m_torrent->have_piece(index)) interesting = true; } - if (is_seed) + if (piece_list.empty()) { #ifndef NDEBUG (*m_logger) << m_socket->sender().as_string() << " *** THIS IS A SEED ***\n"; diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index 83c96231d..171f208ee 100755 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -326,7 +326,7 @@ namespace libtorrent #endif } - bool piece_picker::inc_refcount(int i) + void piece_picker::inc_refcount(int i) { assert(i >= 0); assert(i < m_piece_map.size()); @@ -336,14 +336,16 @@ namespace libtorrent m_piece_map[i].peer_count++; - if (index == 0xffffff) return false; + // if we have the piece, we don't have to move + // any entries in the piece_info vector + if (index == 0xffffff) return; move(m_piece_map[i].downloading, peer_count, index); #ifndef NDEBUG // integrity_check(); #endif - return true; + return; } void piece_picker::dec_refcount(int i) diff --git a/src/policy.cpp b/src/policy.cpp index 3a7776818..1a705c53b 100755 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -568,6 +568,8 @@ namespace libtorrent // called when a peer is no longer interested in us void policy::not_interested(peer_connection& c) { + // TODO: return the diff() of this peer to the + // pool of undistributed free upload } bool policy::unchoke_one_peer() diff --git a/src/torrent.cpp b/src/torrent.cpp index 9ff799e8e..beb1dfac4 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -411,9 +411,23 @@ namespace libtorrent m_picker.abort_download(*i); } - for (std::size_t i = 0; i < torrent_file().num_pieces(); ++i) + std::vector piece_list; + const std::vector& pieces = p->get_bitfield(); + + for (std::vector::const_iterator i = pieces.begin(); + i != pieces.end(); + ++i) { - if (p->has_piece(i)) peer_lost(i); + if (*i) piece_list.push_back(i - pieces.begin()); + } + + std::random_shuffle(piece_list.begin(), piece_list.end()); + + for (std::vector::iterator i = piece_list.begin(); + i != piece_list.end(); + ++i) + { + peer_lost(*i); } // std::cout << p->get_socket()->sender().as_string() << " *** DISCONNECT\n";