minor cleanup/refactor (#1036)

This commit is contained in:
Alden Torres 2016-08-28 00:04:16 -04:00 committed by Arvid Norberg
parent c7eb76593c
commit 118e30fd86
2 changed files with 58 additions and 72 deletions

View File

@ -48,7 +48,7 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent namespace libtorrent
{ {
// TODO: 2 facto these functions out // TODO: 2 factor these functions out
TORRENT_EXTRA_EXPORT bool is_local(address const& a); TORRENT_EXTRA_EXPORT bool is_local(address const& a);
TORRENT_EXTRA_EXPORT bool is_loopback(address const& addr); TORRENT_EXTRA_EXPORT bool is_loopback(address const& addr);
TORRENT_EXTRA_EXPORT bool is_any(address const& addr); TORRENT_EXTRA_EXPORT bool is_any(address const& addr);

View File

@ -228,7 +228,6 @@ namespace libtorrent
#if TORRENT_USE_ASSERTS #if TORRENT_USE_ASSERTS
piece_failed = false; piece_failed = false;
#endif #endif
std::fill(m_peer_id.begin(), m_peer_id.end(), 0);
} }
int peer_connection::timeout() const int peer_connection::timeout() const
@ -393,7 +392,7 @@ namespace libtorrent
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (t) if (t)
t->debug_log("START connect [%p] (%d)", static_cast<void*>(this) t->debug_log("START connect [%p] (%d)", static_cast<void*>(this)
, int(t->num_peers())); , t->num_peers());
#endif #endif
m_socket->async_connect(m_remote m_socket->async_connect(m_remote
@ -689,7 +688,7 @@ namespace libtorrent
TORRENT_ASSERT(m_num_pieces == m_have_piece.count()); TORRENT_ASSERT(m_num_pieces == m_have_piece.count());
if (m_num_pieces == int(m_have_piece.size())) if (m_num_pieces == m_have_piece.size())
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::info, "INIT", "this is a seed p: %p" peer_log(peer_log_alert::info, "INIT", "this is a seed p: %p"
@ -721,7 +720,7 @@ namespace libtorrent
TORRENT_ASSERT(m_have_piece.size() == t->torrent_file().num_pieces()); TORRENT_ASSERT(m_have_piece.size() == t->torrent_file().num_pieces());
t->peer_has(m_have_piece, this); t->peer_has(m_have_piece, this);
bool interesting = false; bool interesting = false;
for (int i = 0; i < int(m_have_piece.size()); ++i) for (int i = 0; i < m_have_piece.size(); ++i)
{ {
if (m_have_piece[i]) if (m_have_piece[i])
{ {
@ -1042,10 +1041,9 @@ namespace libtorrent
#ifndef TORRENT_DISABLE_EXTENSIONS #ifndef TORRENT_DISABLE_EXTENSIONS
if (bytes_payload) if (bytes_payload)
{ {
for (extension_list_t::iterator i = m_extensions.begin() for (auto const& e : m_extensions)
, end(m_extensions.end()); i != end; ++i)
{ {
(*i)->sent_payload(bytes_payload); e->sent_payload(bytes_payload);
} }
} }
#endif #endif
@ -1219,7 +1217,7 @@ namespace libtorrent
{ {
// paused torrents will not accept // paused torrents will not accept
// incoming connections unless they are auto managed // incoming connections unless they are auto managed
// and inconing_starts_queued_torrents is true // and incoming_starts_queued_torrents is true
// torrents that have errors should always reject // torrents that have errors should always reject
// incoming peers // incoming peers
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
@ -1343,10 +1341,9 @@ namespace libtorrent
INVARIANT_CHECK; INVARIANT_CHECK;
#ifndef TORRENT_DISABLE_EXTENSIONS #ifndef TORRENT_DISABLE_EXTENSIONS
for (extension_list_t::iterator i = m_extensions.begin() for (auto const& e : m_extensions)
, end(m_extensions.end()); i != end; ++i)
{ {
if ((*i)->on_choke()) return; if (e->on_choke()) return;
} }
#endif #endif
if (is_disconnecting()) return; if (is_disconnecting()) return;
@ -1419,10 +1416,9 @@ namespace libtorrent
#endif #endif
#ifndef TORRENT_DISABLE_EXTENSIONS #ifndef TORRENT_DISABLE_EXTENSIONS
for (extension_list_t::iterator i = m_extensions.begin() for (auto const& e : m_extensions)
, end(m_extensions.end()); i != end; ++i)
{ {
if ((*i)->on_reject(r)) return; if (e->on_reject(r)) return;
} }
#endif #endif
@ -1532,11 +1528,11 @@ namespace libtorrent
if (t->valid_metadata()) if (t->valid_metadata())
{ {
if (index >= int(m_have_piece.size())) if (index >= m_have_piece.size())
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::incoming_message, "INVALID_SUGGEST" peer_log(peer_log_alert::incoming_message, "INVALID_SUGGEST"
, "%d s: %d", index, int(m_have_piece.size())); , "%d s: %d", index, m_have_piece.size());
#endif #endif
return; return;
} }
@ -1678,7 +1674,7 @@ namespace libtorrent
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::info, "UNCHOKE", "about to unchoke, peer ignores unchoke slots"); peer_log(peer_log_alert::info, "UNCHOKE", "about to unchoke, peer ignores unchoke slots");
#endif #endif
// if this peer is expempted from the choker // if this peer is exempted from the choker
// just unchoke it immediately // just unchoke it immediately
send_unchoke(); send_unchoke();
} }
@ -1802,7 +1798,7 @@ namespace libtorrent
if (is_disconnecting()) return; if (is_disconnecting()) return;
if (!t->valid_metadata() && index >= int(m_have_piece.size())) if (!t->valid_metadata() && index >= m_have_piece.size())
{ {
if (index < 131072) if (index < 131072)
{ {
@ -1821,11 +1817,11 @@ namespace libtorrent
} }
// if we got an invalid message, abort // if we got an invalid message, abort
if (index >= int(m_have_piece.size()) || index < 0) if (index >= m_have_piece.size() || index < 0)
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::info, "ERROR", "have-metadata have_piece: %d size: %d" peer_log(peer_log_alert::info, "ERROR", "have-metadata have_piece: %d size: %d"
, index, int(m_have_piece.size())); , index, m_have_piece.size());
#endif #endif
disconnect(errors::invalid_have, op_bittorrent, 2); disconnect(errors::invalid_have, op_bittorrent, 2);
return; return;
@ -1903,7 +1899,7 @@ namespace libtorrent
#endif #endif
} }
// it's important to update whether we're intersted in this peer before // it's important to update whether we're interested in this peer before
// calling disconnect_if_redundant, otherwise we may disconnect even if // calling disconnect_if_redundant, otherwise we may disconnect even if
// we are interested // we are interested
if (!t->has_piece_passed(index) if (!t->has_piece_passed(index)
@ -1946,10 +1942,9 @@ namespace libtorrent
TORRENT_ASSERT(t); TORRENT_ASSERT(t);
#ifndef TORRENT_DISABLE_EXTENSIONS #ifndef TORRENT_DISABLE_EXTENSIONS
for (extension_list_t::iterator i = m_extensions.begin() for (auto const& e : m_extensions)
, end(m_extensions.end()); i != end; ++i)
{ {
if ((*i)->on_dont_have(index)) return; if (e->on_dont_have(index)) return;
} }
#endif #endif
@ -1960,7 +1955,7 @@ namespace libtorrent
#endif #endif
// if we got an invalid message, abort // if we got an invalid message, abort
if (index >= int(m_have_piece.size()) || index < 0) if (index >= m_have_piece.size() || index < 0)
{ {
disconnect(errors::invalid_dont_have, op_bittorrent, 2); disconnect(errors::invalid_dont_have, op_bittorrent, 2);
return; return;
@ -2017,21 +2012,21 @@ namespace libtorrent
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
std::string bitfield_str; std::string bitfield_str;
bitfield_str.resize(bits.size()); bitfield_str.resize(bits.size());
for (int i = 0; i < int(bits.size()); ++i) for (int i = 0; i < bits.size(); ++i)
bitfield_str[i] = bits[i] ? '1' : '0'; bitfield_str[i] = bits[i] ? '1' : '0';
peer_log(peer_log_alert::incoming_message, "BITFIELD" peer_log(peer_log_alert::incoming_message, "BITFIELD"
, "%s", bitfield_str.c_str()); , "%s", bitfield_str.c_str());
#endif #endif
// if we don't have the metedata, we cannot // if we don't have the metadata, we cannot
// verify the bitfield size // verify the bitfield size
if (t->valid_metadata() if (t->valid_metadata()
&& bits.size() != int(m_have_piece.size())) && bits.size() != m_have_piece.size())
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::incoming_message, "BITFIELD" peer_log(peer_log_alert::incoming_message, "BITFIELD"
, "invalid size: %d expected %d", bits.size() , "invalid size: %d expected %d", bits.size()
, int(m_have_piece.size())); , m_have_piece.size());
#endif #endif
disconnect(errors::invalid_bitfield_size, op_bittorrent, 2); disconnect(errors::invalid_bitfield_size, op_bittorrent, 2);
return; return;
@ -2060,13 +2055,13 @@ namespace libtorrent
if (!t->ready_for_connections()) if (!t->ready_for_connections())
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
if (m_num_pieces == int(bits.size())) if (m_num_pieces == bits.size())
peer_log(peer_log_alert::info, "SEED", "this is a seed. p: %p" peer_log(peer_log_alert::info, "SEED", "this is a seed. p: %p"
, static_cast<void*>(m_peer_info)); , static_cast<void*>(m_peer_info));
#endif #endif
m_have_piece = bits; m_have_piece = bits;
m_num_pieces = bits.count(); m_num_pieces = bits.count();
t->set_seed(m_peer_info, m_num_pieces == int(bits.size())); t->set_seed(m_peer_info, m_num_pieces == bits.size());
#if TORRENT_USE_INVARIANT_CHECKS #if TORRENT_USE_INVARIANT_CHECKS
if (t && t->has_picker()) if (t && t->has_picker())
@ -2078,7 +2073,7 @@ namespace libtorrent
TORRENT_ASSERT(t->valid_metadata()); TORRENT_ASSERT(t->valid_metadata());
int num_pieces = bits.count(); int num_pieces = bits.count();
if (num_pieces == int(m_have_piece.size())) if (num_pieces == m_have_piece.size())
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::info, "SEED", "this is a seed. p: %p" peer_log(peer_log_alert::info, "SEED", "this is a seed. p: %p"
@ -2199,7 +2194,7 @@ namespace libtorrent
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
const bool valid_piece_index const bool valid_piece_index
= r.piece >= 0 && r.piece < int(t->torrent_file().num_pieces()); = r.piece >= 0 && r.piece < t->torrent_file().num_pieces();
peer_log(peer_log_alert::incoming_message, "REQUEST" peer_log(peer_log_alert::incoming_message, "REQUEST"
, "piece: %d s: %x l: %x", r.piece, r.start, r.length); , "piece: %d s: %x l: %x", r.piece, r.start, r.length);
@ -2215,8 +2210,7 @@ namespace libtorrent
"i: %d t: %d n: %d h: %d ss1: %d ss2: %d" "i: %d t: %d n: %d h: %d ss1: %d ss2: %d"
, m_peer_interested , m_peer_interested
, valid_piece_index , valid_piece_index
? int(t->torrent_file().piece_size(r.piece)) ? t->torrent_file().piece_size(r.piece) : -1
: -1
, t->torrent_file().num_pieces() , t->torrent_file().num_pieces()
, valid_piece_index ? t->has_piece_passed(r.piece) : 0 , valid_piece_index ? t->has_piece_passed(r.piece) : 0
, m_superseed_piece[0] , m_superseed_piece[0]
@ -2293,7 +2287,7 @@ namespace libtorrent
peer_log(peer_log_alert::info, "INVALID_REQUEST", "peer is not interested " peer_log(peer_log_alert::info, "INVALID_REQUEST", "peer is not interested "
" t: %d n: %d block_limit: %d" " t: %d n: %d block_limit: %d"
, valid_piece_index , valid_piece_index
? int(t->torrent_file().piece_size(r.piece)) : -1 ? t->torrent_file().piece_size(r.piece) : -1
, t->torrent_file().num_pieces() , t->torrent_file().num_pieces()
, t->block_size()); , t->block_size());
peer_log(peer_log_alert::info, "INTERESTED", "artificial incoming INTERESTED message"); peer_log(peer_log_alert::info, "INTERESTED", "artificial incoming INTERESTED message");
@ -2334,7 +2328,7 @@ namespace libtorrent
, "i: %d t: %d n: %d h: %d block_limit: %d" , "i: %d t: %d n: %d h: %d block_limit: %d"
, m_peer_interested , m_peer_interested
, valid_piece_index , valid_piece_index
? int(t->torrent_file().piece_size(r.piece)) : -1 ? t->torrent_file().piece_size(r.piece) : -1
, t->torrent_file().num_pieces() , t->torrent_file().num_pieces()
, t->has_piece_passed(r.piece) , t->has_piece_passed(r.piece)
, t->block_size()); , t->block_size());
@ -2382,8 +2376,8 @@ namespace libtorrent
// if we have choked the client // if we have choked the client
// ignore the request // ignore the request
const int blocks_per_piece = static_cast<int>( const int blocks_per_piece =
(t->torrent_file().piece_length() + t->block_size() - 1) / t->block_size()); (t->torrent_file().piece_length() + t->block_size() - 1) / t->block_size();
// disconnect peers that downloads more than foo times an allowed // disconnect peers that downloads more than foo times an allowed
// fast piece // fast piece
@ -2539,7 +2533,7 @@ namespace libtorrent
if (t->alerts().should_post<unwanted_block_alert>()) if (t->alerts().should_post<unwanted_block_alert>())
{ {
t->alerts().emplace_alert<unwanted_block_alert>(t->get_handle() t->alerts().emplace_alert<unwanted_block_alert>(t->get_handle()
, m_remote, m_peer_id, int(b.block_index), int(b.piece_index)); , m_remote, m_peer_id, b.block_index, b.piece_index);
} }
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::info, "INVALID_REQUEST" peer_log(peer_log_alert::info, "INVALID_REQUEST"
@ -2604,7 +2598,7 @@ namespace libtorrent
// time, regardless of whether the cache size is exceeded or not. If this // time, regardless of whether the cache size is exceeded or not. If this
// was not the case, when the cache size setting is very small, most peers // was not the case, when the cache size setting is very small, most peers
// would be blocked most of the time, because the disk cache would // would be blocked most of the time, because the disk cache would
// continously be in exceeded state. Only rarely would it actually drop // continuously be in exceeded state. Only rarely would it actually drop
// down to 0 and unblock all peers. // down to 0 and unblock all peers.
if (exceeded && m_outstanding_writing_bytes > 0) if (exceeded && m_outstanding_writing_bytes > 0)
{ {
@ -2652,7 +2646,7 @@ namespace libtorrent
update_desired_queue_size(); update_desired_queue_size();
#ifndef TORRENT_DISABLE_EXTENSIONS #ifndef TORRENT_DISABLE_EXTENSIONS
for (auto& e : m_extensions) for (auto const& e : m_extensions)
{ {
if (e->on_piece(p, {data.get(), size_t(p.length)})) if (e->on_piece(p, {data.get(), size_t(p.length)}))
{ {
@ -2729,8 +2723,8 @@ namespace libtorrent
if (t->alerts().should_post<unwanted_block_alert>()) if (t->alerts().should_post<unwanted_block_alert>())
{ {
t->alerts().emplace_alert<unwanted_block_alert>(t->get_handle() t->alerts().emplace_alert<unwanted_block_alert>(t->get_handle()
, m_remote, m_peer_id, int(block_finished.block_index) , m_remote, m_peer_id, block_finished.block_index
, int(block_finished.piece_index)); , block_finished.piece_index);
} }
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::info, "INVALID_REQUEST", "The block we just got was not in the request queue"); peer_log(peer_log_alert::info, "INVALID_REQUEST", "The block we just got was not in the request queue");
@ -3028,8 +3022,8 @@ namespace libtorrent
if (t->alerts().should_post<block_finished_alert>()) if (t->alerts().should_post<block_finished_alert>())
{ {
t->alerts().emplace_alert<block_finished_alert>(t->get_handle(), t->alerts().emplace_alert<block_finished_alert>(t->get_handle(),
remote(), pid(), int(block_finished.block_index) remote(), pid(), block_finished.block_index
, int(block_finished.piece_index)); , block_finished.piece_index);
} }
disconnect_if_redundant(); disconnect_if_redundant();
@ -3065,10 +3059,9 @@ namespace libtorrent
INVARIANT_CHECK; INVARIANT_CHECK;
#ifndef TORRENT_DISABLE_EXTENSIONS #ifndef TORRENT_DISABLE_EXTENSIONS
for (extension_list_t::iterator i = m_extensions.begin() for (auto const& e : m_extensions)
, end(m_extensions.end()); i != end; ++i)
{ {
if ((*i)->on_cancel(r)) return; if (e->on_cancel(r)) return;
} }
#endif #endif
if (is_disconnecting()) return; if (is_disconnecting()) return;
@ -3307,11 +3300,11 @@ namespace libtorrent
if (t->valid_metadata()) if (t->valid_metadata())
{ {
if (index >= int(m_have_piece.size())) if (index >= m_have_piece.size())
{ {
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::incoming_message, "INVALID_ALLOWED_FAST" peer_log(peer_log_alert::incoming_message, "INVALID_ALLOWED_FAST"
, "%d s: %d", index, int(m_have_piece.size())); , "%d s: %d", index, m_have_piece.size());
#endif #endif
return; return;
} }
@ -3928,10 +3921,9 @@ namespace libtorrent
#ifndef TORRENT_DISABLE_EXTENSIONS #ifndef TORRENT_DISABLE_EXTENSIONS
bool handled = false; bool handled = false;
for (extension_list_t::iterator i = m_extensions.begin() for (auto const& e : m_extensions)
, end(m_extensions.end()); i != end; ++i)
{ {
handled = (*i)->write_request(r); handled = e->write_request(r);
if (handled) break; if (handled) break;
} }
if (is_disconnecting()) return; if (is_disconnecting()) return;
@ -4319,15 +4311,11 @@ namespace libtorrent
return false; return false;
} }
// defined in upnp.cpp
bool is_local(address const& a);
bool peer_connection::on_local_network() const bool peer_connection::on_local_network() const
{ {
TORRENT_ASSERT(is_single_thread()); TORRENT_ASSERT(is_single_thread());
if (libtorrent::is_local(m_remote.address()) return is_local(m_remote.address())
|| is_loopback(m_remote.address())) return true; || is_loopback(m_remote.address());
return false;
} }
int peer_connection::request_timeout() const int peer_connection::request_timeout() const
@ -4400,7 +4388,7 @@ namespace libtorrent
, m_download_queue.end() , m_download_queue.end()
, &pending_block_in_buffer)); , &pending_block_in_buffer));
p.target_dl_queue_length = int(desired_queue_size()); p.target_dl_queue_length = desired_queue_size();
p.upload_queue_length = int(upload_queue().size()); p.upload_queue_length = int(upload_queue().size());
p.timed_out_requests = 0; p.timed_out_requests = 0;
p.busy_requests = 0; p.busy_requests = 0;
@ -4681,10 +4669,9 @@ namespace libtorrent
if (is_disconnecting()) return; if (is_disconnecting()) return;
#ifndef TORRENT_DISABLE_EXTENSIONS #ifndef TORRENT_DISABLE_EXTENSIONS
for (extension_list_t::iterator i = m_extensions.begin() for (auto const& e : m_extensions)
, end(m_extensions.end()); i != end; ++i)
{ {
(*i)->tick(); e->tick();
} }
if (is_disconnecting()) return; if (is_disconnecting()) return;
#endif #endif
@ -4840,7 +4827,7 @@ namespace libtorrent
peer_log(peer_log_alert::info, "SLOW_START", "exit slow start: " peer_log(peer_log_alert::info, "SLOW_START", "exit slow start: "
"prev-dl: %d dl: %d" "prev-dl: %d dl: %d"
, int(m_downloaded_last_second) , int(m_downloaded_last_second)
, int(m_statistics.last_payload_downloaded())); , m_statistics.last_payload_downloaded());
#endif #endif
} }
m_downloaded_last_second = m_statistics.last_payload_downloaded(); m_downloaded_last_second = m_statistics.last_payload_downloaded();
@ -5533,7 +5520,7 @@ namespace libtorrent
, "quota: %d buf: %d connecting: %s disconnecting: %s " , "quota: %d buf: %d connecting: %s disconnecting: %s "
"pending_disk: %d piece-requests: %d" "pending_disk: %d piece-requests: %d"
, m_quota[upload_channel] , m_quota[upload_channel]
, int(m_send_buffer.size()), m_connecting?"yes":"no" , m_send_buffer.size(), m_connecting?"yes":"no"
, m_disconnecting?"yes":"no", m_reading_bytes , m_disconnecting?"yes":"no", m_reading_bytes
, int(m_requests.size())); , int(m_requests.size()));
} }
@ -5543,7 +5530,7 @@ namespace libtorrent
, "quota: %d buf: %d connecting: %s disconnecting: %s " , "quota: %d buf: %d connecting: %s disconnecting: %s "
"pending_disk: %d" "pending_disk: %d"
, m_quota[upload_channel] , m_quota[upload_channel]
, int(m_send_buffer.size()), m_connecting?"yes":"no" , m_send_buffer.size(), m_connecting?"yes":"no"
, m_disconnecting?"yes":"no", m_reading_bytes); , m_disconnecting?"yes":"no", m_reading_bytes);
} }
#endif #endif
@ -5760,7 +5747,7 @@ namespace libtorrent
// account receiver buffer size stats to the session // account receiver buffer size stats to the session
m_ses.received_buffer(bytes_transferred); m_ses.received_buffer(bytes_transferred);
// estimage transport protocol overhead // estimate transport protocol overhead
trancieve_ip_packet(bytes_transferred, m_remote.address().is_v6()); trancieve_ip_packet(bytes_transferred, m_remote.address().is_v6());
#ifndef TORRENT_DISABLE_LOGGING #ifndef TORRENT_DISABLE_LOGGING
@ -6107,10 +6094,9 @@ namespace libtorrent
#endif #endif
#ifndef TORRENT_DISABLE_EXTENSIONS #ifndef TORRENT_DISABLE_EXTENSIONS
for (extension_list_t::iterator i = m_extensions.begin() for (auto const& ext : m_extensions)
, end(m_extensions.end()); i != end; ++i)
{ {
(*i)->on_connected(); ext->on_connected();
} }
#endif #endif