some refactor, more use of auto and const in peer_connection.cpp (#3105)

This commit is contained in:
Alden Torres 2018-06-16 11:22:04 -04:00 committed by Arvid Norberg
parent dfe47aa2a9
commit 03971be80b
1 changed files with 33 additions and 31 deletions

View File

@ -183,7 +183,7 @@ namespace libtorrent {
}
#endif
// this counter should not be incremeneted until we know constructing this
// this counter should not be incremented until we know constructing this
// peer object can't fail anymore
if (m_connecting && t) t->inc_num_connecting(m_peer_info);
@ -615,14 +615,13 @@ namespace libtorrent {
if (!t->valid_metadata()) return;
int const num_pieces = t->torrent_file().num_pieces();
piece_index_t const end_piece = t->torrent_file().end_piece();
if (num_allowed_pieces >= num_pieces)
{
// this is a special case where we have more allowed
// fast pieces than pieces in the torrent. Just send
// an allowed fast message for every single piece
for (piece_index_t i(0); i < end_piece; ++i)
for (auto const i : t->torrent_file().piece_range())
{
// there's no point in offering fast pieces
// that the peer already has
@ -778,7 +777,7 @@ namespace libtorrent {
TORRENT_ASSERT(m_have_piece.size() == t->torrent_file().num_pieces());
t->peer_has(m_have_piece, this);
bool interesting = false;
for (piece_index_t i(0); i < m_have_piece.end_index(); ++i)
for (auto const i : m_have_piece.range())
{
if (!m_have_piece[i]) continue;
// if the peer has a piece and we don't, the peer is interesting
@ -1177,7 +1176,7 @@ namespace libtorrent {
// verifies a piece to see if it is valid (is within a valid range)
// and if it can correspond to a request generated by libtorrent.
bool peer_connection::verify_piece(const peer_request& p) const
bool peer_connection::verify_piece(peer_request const& p) const
{
TORRENT_ASSERT(is_single_thread());
std::shared_ptr<torrent> t = m_torrent.lock();
@ -1319,8 +1318,16 @@ namespace libtorrent {
}
// find the lowest ranking peer and disconnect that
peer_connection* p = other_t->find_lowest_ranking_peer();
p->disconnect(errors::too_many_connections, operation_t::bittorrent);
peer_disconnected_other();
if (p != nullptr)
{
p->disconnect(errors::too_many_connections, operation_t::bittorrent);
peer_disconnected_other();
}
else
{
disconnect(errors::too_many_connections, operation_t::bittorrent);
return;
}
}
else
{
@ -1451,17 +1458,6 @@ namespace libtorrent {
}
}
namespace {
bool match_request(peer_request const& r, piece_block const& b, int const block_size)
{
if (b.piece_index != r.piece) return false;
if (b.block_index != r.start / block_size) return false;
if (r.start % block_size != 0) return false;
return true;
}
}
// -----------------------------
// -------- REJECT PIECE -------
// -----------------------------
@ -1488,10 +1484,17 @@ namespace libtorrent {
if (is_disconnecting()) return;
int const block_size = t->block_size();
auto const dlq_iter = std::find_if(
m_download_queue.begin(), m_download_queue.end()
, std::bind(match_request, std::cref(r), std::bind(&pending_block::block, _1)
, t->block_size()));
, [&r, block_size](pending_block const& pb)
{
auto const& b = pb.block;
if (b.piece_index != r.piece) return false;
if (b.block_index != r.start / block_size) return false;
if (r.start % block_size != 0) return false;
return true;
});
if (dlq_iter != m_download_queue.end())
{
@ -1878,7 +1881,7 @@ namespace libtorrent {
}
// if we got an invalid message, abort
if (index >= piece_index_t(m_have_piece.size()) || index < piece_index_t(0))
if (index >= m_have_piece.end_index() || index < piece_index_t(0))
{
#ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::info, "ERROR", "have-metadata have_piece: %d size: %d"
@ -2006,7 +2009,7 @@ namespace libtorrent {
#endif
// if we got an invalid message, abort
if (index >= piece_index_t(m_have_piece.size()) || index < piece_index_t(0))
if (index >= m_have_piece.end_index() || index < piece_index_t(0))
{
disconnect(errors::invalid_dont_have, operation_t::bittorrent, 2);
return;
@ -2065,7 +2068,7 @@ namespace libtorrent {
{
std::string bitfield_str;
bitfield_str.resize(aux::numeric_cast<std::size_t>(bits.size()));
for (piece_index_t i(0); i != bits.end_index(); ++i)
for (auto const i : bits.range())
bitfield_str[std::size_t(static_cast<int>(i))] = bits[i] ? '1' : '0';
peer_log(peer_log_alert::incoming_message, "BITFIELD"
, "%s", bitfield_str.c_str());
@ -2252,7 +2255,7 @@ namespace libtorrent {
#ifndef TORRENT_DISABLE_LOGGING
const bool valid_piece_index
= r.piece >= piece_index_t(0)
&& r.piece < piece_index_t(t->torrent_file().num_pieces());
&& r.piece < t->torrent_file().end_piece();
peer_log(peer_log_alert::incoming_message, "REQUEST"
, "piece: %d s: %x l: %x", static_cast<int>(r.piece), r.start, r.length);
@ -3107,10 +3110,9 @@ namespace libtorrent {
#if TORRENT_USE_ASSERTS
if (t->has_picker())
{
std::vector<piece_picker::downloading_piece> const& q
= picker.get_download_queue();
auto const& q = picker.get_download_queue();
for (piece_picker::downloading_piece const& dp : q)
for (auto const& dp : q)
{
if (dp.index != block_finished.piece_index) continue;
auto const info = picker.blocks_for_piece(dp);
@ -3370,7 +3372,7 @@ namespace libtorrent {
if (t->valid_metadata())
{
if (index >= piece_index_t(m_have_piece.size()))
if (index >= m_have_piece.end_index())
{
#ifndef TORRENT_DISABLE_LOGGING
peer_log(peer_log_alert::incoming_message, "INVALID_ALLOWED_FAST"
@ -4702,7 +4704,7 @@ namespace libtorrent {
void peer_connection::second_tick(int const tick_interval_ms)
{
TORRENT_ASSERT(is_single_thread());
time_point now = aux::time_now();
time_point const now = aux::time_now();
std::shared_ptr<peer_connection> me(self());
// the invariant check must be run before me is destructed
@ -5290,7 +5292,7 @@ namespace libtorrent {
void peer_connection::on_disk_read_complete(disk_buffer_holder buffer
, disk_job_flags_t const flags, storage_error const& error
, peer_request const& r, time_point issue_time)
, peer_request const& r, time_point const issue_time)
{
TORRENT_ASSERT(is_single_thread());
// return value:
@ -5479,7 +5481,7 @@ namespace libtorrent {
bandwidth_manager* manager = m_ses.get_bandwidth_manager(channel);
int ret = manager->request_bandwidth(self()
int const ret = manager->request_bandwidth(self()
, bytes, priority, channels.data(), c);
if (ret == 0)