diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 17cf4a093..1bb9def24 100755 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -252,9 +252,6 @@ namespace libtorrent bool support_extensions() const { return m_supports_extensions; } - const boost::posix_time::time_duration& last_piece_time() const - { return m_last_piece_time; } - // a connection is local if it was initiated by us. // if it was an incoming connection, it is remote bool is_local() const @@ -523,10 +520,6 @@ namespace libtorrent // message from this peer boost::posix_time::ptime m_last_piece; - // the time it took for the peer to send the piece - // message - boost::posix_time::time_duration m_last_piece_time; - // this is true if this connection has been added // to the list of connections that will be closed. bool m_disconnecting; diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index da3fcc003..a3ad01078 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -106,7 +106,6 @@ namespace libtorrent , m_trust_points(0) , m_num_invalid_requests(0) , m_last_piece(boost::posix_time::second_clock::local_time()) - , m_last_piece_time(boost::posix_time::seconds(0)) , m_disconnecting(false) , m_became_uninterested(boost::posix_time::second_clock::local_time()) , m_became_uninteresting(boost::posix_time::second_clock::local_time()) @@ -171,7 +170,6 @@ namespace libtorrent , m_trust_points(0) , m_num_invalid_requests(0) , m_last_piece(boost::posix_time::second_clock::local_time()) - , m_last_piece_time(boost::posix_time::seconds(0)) , m_disconnecting(false) , m_became_uninterested(boost::posix_time::second_clock::local_time()) , m_became_uninteresting(boost::posix_time::second_clock::local_time()) @@ -647,9 +645,6 @@ namespace libtorrent if (m_recv_pos < m_packet_size) return; - m_last_piece_time = m_last_piece - - boost::posix_time::second_clock::local_time(); - const char* ptr = &m_recv_buffer[1]; peer_request p; p.piece = detail::read_int32(ptr); diff --git a/src/policy.cpp b/src/policy.cpp index 2c4bc4aac..89f25d571 100755 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -63,7 +63,7 @@ namespace { // the limits of the download queue size max_request_queue = 16, - min_request_queue = 4, + min_request_queue = 2, // the amount of free upload allowed before // the peer is choked @@ -157,78 +157,81 @@ namespace // (then we can cancel those and request them // from this peer instead) - peer_connection* peer = 0; - float min_weight = std::numeric_limits::max(); - // find the peer with the lowest download - // speed that also has a piece that this - // peer could send us - for (torrent::peer_iterator i = t.begin(); - i != t.end(); - ++i) + while (num_requests > 0) { - // don't try to take over blocks from ourself - if (i->second == &c) - continue; + peer_connection* peer = 0; + float min_weight = std::numeric_limits::max(); + // find the peer with the lowest download + // speed that also has a piece that this + // peer could send us + for (torrent::peer_iterator i = t.begin(); + i != t.end(); + ++i) + { + // don't try to take over blocks from ourself + if (i->second == &c) + continue; - // ignore all peers in the ignore list - if (std::find(ignore.begin(), ignore.end(), i->second) != ignore.end()) - continue; + // ignore all peers in the ignore list + if (std::find(ignore.begin(), ignore.end(), i->second) != ignore.end()) + continue; - const std::deque& queue = i->second->download_queue(); - const int queue_size = (int)i->second->download_queue().size(); + const std::deque& queue = i->second->download_queue(); + const int queue_size = (int)i->second->download_queue().size(); + const float weight = queue_size == 0 + ? std::numeric_limits::max() + : i->second->statistics().down_peak() / queue_size; + + if (weight < min_weight + && std::find_first_of( + busy_pieces.begin() + , busy_pieces.end() + , queue.begin() + , queue.end()) != busy_pieces.end()) + { + peer = i->second; + min_weight = weight; + } + } + + if (peer == 0) + { + // we probably couldn't request the block because + // we are ignoring some peers + return; + } + + // this peer doesn't have a faster connection than the + // slowest peer. Don't take over any blocks + const int queue_size = (int)c.download_queue().size(); const float weight = queue_size == 0 ? std::numeric_limits::max() - : i->second->statistics().down_peak() / queue_size; + : c.statistics().down_peak() / queue_size; - if (weight < min_weight - && std::find_first_of( - busy_pieces.begin() - , busy_pieces.end() - , queue.begin() - , queue.end()) != busy_pieces.end()) - { - peer = i->second; - min_weight = weight; - } + if (weight <= min_weight) return; + + // find a suitable block to take over from this peer + + std::deque::const_reverse_iterator common_block = + std::find_first_of( + peer->download_queue().rbegin() + , peer->download_queue().rend() + , busy_pieces.begin() + , busy_pieces.end()); + + assert(common_block != peer->download_queue().rend()); + piece_block block = *common_block; + peer->send_cancel(block); + c.send_request(block); + + // the one we interrupted may need to request a new piece + // make sure it doesn't take over a block from the peer + // that just took over its block + ignore.push_back(&c); + request_a_block(t, *peer, ignore); + + num_requests--; } - - if (peer == 0) - { - // we probably couldn't request the block because - // we are ignoring some peers - return; - } - - // this peer doesn't have a faster connection than the - // slowest peer. Don't take over any blocks - const int queue_size = (int)c.download_queue().size(); - const float weight = queue_size == 0 - ? std::numeric_limits::max() - : c.statistics().down_peak() / queue_size; - - if (weight <= min_weight) return; - - // find a suitable block to take over from this peer - - std::deque::const_reverse_iterator common_block = - std::find_first_of( - peer->download_queue().rbegin() - , peer->download_queue().rend() - , busy_pieces.begin() - , busy_pieces.end()); - - assert(common_block != peer->download_queue().rend()); - piece_block block = *common_block; - peer->send_cancel(block); - c.send_request(block); - - // the one we interrupted may need to request a new piece - // make sure it doesn't take over a block from the peer - // that just took over its block - ignore.push_back(&c); - request_a_block(t, *peer, ignore); - - num_requests--; }