some consts refs and refactor (#1075)

some consts refs and refactor
This commit is contained in:
Alden Torres 2016-09-09 15:02:20 -04:00 committed by Arvid Norberg
parent 99da5c8017
commit 5480e2eedc
14 changed files with 78 additions and 107 deletions

View File

@ -36,7 +36,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/config.hpp"
#include <string>
#include <cstdint>
namespace libtorrent {
struct settings_pack;

View File

@ -322,7 +322,7 @@ namespace libtorrent
bool announce_dht() const override { return !m_listen_sockets.empty(); }
void add_dht_node_name(std::pair<std::string, int> const& node);
void add_dht_node(udp::endpoint n) override;
void add_dht_node(udp::endpoint const& n) override;
void add_dht_router(std::pair<std::string, int> const& node);
void set_dht_settings(dht_settings const& s);
dht_settings const& get_dht_settings() const { return m_dht_settings; }

View File

@ -323,7 +323,7 @@ namespace libtorrent { namespace aux
#ifndef TORRENT_DISABLE_DHT
virtual bool announce_dht() const = 0;
virtual void add_dht_node(udp::endpoint n) = 0;
virtual void add_dht_node(udp::endpoint const& n) = 0;
virtual bool has_dht() const = 0;
virtual int external_udp_port() const = 0;
virtual dht::dht_tracker* dht() = 0;
@ -339,4 +339,3 @@ namespace libtorrent { namespace aux
}}
#endif

View File

@ -102,4 +102,3 @@ namespace libtorrent
}
#endif // TORRENT_HTTP_TRACKER_CONNECTION_HPP_INCLUDED

View File

@ -84,7 +84,7 @@ namespace libtorrent { namespace dht
// understanding of its external address (which may have changed)
void update_node_id();
void add_node(udp::endpoint node);
void add_node(udp::endpoint const& node);
void add_router_node(udp::endpoint const& node);
entry state() const;

View File

@ -72,7 +72,7 @@ namespace libtorrent { namespace dht
struct traversal_algorithm;
struct dht_observer;
void TORRENT_EXTRA_EXPORT write_nodes_entry(entry& r, nodes_t const& nodes);
TORRENT_EXTRA_EXPORT void write_nodes_entry(entry& r, nodes_t const& nodes);
struct null_type {};
@ -142,7 +142,7 @@ public:
enum flags_t { flag_seed = 1, flag_implied_port = 2 };
void get_peers(sha1_hash const& info_hash
, std::function<void(std::vector<tcp::endpoint> const&)> dcallback
, std::function<void(std::vector<std::pair<node_entry, std::string> > const&)> ncallback
, std::function<void(std::vector<std::pair<node_entry, std::string>> const&)> ncallback
, bool noseeds);
void announce(sha1_hash const& info_hash, int listen_port, int flags
, std::function<void(std::vector<tcp::endpoint> const&)> f);
@ -173,7 +173,7 @@ public:
// pings the given node, and adds it to
// the routing table if it response and if the
// bucket is not full.
void add_node(udp::endpoint node);
void add_node(udp::endpoint const& node);
void replacement_cache(bucket_t& nodes) const
{ m_table.replacement_cache(nodes); }
@ -210,7 +210,7 @@ public:
char const* protocol_family_name() const { return m_protocol.family_name; }
char const* protocol_nodes_key() const { return m_protocol.nodes_key; }
bool native_address(udp::endpoint ep) const
bool native_address(udp::endpoint const& ep) const
{ return ep.protocol().family() == m_protocol.protocol.family(); }
bool native_address(tcp::endpoint ep) const
{ return ep.protocol().family() == m_protocol.protocol.family(); }

View File

@ -33,15 +33,14 @@ POSSIBILITY OF SUCH DAMAGE.
#ifndef TORRENT_SESSION_SETTINGS_HPP_INCLUDED
#define TORRENT_SESSION_SETTINGS_HPP_INCLUDED
#include "libtorrent/version.hpp"
#include "libtorrent/config.hpp"
#include "libtorrent/settings_pack.hpp"
#include "libtorrent/aux_/proxy_settings.hpp"
#ifndef TORRENT_NO_DEPRECATE
#include "libtorrent/version.hpp"
#endif
#include <cstdint>
#include <string>
#include <vector>
#include <utility>
namespace libtorrent
{

View File

@ -1658,4 +1658,3 @@ namespace libtorrent
}
#endif // TORRENT_TORRENT_HPP_INCLUDED

View File

@ -115,10 +115,10 @@ namespace libtorrent
// this is used to make sure that the block_index stays within
// bounds. If the entire piece is downloaded, the block_index
// would otherwise point to one past the end
int correction = ret.bytes_downloaded ? -1 : 0;
int const correction = ret.bytes_downloaded ? -1 : 0;
ret.block_index = (pr.start + ret.bytes_downloaded + correction) / t->block_size();
ret.full_block_bytes = t->block_size();
const int last_piece = t->torrent_file().num_pieces() - 1;
int const last_piece = t->torrent_file().num_pieces() - 1;
if (ret.piece_index == last_piece && ret.block_index
== t->torrent_file().piece_size(last_piece) / t->block_size())
ret.full_block_bytes = t->torrent_file().piece_size(last_piece) % t->block_size();

View File

@ -64,7 +64,7 @@ namespace libtorrent
}
// send CONNECT
std::back_insert_iterator<std::vector<char> > p(m_buffer);
std::back_insert_iterator<std::vector<char>> p(m_buffer);
std::string endpoint;
if (!m_hostname.empty())
{
@ -99,7 +99,7 @@ namespace libtorrent
{
if (handle_error(e, h)) return;
int read_pos = int(m_buffer.size());
int const read_pos = int(m_buffer.size());
// look for \n\n and \r\n\r\n
// both of which means end of http response header
bool found_end = false;
@ -152,4 +152,3 @@ namespace libtorrent
}
}

View File

@ -361,12 +361,9 @@ namespace libtorrent
std::list<address> ip_list;
if (m_tracker_connection)
{
error_code ignore;
std::vector<tcp::endpoint> const& epts = m_tracker_connection->endpoints();
for (std::vector<tcp::endpoint>::const_iterator i = epts.begin()
, end(epts.end()); i != end; ++i)
for (auto const& endp : m_tracker_connection->endpoints())
{
ip_list.push_back(i->address());
ip_list.push_back(endp.address());
}
}
@ -388,7 +385,7 @@ namespace libtorrent
bdecode_node i = info.dict_find_string("peer id");
if (i && i.string_length() == 20)
{
std::copy(i.string_ptr(), i.string_ptr()+20, ret.pid.begin());
std::copy(i.string_ptr(), i.string_ptr() + 20, ret.pid.begin());
}
else
{
@ -596,4 +593,3 @@ namespace libtorrent
return resp;
}
}

View File

@ -65,8 +65,6 @@ using namespace std::placeholders;
namespace libtorrent { namespace dht
{
void incoming_error(entry& e, char const* msg);
namespace {
// generate a new write token key every 5 minutes
@ -325,7 +323,7 @@ namespace libtorrent { namespace dht
void dht_tracker::get_peers(sha1_hash const& ih
, std::function<void(std::vector<tcp::endpoint> const&)> f)
{
std::function<void(std::vector<std::pair<node_entry, std::string> > const&)> empty;
std::function<void(std::vector<std::pair<node_entry, std::string>> const&)> empty;
m_dht.get_peers(ih, f, empty, false);
#if TORRENT_USE_IPV6
m_dht6.get_peers(ih, f, empty, false);
@ -424,8 +422,7 @@ namespace libtorrent { namespace dht
void dht_tracker::get_item(sha1_hash const& target
, std::function<void(item const&)> cb)
{
std::shared_ptr<get_immutable_item_ctx>
ctx = std::make_shared<get_immutable_item_ctx>((TORRENT_USE_IPV6) ? 2 : 1);
auto ctx = std::make_shared<get_immutable_item_ctx>((TORRENT_USE_IPV6) ? 2 : 1);
m_dht.get_item(target, std::bind(&get_immutable_item_callback, _1, ctx, cb));
#if TORRENT_USE_IPV6
m_dht6.get_item(target, std::bind(&get_immutable_item_callback, _1, ctx, cb));
@ -438,8 +435,7 @@ namespace libtorrent { namespace dht
, std::function<void(item const&, bool)> cb
, std::string salt)
{
std::shared_ptr<get_mutable_item_ctx>
ctx = std::make_shared<get_mutable_item_ctx>((TORRENT_USE_IPV6) ? 2 : 1);
auto ctx = std::make_shared<get_mutable_item_ctx>((TORRENT_USE_IPV6) ? 2 : 1);
m_dht.get_item(key, salt, std::bind(&get_mutable_item_callback, _1, _2, ctx, cb));
#if TORRENT_USE_IPV6
m_dht6.get_item(key, salt, std::bind(&get_mutable_item_callback, _1, _2, ctx, cb));
@ -453,8 +449,7 @@ namespace libtorrent { namespace dht
bencode(std::back_inserter(flat_data), data);
sha1_hash const target = item_target_id(flat_data);
std::shared_ptr<put_item_ctx>
ctx = std::make_shared<put_item_ctx>((TORRENT_USE_IPV6) ? 2 : 1);
auto ctx = std::make_shared<put_item_ctx>((TORRENT_USE_IPV6) ? 2 : 1);
m_dht.put_item(target, data, std::bind(&put_immutable_item_callback
, _1, ctx, cb));
#if TORRENT_USE_IPV6
@ -467,9 +462,7 @@ namespace libtorrent { namespace dht
, std::function<void(item const&, int)> cb
, std::function<void(item&)> data_cb, std::string salt)
{
std::shared_ptr<put_item_ctx>
ctx = std::make_shared<put_item_ctx>((TORRENT_USE_IPV6) ? 2 : 1);
auto ctx = std::make_shared<put_item_ctx>((TORRENT_USE_IPV6) ? 2 : 1);
m_dht.put_item(key, salt, std::bind(&put_mutable_item_callback
, _1, _2, ctx, cb), data_cb);
#if TORRENT_USE_IPV6
@ -510,10 +503,10 @@ namespace libtorrent { namespace dht
}
}
bool dht_tracker::incoming_packet( udp::endpoint const& ep
bool dht_tracker::incoming_packet(udp::endpoint const& ep
, char const* buf, int size)
{
if (size <= 20 || *buf != 'd' || buf[size-1] != 'e') return false;
if (size <= 20 || *buf != 'd' || buf[size - 1] != 'e') return false;
m_counters.inc_stats_counter(counters::dht_bytes_in, size);
// account for IP and UDP overhead
@ -530,8 +523,7 @@ namespace libtorrent { namespace dht
static std::uint8_t const class_a[] = { 3, 6, 7, 9, 11, 19, 21, 22, 25
, 26, 28, 29, 30, 33, 34, 48, 51, 56 };
int num = sizeof(class_a)/sizeof(class_a[0]);
if (std::find(class_a, class_a + num, b[0]) != class_a + num)
if (std::find(std::begin(class_a), std::end(class_a), b[0]) != std::end(class_a))
{
m_counters.inc_stats_counter(counters::dht_messages_in_dropped);
return true;
@ -600,12 +592,11 @@ namespace libtorrent { namespace dht
dht.m_table.for_each_node(&add_node_fun, &add_node_fun, &nodes);
bucket_t cache;
dht.replacement_cache(cache);
for (bucket_t::iterator i(cache.begin())
, end(cache.end()); i != end; ++i)
for (auto const& b : cache)
{
std::string node;
std::back_insert_iterator<std::string> out(node);
write_endpoint(i->ep(), out);
write_endpoint(b.ep(), out);
nodes.list().push_back(entry(node));
}
if (!nodes.list().empty())
@ -626,7 +617,7 @@ namespace libtorrent { namespace dht
return ret;
}
void dht_tracker::add_node(udp::endpoint node)
void dht_tracker::add_node(udp::endpoint const& node)
{
m_dht.add_node(node);
#if TORRENT_USE_IPV6
@ -676,8 +667,7 @@ namespace libtorrent { namespace dht
m_send_quota -= int(m_send_buf.size());
error_code ec;
m_send_fun(addr, span<char const>(&m_send_buf[0]
, int(m_send_buf.size())), ec, 0);
m_send_fun(addr, m_send_buf, ec, 0);
if (ec)
{
m_counters.inc_stats_counter(counters::dht_messages_out_dropped);

View File

@ -95,7 +95,7 @@ node_id calculate_node_id(node_id const& nid, dht_observer* observer, udp protoc
node::node(udp proto, udp_socket_interface* sock
, dht_settings const& settings, node_id nid
, dht_observer* observer
, struct counters& cnt
, counters& cnt
, std::map<std::string, node*> const& nodes
, dht_storage_interface& storage)
: m_settings(settings)
@ -146,7 +146,7 @@ bool node::verify_token(string_view token, sha1_hash const& info_hash
if (token.length() != 4)
{
#ifndef TORRENT_DISABLE_LOGGING
if (m_observer)
if (m_observer != nullptr)
{
m_observer->log(dht_logger::node, "token of incorrect length: %d"
, int(token.length()));
@ -221,7 +221,7 @@ void node::bootstrap(std::vector<udp::endpoint> const& nodes
r->trim_seed_nodes();
#ifndef TORRENT_DISABLE_LOGGING
if (m_observer)
if (m_observer != nullptr)
m_observer->log(dht_logger::node, "bootstrapping with %d nodes", count);
#endif
r->start();
@ -274,8 +274,8 @@ void node::incoming(msg const& m)
{
// this node claims we use the wrong node-ID!
address_v6::bytes_type b;
memcpy(&b[0], ext_ip.string_ptr(), 16);
if (m_observer)
std::memcpy(&b[0], ext_ip.string_ptr(), 16);
if (m_observer != nullptr)
m_observer->set_external_address(address_v6(b)
, m.addr.address());
} else
@ -283,8 +283,8 @@ void node::incoming(msg const& m)
if (ext_ip && ext_ip.string_length() >= 4)
{
address_v4::bytes_type b;
memcpy(&b[0], ext_ip.string_ptr(), 4);
if (m_observer)
std::memcpy(&b[0], ext_ip.string_ptr(), 4);
if (m_observer != nullptr)
m_observer->set_external_address(address_v4(b)
, m.addr.address());
}
@ -320,13 +320,12 @@ void node::incoming(msg const& m)
case 'e':
{
#ifndef TORRENT_DISABLE_LOGGING
if (m_observer)
if (m_observer != nullptr && m_observer->should_log(dht_logger::node))
{
bdecode_node err = m.message.dict_find_list("e");
if (err && err.list_size() >= 2
&& err.list_at(0).type() == bdecode_node::int_t
&& err.list_at(1).type() == bdecode_node::string_t
&& m_observer)
&& err.list_at(1).type() == bdecode_node::string_t)
{
m_observer->log(dht_logger::node, "INCOMING ERROR: (%" PRId64 ") %s"
, err.list_int_value_at(0)
@ -351,7 +350,8 @@ namespace
, node& node, int listen_port, sha1_hash const& ih, int flags)
{
#ifndef TORRENT_DISABLE_LOGGING
if (node.observer())
if (node.observer() != nullptr
&& node.observer()->should_log(dht_logger::node))
{
char hex_ih[41];
aux::to_hex(ih, hex_ih);
@ -366,7 +366,8 @@ namespace
for (auto const& p : v)
{
#ifndef TORRENT_DISABLE_LOGGING
if (node.observer())
if (node.observer() != nullptr
&& node.observer()->should_log(dht_logger::node))
{
node.observer()->log(dht_logger::node, "announce-distance: %d"
, (160 - distance_exp(ih, p.first.id)));
@ -397,7 +398,7 @@ namespace
void node::add_router_node(udp::endpoint router)
{
#ifndef TORRENT_DISABLE_LOGGING
if (m_observer)
if (m_observer != nullptr && m_observer->should_log(dht_logger::node))
{
m_observer->log(dht_logger::node, "adding router node: %s"
, print_endpoint(router).c_str());
@ -406,7 +407,7 @@ void node::add_router_node(udp::endpoint router)
m_table.add_router_node(router);
}
void node::add_node(udp::endpoint node)
void node::add_node(udp::endpoint const& node)
{
if (!native_address(node)) return;
// ping the node, and if we get a reply, it
@ -439,7 +440,7 @@ void node::announce(sha1_hash const& info_hash, int listen_port, int flags
, std::function<void(std::vector<tcp::endpoint> const&)> f)
{
#ifndef TORRENT_DISABLE_LOGGING
if (m_observer)
if (m_observer != nullptr && m_observer->should_log(dht_logger::node))
{
char hex_ih[41];
aux::to_hex(info_hash, hex_ih);
@ -471,7 +472,7 @@ void node::get_item(sha1_hash const& target
, std::function<void(item const&)> f)
{
#ifndef TORRENT_DISABLE_LOGGING
if (m_observer)
if (m_observer != nullptr && m_observer->should_log(dht_logger::node))
{
char hex_target[41];
aux::to_hex(target, hex_target);
@ -489,7 +490,7 @@ void node::get_item(public_key const& pk, std::string const& salt
, std::function<void(item const&, bool)> f)
{
#ifndef TORRENT_DISABLE_LOGGING
if (m_observer)
if (m_observer != nullptr && m_observer->should_log(dht_logger::node))
{
char hex_key[65];
aux::to_hex(pk.bytes, hex_key);
@ -528,7 +529,7 @@ void put_data_cb(item i, bool auth
void node::put_item(sha1_hash const& target, entry const& data, std::function<void(int)> f)
{
#ifndef TORRENT_DISABLE_LOGGING
if (m_observer)
if (m_observer != nullptr && m_observer->should_log(dht_logger::node))
{
char hex_target[41];
aux::to_hex(target, hex_target);
@ -551,14 +552,14 @@ void node::put_item(public_key const& pk, std::string const& salt
, std::function<void(item const&, int)> f
, std::function<void(item&)> data_cb)
{
#ifndef TORRENT_DISABLE_LOGGING
if (m_observer)
#ifndef TORRENT_DISABLE_LOGGING
if (m_observer != nullptr && m_observer->should_log(dht_logger::node))
{
char hex_key[65];
aux::to_hex(pk.bytes, hex_key);
m_observer->log(dht_logger::node, "starting get for [ key: %s ]", hex_key);
}
#endif
#endif
auto put_ta = std::make_shared<dht::put_data>(*this, f);
@ -630,12 +631,12 @@ void node::tick()
// expanding the routing table buckets closer to us.
// if m_table.depth() < 4, means routing_table doesn't
// have enough nodes.
time_point now = aux::time_now();
time_point const now = aux::time_now();
if (m_last_self_refresh + minutes(10) < now && m_table.depth() < 4)
{
node_id target = m_id;
make_id_secret(target);
auto r = std::make_shared<dht::bootstrap>(*this, target, std::bind(&nop));
auto const r = std::make_shared<dht::bootstrap>(*this, target, std::bind(&nop));
r->start();
m_last_self_refresh = now;
return;
@ -648,12 +649,12 @@ void node::tick()
TORRENT_ASSERT(m_id != ne->id);
if (ne->id == m_id) return;
int bucket = 159 - distance_exp(m_id, ne->id);
int const bucket = 159 - distance_exp(m_id, ne->id);
TORRENT_ASSERT(bucket < 160);
send_single_refresh(ne->ep(), bucket, ne->id);
}
void node::send_single_refresh(udp::endpoint const& ep, int bucket
void node::send_single_refresh(udp::endpoint const& ep, int const bucket
, node_id const& id)
{
TORRENT_ASSERT(id != m_id);
@ -668,9 +669,7 @@ void node::send_single_refresh(udp::endpoint const& ep, int bucket
target |= m_id & mask;
// create a dummy traversal_algorithm
// this is unfortunately necessary for the observer
// to free itself from the pool when it's being released
auto algo = std::make_shared<traversal_algorithm>(*this, node_id());
auto const algo = std::make_shared<traversal_algorithm>(*this, node_id());
auto o = m_rpc.allocate_observer<ping_observer>(algo, ep, id);
if (!o) return;
#if TORRENT_USE_ASSERTS
@ -717,12 +716,11 @@ void node::status(std::vector<dht_routing_bucket>& table
m_table.status(table);
for (std::set<traversal_algorithm*>::iterator i = m_running_requests.begin()
, end(m_running_requests.end()); i != end; ++i)
for (auto const& r : m_running_requests)
{
requests.push_back(dht_lookup());
dht_lookup& lookup = requests.back();
(*i)->status(lookup);
r->status(lookup);
}
}
@ -760,14 +758,13 @@ void node::lookup_peers(sha1_hash const& info_hash, entry& reply
m_storage.get_peers(info_hash, noseed, scrape, reply);
}
void TORRENT_EXTRA_EXPORT write_nodes_entry(entry& n, nodes_t const& nodes)
void write_nodes_entry(entry& r, nodes_t const& nodes)
{
std::back_insert_iterator<std::string> out(n.string());
for (nodes_t::const_iterator i = nodes.begin()
, end(nodes.end()); i != end; ++i)
std::back_insert_iterator<std::string> out(r.string());
for (auto const& n : nodes)
{
std::copy(i->id.begin(), i->id.end(), out);
write_endpoint(udp::endpoint(i->addr(), i->port()), out);
std::copy(n.id.begin(), n.id.end(), out);
write_endpoint(udp::endpoint(n.addr(), n.port()), out);
}
}

View File

@ -3387,7 +3387,7 @@ namespace aux {
#ifndef TORRENT_DISABLE_DHT
void session_impl::add_dht_node(udp::endpoint n)
void session_impl::add_dht_node(udp::endpoint const& n)
{
TORRENT_ASSERT(is_single_thread());
@ -5607,10 +5607,9 @@ namespace aux {
return;
}
for (std::vector<address>::const_iterator i = addresses.begin()
, end(addresses.end()); i != end; ++i)
for (auto const& addr : addresses)
{
udp::endpoint ep(*i, port);
udp::endpoint ep(addr, port);
add_dht_node(ep);
}
}
@ -5641,11 +5640,10 @@ namespace aux {
}
for (std::vector<address>::const_iterator i = addresses.begin()
, end(addresses.end()); i != end; ++i)
for (auto const& addr : addresses)
{
// router nodes should be added before the DHT is started (and bootstrapped)
udp::endpoint ep(*i, port);
udp::endpoint ep(addr, port);
if (m_dht) m_dht->add_router_node(ep);
m_dht_router_nodes.push_back(ep);
}
@ -6259,10 +6257,9 @@ namespace aux {
{
// the number of torrents that are above average
int num_above = 0;
for (torrent_map::iterator i = m_torrents.begin()
, end(m_torrents.end()); i != end; ++i)
for (auto const& t : m_torrents)
{
int num = i->second->num_peers();
int const num = t.second->num_peers();
if (num <= last_average) continue;
if (num > average) ++num_above;
if (num < average) extra += average - num;
@ -6493,11 +6490,10 @@ namespace aux {
if (!m_natpmp) return;
m_natpmp->close();
for (std::list<listen_socket_t>::iterator i = m_listen_sockets.begin()
, end(m_listen_sockets.end()); i != end; ++i)
for (auto& s : m_listen_sockets)
{
i->tcp_port_mapping[0] = -1;
i->udp_port_mapping[0] = -1;
s.tcp_port_mapping[0] = -1;
s.udp_port_mapping[0] = -1;
}
m_natpmp.reset();
@ -6508,11 +6504,10 @@ namespace aux {
if (!m_upnp) return;
m_upnp->close();
for (std::list<listen_socket_t>::iterator i = m_listen_sockets.begin()
, end(m_listen_sockets.end()); i != end; ++i)
for (auto& s : m_listen_sockets)
{
i->tcp_port_mapping[1] = -1;
i->udp_port_mapping[1] = -1;
s.tcp_port_mapping[1] = -1;
s.udp_port_mapping[1] = -1;
}
m_upnp.reset();
}
@ -6630,10 +6625,9 @@ namespace aux {
if (m_alerts.should_post<external_ip_alert>())
m_alerts.emplace_alert<external_ip_alert>(ip);
for (torrent_map::iterator i = m_torrents.begin()
, end(m_torrents.end()); i != end; ++i)
for (auto const& t : m_torrents)
{
i->second->new_external_ip();
t.second->new_external_ip();
}
// since we have a new external IP now, we need to