refactor traversal_algorithm look_for_nodes (#1982)

This commit is contained in:
Pavel Pimenov 2017-05-29 19:20:52 +03:00 committed by Arvid Norberg
parent 8e600c2201
commit 98a0344196
7 changed files with 32 additions and 41 deletions

View File

@ -50,6 +50,7 @@ namespace libtorrent { struct dht_lookup; }
namespace libtorrent { namespace dht {
class node;
struct node_endpoint;
// this class may not be instantiated as a stack object
struct TORRENT_EXTRA_EXPORT traversal_algorithm : boost::noncopyable
@ -141,6 +142,9 @@ private:
#endif
};
void look_for_nodes(char const* nodes_key, udp const& protocol
, bdecode_node const& r, std::function<void(node_endpoint const&)> f);
struct traversal_observer : observer
{
traversal_observer(

View File

@ -514,7 +514,7 @@ namespace libtorrent { namespace dht {
int pos;
error_code err;
int ret = bdecode(buf.data(), buf.data() + buf_size, m_msg, err, &pos, 10, 500);
int const ret = bdecode(buf.data(), buf.data() + buf_size, m_msg, err, &pos, 10, 500);
if (ret != 0)
{
m_counters.inc_stats_counter(counters::dht_messages_in_dropped);
@ -538,7 +538,7 @@ namespace libtorrent { namespace dht {
m_log->log_packet(dht_logger::incoming_message, buf, ep);
#endif
libtorrent::dht::msg m(m_msg, ep);
libtorrent::dht::msg const m(m_msg, ep);
for (auto& n : m_nodes)
n.second.dht.incoming(m);
return true;

View File

@ -45,7 +45,7 @@ namespace libtorrent { namespace dht {
void find_data_observer::reply(msg const& m)
{
bdecode_node r = m.message.dict_find_dict("r");
bdecode_node const r = m.message.dict_find_dict("r");
if (!r)
{
#ifndef TORRENT_DISABLE_LOGGING

View File

@ -175,7 +175,7 @@ void get_item_observer::reply(msg const& m)
signature sig;
sequence_number seq{0};
bdecode_node r = m.message.dict_find_dict("r");
bdecode_node const r = m.message.dict_find_dict("r");
if (!r)
{
#ifndef TORRENT_DISABLE_LOGGING

View File

@ -44,7 +44,7 @@ namespace libtorrent { namespace dht {
void get_peers_observer::reply(msg const& m)
{
bdecode_node r = m.message.dict_find_dict("r");
bdecode_node const r = m.message.dict_find_dict("r");
if (!r)
{
#ifndef TORRENT_DISABLE_LOGGING

View File

@ -591,23 +591,8 @@ struct ping_observer : observer
#endif
return;
}
// look for nodes
udp const protocol = algorithm()->get_node().protocol();
int const protocol_size = int(detail::address_size(protocol));
char const* nodes_key = algorithm()->get_node().protocol_nodes_key();
bdecode_node const n = r.dict_find_string(nodes_key);
if (n)
{
char const* nodes = n.string_ptr();
char const* end = nodes + n.string_length();
while (end - nodes >= 20 + protocol_size + 2)
{
node_endpoint nep = read_node_endpoint(protocol, nodes);
algorithm()->get_node().m_table.heard_about(nep.id, nep.ep);
}
}
look_for_nodes(algorithm()->get_node().protocol_nodes_key(), algorithm()->get_node().protocol(), r,
[this](node_endpoint const& nep) { algorithm()->get_node().m_table.heard_about(nep.id, nep.ep); });
}
};

View File

@ -593,6 +593,22 @@ void traversal_algorithm::status(dht_lookup& l)
l.last_sent = last_sent;
}
void look_for_nodes(char const* nodes_key, udp const& protocol, bdecode_node const& r, std::function<void(const node_endpoint&)> f)
{
bdecode_node const n = r.dict_find_string(nodes_key);
if (n)
{
char const* nodes = n.string_ptr();
char const* end = nodes + n.string_length();
int const protocol_size = int(detail::address_size(protocol));
while (end - nodes >= 20 + protocol_size + 2)
{
f(read_node_endpoint(protocol, nodes));
}
}
}
void traversal_observer::reply(msg const& m)
{
bdecode_node const r = m.message.dict_find_dict("r");
@ -609,13 +625,14 @@ void traversal_observer::reply(msg const& m)
return;
}
bdecode_node const id = r.dict_find_string("id");
#ifndef TORRENT_DISABLE_LOGGING
dht_observer* logger = get_observer();
if (logger != nullptr && logger->should_log(dht_logger::traversal))
{
bdecode_node const nid = r.dict_find_string("id");
char hex_id[41];
aux::to_hex({nid.string_ptr(), 20}, hex_id);
aux::to_hex({id.string_ptr(), 20}, hex_id);
logger->log(dht_logger::traversal
, "[%u] RESPONSE id: %s invoke-count: %d addr: %s type: %s"
, algorithm()->id(), hex_id, algorithm()->invoke_count()
@ -623,24 +640,9 @@ void traversal_observer::reply(msg const& m)
}
#endif
// look for nodes
udp const protocol = algorithm()->get_node().protocol();
int const protocol_size = int(detail::address_size(protocol));
char const* nodes_key = algorithm()->get_node().protocol_nodes_key();
bdecode_node const n = r.dict_find_string(nodes_key);
if (n)
{
char const* nodes = n.string_ptr();
char const* end = nodes + n.string_length();
look_for_nodes(algorithm()->get_node().protocol_nodes_key(), algorithm()->get_node().protocol(), r,
[this](node_endpoint const& nep) { algorithm()->traverse(nep.id, nep.ep); });
while (end - nodes >= 20 + protocol_size + 2)
{
node_endpoint nep = read_node_endpoint(protocol, nodes);
algorithm()->traverse(nep.id, nep.ep);
}
}
bdecode_node const id = r.dict_find_string("id");
if (!id || id.string_length() != 20)
{
#ifndef TORRENT_DISABLE_LOGGING