From a9b3c6dd411137897a7944168f679504ea42a947 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 27 Mar 2004 22:02:31 +0000 Subject: [PATCH] *** empty log message *** --- include/libtorrent/entry.hpp | 6 ++-- include/libtorrent/resource_request.hpp | 15 +++++++-- src/allocate_resources.cpp | 45 ++++++++++++++----------- src/entry.cpp | 1 + src/http_tracker_connection.cpp | 7 ++-- src/peer_connection.cpp | 28 ++++++++++----- src/torrent.cpp | 19 +++++++---- src/torrent_handle.cpp | 4 +-- src/torrent_info.cpp | 2 +- 9 files changed, 79 insertions(+), 48 deletions(-) diff --git a/include/libtorrent/entry.hpp b/include/libtorrent/entry.hpp index c84b5dba6..4fd2fa957 100755 --- a/include/libtorrent/entry.hpp +++ b/include/libtorrent/entry.hpp @@ -61,7 +61,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -#include +#include #include #include #include @@ -111,9 +111,9 @@ namespace libtorrent // of elements. Since the info-hash is reconstructed // from an entry, it's important that the order is // preserved. - typedef std::vector > dictionary_type; + typedef std::list > dictionary_type; typedef std::string string_type; - typedef std::vector list_type; + typedef std::list list_type; typedef size_type integer_type; enum data_type diff --git a/include/libtorrent/resource_request.hpp b/include/libtorrent/resource_request.hpp index 774968f57..5823441b5 100755 --- a/include/libtorrent/resource_request.hpp +++ b/include/libtorrent/resource_request.hpp @@ -37,12 +37,21 @@ namespace libtorrent { struct resource_request { - resource_request() : used(0), wanted(0), given(0) {} + resource_request() + : used(0) + , min(0) + , max(0) + , given(0) + {} // I'm right now actively using: int used; - // I would like to use this much: - int wanted; + + // given cannot be smaller than min + // and not greater than max. + int min; + int max; + // Reply: Okay, you're allowed to use this much (a compromise): int given; }; diff --git a/src/allocate_resources.cpp b/src/allocate_resources.cpp index caaa8d16a..c4fcefebc 100644 --- a/src/allocate_resources.cpp +++ b/src/allocate_resources.cpp @@ -67,13 +67,13 @@ namespace libtorrent int give(resource_request& r, int num_resources) { assert(num_resources >= 0); - assert(r.given <= r.wanted); + assert(r.given <= r.max); - int accepted = std::min(num_resources, r.wanted - r.given); + int accepted = std::min(num_resources, r.max - r.given); assert(accepted >= 0); r.given += accepted; - assert(r.given <= r.wanted); + assert(r.given <= r.max); return accepted; } @@ -103,7 +103,7 @@ namespace libtorrent for (It i = m_start, end(m_end); i != end; ++i) { assert(((*i).*m_res).used >= 0); - assert(((*i).*m_res).wanted >= 0); + assert(((*i).*m_res).max >= 0); assert(((*i).*m_res).given >= 0); } } @@ -111,17 +111,17 @@ namespace libtorrent ~allocate_resources_contract_check() { int sum_given = 0; - int sum_wanted = 0; + int sum_max = 0; for (It i = m_start, end(m_end); i != end; ++i) { - assert(((*i).*m_res).wanted >= 0); + assert(((*i).*m_res).max >= 0); assert(((*i).*m_res).given >= 0); - assert(((*i).*m_res).given <= ((*i).*m_res).wanted); + assert(((*i).*m_res).given <= ((*i).*m_res).max); sum_given = saturated_add(sum_given, ((*i).*m_res).given); - sum_wanted = saturated_add(sum_wanted, ((*i).*m_res).wanted); + sum_max = saturated_add(sum_max, ((*i).*m_res).max); } - assert(sum_given == std::min(m_resources, sum_wanted)); + assert(sum_given == std::min(m_resources, sum_max)); } }; @@ -148,25 +148,30 @@ namespace libtorrent // Just give everyone what they want. for (It i = start; i != end; ++i) { - ((*i).*res).given = ((*i).*res).wanted; + ((*i).*res).given = ((*i).*res).max; } return; } // Resources are scarce - int total_wanted = 0; + int sum_max = 0; + int sum_min = 0; for (It i = start; i != end; ++i) { - ((*i).*res).given = 0; - total_wanted = saturated_add(total_wanted, ((*i).*res).wanted); + sum_max = saturated_add(sum_max, ((*i).*res).max); + assert(((*i).*res).min < std::numeric_limits::max()); + assert(((*i).*res).min >= 0); + assert(((*i).*res).min <= ((*i).*res).max); + sum_min += ((*i).*res).min; + ((*i).*res).given = ((*i).*res).min; } - if (resources == 0 || total_wanted == 0) + if (resources == 0 || sum_max == 0) return; - int resources_to_distribute = std::min(resources, total_wanted); - assert(resources_to_distribute > 0); + int resources_to_distribute = std::min(resources, sum_max) - sum_min; + assert(resources_to_distribute >= 0); while (resources_to_distribute > 0) { @@ -175,9 +180,9 @@ namespace libtorrent for (It i = start; i != end; ++i) { resource_request& r = (*i).*res; - if(r.given == r.wanted) continue; + if(r.given == r.max) continue; - assert(r.given < r.wanted); + assert(r.given < r.max); max_used = std::max(max_used, (size_type)r.used + 1); total_used += (size_type)r.used + 1; @@ -195,9 +200,9 @@ namespace libtorrent for (It i = start; i != end && resources_to_distribute > 0; ++i) { resource_request& r = (*i).*res; - if(r.given == r.wanted) continue; + if(r.given == r.max) continue; - assert(r.given < r.wanted); + assert(r.given < r.max); size_type used = (size_type)r.used + 1; size_type toGive = used * kNumer / kDenom; diff --git a/src/entry.cpp b/src/entry.cpp index 2649197a8..2c2c4d94c 100755 --- a/src/entry.cpp +++ b/src/entry.cpp @@ -30,6 +30,7 @@ POSSIBILITY OF SUCH DAMAGE. */ +#include #include "libtorrent/entry.hpp" #include diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp index 76a635975..b554a98bc 100755 --- a/src/http_tracker_connection.cpp +++ b/src/http_tracker_connection.cpp @@ -152,10 +152,11 @@ namespace libtorrent m_send_buffer += event_string[req.event - 1]; } m_send_buffer += "&key="; - // TODO: this should be encoded as hex - m_send_buffer += boost::lexical_cast((unsigned int)req.key); + std::stringstream key_string; + key_string << std::hex << req.key; + m_send_buffer += key_string.str(); m_send_buffer += "&compact=1"; - m_send_buffer += "&num_want="; + m_send_buffer += "&numwant="; m_send_buffer += boost::lexical_cast(req.num_want); // extension that tells the tracker that diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 48458cbdd..23793d0ce 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -107,10 +107,12 @@ namespace libtorrent , m_disconnecting(false) , m_became_uninterested(boost::posix_time::second_clock::local_time()) , m_became_uninteresting(boost::posix_time::second_clock::local_time()) -// , m_upload_bandwidth_quota_used(0) { INVARIANT_CHECK; + m_upload_bandwidth_quota.min = 10; + m_upload_bandwidth_quota.max = 10; + assert(!m_socket->is_blocking()); assert(m_torrent != 0); @@ -174,6 +176,14 @@ namespace libtorrent { INVARIANT_CHECK; + // upload bandwidth will only be given to connections + // that are part of a torrent. Since this is an incoming + // connection, we have to give it some initial bandwidth + // to send the handshake + m_upload_bandwidth_quota.min = 10; + m_upload_bandwidth_quota.max = 400; + m_upload_bandwidth_quota.given = 400; + assert(!m_socket->is_blocking()); std::fill(m_peer_id.begin(), m_peer_id.end(), 0); @@ -1319,9 +1329,9 @@ namespace libtorrent // than we have uploaded OR if we are a seed // have an unlimited upload rate if(!m_send_buffer.empty() || (!m_requests.empty() && !is_choked())) - m_upload_bandwidth_quota.wanted = std::numeric_limits::max(); + m_upload_bandwidth_quota.max = std::numeric_limits::max(); else - m_upload_bandwidth_quota.wanted = 1; + m_upload_bandwidth_quota.max = m_upload_bandwidth_quota.min; } else { @@ -1336,17 +1346,17 @@ namespace libtorrent size_type soon_downloaded = have_downloaded + (size_type)(download_speed * break_even_time*1.5); - if(m_torrent->ratio() != 1.0f) + if(m_torrent->ratio() != 1.f) soon_downloaded = (size_type)(soon_downloaded*(double)m_torrent->ratio()); double upload_speed_limit = (soon_downloaded - have_uploaded + bias) / break_even_time; - upload_speed_limit=std::max(upload_speed_limit,1.0); - upload_speed_limit=std::min(upload_speed_limit, - (double)std::numeric_limits::max()); + upload_speed_limit = std::min(upload_speed_limit, + (double)std::numeric_limits::max()); - m_upload_bandwidth_quota.wanted = (int) upload_speed_limit; + m_upload_bandwidth_quota.max + = std::max((int)upload_speed_limit, m_upload_bandwidth_quota.min); } /* @@ -1924,7 +1934,7 @@ namespace libtorrent return; } - assert(send_quota_left()>0); + assert(send_quota_left() > 0); assert(has_data()); if (!m_added_to_selector) { diff --git a/src/torrent.cpp b/src/torrent.cpp index 606f4c505..4e577a730 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -197,6 +197,7 @@ namespace libtorrent m_currently_trying_tracker = 0; m_duration = interval; + m_next_request = boost::posix_time::second_clock::local_time() + boost::posix_time::seconds(m_duration); // connect to random peers from the list std::random_shuffle(peer_list.begin(), peer_list.end()); @@ -412,7 +413,9 @@ namespace libtorrent tracker_request torrent::generate_tracker_request() { m_duration = 1800; - m_next_request = boost::posix_time::second_clock::local_time() + boost::posix_time::seconds(m_duration); + m_next_request + = boost::posix_time::second_clock::local_time() + + boost::posix_time::seconds(tracker_retry_delay); tracker_request req; req.info_hash = m_torrent_file.info_hash(); @@ -637,7 +640,8 @@ namespace libtorrent } m_upload_bandwidth_quota.used = 0; - m_upload_bandwidth_quota.wanted = 0; + m_upload_bandwidth_quota.max = 0; + m_upload_bandwidth_quota.min = 0; for (peer_iterator i = m_connections.begin(); i != m_connections.end(); @@ -647,13 +651,14 @@ namespace libtorrent m_stat += p->statistics(); p->second_tick(); m_upload_bandwidth_quota.used += p->m_upload_bandwidth_quota.used; - m_upload_bandwidth_quota.wanted = saturated_add( - m_upload_bandwidth_quota.wanted - , p->m_upload_bandwidth_quota.wanted); + m_upload_bandwidth_quota.min += p->m_upload_bandwidth_quota.min; + m_upload_bandwidth_quota.max = saturated_add( + m_upload_bandwidth_quota.max + , p->m_upload_bandwidth_quota.max); } - m_upload_bandwidth_quota.wanted - = std::min(m_upload_bandwidth_quota.wanted, m_upload_bandwidth_limit); + m_upload_bandwidth_quota.max + = std::min(m_upload_bandwidth_quota.max, m_upload_bandwidth_limit); m_stat.second_tick(); } diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index 97725db53..2ce6caa3b 100755 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -451,10 +451,10 @@ namespace libtorrent else p.upload_limit = peer->upload_bandwidth_quota()->given; - if (peer->upload_bandwidth_quota()->wanted == std::numeric_limits::max()) + if (peer->upload_bandwidth_quota()->max == std::numeric_limits::max()) p.upload_ceiling = -1; else - p.upload_ceiling = peer->upload_bandwidth_quota()->wanted; + p.upload_ceiling = peer->upload_bandwidth_quota()->max; p.load_balancing = peer->total_free_upload(); diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index 74a0455d2..94d6ccd8c 100755 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -138,7 +138,7 @@ namespace libtorrent for (entry::list_type::const_iterator k = ll.begin(); k != ll.end(); ++k) { announce_entry e; - e.tier = (int)(j - l.begin()); + e.tier = (int)std::distance(l.begin(), j); e.url = k->string(); m_urls.push_back(e); }