From 80aee329727dfed0d7b373b750c4a9d657fbea1d Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Mon, 10 Sep 2007 08:07:18 +0000 Subject: [PATCH] fixed piece picker problem when a downloading piece could not be picked with the right speed category --- examples/client_test.cpp | 5 +++-- include/libtorrent/peer_connection.hpp | 3 +-- include/libtorrent/peer_info.hpp | 5 +++++ src/peer_connection.cpp | 5 +++-- src/piece_picker.cpp | 19 ++++++++++++++++--- src/policy.cpp | 2 ++ 6 files changed, 30 insertions(+), 9 deletions(-) diff --git a/examples/client_test.cpp b/examples/client_test.cpp index a30455d10..fbf7c5376 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -303,7 +303,7 @@ int peer_index(libtorrent::tcp::endpoint addr, std::vector const& peers) { using namespace libtorrent; - out << " down (total) up (total) que req flags source fail hshf sndb inactive wait disk block-progress " + out << " down (total) up (total) sent-req recv flags source fail hshf sndb inactive wait disk block-progress " #ifndef TORRENT_DISABLE_RESOLVE_COUNTRIES "country " #endif @@ -321,7 +321,8 @@ void print_peer_info(std::ostream& out, std::vector const << "(" << add_suffix(i->total_download) << ") " << esc("0") << esc("31") << (i->up_speed > 0 ? add_suffix(i->up_speed) + "/s ": " ") << "(" << add_suffix(i->total_upload) << ") " << esc("0") - << to_string(i->download_queue_length, 3) << " " + << to_string(i->download_queue_length, 3) << " (" + << to_string(i->target_dl_queue_length, 3) << ") " << to_string(i->upload_queue_length, 3) << " " << ((i->flags & peer_info::interesting)?'I':'.') << ((i->flags & peer_info::choked)?'C':'.') diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 5e65f767a..ea16a8d0a 100755 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -155,8 +155,7 @@ namespace libtorrent int prefer_whole_pieces() const { - if (m_prefer_whole_pieces == 0) - return peer_info_struct() && peer_info_struct()->on_parole ? 1 : 0; + if (on_parole()) return 1; return m_prefer_whole_pieces; } diff --git a/include/libtorrent/peer_info.hpp b/include/libtorrent/peer_info.hpp index 82a4675e7..046df2a6b 100755 --- a/include/libtorrent/peer_info.hpp +++ b/include/libtorrent/peer_info.hpp @@ -117,6 +117,11 @@ namespace libtorrent // for yet int download_queue_length; + // the number of requests that is + // tried to be maintained (this is + // typically a function of download speed) + int target_dl_queue_length; + // this is the number of requests // the peer has sent to us // that we haven't sent yet diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index a3d522b89..ad2102f0d 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -2019,8 +2019,9 @@ namespace libtorrent p.load_balancing = total_free_upload(); - p.download_queue_length = (int)download_queue().size(); - p.upload_queue_length = (int)upload_queue().size(); + p.download_queue_length = int(download_queue().size() + m_request_queue.size()); + p.target_dl_queue_length = int(desired_queue_size()); + p.upload_queue_length = int(upload_queue().size()); if (boost::optional ret = downloading_piece_progress()) { diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index 8c8911e7c..8fa623fa3 100755 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -1125,7 +1125,7 @@ namespace libtorrent , interesting_blocks, backup_blocks, num_blocks , prefer_whole_pieces, peer, speed, on_parole); - if (num_blocks == 0) return; + if (num_blocks <= 0) return; if (rarest_first) { @@ -1364,6 +1364,13 @@ namespace libtorrent if (prefer_whole_pieces > 0 && !exclusive_active) continue; + // don't pick too many back-up blocks + if (i->state != none + && i->state != speed + && !exclusive_active + && int(backup_blocks.size()) >= num_blocks) + continue; + for (int j = 0; j < num_blocks_in_piece; ++j) { // ignore completed blocks and already requested blocks @@ -1412,9 +1419,15 @@ namespace libtorrent if (num_blocks <= 0) return 0; if (on_parole) return num_blocks; + int to_copy; + if (prefer_whole_pieces == 0) + to_copy = (std::min)(int(backup_blocks.size()), num_blocks); + else + to_copy = int(backup_blocks.size()); + interesting_blocks.insert(interesting_blocks.end() - , backup_blocks.begin(), backup_blocks.end()); - num_blocks -= int(backup_blocks.size()); + , backup_blocks.begin(), backup_blocks.begin() + to_copy); + num_blocks -= to_copy; backup_blocks.clear(); if (num_blocks <= 0) return 0; diff --git a/src/policy.cpp b/src/policy.cpp index 3193ae455..6e81da0d5 100755 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -284,6 +284,8 @@ namespace libtorrent for (std::vector::iterator i = interesting_pieces.begin(); i != interesting_pieces.end(); ++i) { + if (prefer_whole_pieces == 0 && num_requests <= 0) break; + if (p.is_requested(*i)) { if (num_requests <= 0) break;