more use of auto, const and minor refactor in torrent.cpp (#2554)

This commit is contained in:
Alden Torres 2017-11-24 19:22:52 -05:00 committed by Arvid Norberg
parent 57ec82cb2e
commit ee1f4aa129
1 changed files with 81 additions and 85 deletions

View File

@ -852,7 +852,7 @@ namespace libtorrent {
rp->blocks_left = blocks_in_piece;
for (int i = 0; i < blocks_in_piece; ++i, r.start += block_size())
{
r.length = (std::min)(piece_size - r.start, block_size());
r.length = std::min(piece_size - r.start, block_size());
m_ses.disk_thread().async_read(m_storage, r
, std::bind(&torrent::on_disk_read_complete
, shared_from_this(), _1, _2, _3, r, rp));
@ -1143,7 +1143,7 @@ namespace libtorrent {
update_gauge();
// some peers that previously was no longer interesting may
// now have become interesting, since we lack this one piece now.
for (peer_iterator i = begin(); i != end();)
for (auto i = begin(); i != end();)
{
peer_connection* p = *i;
// update_interest may disconnect the peer and
@ -1377,7 +1377,7 @@ namespace libtorrent {
peer_request torrent::to_req(piece_block const& p) const
{
int block_offset = p.block_index * block_size();
int block = (std::min)(torrent_file().piece_size(
int block = std::min(torrent_file().piece_size(
p.piece_index) - block_offset, block_size());
TORRENT_ASSERT(block > 0);
TORRENT_ASSERT(block <= block_size());
@ -1405,7 +1405,7 @@ namespace libtorrent {
void torrent::remove_extension(std::shared_ptr<torrent_plugin> ext)
{
auto i = std::find(m_extensions.begin(), m_extensions.end(), ext);
auto const i = std::find(m_extensions.begin(), m_extensions.end(), ext);
if (i == m_extensions.end()) return;
m_extensions.erase(i);
}
@ -1660,8 +1660,8 @@ namespace libtorrent {
peer_connection* torrent::find_lowest_ranking_peer() const
{
const_peer_iterator lowest_rank = end();
for (const_peer_iterator i = begin(); i != end(); ++i)
auto lowest_rank = end();
for (auto i = begin(); i != end(); ++i)
{
// disconnecting peers don't count
if ((*i)->is_disconnecting()) continue;
@ -3054,9 +3054,9 @@ namespace libtorrent {
{
for (auto const& aep : t.endpoints)
{
complete = (std::max)(aep.scrape_complete, complete);
incomplete = (std::max)(aep.scrape_incomplete, incomplete);
downloaded = (std::max)(aep.scrape_downloaded, downloaded);
complete = std::max(aep.scrape_complete, complete);
incomplete = std::max(aep.scrape_incomplete, incomplete);
downloaded = std::max(aep.scrape_downloaded, downloaded);
}
}
@ -3281,7 +3281,7 @@ namespace libtorrent {
// this is the first tracker response for this torrent
// instead of waiting one second for session_impl::on_tick()
// to be called, connect to a few peers immediately
int conns = (std::min)(
int conns = std::min(
settings().get_int(settings_pack::torrent_connect_boost)
, settings().get_int(settings_pack::connections_limit) - m_ses.num_connections());
@ -3500,17 +3500,17 @@ namespace libtorrent {
file_storage const& fs = m_torrent_file->files();
int const piece_size = m_torrent_file->piece_size(p.piece_index);
int const offset = p.block_index * block_size();
if (m_padding == 0) return (std::min)(piece_size - offset, block_size());
if (m_padding == 0) return std::min(piece_size - offset, block_size());
std::vector<file_slice> const files = fs.map_block(
p.piece_index, offset, (std::min)(piece_size - offset, block_size()));
p.piece_index, offset, std::min(piece_size - offset, block_size()));
std::int64_t ret = 0;
for (auto const& i : files)
{
if (fs.pad_file_at(i.file_index)) continue;
ret += i.size;
}
TORRENT_ASSERT(ret <= (std::min)(piece_size - offset, block_size()));
TORRENT_ASSERT(ret <= std::min(piece_size - offset, block_size()));
return aux::numeric_cast<int>(ret);
}
@ -3626,7 +3626,7 @@ namespace libtorrent {
TORRENT_ASSERT(st.total_wanted_done >= 0);
TORRENT_ASSERT(st.total_done >= st.total_wanted_done);
std::vector<piece_picker::downloading_piece> dl_queue
std::vector<piece_picker::downloading_piece> const dl_queue
= m_picker->get_download_queue();
// look at all unfinished pieces and add the completed
@ -4056,7 +4056,7 @@ namespace libtorrent {
if (m_ses.alerts().should_post<hash_failed_alert>())
m_ses.alerts().emplace_alert<hash_failed_alert>(get_handle(), index);
auto it = std::lower_bound(m_predictive_pieces.begin()
auto const it = std::lower_bound(m_predictive_pieces.begin()
, m_predictive_pieces.end(), index);
if (it != m_predictive_pieces.end() && *it == index)
{
@ -5147,7 +5147,7 @@ namespace libtorrent {
// updates the interested flag in peers
void torrent::update_peer_interest(bool const was_finished)
{
for (peer_iterator i = begin(); i != end();)
for (auto i = begin(); i != end();)
{
peer_connection* p = *i;
// update_interest may disconnect the peer and
@ -5525,7 +5525,7 @@ namespace libtorrent {
std::vector<std::shared_ptr<peer_connection>> peers;
m_peers_to_disconnect.swap(peers);
for (auto p : peers)
for (auto const& p : peers)
{
TORRENT_ASSERT(p);
TORRENT_ASSERT(p->associated_torrent().lock().get() == this);
@ -5823,7 +5823,7 @@ namespace libtorrent {
void torrent::on_name_lookup(error_code const& e
, std::vector<address> const& addrs
, int port
, int const port
, std::list<web_seed_t>::iterator web) try
{
TORRENT_ASSERT(is_single_thread());
@ -6131,7 +6131,7 @@ namespace libtorrent {
= m_picker->get_download_queue();
// info for each unfinished piece
for (piece_picker::downloading_piece const& dp : q)
for (auto const& dp : q)
{
if (dp.finished == 0) continue;
@ -6144,12 +6144,12 @@ namespace libtorrent {
if (info[i].state == piece_picker::block_info::state_finished)
bitmask.set_bit(i);
}
ret.unfinished_pieces.insert({dp.index, std::move(bitmask)});
ret.unfinished_pieces.emplace(dp.index, std::move(bitmask));
}
}
// save trackers
for (announce_entry const& tr : m_trackers)
for (auto const& tr : m_trackers)
{
ret.trackers.push_back(tr.url);
ret.tracker_tiers.push_back(tr.tier);
@ -6158,7 +6158,7 @@ namespace libtorrent {
// save web seeds
if (!m_web_seeds.empty())
{
for (web_seed_t const& ws : m_web_seeds)
for (auto const& ws : m_web_seeds)
{
if (ws.removed || ws.ephemeral) continue;
if (ws.type == web_seed_entry::url_seed)
@ -6377,8 +6377,7 @@ namespace libtorrent {
std::memset(blk.data(), 0, sizeof(blk[0]) * blk.size());
int counter = 0;
for (std::vector<piece_picker::downloading_piece>::const_iterator i
= q.begin(); i != q.end(); ++i, ++counter)
for (auto i = q.begin(); i != q.end(); ++i, ++counter)
{
partial_piece_info pi;
pi.blocks_in_piece = p.blocks_in_piece(i->index);
@ -7197,9 +7196,9 @@ namespace libtorrent {
std::int64_t lhs_transferred = lhs->statistics().total_payload_download();
std::int64_t rhs_transferred = rhs->statistics().total_payload_download();
time_point now = aux::time_now();
std::int64_t lhs_time_connected = total_seconds(now - lhs->connected_time());
std::int64_t rhs_time_connected = total_seconds(now - rhs->connected_time());
time_point const now = aux::time_now();
std::int64_t const lhs_time_connected = total_seconds(now - lhs->connected_time());
std::int64_t const rhs_time_connected = total_seconds(now - rhs->connected_time());
lhs_transferred /= lhs_time_connected + 1;
rhs_transferred /= (rhs_time_connected + 1);
@ -7601,7 +7600,6 @@ namespace libtorrent {
m_ses.disk_thread().async_rename_file(m_storage, index, std::move(name)
, std::bind(&torrent::on_file_renamed, shared_from_this(), _1, _2, _3));
return;
}
void torrent::move_storage(std::string const& save_path, move_flags_t const flags)
@ -7961,7 +7959,7 @@ namespace libtorrent {
}
#endif
void torrent::set_sequential_download(bool sd)
void torrent::set_sequential_download(bool const sd)
{
TORRENT_ASSERT(is_single_thread());
if (m_sequential_download == sd) return;
@ -8049,7 +8047,7 @@ namespace libtorrent {
set_need_save_resume();
}
void torrent::set_upload_limit(int limit)
void torrent::set_upload_limit(int const limit)
{
set_limit_impl(limit, peer_connection::upload_channel);
set_need_save_resume();
@ -8058,7 +8056,7 @@ namespace libtorrent {
#endif
}
void torrent::set_download_limit(int limit)
void torrent::set_download_limit(int const limit)
{
set_limit_impl(limit, peer_connection::download_channel);
set_need_save_resume();
@ -8067,7 +8065,7 @@ namespace libtorrent {
#endif
}
void torrent::set_limit_impl(int limit, int channel, bool state_update)
void torrent::set_limit_impl(int limit, int const channel, bool const state_update)
{
TORRENT_ASSERT(is_single_thread());
TORRENT_ASSERT(limit >= -1);
@ -8093,13 +8091,13 @@ namespace libtorrent {
add_class(m_ses.peer_classes(), m_peer_class);
}
int torrent::limit_impl(int channel) const
int torrent::limit_impl(int const channel) const
{
TORRENT_ASSERT(is_single_thread());
if (m_peer_class == peer_class_t{0}) return -1;
int limit = m_ses.peer_classes().at(m_peer_class)->channel[channel].throttle();
if (limit == (std::numeric_limits<int>::max)()) limit = -1;
if (limit == std::numeric_limits<int>::max()) limit = -1;
return limit;
}
@ -8140,9 +8138,9 @@ namespace libtorrent {
{
TORRENT_ASSERT(is_single_thread());
if (!m_error) return;
bool checking_files = should_check_files();
bool const checking_files = should_check_files();
m_ses.trigger_auto_manage();
m_error = error_code();
m_error.clear();
m_error_file = torrent_status::error_file_none;
update_gauge();
@ -8240,7 +8238,7 @@ namespace libtorrent {
namespace {
std::uint16_t clamped_subtract_u16(int a, int b)
std::uint16_t clamped_subtract_u16(int const a, int const b)
{
if (a < b) return 0;
return std::uint16_t(a - b);
@ -8248,8 +8246,8 @@ namespace libtorrent {
#ifndef TORRENT_NO_DEPRECATE
std::int16_t clamped_subtract_s16(int a, int b)
{
if (a + (std::numeric_limits<std::int16_t>::min)() < b)
return (std::numeric_limits<std::int16_t>::min)();
if (a + std::numeric_limits<std::int16_t>::min() < b)
return std::numeric_limits<std::int16_t>::min();
return std::int16_t(a - b);
}
#endif
@ -8305,7 +8303,7 @@ namespace libtorrent {
// if we haven't yet met the seed limits, set the seed_ratio_not_met
// flag. That will make this seed prioritized
// downloaded may be 0 if the torrent is 0-sized
std::int64_t const downloaded = (std::max)(m_total_downloaded, m_torrent_file->total_size());
std::int64_t const downloaded = std::max(m_total_downloaded, m_torrent_file->total_size());
if (fin_time < seconds(s.get_int(settings_pack::seed_time_limit))
&& (download_time.count() > 1
&& fin_time * 100 / download_time < s.get_int(settings_pack::seed_time_ratio_limit))
@ -8429,7 +8427,7 @@ namespace libtorrent {
return m_paused || m_session_paused;
}
void torrent::pause(bool graceful)
void torrent::pause(bool const graceful)
{
TORRENT_ASSERT(is_single_thread());
INVARIANT_CHECK;
@ -8589,7 +8587,7 @@ namespace libtorrent {
// add or remove a url that will be attempted for
// finding the file(s) in this torrent.
web_seed_t* torrent::add_web_seed(std::string const& url
, web_seed_entry::type_t type
, web_seed_entry::type_t const type
, std::string const& auth
, web_seed_entry::headers_t const& extra_headers
, bool const ephemeral)
@ -8598,7 +8596,7 @@ namespace libtorrent {
ent.ephemeral = ephemeral;
// don't add duplicates
auto it = std::find(m_web_seeds.begin(), m_web_seeds.end(), ent);
auto const it = std::find(m_web_seeds.begin(), m_web_seeds.end(), ent);
if (it != m_web_seeds.end()) return &*it;
m_web_seeds.push_back(ent);
set_need_save_resume();
@ -8617,7 +8615,7 @@ namespace libtorrent {
else do_resume();
}
void torrent::set_paused(bool b, int flags)
void torrent::set_paused(bool const b, int flags)
{
TORRENT_ASSERT(is_single_thread());
@ -8803,7 +8801,7 @@ namespace libtorrent {
}
else
{
time_point32 next_tracker_announce = std::max(aep.next_announce, aep.min_announce);
time_point32 const next_tracker_announce = std::max(aep.next_announce, aep.min_announce);
if (next_tracker_announce < next_announce
&& (!state.found_working || aep.is_working()))
next_announce = next_tracker_announce;
@ -8969,7 +8967,7 @@ namespace libtorrent {
return aux::time_now32() - m_upload_mode_time;
}
void torrent::second_tick(int tick_interval_ms)
void torrent::second_tick(int const tick_interval_ms)
{
TORRENT_ASSERT(want_tick());
TORRENT_ASSERT(is_single_thread());
@ -9012,8 +9010,8 @@ namespace libtorrent {
if (settings().get_bool(settings_pack::rate_limit_ip_overhead))
{
int up_limit = upload_limit();
int down_limit = download_limit();
int const up_limit = upload_limit();
int const down_limit = download_limit();
if (down_limit > 0
&& m_stat.download_ip_overhead() >= down_limit
@ -9072,7 +9070,7 @@ namespace libtorrent {
TORRENT_INCREMENT(m_iterating_connections);
// look for the peer that saw a seed most recently
m_swarm_last_seen_complete = (std::max)(p->last_seen_complete(), m_swarm_last_seen_complete);
m_swarm_last_seen_complete = std::max(p->last_seen_complete(), m_swarm_last_seen_complete);
// updates the peer connection's ul/dl bandwidth
// resource requests
@ -9135,7 +9133,7 @@ namespace libtorrent {
if (ec) return;
bool is_inactive = is_inactive_internal();
bool const is_inactive = is_inactive_internal();
if (is_inactive == m_inactive) return;
m_inactive = is_inactive;
@ -9177,7 +9175,7 @@ namespace libtorrent {
// first find out which web seeds we are connected to
for (auto i = m_web_seeds.begin(); i != m_web_seeds.end() && limit > 0;)
{
auto w = i++;
auto const w = i++;
if (w->removed || w->retry > now)
continue;
@ -9261,7 +9259,7 @@ namespace libtorrent {
// now, download at least one piece, otherwise download one more
// piece if our downloaded (and downloading) pieces is less than 50%
// of the uploaded bytes
int num_downloaded_pieces = std::max(m_picker->num_have()
int const num_downloaded_pieces = std::max(m_picker->num_have()
, pieces_in_torrent - m_picker->num_filtered());
if (std::int64_t(num_downloaded_pieces) * m_torrent_file->piece_length()
@ -9282,7 +9280,7 @@ namespace libtorrent {
aux::vector<piece_index_t> rarest_pieces;
int num_pieces = m_torrent_file->num_pieces();
int const num_pieces = m_torrent_file->num_pieces();
int rarest_rarity = INT_MAX;
for (piece_index_t i(0); i < piece_index_t(num_pieces); ++i)
{
@ -9329,31 +9327,31 @@ namespace libtorrent {
update_want_peers();
}
void torrent::sent_bytes(int bytes_payload, int bytes_protocol)
void torrent::sent_bytes(int const bytes_payload, int const bytes_protocol)
{
m_stat.sent_bytes(bytes_payload, bytes_protocol);
m_ses.sent_bytes(bytes_payload, bytes_protocol);
}
void torrent::received_bytes(int bytes_payload, int bytes_protocol)
void torrent::received_bytes(int const bytes_payload, int const bytes_protocol)
{
m_stat.received_bytes(bytes_payload, bytes_protocol);
m_ses.received_bytes(bytes_payload, bytes_protocol);
}
void torrent::trancieve_ip_packet(int bytes, bool ipv6)
void torrent::trancieve_ip_packet(int const bytes, bool const ipv6)
{
m_stat.trancieve_ip_packet(bytes, ipv6);
m_ses.trancieve_ip_packet(bytes, ipv6);
}
void torrent::sent_syn(bool ipv6)
void torrent::sent_syn(bool const ipv6)
{
m_stat.sent_syn(ipv6);
m_ses.sent_syn(ipv6);
}
void torrent::received_synack(bool ipv6)
void torrent::received_synack(bool const ipv6)
{
m_stat.received_synack(ipv6);
m_ses.received_synack(ipv6);
@ -9462,9 +9460,9 @@ namespace libtorrent {
};
void pick_busy_blocks(piece_picker const* picker
, piece_index_t piece
, int blocks_in_piece
, int timed_out
, piece_index_t const piece
, int const blocks_in_piece
, int const timed_out
, std::vector<piece_block>& interesting_blocks
, piece_picker::downloading_piece const& pi)
{
@ -9526,15 +9524,15 @@ namespace libtorrent {
, piece_picker::downloading_piece const& pi
, time_critical_piece* i
, piece_picker const* picker
, int blocks_in_piece
, int timed_out)
, int const blocks_in_piece
, int const timed_out)
{
std::vector<piece_block> interesting_blocks;
std::vector<piece_block> backup1;
std::vector<piece_block> backup2;
std::vector<piece_index_t> ignore;
time_point now = aux::time_now();
time_point const now = aux::time_now();
// loop until every block has been requested from this piece (i->piece)
do
@ -9804,12 +9802,12 @@ namespace libtorrent {
// the number of "times" this piece has timed out.
int timed_out = 0;
int blocks_in_piece = m_picker->blocks_in_piece(i.piece);
int const blocks_in_piece = m_picker->blocks_in_piece(i.piece);
#if TORRENT_DEBUG_STREAMING > 0
i.timed_out = timed_out;
#endif
int free_to_request = blocks_in_piece
int const free_to_request = blocks_in_piece
- pi.finished - pi.writing - pi.requested;
if (free_to_request == 0)
@ -9822,7 +9820,7 @@ namespace libtorrent {
// one more request per block
if (m_average_piece_time > 0)
timed_out = int(total_milliseconds(now - i.last_requested)
/ (std::max)(int(m_average_piece_time + m_piece_time_deviation / 2), 1));
/ std::max(int(m_average_piece_time + m_piece_time_deviation / 2), 1));
#if TORRENT_DEBUG_STREAMING > 0
i.timed_out = timed_out;
@ -9889,7 +9887,7 @@ namespace libtorrent {
}
}
std::set<std::string> torrent::web_seeds(web_seed_entry::type_t type) const
std::set<std::string> torrent::web_seeds(web_seed_entry::type_t const type) const
{
TORRENT_ASSERT(is_single_thread());
std::set<std::string> ret;
@ -9903,7 +9901,7 @@ namespace libtorrent {
return ret;
}
void torrent::remove_web_seed(std::string const& url, web_seed_entry::type_t type)
void torrent::remove_web_seed(std::string const& url, web_seed_entry::type_t const type)
{
auto const i = std::find_if(m_web_seeds.begin(), m_web_seeds.end()
, [&] (web_seed_t const& w) { return w.url == url && w.type == type; });
@ -9932,7 +9930,7 @@ namespace libtorrent {
}
void torrent::remove_web_seed_conn(peer_connection* p, error_code const& ec
, operation_t op, int error)
, operation_t const op, int const error)
{
auto const i = std::find_if(m_web_seeds.begin(), m_web_seeds.end()
, [p] (web_seed_t const& ws) { return ws.peer_info.connection == p; });
@ -10012,7 +10010,7 @@ namespace libtorrent {
}
torrent_peer* torrent::add_peer(tcp::endpoint const& adr
, peer_source_flags_t const source, int flags)
, peer_source_flags_t const source, int const flags)
{
TORRENT_ASSERT(is_single_thread());
@ -10035,8 +10033,7 @@ namespace libtorrent {
// as well, to figure out if it supports
// DHT (uTorrent and BitComet don't
// advertise support)
udp::endpoint node(adr.address(), adr.port());
session().add_dht_node(node);
session().add_dht_node({adr.address(), adr.port()});
}
#endif
@ -10133,7 +10130,7 @@ namespace libtorrent {
return true;
}
void torrent::set_seed(torrent_peer* p, bool s)
void torrent::set_seed(torrent_peer* p, bool const s)
{
if (p->seed != s)
{
@ -10167,7 +10164,7 @@ namespace libtorrent {
return m_peer_list->find_peers(a);
}
void torrent::update_peer_port(int port, torrent_peer* p
void torrent::update_peer_port(int const port, torrent_peer* p
, peer_source_flags_t const src)
{
need_peer_list();
@ -10413,7 +10410,7 @@ namespace libtorrent {
namespace {
bool is_downloading_state(int st)
bool is_downloading_state(int const st)
{
switch (st)
{
@ -10434,7 +10431,7 @@ namespace {
}
}
void torrent::stop_when_ready(bool b)
void torrent::stop_when_ready(bool const b)
{
m_stop_when_ready = b;
@ -10523,7 +10520,7 @@ namespace {
#ifndef TORRENT_DISABLE_EXTENSIONS
void torrent::notify_extension_add_peer(tcp::endpoint const& ip
, peer_source_flags_t const src, int flags)
, peer_source_flags_t const src, int const flags)
{
for (auto& ext : m_extensions)
{
@ -10566,7 +10563,7 @@ namespace {
m_links[aux::session_interface::torrent_state_updates].insert(list, this);
}
void torrent::status(torrent_status* st, status_flags_t flags)
void torrent::status(torrent_status* st, status_flags_t const flags)
{
INVARIANT_CHECK;
@ -10609,7 +10606,7 @@ namespace {
st->completed_time = m_completed_time;
#ifndef TORRENT_NO_DEPRECATE
st->last_scrape = m_last_scrape == (std::numeric_limits<std::int16_t>::min)() ? -1
st->last_scrape = m_last_scrape == std::numeric_limits<std::int16_t>::min() ? -1
: clamped_subtract_u16(m_ses.session_time(), m_last_scrape);
#endif
@ -10880,7 +10877,7 @@ namespace {
}
void torrent::tracker_request_error(tracker_request const& r
, int response_code, error_code const& ec, std::string const& msg
, int const response_code, error_code const& ec, std::string const& msg
, seconds32 const retry_interval)
{
TORRENT_ASSERT(is_single_thread());
@ -10924,7 +10921,6 @@ namespace {
#endif
}
int const tracker_index = int(ae - m_trackers.data());
// never talk to this tracker again
@ -10947,7 +10943,7 @@ namespace {
if (response_code == 410)
{
// never talk to this tracker again
if (ae) ae->fail_limit = 1;
if (ae != nullptr) ae->fail_limit = 1;
}
// if this was triggered manually we need to post this unconditionally,
@ -10957,10 +10953,10 @@ namespace {
|| r.triggered_manually)
{
tcp::endpoint local_endpoint;
if (ae)
if (ae != nullptr)
{
auto aep = ae->find_endpoint(r.outgoing_socket);
if (aep) local_endpoint = aep->local_endpoint;
if (aep != nullptr) local_endpoint = aep->local_endpoint;
}
m_ses.alerts().emplace_alert<scrape_failed_alert>(get_handle(), local_endpoint, r.url, ec);