From 9e1305ed7550d23dacf291744542d9fbb9896635 Mon Sep 17 00:00:00 2001 From: Alden Torres Date: Tue, 15 Nov 2016 12:03:11 -0500 Subject: [PATCH] auto loop, consts and minor cleanup refactor --- include/libtorrent/create_torrent.hpp | 11 +----- include/libtorrent/magnet_uri.hpp | 1 - src/create_torrent.cpp | 57 ++++++++++++--------------- src/disk_io_thread.cpp | 12 +++--- src/magnet_uri.cpp | 4 -- src/session_impl.cpp | 9 ++--- src/torrent.cpp | 50 ++++++++++------------- 7 files changed, 57 insertions(+), 87 deletions(-) diff --git a/include/libtorrent/create_torrent.hpp b/include/libtorrent/create_torrent.hpp index 679c315e3..15a523d94 100644 --- a/include/libtorrent/create_torrent.hpp +++ b/include/libtorrent/create_torrent.hpp @@ -44,10 +44,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -#include "libtorrent/aux_/disable_warnings_push.hpp" -#include -#include "libtorrent/aux_/disable_warnings_pop.hpp" - // OVERVIEW // // This section describes the functions and classes that are used @@ -303,8 +299,7 @@ namespace libtorrent entry m_info_dict; // the urls to the trackers - typedef std::pair announce_entry; - std::vector m_urls; + std::vector> m_urls; std::vector m_url_seeds; std::vector m_http_seeds; @@ -322,8 +317,7 @@ namespace libtorrent mutable std::vector m_merkle_tree; // dht nodes to add to the routing table/bootstrap from - using nodes_t = std::vector>; - nodes_t m_nodes; + std::vector> m_nodes; // the hash that identifies this torrent // is mutable because it's calculated @@ -485,4 +479,3 @@ namespace libtorrent } #endif - diff --git a/include/libtorrent/magnet_uri.hpp b/include/libtorrent/magnet_uri.hpp index 0ec293324..5fb161bd7 100644 --- a/include/libtorrent/magnet_uri.hpp +++ b/include/libtorrent/magnet_uri.hpp @@ -81,4 +81,3 @@ namespace libtorrent } #endif - diff --git a/src/create_torrent.cpp b/src/create_torrent.cpp index 72c48d256..a2483fd13 100644 --- a/src/create_torrent.cpp +++ b/src/create_torrent.cpp @@ -32,8 +32,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/create_torrent.hpp" #include "libtorrent/utf8.hpp" -#include "libtorrent/file_pool.hpp" -#include "libtorrent/storage.hpp" #include "libtorrent/aux_/escape_string.hpp" // for convert_to_wstring #include "libtorrent/disk_io_thread.hpp" #include "libtorrent/aux_/merkle.hpp" // for merkle_*() @@ -112,7 +110,8 @@ namespace libtorrent } void add_files_impl(file_storage& fs, std::string const& p - , std::string const& l, std::function pred, std::uint32_t flags) + , std::string const& l, std::function pred + , std::uint32_t const flags) { std::string f = combine_path(p, l); if (!pred(f)) return; @@ -420,12 +419,11 @@ namespace libtorrent { entry& nodes = dict["nodes"]; entry::list_type& nodes_list = nodes.list(); - for (nodes_t::const_iterator i = m_nodes.begin() - , end(m_nodes.end()); i != end; ++i) + for (auto const& n : m_nodes) { entry::list_type node; - node.push_back(entry(i->first)); - node.push_back(entry(i->second)); + node.push_back(entry(n.first)); + node.push_back(entry(n.second)); nodes_list.push_back(entry(node)); } } @@ -435,16 +433,15 @@ namespace libtorrent entry trackers(entry::list_t); entry tier(entry::list_t); int current_tier = m_urls.front().second; - for (std::vector::const_iterator i = m_urls.begin(); - i != m_urls.end(); ++i) + for (auto const& url : m_urls) { - if (i->second != current_tier) + if (url.second != current_tier) { - current_tier = i->second; + current_tier = url.second; trackers.list().push_back(tier); tier.list().clear(); } - tier.list().push_back(entry(i->first)); + tier.list().push_back(entry(url.first)); } trackers.list().push_back(tier); dict["announce-list"] = trackers; @@ -467,10 +464,9 @@ namespace libtorrent else { entry& list = dict["url-list"]; - for (std::vector::const_iterator i - = m_url_seeds.begin(); i != m_url_seeds.end(); ++i) + for (auto const& url : m_url_seeds) { - list.list().push_back(entry(*i)); + list.list().push_back(entry(url)); } } } @@ -484,10 +480,9 @@ namespace libtorrent else { entry& list = dict["httpseeds"]; - for (std::vector::const_iterator i - = m_http_seeds.begin(); i != m_http_seeds.end(); ++i) + for (auto const& url : m_http_seeds) { - list.list().push_back(entry(*i)); + list.list().push_back(entry(url)); } } } @@ -503,20 +498,18 @@ namespace libtorrent if (!m_collections.empty()) { entry& list = info["collections"]; - for (std::vector::const_iterator i - = m_collections.begin(); i != m_collections.end(); ++i) + for (auto const& c : m_collections) { - list.list().push_back(entry(*i)); + list.list().push_back(entry(c)); } } if (!m_similar.empty()) { entry& list = info["similar"]; - for (std::vector::const_iterator i - = m_similar.begin(); i != m_similar.end(); ++i) + for (auto const& ih : m_similar) { - list.list().push_back(entry(i->to_string())); + list.list().push_back(entry(ih.to_string())); } } @@ -582,7 +575,7 @@ namespace libtorrent path_e.list().push_back(entry(e)); } - int flags = m_files.file_flags(i); + int const flags = m_files.file_flags(i); if (flags != 0) { std::string& attr = file_e["attr"].string(); @@ -612,16 +605,15 @@ namespace libtorrent info["piece length"] = m_files.piece_length(); if (m_merkle_torrent) { - int num_leafs = merkle_num_leafs(m_files.num_pieces()); - int num_nodes = merkle_num_nodes(num_leafs); - int first_leaf = num_nodes - num_leafs; + int const num_leafs = merkle_num_leafs(m_files.num_pieces()); + int const num_nodes = merkle_num_nodes(num_leafs); + int const first_leaf = num_nodes - num_leafs; m_merkle_tree.resize(num_nodes); - int num_pieces = int(m_piece_hash.size()); + int const num_pieces = int(m_piece_hash.size()); for (int i = 0; i < num_pieces; ++i) m_merkle_tree[first_leaf + i] = m_piece_hash[i]; - sha1_hash filler(nullptr); for (int i = num_pieces; i < num_leafs; ++i) - m_merkle_tree[first_leaf + i] = filler; + m_merkle_tree[first_leaf + i].clear(); // now that we have initialized all leaves, build // each level bottom-up @@ -634,7 +626,7 @@ namespace libtorrent { hasher h; h.update(m_merkle_tree[i]); - h.update(m_merkle_tree[i+1]); + h.update(m_merkle_tree[i + 1]); m_merkle_tree[parent] = h.final(); } level_start = merkle_get_parent(level_start); @@ -660,6 +652,7 @@ namespace libtorrent void create_torrent::add_tracker(string_view url, int const tier) { + using announce_entry = std::pair; auto i = std::find_if(m_urls.begin(), m_urls.end() , [&url](announce_entry const& ae) { return ae.first == url.to_string(); }); if (i != m_urls.end()) return; diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index c68b4650c..8e35b9aa1 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -276,7 +276,7 @@ namespace libtorrent // flush all blocks that are below p->hash.offset, since we've // already hashed those blocks, they won't cause any read-back - int disk_io_thread::try_flush_hashed(cached_piece_entry* p, int cont_block + int disk_io_thread::try_flush_hashed(cached_piece_entry* p, int const cont_block , jobqueue_t& completed_jobs, std::unique_lock& l) { TORRENT_ASSERT(m_magic == 0x1337); @@ -297,7 +297,7 @@ namespace libtorrent // end is one past the end // round offset up to include the last block, which might // have an odd size - int block_size = m_disk_cache.block_size(); + int const block_size = m_disk_cache.block_size(); int end = p->hashing_done ? p->blocks_in_piece : (p->hash->offset + block_size - 1) / block_size; // nothing has been hashed yet, don't flush anything @@ -602,7 +602,7 @@ namespace libtorrent // first piece, if the iovec spans multiple pieces void disk_io_thread::flush_iovec(cached_piece_entry* pe , span iov, span flushing - , int num_blocks, storage_error& error) + , int const num_blocks, storage_error& error) { TORRENT_PIECE_ASSERT(!error, pe); TORRENT_PIECE_ASSERT(num_blocks > 0, pe); @@ -672,7 +672,7 @@ namespace libtorrent // build_iovec, to reset their state to not being flushed anymore // the cache needs to be locked when calling this function void disk_io_thread::iovec_flushed(cached_piece_entry* pe - , int* flushing, int num_blocks, int block_offset + , int* flushing, int const num_blocks, int const block_offset , storage_error const& error , jobqueue_t& completed_jobs) { @@ -812,7 +812,7 @@ namespace libtorrent } } - void disk_io_thread::flush_cache(storage_interface* storage, std::uint32_t flags + void disk_io_thread::flush_cache(storage_interface* storage, std::uint32_t const flags , jobqueue_t& completed_jobs, std::unique_lock& l) { if (storage) @@ -1509,7 +1509,7 @@ namespace libtorrent void disk_io_thread::async_read(storage_interface* storage, peer_request const& r , std::function handler, void* requester - , int flags) + , int const flags) { TORRENT_ASSERT(r.length <= m_disk_cache.block_size()); TORRENT_ASSERT(r.length <= 16 * 1024); diff --git a/src/magnet_uri.cpp b/src/magnet_uri.cpp index 5dfd4a1e5..d2c1013c5 100644 --- a/src/magnet_uri.cpp +++ b/src/magnet_uri.cpp @@ -39,8 +39,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/hex.hpp" // to_hex, from_hex #include "libtorrent/socket_io.hpp" -#include - namespace libtorrent { std::string make_magnet_uri(torrent_handle const& handle) @@ -287,5 +285,3 @@ namespace libtorrent if (!name.empty()) p.name = name; } } - - diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 494b28ffb..f5c7fd623 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -4724,11 +4724,8 @@ namespace aux { #ifndef TORRENT_DISABLE_DHT // add params.dht_nodes to the DHT, if enabled - if (!params.dht_nodes.empty()) - { - for (auto const& n : params.dht_nodes) - add_dht_node_name(n); - } + for (auto const& n : params.dht_nodes) + add_dht_node_name(n); #endif INVARIANT_CHECK; @@ -4755,7 +4752,7 @@ namespace aux { } #endif - if (params.info_hash == sha1_hash(nullptr)) + if (params.info_hash.is_all_zeros()) { ec = errors::missing_info_hash_in_uri; return std::make_pair(ptr_t(), false); diff --git a/src/torrent.cpp b/src/torrent.cpp index 3c3e452f1..0e34e778f 100644 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -368,7 +368,7 @@ namespace libtorrent m_total_uploaded = p.total_uploaded; m_total_downloaded = p.total_downloaded; - // the numeber of seconds this torrent has spent in started, finished and + // the number of seconds this torrent has spent in started, finished and // seeding state so far, respectively. m_active_time = p.active_time; m_finished_time = p.finished_time; @@ -456,17 +456,16 @@ namespace libtorrent // if the user added any trackers while downloading the // .torrent file, merge them into the new tracker list std::vector new_trackers = m_torrent_file->trackers(); - for (std::vector::iterator i = m_trackers.begin() - , end(m_trackers.end()); i != end; ++i) + for (auto const& tr : m_trackers) { // if we already have this tracker, ignore it if (std::any_of(new_trackers.begin(), new_trackers.end() - , [i] (announce_entry const& ae) { return ae.url == i->url; })) + , [&tr] (announce_entry const& ae) { return ae.url == tr.url; })) continue; // insert the tracker ordered by tier new_trackers.insert(std::find_if(new_trackers.begin(), new_trackers.end() - , [i] (announce_entry const& ae) { return ae.tier >= i->tier; }), *i); + , [&tr] (announce_entry const& ae) { return ae.tier >= tr.tier; }), tr); } m_trackers.swap(new_trackers); @@ -780,9 +779,8 @@ namespace libtorrent if (!settings().get_bool(settings_pack::use_dht_as_fallback)) return true; int verified_trackers = 0; - for (std::vector::const_iterator i = m_trackers.begin() - , end(m_trackers.end()); i != end; ++i) - if (i->verified) ++verified_trackers; + for (auto const& tr : m_trackers) + if (tr.verified) ++verified_trackers; return verified_trackers == 0; } @@ -1778,7 +1776,7 @@ namespace libtorrent // in case file priorities were passed in via the add_torrent_params // and also in the case of share mode, we need to update the priorities if (!m_file_priority.empty() && std::find(m_file_priority.begin() - , m_file_priority.end(), 0) != m_file_priority.end()) + , m_file_priority.end(), 0) != m_file_priority.end()) { update_piece_priorities(); } @@ -1821,13 +1819,13 @@ namespace libtorrent piece_block pb(pr.piece, pr.start / block); for (; pr.length >= block; pr.length -= block, ++pb.block_index) { - if (int(pb.block_index) == blocks_per_piece) { pb.block_index = 0; ++pb.piece_index; } + if (pb.block_index == blocks_per_piece) { pb.block_index = 0; ++pb.piece_index; } m_picker->mark_as_finished(pb, nullptr); } // ugly edge case where padfiles are not used they way they're // supposed to be. i.e. added back-to back or at the end if (pb.block_index == blocks_per_piece) { pb.block_index = 0; ++pb.piece_index; } - if (pr.length > 0 && ((i+1 != fs.num_files() && fs.pad_file_at(i+1)) + if (pr.length > 0 && ((i+1 != fs.num_files() && fs.pad_file_at(i + 1)) || i + 1 == fs.num_files())) { m_picker->mark_as_finished(pb, nullptr); @@ -1844,20 +1842,18 @@ namespace libtorrent std::vector have_pieces; - for (std::vector::const_iterator i - = dq.begin(); i != dq.end(); ++i) + for (auto const& p : dq) { - int num_blocks = m_picker->blocks_in_piece(i->index); - if (i->finished < num_blocks) continue; - have_pieces.push_back(i->index); + int num_blocks = m_picker->blocks_in_piece(p.index); + if (p.finished < num_blocks) continue; + have_pieces.push_back(p.index); } - for (std::vector::iterator i = have_pieces.begin(); - i != have_pieces.end(); ++i) + for (auto const i : have_pieces) { - picker().piece_passed(*i); - TORRENT_ASSERT(picker().have_piece(*i)); - we_have(*i); + picker().piece_passed(i); + TORRENT_ASSERT(picker().have_piece(i)); + we_have(i); } } @@ -1871,11 +1867,9 @@ namespace libtorrent { resolve_links res(m_torrent_file); - std::vector s = m_torrent_file->similar_torrents(); - for (std::vector::iterator i = s.begin(), end(s.end()); - i != end; ++i) + for (auto const& ih : m_torrent_file->similar_torrents()) { - std::shared_ptr t = m_ses.find_torrent(*i).lock(); + std::shared_ptr t = m_ses.find_torrent(ih).lock(); if (!t) continue; // Only attempt to reuse files from torrents that are seeding. @@ -1885,11 +1879,9 @@ namespace libtorrent res.match(t->get_torrent_copy(), t->save_path()); } - std::vector c = m_torrent_file->collections(); - for (std::vector::iterator i = c.begin(), end(c.end()); - i != end; ++i) + for (auto const& c : m_torrent_file->collections()) { - std::vector> ts = m_ses.find_collection(*i); + std::vector> ts = m_ses.find_collection(c); for (std::vector>::iterator k = ts.begin() , end2(ts.end()); k != end2; ++k)