simplify and clean up peer_list usage in torrent.cpp (#772)

This commit is contained in:
Arvid Norberg 2016-06-01 01:05:17 -04:00
parent 558c3af360
commit 92cd0ec7d1
3 changed files with 33 additions and 47 deletions

View File

@ -176,10 +176,10 @@ namespace libtorrent
using iterator = peers_t::iterator; using iterator = peers_t::iterator;
using const_iterator = peers_t::const_iterator; using const_iterator = peers_t::const_iterator;
iterator begin_peer() { return m_peers.begin(); } iterator begin() { return m_peers.begin(); }
iterator end_peer() { return m_peers.end(); } iterator end() { return m_peers.end(); }
const_iterator begin_peer() const { return m_peers.begin(); } const_iterator begin() const { return m_peers.begin(); }
const_iterator end_peer() const { return m_peers.end(); } const_iterator end() const { return m_peers.end(); }
std::pair<iterator, iterator> find_peers(address const& a) std::pair<iterator, iterator> find_peers(address const& a)
{ {

View File

@ -38,13 +38,12 @@ POSSIBILITY OF SUCH DAMAGE.
#include <set> #include <set>
#include <list> #include <list>
#include <deque> #include <deque>
#include <limits> // for numeric_limits
#include <memory> // for unique_ptr
#include "libtorrent/aux_/disable_warnings_push.hpp" #include "libtorrent/aux_/disable_warnings_push.hpp"
#include <boost/limits.hpp>
#include <boost/tuple/tuple.hpp>
#include <boost/enable_shared_from_this.hpp> #include <boost/enable_shared_from_this.hpp>
#include <boost/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp> #include <boost/shared_ptr.hpp>
#include <boost/version.hpp> #include <boost/version.hpp>
@ -184,7 +183,7 @@ namespace libtorrent
// it in torrent::need_picker(). In order to tell the // it in torrent::need_picker(). In order to tell the
// difference between having everything and nothing in // difference between having everything and nothing in
// the case there is no piece picker, see m_have_all. // the case there is no piece picker, see m_have_all.
boost::scoped_ptr<piece_picker> m_picker; std::unique_ptr<piece_picker> m_picker;
// TODO: make this a raw pointer. perhaps keep the shared_ptr // TODO: make this a raw pointer. perhaps keep the shared_ptr
// around further down the object to maintain an owner // around further down the object to maintain an owner
@ -258,7 +257,7 @@ namespace libtorrent
// the state of this torrent (queued, checking, downloading, etc.) // the state of this torrent (queued, checking, downloading, etc.)
boost::uint32_t m_state:3; boost::uint32_t m_state:3;
boost::scoped_ptr<peer_list> m_peer_list; std::unique_ptr<peer_list> m_peer_list;
}; };
// a torrent is a class that holds information // a torrent is a class that holds information
@ -1294,13 +1293,13 @@ namespace libtorrent
// used if there is any resume data. Some of the information from the // used if there is any resume data. Some of the information from the
// add_torrent_params struct are needed later in the torrent object's life // add_torrent_params struct are needed later in the torrent object's life
// cycle, and not in the constructor. So we need to save if away here // cycle, and not in the constructor. So we need to save if away here
boost::scoped_ptr<add_torrent_params> m_add_torrent_params; std::unique_ptr<add_torrent_params> m_add_torrent_params;
// if the torrent is started without metadata, it may // if the torrent is started without metadata, it may
// still be given a name until the metadata is received // still be given a name until the metadata is received
// once the metadata is received this field will no // once the metadata is received this field will no
// longer be used and will be reset // longer be used and will be reset
boost::scoped_ptr<std::string> m_name; std::unique_ptr<std::string> m_name;
storage_constructor_type m_storage_constructor; storage_constructor_type m_storage_constructor;

View File

@ -1044,10 +1044,9 @@ namespace libtorrent
else if (m_peer_list) else if (m_peer_list)
{ {
// reset last_connected, to force fast reconnect after leaving upload mode // reset last_connected, to force fast reconnect after leaving upload mode
for (peer_list::iterator i = m_peer_list->begin_peer() for (auto pe : *m_peer_list)
, end(m_peer_list->end_peer()); i != end; ++i)
{ {
(*i)->last_connected = 0; pe->last_connected = 0;
} }
// send_block_requests on all peers // send_block_requests on all peers
@ -2135,10 +2134,10 @@ namespace libtorrent
bt_peer_connection* torrent::find_introducer(tcp::endpoint const& ep) const bt_peer_connection* torrent::find_introducer(tcp::endpoint const& ep) const
{ {
#ifndef TORRENT_DISABLE_EXTENSIONS #ifndef TORRENT_DISABLE_EXTENSIONS
for (const_peer_iterator i = m_connections.begin(); i != m_connections.end(); ++i) for (auto pe : m_connections)
{ {
if ((*i)->type() != peer_connection::bittorrent_connection) continue; if (pe->type() != peer_connection::bittorrent_connection) continue;
bt_peer_connection* p = static_cast<bt_peer_connection*>(*i); bt_peer_connection* p = static_cast<bt_peer_connection*>(pe);
if (!p->supports_holepunch()) continue; if (!p->supports_holepunch()) continue;
peer_plugin const* pp = p->find_plugin("ut_pex"); peer_plugin const* pp = p->find_plugin("ut_pex");
if (!pp) continue; if (!pp) continue;
@ -2147,14 +2146,13 @@ namespace libtorrent
#else #else
TORRENT_UNUSED(ep); TORRENT_UNUSED(ep);
#endif #endif
return NULL; return nullptr;
} }
bt_peer_connection* torrent::find_peer(tcp::endpoint const& ep) const bt_peer_connection* torrent::find_peer(tcp::endpoint const& ep) const
{ {
for (const_peer_iterator i = m_connections.begin(); i != m_connections.end(); ++i) for (auto p : m_connections)
{ {
peer_connection* p = *i;
if (p->type() != peer_connection::bittorrent_connection) continue; if (p->type() != peer_connection::bittorrent_connection) continue;
if (p->remote() == ep) return static_cast<bt_peer_connection*>(p); if (p->remote() == ep) return static_cast<bt_peer_connection*>(p);
} }
@ -2163,9 +2161,8 @@ namespace libtorrent
peer_connection* torrent::find_peer(sha1_hash const& pid) peer_connection* torrent::find_peer(sha1_hash const& pid)
{ {
for (peer_iterator i = m_connections.begin(); i != m_connections.end(); ++i) for (auto p : m_connections)
{ {
peer_connection* p = *i;
if (p->pid() == pid) return p; if (p->pid() == pid) return p;
} }
return 0; return 0;
@ -2206,18 +2203,14 @@ namespace libtorrent
{ {
// --- PEERS --- // --- PEERS ---
for (std::vector<tcp::endpoint>::const_iterator i for (auto const& p : m_add_torrent_params->peers)
= m_add_torrent_params->peers.begin()
, end(m_add_torrent_params->peers.end()); i != end; ++i)
{ {
add_peer(*i , peer_info::resume_data); add_peer(p , peer_info::resume_data);
} }
for (std::vector<tcp::endpoint>::const_iterator i for (auto const& p : m_add_torrent_params->banned_peers)
= m_add_torrent_params->banned_peers.begin()
, end(m_add_torrent_params->banned_peers.end()); i != end; ++i)
{ {
torrent_peer* peer = add_peer(*i, peer_info::resume_data); torrent_peer* peer = add_peer(p, peer_info::resume_data);
if (peer) ban_peer(peer); if (peer) ban_peer(peer);
} }
@ -6673,11 +6666,9 @@ namespace libtorrent
if (m_peer_list) if (m_peer_list)
{ {
for (peer_list::const_iterator i = m_peer_list->begin_peer() for (auto p : *m_peer_list)
, end(m_peer_list->end_peer()); i != end; ++i)
{ {
error_code ec; error_code ec;
torrent_peer const* p = *i;
address addr = p->address(); address addr = p->address();
if (p->is_i2p_addr) if (p->is_i2p_addr)
continue; continue;
@ -6816,14 +6807,13 @@ namespace libtorrent
if (!m_peer_list) return; if (!m_peer_list) return;
v->reserve(m_peer_list->num_peers()); v->reserve(m_peer_list->num_peers());
for (peer_list::const_iterator i = m_peer_list->begin_peer(); for (auto p : *m_peer_list)
i != m_peer_list->end_peer(); ++i)
{ {
peer_list_entry e; peer_list_entry e;
e.ip = (*i)->ip(); e.ip = p->ip();
e.flags = (*i)->banned ? peer_list_entry::banned : 0; e.flags = p->banned ? peer_list_entry::banned : 0;
e.failcount = (*i)->failcount; e.failcount = p->failcount;
e.source = (*i)->source; e.source = p->source;
v->push_back(e); v->push_back(e);
} }
} }
@ -8424,11 +8414,11 @@ namespace libtorrent
#ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS #ifdef TORRENT_EXPENSIVE_INVARIANT_CHECKS
// make sure we haven't modified the peer object // make sure we haven't modified the peer object
// in a way that breaks the sort order // in a way that breaks the sort order
if (m_peer_list && m_peer_list->begin_peer() != m_peer_list->end_peer()) if (m_peer_list && m_peer_list->begin() != m_peer_list->end())
{ {
peer_list::const_iterator i = m_peer_list->begin_peer(); auto i = m_peer_list->begin();
peer_list::const_iterator p = i++; auto p = i++;
peer_list::const_iterator end(m_peer_list->end_peer()); auto end(m_peer_list->end());
peer_address_compare cmp; peer_address_compare cmp;
for (; i != end; ++i, ++p) for (; i != end; ++i, ++p)
{ {
@ -8781,11 +8771,8 @@ namespace libtorrent
{ {
if (m_peer_list) if (m_peer_list)
{ {
for (peer_list::iterator j = m_peer_list->begin_peer() for (auto pe : *m_peer_list)
, end(m_peer_list->end_peer()); j != end; ++j)
{ {
torrent_peer* pe = *j;
pe->last_optimistically_unchoked pe->last_optimistically_unchoked
= clamped_subtract(pe->last_optimistically_unchoked, seconds); = clamped_subtract(pe->last_optimistically_unchoked, seconds);
pe->last_connected = clamped_subtract(pe->last_connected, seconds); pe->last_connected = clamped_subtract(pe->last_connected, seconds);