diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 9224f3cef..063c96c3f 100755 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -214,7 +214,8 @@ namespace libtorrent // quota is unlimited. int send_quota_left() const { return m_send_quota_left; } - void update_send_quota_left() { + void update_send_quota_left() + { m_send_quota_left = upload_bandwidth.given; if (m_send_quota_left > 0) send_buffer_updated(); } diff --git a/src/allocate_resources.cpp b/src/allocate_resources.cpp index 44eb7114f..cca105fe0 100644 --- a/src/allocate_resources.cpp +++ b/src/allocate_resources.cpp @@ -35,7 +35,8 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -namespace libtorrent { +namespace libtorrent +{ namespace { int saturated_add(int a, int b) @@ -74,7 +75,7 @@ namespace libtorrent { assert(num_resources > 0); assert(r->given <= r->wanted); - int accepted=std::min(num_resources, r->wanted - r->given); + int accepted = std::min(num_resources, r->wanted - r->given); assert(accepted >= 0); r->given += accepted; @@ -99,48 +100,55 @@ namespace libtorrent { assert(total_wanted>=0); return total_wanted; } - } #ifndef NDEBUG - class allocate_resources_contract_check - { - int resources; - std::vector & requests; - public: - allocate_resources_contract_check(int resources_,std::vector & requests_) - : resources(resources_) - , requests(requests_) - { - assert(resources >= 0); - for(int i=0;i<(int)requests.size();i++) - { - assert(requests[i]->used >= 0); - assert(requests[i]->wanted >= 0); - assert(requests[i]->given >= 0); - } - } - ~allocate_resources_contract_check() + class allocate_resources_contract_check { - int sum_given = 0; - int sum_wanted = 0; - for(int i=0;i<(int)requests.size();i++) - { - assert(requests[i]->used >= 0); - assert(requests[i]->wanted >= 0); - assert(requests[i]->given >= 0); - assert(requests[i]->given <= requests[i]->wanted); - sum_given = saturated_add(sum_given, requests[i]->given); - sum_wanted = saturated_add(sum_wanted, requests[i]->wanted); + int resources; + std::vector & requests; + + public: + + allocate_resources_contract_check( + int resources_ + , std::vector& requests_) + : resources(resources_) + , requests(requests_) + { + assert(resources >= 0); + for (int i = 0; i < (int)requests.size(); ++i) + { + assert(requests[i]->used >= 0); + assert(requests[i]->wanted >= 0); + assert(requests[i]->given >= 0); + } } - assert(sum_given == std::min(resources,sum_wanted)); - } - }; + + ~allocate_resources_contract_check() + { + int sum_given = 0; + int sum_wanted = 0; + for (int i = 0; i < (int)requests.size(); ++i) + { + assert(requests[i]->used >= 0); + assert(requests[i]->wanted >= 0); + assert(requests[i]->given >= 0); + assert(requests[i]->given <= requests[i]->wanted); + + sum_given = saturated_add(sum_given, requests[i]->given); + sum_wanted = saturated_add(sum_wanted, requests[i]->wanted); + } + assert(sum_given == std::min(resources,sum_wanted)); + } + }; + #endif + } // namespace unnamed - void allocate_resources(int resources, - std::vector & requests) + void allocate_resources(int resources + , std::vector& requests) { #ifndef NDEBUG allocate_resources_contract_check @@ -158,10 +166,10 @@ namespace libtorrent { { // Resources are scarce - for(int i=0;i < (int)requests.size();i++) + for (int i = 0; i < (int)requests.size(); ++i) requests[i]->given = 0; - if(resources == 0) + if (resources == 0) return; int resources_to_distribute = @@ -172,13 +180,17 @@ namespace libtorrent { if (resources_to_distribute == 0) return; - assert(resources_to_distribute>0); + assert(resources_to_distribute > 0); - std::random_shuffle(requests.begin(),requests.end()); - std::sort(requests.begin(),requests.end(),by_used); + std::random_shuffle(requests.begin(), requests.end()); + std::sort(requests.begin(), requests.end(), by_used); while(resources_to_distribute > 0) - for(int i = 0; i < (int)requests.size() && resources_to_distribute>0; i++) + { + for(int i = 0; + i < (int)requests.size() && resources_to_distribute > 0; + ++i) + { resources_to_distribute -= give( requests[i], @@ -187,7 +199,9 @@ namespace libtorrent { round_up_division( (int)resources_to_distribute, (int)requests.size()-i))); + } + } assert(resources_to_distribute == 0); } } -} \ No newline at end of file +} diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 12186c54d..a42f14b55 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -1266,9 +1266,9 @@ namespace libtorrent /* size_type diff = share_diff(); - enum { block_limit=2 }; // how many blocks difference is considered unfair + enum { block_limit = 2 }; // how many blocks difference is considered unfair - // if the peer has been choked, send tha current piece + // if the peer has been choked, send the current piece // as fast as possible if (diff > block_limit*m_torrent->block_size() || m_torrent->is_seed() || is_choked()) { diff --git a/src/session.cpp b/src/session.cpp index aefcd44a6..c40656398 100755 --- a/src/session.cpp +++ b/src/session.cpp @@ -480,8 +480,9 @@ namespace libtorrent boost::shared_ptr c( new peer_connection(*this, m_selector, s)); - if (m_upload_rate != -1) { - c->upload_bandwidth.given=0; + if (m_upload_rate != -1) + { + c->upload_bandwidth.given = 0; c->update_send_quota_left(); } @@ -636,8 +637,8 @@ namespace libtorrent else if (i->second->should_request()) { m_tracker_manager.queue_request( - i->second->generate_tracker_request(m_listen_port), - boost::get_pointer(i->second)); + i->second->generate_tracker_request(m_listen_port) + , boost::get_pointer(i->second)); } i->second->second_tick(); @@ -647,10 +648,11 @@ namespace libtorrent // distribute the maximum upload rate among the peers - control_upload_rates(m_upload_rate == -1 - ? std::numeric_limits::max() - : 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(); @@ -891,7 +893,7 @@ namespace libtorrent = m_impl.m_connections.begin(); i != m_impl.m_connections.end();) { - i->second->upload_bandwidth.given=std::numeric_limits::max(); + i->second->upload_bandwidth.given = std::numeric_limits::max(); i->second->update_send_quota_left(); } }