refactor to use move with dht_state (#1163)

refactor to use move with dht_state
This commit is contained in:
Alden Torres 2016-09-29 08:25:51 -04:00 committed by Arvid Norberg
parent af92e91a90
commit 331a8d53c0
6 changed files with 40 additions and 32 deletions

View File

@ -331,11 +331,10 @@ namespace libtorrent
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; }
void set_dht_state(dht::dht_state const& state);
void set_dht_state(dht::dht_state state);
void set_dht_storage(dht::dht_storage_constructor_type sc);
void start_dht();
void stop_dht();
void start_dht(dht::dht_state const& startup_state);
bool has_dht() const override;
// this is called for torrents when they are started

View File

@ -57,8 +57,6 @@ namespace dht
// .. _BEP32: http://bittorrent.org/beps/bep_0032.html
struct TORRENT_EXPORT dht_state
{
dht_state() = default;
// the id of the IPv4 node
node_id nid;
// the id of the IPv6 node
@ -68,6 +66,8 @@ namespace dht
std::vector<udp::endpoint> nodes;
// the bootstrap nodes saved from the IPv6 buckets node
std::vector<udp::endpoint> nodes6;
void clear();
};
TORRENT_EXTRA_EXPORT dht_state read_dht_state(bdecode_node const& e);

View File

@ -68,11 +68,10 @@ namespace libtorrent { namespace dht
, dht_settings const& settings
, counters& cnt
, dht_storage_interface& storage
, dht_state const& state);
, dht_state state);
virtual ~dht_tracker();
void start(dht_state const& bootstrap
, find_data::nodes_callback const& f);
void start(find_data::nodes_callback const& f);
void stop();
// tell the node to recalculate its node id based on the current
@ -147,6 +146,7 @@ namespace libtorrent { namespace dht
counters& m_counters;
dht_storage_interface& m_storage;
dht_state m_state; // to be used only once
node m_dht;
#if TORRENT_USE_IPV6
node m_dht6;

View File

@ -63,6 +63,17 @@ namespace
}
} // anonymous namespace
void dht_state::clear()
{
nid.clear();
nid6.clear();
nodes.clear();
nodes.shrink_to_fit();
nodes6.clear();
nodes6.shrink_to_fit();
}
dht_state read_dht_state(bdecode_node const& e)
{
dht_state ret;

View File

@ -79,13 +79,14 @@ namespace libtorrent { namespace dht
, dht_settings const& settings
, counters& cnt
, dht_storage_interface& storage
, dht_state const& state)
, dht_state state)
: m_counters(cnt)
, m_storage(storage)
, m_dht(udp::v4(), this, settings, state.nid
, m_state(std::move(state))
, m_dht(udp::v4(), this, settings, m_state.nid
, observer, cnt, m_nodes, storage)
#if TORRENT_USE_IPV6
, m_dht6(udp::v6(), this, settings, state.nid6
, m_dht6(udp::v6(), this, settings, m_state.nid6
, observer, cnt, m_nodes, storage)
#endif
, m_send_fun(send_fun)
@ -136,8 +137,7 @@ namespace libtorrent { namespace dht
update_storage_node_ids();
}
void dht_tracker::start(dht_state const& bootstrap
, find_data::nodes_callback const& f)
void dht_tracker::start(find_data::nodes_callback const& f)
{
error_code ec;
refresh_key(ec);
@ -154,10 +154,12 @@ namespace libtorrent { namespace dht
m_refresh_timer.expires_from_now(seconds(5), ec);
m_refresh_timer.async_wait(std::bind(&dht_tracker::refresh_timeout, self(), _1));
m_dht.bootstrap(bootstrap.nodes, f);
m_dht.bootstrap(m_state.nodes, f);
#if TORRENT_USE_IPV6
m_dht6.bootstrap(bootstrap.nodes6, f);
m_dht6.bootstrap(m_state.nodes6, f);
#endif
m_state.clear();
}
void dht_tracker::stop()
@ -440,7 +442,6 @@ namespace libtorrent { namespace dht
else
#endif
m_dht.direct_request(ep, e, f);
}
void dht_tracker::incoming_error(error_code const& ec, udp::endpoint const& ep)

View File

@ -80,6 +80,7 @@ const rlim_t rlim_infinity = RLIM_INFINITY;
#ifndef TORRENT_DISABLE_DHT
#include "libtorrent/kademlia/dht_tracker.hpp"
#include "libtorrent/kademlia/types.hpp"
#include "libtorrent/kademlia/node_entry.hpp"
#endif
#include "libtorrent/enum_net.hpp"
#include "libtorrent/config.hpp"
@ -5601,18 +5602,6 @@ namespace aux {
#ifndef TORRENT_DISABLE_DHT
void session_impl::start_dht()
{ start_dht(m_dht_state); }
namespace {
void on_bootstrap(alert_manager& alerts)
{
if (alerts.should_post<dht_bootstrap_alert>())
alerts.emplace_alert<dht_bootstrap_alert>();
}
}
void session_impl::start_dht(dht::dht_state const& startup_state)
{
INVARIANT_CHECK;
@ -5629,7 +5618,7 @@ namespace aux {
, m_dht_settings
, m_stats_counters
, *m_dht_storage
, startup_state);
, std::move(m_dht_state));
for (auto const& n : m_dht_router_nodes)
{
@ -5641,8 +5630,15 @@ namespace aux {
m_dht->add_node(n);
}
m_dht_nodes.clear();
m_dht_nodes.shrink_to_fit();
m_dht->start(startup_state, std::bind(&on_bootstrap, std::ref(m_alerts)));
auto cb = [this](
std::vector<std::pair<dht::node_entry, std::string>> const&)
{
if (m_alerts.should_post<dht_bootstrap_alert>())
m_alerts.emplace_alert<dht_bootstrap_alert>();
};
m_dht->start(cb);
}
void session_impl::stop_dht()
@ -5661,9 +5657,9 @@ namespace aux {
m_dht_settings = settings;
}
void session_impl::set_dht_state(dht::dht_state const& state)
void session_impl::set_dht_state(dht::dht_state state)
{
m_dht_state = state;
m_dht_state = std::move(state);
}
void session_impl::set_dht_storage(dht::dht_storage_constructor_type sc)
@ -5687,7 +5683,8 @@ namespace aux {
error_code ec;
if (tmp.empty() || bdecode(&tmp[0], &tmp[0] + tmp.size(), e, ec) != 0)
return;
start_dht(dht::read_dht_state(e));
m_dht_state = dht::read_dht_state(e);
start_dht();
}
#endif