loop conversion #1 (#1783)

loop conversion #1
This commit is contained in:
Andrei Kurushin 2017-03-10 02:17:51 +03:00 committed by Arvid Norberg
parent a8b10a7449
commit a404673147
1 changed files with 64 additions and 86 deletions

View File

@ -2498,9 +2498,8 @@ namespace libtorrent
if (settings().get_bool(settings_pack::use_dht_as_fallback)) if (settings().get_bool(settings_pack::use_dht_as_fallback))
{ {
int verified_trackers = 0; int verified_trackers = 0;
for (std::vector<announce_entry>::const_iterator i = m_trackers.begin() for (auto const& t : m_trackers)
, end(m_trackers.end()); i != end; ++i) if (t.verified) ++verified_trackers;
if (i->verified) ++verified_trackers;
if (verified_trackers > 0) if (verified_trackers > 0)
debug_log("DHT: only using DHT as fallback, and there are %d working trackers", verified_trackers); debug_log("DHT: only using DHT as fallback, and there are %d working trackers", verified_trackers);
@ -2979,10 +2978,9 @@ namespace libtorrent
if (should_log()) if (should_log())
{ {
std::string resolved_to; std::string resolved_to;
for (std::list<address>::const_iterator i = tracker_ips.begin() for (auto const& i : tracker_ips)
, end(tracker_ips.end()); i != end; ++i)
{ {
resolved_to += i->to_string(); resolved_to += i.to_string();
resolved_to += ", "; resolved_to += ", ";
} }
debug_log("TRACKER RESPONSE\n" debug_log("TRACKER RESPONSE\n"
@ -2996,45 +2994,41 @@ namespace libtorrent
, resolved_to.c_str() , resolved_to.c_str()
, print_address(tracker_ip).c_str()); , print_address(tracker_ip).c_str());
for (std::vector<peer_entry>::const_iterator i = resp.peers.begin(); for (auto const& i : resp.peers)
i != resp.peers.end(); ++i)
{ {
debug_log(" %16s %5d %s %s", i->hostname.c_str(), i->port debug_log(" %16s %5d %s %s", i.hostname.c_str(), i.port
, i->pid.is_all_zeros()?"":aux::to_hex(i->pid).c_str() , i.pid.is_all_zeros()?"":aux::to_hex(i.pid).c_str()
, identify_client(i->pid).c_str()); , identify_client(i.pid).c_str());
} }
for (std::vector<ipv4_peer_entry>::const_iterator i = resp.peers4.begin(); for (auto const& i : resp.peers4)
i != resp.peers4.end(); ++i)
{ {
debug_log(" %s:%d", print_address(address_v4(i->ip)).c_str(), i->port); debug_log(" %s:%d", print_address(address_v4(i.ip)).c_str(), i.port);
} }
#if TORRENT_USE_IPV6 #if TORRENT_USE_IPV6
for (std::vector<ipv6_peer_entry>::const_iterator i = resp.peers6.begin(); for (auto const& i : resp.peers6)
i != resp.peers6.end(); ++i)
{ {
debug_log(" [%s]:%d", print_address(address_v6(i->ip)).c_str(), i->port); debug_log(" [%s]:%d", print_address(address_v6(i.ip)).c_str(), i.port);
} }
#endif #endif
} }
#endif #endif
// for each of the peers we got from the tracker // for each of the peers we got from the tracker
for (std::vector<peer_entry>::const_iterator i = resp.peers.begin(); for (auto const& i : resp.peers)
i != resp.peers.end(); ++i)
{ {
// don't make connections to ourself // don't make connections to ourself
if (i->pid == m_ses.get_peer_id()) if (i.pid == m_ses.get_peer_id())
continue; continue;
#if TORRENT_USE_I2P #if TORRENT_USE_I2P
if (r.i2pconn && string_ends_with(i->hostname, ".i2p")) if (r.i2pconn && string_ends_with(i.hostname, ".i2p"))
{ {
// this is an i2p name, we need to use the SAM connection // this is an i2p name, we need to use the SAM connection
// to do the name lookup // to do the name lookup
if (string_ends_with(i->hostname, ".b32.i2p")) if (string_ends_with(i.hostname, ".b32.i2p"))
{ {
ADD_OUTSTANDING_ASYNC("torrent::on_i2p_resolve"); ADD_OUTSTANDING_ASYNC("torrent::on_i2p_resolve");
r.i2pconn->async_name_lookup(i->hostname.c_str() r.i2pconn->async_name_lookup(i.hostname.c_str()
, std::bind(&torrent::on_i2p_resolve , std::bind(&torrent::on_i2p_resolve
, shared_from_this(), _1, _2)); , shared_from_this(), _1, _2));
} }
@ -3042,7 +3036,7 @@ namespace libtorrent
{ {
torrent_state st = get_peer_list_state(); torrent_state st = get_peer_list_state();
need_peer_list(); need_peer_list();
if (m_peer_list->add_i2p_peer(i->hostname.c_str (), peer_info::tracker, 0, &st)) if (m_peer_list->add_i2p_peer(i.hostname.c_str (), peer_info::tracker, 0, &st))
state_updated(); state_updated();
peers_erased(st.erased); peers_erased(st.erased);
} }
@ -3051,9 +3045,9 @@ namespace libtorrent
#endif #endif
{ {
ADD_OUTSTANDING_ASYNC("torrent::on_peer_name_lookup"); ADD_OUTSTANDING_ASYNC("torrent::on_peer_name_lookup");
m_ses.async_resolve(i->hostname, resolver_interface::abort_on_shutdown m_ses.async_resolve(i.hostname, resolver_interface::abort_on_shutdown
, std::bind(&torrent::on_peer_name_lookup , std::bind(&torrent::on_peer_name_lookup
, shared_from_this(), _1, _2, i->port)); , shared_from_this(), _1, _2, i.port));
} }
} }
@ -3067,18 +3061,16 @@ namespace libtorrent
// peers on the same local network // peers on the same local network
bool need_update = false; bool need_update = false;
for (std::vector<ipv4_peer_entry>::const_iterator i = resp.peers4.begin(); for (auto const& i : resp.peers4)
i != resp.peers4.end(); ++i)
{ {
tcp::endpoint a(address_v4(i->ip), i->port); tcp::endpoint a(address_v4(i.ip), i.port);
need_update |= bool(add_peer(a, peer_info::tracker) != nullptr); need_update |= bool(add_peer(a, peer_info::tracker) != nullptr);
} }
#if TORRENT_USE_IPV6 #if TORRENT_USE_IPV6
for (std::vector<ipv6_peer_entry>::const_iterator i = resp.peers6.begin(); for (auto const& i : resp.peers6)
i != resp.peers6.end(); ++i)
{ {
tcp::endpoint a(address_v6(i->ip), i->port); tcp::endpoint a(address_v6(i.ip), i.port);
need_update |= bool(add_peer(a, peer_info::tracker) != nullptr); need_update |= bool(add_peer(a, peer_info::tracker) != nullptr);
} }
#endif #endif
@ -3398,11 +3390,10 @@ namespace libtorrent
std::vector<file_slice> files = fs.map_block( std::vector<file_slice> 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; std::int64_t ret = 0;
for (std::vector<file_slice>::iterator i = files.begin() for (auto const& i : files)
, end(files.end()); i != end; ++i)
{ {
if (fs.pad_file_at(i->file_index)) continue; if (fs.pad_file_at(i.file_index)) continue;
ret += i->size; 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); return aux::numeric_cast<int>(ret);
@ -3985,10 +3976,8 @@ namespace libtorrent
std::copy(downloaders.begin(), downloaders.end(), std::inserter(peers, peers.begin())); std::copy(downloaders.begin(), downloaders.end(), std::inserter(peers, peers.begin()));
#if TORRENT_USE_ASSERTS #if TORRENT_USE_ASSERTS
for (std::vector<torrent_peer*>::iterator i = downloaders.begin() for (auto const& p : downloaders)
, end(downloaders.end()); i != end; ++i)
{ {
torrent_peer* p = static_cast<torrent_peer*>(*i);
if (p && p->connection) if (p && p->connection)
{ {
peer_connection* peer = static_cast<peer_connection*>(p->connection); peer_connection* peer = static_cast<peer_connection*>(p->connection);
@ -4102,10 +4091,8 @@ namespace libtorrent
} }
#if TORRENT_USE_ASSERTS #if TORRENT_USE_ASSERTS
for (std::vector<torrent_peer*>::iterator i = downloaders.begin() for (auto const& p : downloaders)
, end(downloaders.end()); i != end; ++i)
{ {
torrent_peer* p = *i;
if (p && p->connection) if (p && p->connection)
{ {
peer_connection* peer = static_cast<peer_connection*>(p->connection); peer_connection* peer = static_cast<peer_connection*>(p->connection);
@ -4152,16 +4139,14 @@ namespace libtorrent
// blocks to this piece // blocks to this piece
for (auto p : m_connections) for (auto p : m_connections)
{ {
std::vector<pending_block> const& dq = p->download_queue(); for (auto const& b : p->download_queue())
std::vector<pending_block> const& rq = p->request_queue();
for (auto const& b : dq)
{ {
if (b.timed_out || b.not_wanted) continue; if (b.timed_out || b.not_wanted) continue;
if (b.block.piece_index != piece) continue; if (b.block.piece_index != piece) continue;
m_picker->mark_as_downloading(b.block, p->peer_info_struct() m_picker->mark_as_downloading(b.block, p->peer_info_struct()
, p->picker_options()); , p->picker_options());
} }
for (auto const& b : rq) for (auto const& b : p->request_queue())
{ {
if (b.block.piece_index != piece) continue; if (b.block.piece_index != piece) continue;
m_picker->mark_as_downloading(b.block, p->peer_info_struct() m_picker->mark_as_downloading(b.block, p->peer_info_struct()
@ -4494,22 +4479,20 @@ namespace libtorrent
// make a copy of the download queue since we may be cancelling entries // make a copy of the download queue since we may be cancelling entries
// from it from within the loop // from it from within the loop
std::vector<pending_block> dq = p->download_queue(); std::vector<pending_block> dq = p->download_queue();
for (std::vector<pending_block>::iterator k = dq.begin() for (auto const& k : dq)
, end2(dq.end()); k != end2; ++k)
{ {
if (time_critical.count(k->block.piece_index)) continue; if (time_critical.count(k.block.piece_index)) continue;
if (k->not_wanted || k->timed_out) continue; if (k.not_wanted || k.timed_out) continue;
p->cancel_request(k->block, true); p->cancel_request(k.block, true);
} }
// make a copy of the download queue since we may be cancelling entries // make a copy of the download queue since we may be cancelling entries
// from it from within the loop // from it from within the loop
std::vector<pending_block> rq = p->request_queue(); std::vector<pending_block> rq = p->request_queue();
for (std::vector<pending_block>::const_iterator k = rq.begin() for (auto const& k : rq)
, end2(rq.end()); k != end2; ++k)
{ {
if (time_critical.count(k->block.piece_index)) continue; if (time_critical.count(k.block.piece_index)) continue;
p->cancel_request(k->block, true); p->cancel_request(k.block, true);
} }
} }
} }
@ -4623,8 +4606,7 @@ namespace libtorrent
void torrent::remove_time_critical_piece(piece_index_t piece, bool finished) void torrent::remove_time_critical_piece(piece_index_t piece, bool finished)
{ {
for (std::vector<time_critical_piece>::iterator i for (auto i = m_time_critical_pieces.begin(), end(m_time_critical_pieces.end());
= m_time_critical_pieces.begin(), end(m_time_critical_pieces.end());
i != end; ++i) i != end; ++i)
{ {
if (i->piece != piece) continue; if (i->piece != piece) continue;
@ -7208,12 +7190,11 @@ namespace libtorrent
if (!m_announcing) return; if (!m_announcing) return;
time_point32 const now = aux::time_now32(); time_point32 const now = aux::time_now32();
for (std::vector<announce_entry>::iterator i = m_trackers.begin() for (auto& t : m_trackers)
, end(m_trackers.end()); i != end; ++i)
{ {
if (i->complete_sent) continue; if (t.complete_sent) continue;
i->next_announce = now; t.next_announce = now;
i->min_announce = now; t.min_announce = now;
} }
announce_with_tracker(); announce_with_tracker();
} }
@ -7529,11 +7510,10 @@ namespace libtorrent
TORRENT_ASSERT(current_stats_state() == int(m_current_gauge_state + counters::num_checking_torrents) TORRENT_ASSERT(current_stats_state() == int(m_current_gauge_state + counters::num_checking_torrents)
|| m_current_gauge_state == no_gauge_state); || m_current_gauge_state == no_gauge_state);
for (std::vector<time_critical_piece>::const_iterator i = m_time_critical_pieces.begin() for (auto const& i : m_time_critical_pieces)
, end(m_time_critical_pieces.end()); i != end; ++i)
{ {
TORRENT_ASSERT(!is_seed()); TORRENT_ASSERT(!is_seed());
TORRENT_ASSERT(!has_picker() || !m_picker->have_piece(i->piece)); TORRENT_ASSERT(!has_picker() || !m_picker->have_piece(i.piece));
} }
switch (current_stats_state()) switch (current_stats_state())
@ -7638,16 +7618,14 @@ namespace libtorrent
if (p.peer_info_struct() && p.peer_info_struct()->seed) if (p.peer_info_struct() && p.peer_info_struct()->seed)
++seeds; ++seeds;
for (std::vector<pending_block>::const_iterator j = p.request_queue().begin() for (auto const& j : p.request_queue())
, end(p.request_queue().end()); j != end; ++j)
{ {
if (!j->not_wanted && !j->timed_out) ++num_requests[j->block]; if (!j.not_wanted && !j.timed_out) ++num_requests[j.block];
} }
for (std::vector<pending_block>::const_iterator j = p.download_queue().begin() for (auto const& j : p.download_queue())
, end(p.download_queue().end()); j != end; ++j)
{ {
if (!j->not_wanted && !j->timed_out) ++num_requests[j->block]; if (!j.not_wanted && !j.timed_out) ++num_requests[j.block];
} }
if (!p.is_choked() && !p.ignore_unchoke_slots()) ++num_uploads; if (!p.is_choked() && !p.ignore_unchoke_slots()) ++num_uploads;
@ -8814,14 +8792,13 @@ namespace libtorrent
std::printf("average piece download time: %.2f s (+/- %.2f s)\n" std::printf("average piece download time: %.2f s (+/- %.2f s)\n"
, m_average_piece_time / 1000.f , m_average_piece_time / 1000.f
, m_piece_time_deviation / 1000.f); , m_piece_time_deviation / 1000.f);
for (std::vector<partial_piece_info>::iterator i = queue.begin() for (auto& i : queue)
, end(queue.end()); i != end; ++i)
{ {
extern void print_piece(libtorrent::partial_piece_info* pp extern void print_piece(libtorrent::partial_piece_info* pp
, std::vector<libtorrent::peer_info> const& peers , std::vector<libtorrent::peer_info> const& peers
, std::vector<time_critical_piece> const& time_critical); , std::vector<time_critical_piece> const& time_critical);
print_piece(&*i, peer_list, m_time_critical_pieces); print_piece(&i, peer_list, m_time_critical_pieces);
} }
#endif // TORRENT_DEBUG_STREAMING #endif // TORRENT_DEBUG_STREAMING
@ -9515,8 +9492,8 @@ namespace libtorrent
// now, iterate over all time critical pieces, in order of importance, and // now, iterate over all time critical pieces, in order of importance, and
// request them from the peers, in order of responsiveness. i.e. request // request them from the peers, in order of responsiveness. i.e. request
// the most time critical pieces from the fastest peers. // the most time critical pieces from the fastest peers.
for (std::vector<time_critical_piece>::iterator i = m_time_critical_pieces.begin() bool first_piece{true};
, end(m_time_critical_pieces.end()); i != end; ++i) for (auto& i : m_time_critical_pieces)
{ {
#if TORRENT_DEBUG_STREAMING > 1 #if TORRENT_DEBUG_STREAMING > 1
std::printf("considering %d\n", i->piece); std::printf("considering %d\n", i->piece);
@ -9533,7 +9510,7 @@ namespace libtorrent
// the +1000 is to compensate for the fact that we only call this // the +1000 is to compensate for the fact that we only call this
// function once per second, so if we need to request it 500 ms from // function once per second, so if we need to request it 500 ms from
// now, we should request it right away // now, we should request it right away
if (i != m_time_critical_pieces.begin() && i->deadline > now if (!first_piece && i.deadline > now
+ milliseconds(m_average_piece_time + m_piece_time_deviation * 4 + 1000)) + milliseconds(m_average_piece_time + m_piece_time_deviation * 4 + 1000))
{ {
// don't request pieces whose deadline is too far in the future // don't request pieces whose deadline is too far in the future
@ -9546,35 +9523,36 @@ namespace libtorrent
#endif #endif
break; break;
} }
first_piece = false;
piece_picker::downloading_piece pi; piece_picker::downloading_piece pi;
m_picker->piece_info(i->piece, pi); m_picker->piece_info(i.piece, pi);
// the number of "times" this piece has timed out. // the number of "times" this piece has timed out.
int timed_out = 0; int timed_out = 0;
int blocks_in_piece = m_picker->blocks_in_piece(i->piece); int blocks_in_piece = m_picker->blocks_in_piece(i.piece);
#if TORRENT_DEBUG_STREAMING > 0 #if TORRENT_DEBUG_STREAMING > 0
i->timed_out = timed_out; i.timed_out = timed_out;
#endif #endif
int free_to_request = blocks_in_piece int free_to_request = blocks_in_piece
- pi.finished - pi.writing - pi.requested; - pi.finished - pi.writing - pi.requested;
if (free_to_request == 0) if (free_to_request == 0)
{ {
if (i->last_requested == min_time()) if (i.last_requested == min_time())
i->last_requested = now; i.last_requested = now;
// if it's been more than half of the typical download time // if it's been more than half of the typical download time
// of a piece since we requested the last block, allow // of a piece since we requested the last block, allow
// one more request per block // one more request per block
if (m_average_piece_time > 0) if (m_average_piece_time > 0)
timed_out = int(total_milliseconds(now - i->last_requested) 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 #if TORRENT_DEBUG_STREAMING > 0
i->timed_out = timed_out; i.timed_out = timed_out;
#endif #endif
// every block in this piece is already requested // every block in this piece is already requested
// there's no need to consider this piece, unless it // there's no need to consider this piece, unless it
@ -9583,7 +9561,7 @@ namespace libtorrent
{ {
#if TORRENT_DEBUG_STREAMING > 1 #if TORRENT_DEBUG_STREAMING > 1
std::printf("skipping %d (full) [req: %d timed_out: %d ]\n" std::printf("skipping %d (full) [req: %d timed_out: %d ]\n"
, i->piece, pi.requested , i.piece, pi.requested
, timed_out); , timed_out);
#endif #endif
@ -9609,7 +9587,7 @@ namespace libtorrent
// since it's ordered by download queue time // since it's ordered by download queue time
pick_time_critical_block(peers, ignore_peers pick_time_critical_block(peers, ignore_peers
, peers_with_requests , peers_with_requests
, pi, &*i, m_picker.get() , pi, &i, m_picker.get()
, blocks_in_piece, timed_out); , blocks_in_piece, timed_out);
// put back the peers we ignored into the peer list for the next piece // put back the peers we ignored into the peer list for the next piece