From a25addd49675cfbad583d61b6eb3accdd80a259d Mon Sep 17 00:00:00 2001 From: Magnus Jonsson Date: Thu, 12 Feb 2004 14:41:39 +0000 Subject: [PATCH] *** empty log message *** --- src/peer_connection.cpp | 4 +-- src/piece_picker.cpp | 2 ++ src/session.cpp | 77 ++++++++++++++++++++++++++++++++++++++++- 3 files changed, 80 insertions(+), 3 deletions(-) diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index a3ad01078..5e2a1be60 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -1716,12 +1716,12 @@ namespace libtorrent void peer_connection::check_invariant() const { assert(has_data() == m_selector.is_writability_monitored(m_socket)); - +/* assert(m_num_pieces == std::count( m_have_piece.begin() , m_have_piece.end() , true)); - } +*/ } #endif bool peer_connection::has_timed_out() const diff --git a/src/piece_picker.cpp b/src/piece_picker.cpp index a448eebf8..cc60d1b33 100755 --- a/src/piece_picker.cpp +++ b/src/piece_picker.cpp @@ -424,7 +424,9 @@ namespace libtorrent // partial is pieces that are partially being downloaded, and // parts of them may be free for download as well, the // partially donloaded pieces will be prioritized + assert(m_piece_info.begin()!=m_piece_info.end()); std::vector >::const_iterator free = m_piece_info.begin()+1; + assert(m_downloading_piece_info.begin()!=m_downloading_piece_info.end()); std::vector >::const_iterator partial = m_downloading_piece_info.begin()+1; while((free != m_piece_info.end()) || (partial != m_downloading_piece_info.end())) diff --git a/src/session.cpp b/src/session.cpp index 27c09a11e..a12732954 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -66,6 +66,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/alert_types.hpp" #include "libtorrent/invariant_check.hpp" #include "libtorrent/file.hpp" +#include "libtorrent/allocate_resources.hpp" #if defined(_MSC_VER) && _MSC_VER < 1300 namespace std @@ -77,8 +78,77 @@ namespace std namespace { + int saturated_add(int a, int b) + { + assert(a>=0); + assert(b>=0); + int sum=a+b; + if(sum<0) + sum=std::numeric_limits::max(); + assert(sum>=a && sum>=b); + return sum; + } + + void control_upload_rates( + int upload_limit + , libtorrent::detail::session_impl::connection_map connections) + { + using namespace libtorrent; + std::vector peers; + + assert(upload_limit >= 0); + + for (detail::session_impl::connection_map::iterator c = connections.begin(); + c != connections.end(); ++c) + { + boost::shared_ptr p = c->second; + int estimated_upload_capacity= + p->has_data() ? (int)ceil(p->statistics().upload_rate()) // std::max(10,(int)ceil(p->statistics().upload_rate()*1.1f)) + : 1; + + int limit=p->send_quota_limit(); + if(limit==-1) + limit=std::numeric_limits::max(); + + peers.push_back(resource_consumer(p,limit,estimated_upload_capacity)); + } + + allocate_resources(upload_limit, peers); + + for (std::vector::iterator r=peers.begin(); + r!=peers.end(); ++r) + { + boost::any_cast > + (r->who())->set_send_quota(r->allowed_use()); + } + +#ifndef NDEBUG + { + int sum_quota = 0; + int sum_quota_limit = 0; + for (detail::session_impl::connection_map::iterator i = connections.begin(); + i != connections.end(); + ++i) + { + peer_connection& p = *i->second; + + int quota=p.send_quota(); + int quota_limit=p.send_quota_limit(); + if(quota==-1) + quota=std::numeric_limits::max(); + if(quota_limit==-1) + quota_limit=std::numeric_limits::max(); + + sum_quota = saturated_add(sum_quota,quota); + sum_quota_limit = saturated_add(sum_quota_limit,quota_limit); + } + assert(sum_quota == std::min(upload_limit,sum_quota_limit)); + } +#endif + } +/* // This struct is used by control_upload_rates() below. It keeps // track how much bandwidth has been allocated to each connection // and other relevant information to assist in the allocation process. @@ -268,6 +338,7 @@ namespace } #endif } +*/ } namespace libtorrent @@ -749,7 +820,11 @@ namespace libtorrent purge_connections(); // distribute the maximum upload rate among the peers - control_upload_rates(m_upload_rate, m_connections); + + control_upload_rates(m_upload_rate == -1 + ? std::numeric_limits::max() + : m_upload_rate + ,m_connections); m_tracker_manager.tick();