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 const_iterator = peers_t::const_iterator;
iterator begin_peer() { return m_peers.begin(); }
iterator end_peer() { return m_peers.end(); }
const_iterator begin_peer() const { return m_peers.begin(); }
const_iterator end_peer() const { return m_peers.end(); }
iterator begin() { return m_peers.begin(); }
iterator end() { return m_peers.end(); }
const_iterator begin() const { return m_peers.begin(); }
const_iterator end() const { return m_peers.end(); }
std::pair<iterator, iterator> find_peers(address const& a)
{

View File

@ -38,13 +38,12 @@ POSSIBILITY OF SUCH DAMAGE.
#include <set>
#include <list>
#include <deque>
#include <limits> // for numeric_limits
#include <memory> // for unique_ptr
#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/scoped_ptr.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/version.hpp>
@ -184,7 +183,7 @@ namespace libtorrent
// it in torrent::need_picker(). In order to tell the
// difference between having everything and nothing in
// 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
// around further down the object to maintain an owner
@ -258,7 +257,7 @@ namespace libtorrent
// the state of this torrent (queued, checking, downloading, etc.)
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
@ -1294,13 +1293,13 @@ namespace libtorrent
// 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
// 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
// still be given a name until the metadata is received
// once the metadata is received this field will no
// 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;

View File

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