From e0e06bc87abfe6416915be7b89e5821bf5968868 Mon Sep 17 00:00:00 2001 From: Steven Siloti Date: Thu, 23 Jul 2015 20:48:35 -0700 Subject: [PATCH] switch to peer_connection_handle for optimistic unchoking This is necessary to enable external plugins since the vector is passed to plugins via on_optimistic_unchoke. --- include/libtorrent/extensions.hpp | 3 +-- src/session_impl.cpp | 26 ++++++++++++++++++-------- 2 files changed, 19 insertions(+), 10 deletions(-) diff --git a/include/libtorrent/extensions.hpp b/include/libtorrent/extensions.hpp index b64774373..f59e2d68c 100644 --- a/include/libtorrent/extensions.hpp +++ b/include/libtorrent/extensions.hpp @@ -190,7 +190,6 @@ namespace libtorrent struct bitfield; class alert; struct torrent_plugin; - struct torrent_peer; struct add_torrent_params; // this is the base class for a session plugin. One primary feature @@ -232,7 +231,7 @@ namespace libtorrent // optimistically unchoked. // if the plugin returns true then the ordering provided will be // used and no other plugin will be allowed to change it. - virtual bool on_optimistic_unchoke(std::vector& /* peers */) + virtual bool on_optimistic_unchoke(std::vector& /* peers */) { return false; } // called when saving settings state diff --git a/src/session_impl.cpp b/src/session_impl.cpp index cb188a441..bd7108af5 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -3638,12 +3638,24 @@ retry: } } + namespace { + struct last_optimistic_unchoke_cmp + { + bool operator()(peer_connection_handle const& l + , peer_connection_handle const& r) + { + return l.native_handle()->peer_info_struct()->last_optimistically_unchoked + < r.native_handle()->peer_info_struct()->last_optimistically_unchoked; + } + }; + } + void session_impl::recalculate_optimistic_unchoke_slots() { TORRENT_ASSERT(is_single_thread()); if (m_stats_counters[counters::num_unchoke_slots] == 0) return; - std::vector opt_unchoke; + std::vector opt_unchoke; for (connection_map::iterator i = m_connections.begin() , end(m_connections.end()); i != end; ++i) @@ -3660,7 +3672,7 @@ retry: if (pi->optimistically_unchoked) { TORRENT_ASSERT(!p->is_choked()); - opt_unchoke.push_back(pi); + opt_unchoke.push_back(peer_connection_handle(*i)); } if (!p->is_connecting() @@ -3671,7 +3683,7 @@ retry: && !p->ignore_unchoke_slots() && t->valid_metadata()) { - opt_unchoke.push_back(pi); + opt_unchoke.push_back(peer_connection_handle(*i)); } } @@ -3683,9 +3695,7 @@ retry: // sort all candidates based on when they were last optimistically // unchoked. - std::sort(opt_unchoke.begin(), opt_unchoke.end() - , boost::bind(&torrent_peer::last_optimistically_unchoked, _1) - < boost::bind(&torrent_peer::last_optimistically_unchoked, _2)); + std::sort(opt_unchoke.begin(), opt_unchoke.end(), last_optimistic_unchoke_cmp()); #ifndef TORRENT_DISABLE_EXTENSIONS for (ses_extension_list_t::iterator i = m_ses_extensions.begin() @@ -3702,10 +3712,10 @@ retry: // unchoke the first num_opt_unchoke peers in the candidate set // and make sure that the others are choked - for (std::vector::iterator i = opt_unchoke.begin() + for (std::vector::iterator i = opt_unchoke.begin() , end(opt_unchoke.end()); i != end; ++i) { - torrent_peer* pi = *i; + torrent_peer* pi = i->native_handle()->peer_info_struct(); if (num_opt_unchoke > 0) { --num_opt_unchoke;