forked from premiere/premiere-libtorrent
some cleanup to dht observers (don't expose internals)
This commit is contained in:
parent
1b93f1e4b8
commit
0052fa6c19
|
@ -48,6 +48,7 @@ POSSIBILITY OF SUCH DAMAGE.
|
||||||
namespace libtorrent {
|
namespace libtorrent {
|
||||||
namespace dht {
|
namespace dht {
|
||||||
|
|
||||||
|
struct dht_observer;
|
||||||
struct observer;
|
struct observer;
|
||||||
struct msg;
|
struct msg;
|
||||||
struct traversal_algorithm;
|
struct traversal_algorithm;
|
||||||
|
@ -96,13 +97,17 @@ struct observer : boost::noncopyable
|
||||||
// this is called when no reply has been received within
|
// this is called when no reply has been received within
|
||||||
// some timeout
|
// some timeout
|
||||||
void timeout();
|
void timeout();
|
||||||
|
|
||||||
// if this is called the destructor should
|
// if this is called the destructor should
|
||||||
// not invoke any new messages, and should
|
// not invoke any new messages, and should
|
||||||
// only clean up. It means the rpc-manager
|
// only clean up. It means the rpc-manager
|
||||||
// is being destructed
|
// is being destructed
|
||||||
void abort();
|
void abort();
|
||||||
|
|
||||||
|
dht_observer* get_observer() const;
|
||||||
|
|
||||||
|
traversal_algorithm* algorithm() const { return m_algorithm.get(); }
|
||||||
|
|
||||||
time_point sent() const { return m_sent; }
|
time_point sent() const { return m_sent; }
|
||||||
|
|
||||||
void set_target(udp::endpoint const& ep);
|
void set_target(udp::endpoint const& ep);
|
||||||
|
@ -129,13 +134,12 @@ struct observer : boost::noncopyable
|
||||||
flag_done = 128
|
flag_done = 128
|
||||||
};
|
};
|
||||||
|
|
||||||
#ifdef TORRENT_DISABLE_LOGGING
|
|
||||||
// TODO: 3 make this private and unconditional
|
|
||||||
protected:
|
protected:
|
||||||
#endif
|
|
||||||
|
|
||||||
void done();
|
void done();
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
time_point m_sent;
|
time_point m_sent;
|
||||||
|
|
||||||
const boost::intrusive_ptr<traversal_algorithm> m_algorithm;
|
const boost::intrusive_ptr<traversal_algorithm> m_algorithm;
|
||||||
|
|
|
@ -53,8 +53,8 @@ void find_data_observer::reply(msg const& m)
|
||||||
if (!r)
|
if (!r)
|
||||||
{
|
{
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] missing response dict"
|
get_observer()->log(dht_logger::traversal, "[%p] missing response dict"
|
||||||
, m_algorithm.get());
|
, algorithm());
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -63,15 +63,15 @@ void find_data_observer::reply(msg const& m)
|
||||||
if (!id || id.string_length() != 20)
|
if (!id || id.string_length() != 20)
|
||||||
{
|
{
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] invalid id in response"
|
get_observer()->log(dht_logger::traversal, "[%p] invalid id in response"
|
||||||
, m_algorithm.get());
|
, algorithm());
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
bdecode_node token = r.dict_find_string("token");
|
bdecode_node token = r.dict_find_string("token");
|
||||||
if (token)
|
if (token)
|
||||||
{
|
{
|
||||||
static_cast<find_data*>(m_algorithm.get())->got_write_token(
|
static_cast<find_data*>(algorithm())->got_write_token(
|
||||||
node_id(id.string_ptr()), token.string_value());
|
node_id(id.string_ptr()), token.string_value());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -258,8 +258,8 @@ void get_item_observer::reply(msg const& m)
|
||||||
if (!r)
|
if (!r)
|
||||||
{
|
{
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] missing response dict"
|
get_observer()->log(dht_logger::traversal, "[%p] missing response dict"
|
||||||
, m_algorithm.get());
|
, algorithm());
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -281,7 +281,7 @@ void get_item_observer::reply(msg const& m)
|
||||||
bdecode_node v = r.dict_find("v");
|
bdecode_node v = r.dict_find("v");
|
||||||
if (v)
|
if (v)
|
||||||
{
|
{
|
||||||
static_cast<get_item*>(m_algorithm.get())->got_data(v, pk, seq, sig);
|
static_cast<get_item*>(algorithm())->got_data(v, pk, seq, sig);
|
||||||
}
|
}
|
||||||
|
|
||||||
find_data_observer::reply(m);
|
find_data_observer::reply(m);
|
||||||
|
|
|
@ -51,8 +51,8 @@ void get_peers_observer::reply(msg const& m)
|
||||||
if (!r)
|
if (!r)
|
||||||
{
|
{
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] missing response dict"
|
get_observer()->log(dht_logger::traversal, "[%p] missing response dict"
|
||||||
, m_algorithm.get());
|
, algorithm());
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -72,14 +72,14 @@ void get_peers_observer::reply(msg const& m)
|
||||||
bdecode_node id = r.dict_find_string("id");
|
bdecode_node id = r.dict_find_string("id");
|
||||||
if (id && id.string_length() == 20)
|
if (id && id.string_length() == 20)
|
||||||
{
|
{
|
||||||
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] PEERS "
|
get_observer()->log(dht_logger::traversal, "[%p] PEERS "
|
||||||
"invoke-count: %d branch-factor: %d addr: %s id: %s distance: %d p: %d"
|
"invoke-count: %d branch-factor: %d addr: %s id: %s distance: %d p: %d"
|
||||||
, m_algorithm.get()
|
, algorithm()
|
||||||
, m_algorithm->invoke_count()
|
, algorithm()->invoke_count()
|
||||||
, m_algorithm->branch_factor()
|
, algorithm()->branch_factor()
|
||||||
, print_endpoint(m.addr).c_str()
|
, print_endpoint(m.addr).c_str()
|
||||||
, to_hex(id.string_value()).c_str()
|
, to_hex(id.string_value()).c_str()
|
||||||
, distance_exp(m_algorithm->target(), node_id(id.string_ptr()))
|
, distance_exp(algorithm()->target(), node_id(id.string_ptr()))
|
||||||
, int((end - peers) / 6));
|
, int((end - peers) / 6));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -94,19 +94,19 @@ void get_peers_observer::reply(msg const& m)
|
||||||
bdecode_node id = r.dict_find_string("id");
|
bdecode_node id = r.dict_find_string("id");
|
||||||
if (id && id.string_length() == 20)
|
if (id && id.string_length() == 20)
|
||||||
{
|
{
|
||||||
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] PEERS "
|
get_observer()->log(dht_logger::traversal, "[%p] PEERS "
|
||||||
"invoke-count: %d branch-factor: %d addr: %s id: %s distance: %d p: %d"
|
"invoke-count: %d branch-factor: %d addr: %s id: %s distance: %d p: %d"
|
||||||
, m_algorithm.get()
|
, algorithm()
|
||||||
, m_algorithm->invoke_count()
|
, algorithm()->invoke_count()
|
||||||
, m_algorithm->branch_factor()
|
, algorithm()->branch_factor()
|
||||||
, print_endpoint(m.addr).c_str()
|
, print_endpoint(m.addr).c_str()
|
||||||
, to_hex(id.string_value()).c_str()
|
, to_hex(id.string_value()).c_str()
|
||||||
, distance_exp(m_algorithm->target(), node_id(id.string_ptr()))
|
, distance_exp(algorithm()->target(), node_id(id.string_ptr()))
|
||||||
, int(n.list_size()));
|
, int(n.list_size()));
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
static_cast<get_peers*>(m_algorithm.get())->got_peers(peer_list);
|
static_cast<get_peers*>(algorithm())->got_peers(peer_list);
|
||||||
}
|
}
|
||||||
|
|
||||||
find_data_observer::reply(m);
|
find_data_observer::reply(m);
|
||||||
|
@ -308,8 +308,8 @@ void obfuscated_get_peers_observer::reply(msg const& m)
|
||||||
if (!r)
|
if (!r)
|
||||||
{
|
{
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] missing response dict"
|
get_observer()->log(dht_logger::traversal, "[%p] missing response dict"
|
||||||
, m_algorithm.get());
|
, algorithm());
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -318,8 +318,8 @@ void obfuscated_get_peers_observer::reply(msg const& m)
|
||||||
if (!id || id.string_length() != 20)
|
if (!id || id.string_length() != 20)
|
||||||
{
|
{
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] invalid id in response"
|
get_observer()->log(dht_logger::traversal, "[%p] invalid id in response"
|
||||||
, m_algorithm.get());
|
, algorithm());
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
|
@ -456,11 +456,11 @@ struct ping_observer : observer
|
||||||
if (!r)
|
if (!r)
|
||||||
{
|
{
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
if (m_algorithm->get_node().observer())
|
if (get_observer())
|
||||||
{
|
{
|
||||||
m_algorithm->get_node().observer()->log(dht_logger::node
|
get_observer()->log(dht_logger::node
|
||||||
, "[%p] missing response dict"
|
, "[%p] missing response dict"
|
||||||
, m_algorithm.get());
|
, algorithm());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
|
@ -478,7 +478,7 @@ struct ping_observer : observer
|
||||||
node_id id;
|
node_id id;
|
||||||
std::copy(nodes, nodes + 20, id.begin());
|
std::copy(nodes, nodes + 20, id.begin());
|
||||||
nodes += 20;
|
nodes += 20;
|
||||||
m_algorithm.get()->get_node().m_table.heard_about(id
|
algorithm()->get_node().m_table.heard_about(id
|
||||||
, detail::read_v4_endpoint<udp::endpoint>(nodes));
|
, detail::read_v4_endpoint<udp::endpoint>(nodes));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -79,12 +79,18 @@ void intrusive_ptr_release(observer const* o)
|
||||||
TORRENT_ASSERT(o->m_refs > 0);
|
TORRENT_ASSERT(o->m_refs > 0);
|
||||||
if (--o->m_refs == 0)
|
if (--o->m_refs == 0)
|
||||||
{
|
{
|
||||||
boost::intrusive_ptr<traversal_algorithm> ta = o->m_algorithm;
|
boost::intrusive_ptr<traversal_algorithm> ta = o->algorithm();
|
||||||
(const_cast<observer*>(o))->~observer();
|
(const_cast<observer*>(o))->~observer();
|
||||||
ta->free_observer(const_cast<observer*>(o));
|
ta->free_observer(const_cast<observer*>(o));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TODO: 3 move this into it's own .cpp file
|
||||||
|
dht_observer* observer::get_observer() const
|
||||||
|
{
|
||||||
|
return m_algorithm->get_node().observer();
|
||||||
|
}
|
||||||
|
|
||||||
void observer::set_target(udp::endpoint const& ep)
|
void observer::set_target(udp::endpoint const& ep)
|
||||||
{
|
{
|
||||||
m_sent = clock_type::now();
|
m_sent = clock_type::now();
|
||||||
|
@ -336,7 +342,7 @@ bool rpc_manager::incoming(msg const& m, node_id* id
|
||||||
|
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
m_log->log(dht_logger::rpc_manager, "[%p] reply with transaction id: %d from %s"
|
m_log->log(dht_logger::rpc_manager, "[%p] reply with transaction id: %d from %s"
|
||||||
, o->m_algorithm.get(), int(transaction_id.size())
|
, o->algorithm(), int(transaction_id.size())
|
||||||
, print_endpoint(m.addr).c_str());
|
, print_endpoint(m.addr).c_str());
|
||||||
#endif
|
#endif
|
||||||
o->reply(m);
|
o->reply(m);
|
||||||
|
@ -376,7 +382,7 @@ time_duration rpc_manager::tick()
|
||||||
{
|
{
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
m_log->log(dht_logger::rpc_manager, "[%p] timing out transaction id: %d from: %s"
|
m_log->log(dht_logger::rpc_manager, "[%p] timing out transaction id: %d from: %s"
|
||||||
, o->m_algorithm.get(), o->transaction_id()
|
, o->algorithm(), o->transaction_id()
|
||||||
, print_endpoint(o->target_ep()).c_str());
|
, print_endpoint(o->target_ep()).c_str());
|
||||||
#endif
|
#endif
|
||||||
m_transactions.erase(i++);
|
m_transactions.erase(i++);
|
||||||
|
@ -390,7 +396,7 @@ time_duration rpc_manager::tick()
|
||||||
{
|
{
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
m_log->log(dht_logger::rpc_manager, "[%p] short-timing out transaction id: %d from: %s"
|
m_log->log(dht_logger::rpc_manager, "[%p] short-timing out transaction id: %d from: %s"
|
||||||
, o->m_algorithm.get(), o->transaction_id()
|
, o->algorithm(), o->transaction_id()
|
||||||
, print_endpoint(o->target_ep()).c_str());
|
, print_endpoint(o->target_ep()).c_str());
|
||||||
#endif
|
#endif
|
||||||
++i;
|
++i;
|
||||||
|
@ -402,10 +408,10 @@ time_duration rpc_manager::tick()
|
||||||
ret = std::min(seconds(timeout) - diff, ret);
|
ret = std::min(seconds(timeout) - diff, ret);
|
||||||
++i;
|
++i;
|
||||||
}
|
}
|
||||||
|
|
||||||
std::for_each(timeouts.begin(), timeouts.end(), boost::bind(&observer::timeout, _1));
|
std::for_each(timeouts.begin(), timeouts.end(), boost::bind(&observer::timeout, _1));
|
||||||
std::for_each(short_timeouts.begin(), short_timeouts.end(), boost::bind(&observer::short_timeout, _1));
|
std::for_each(short_timeouts.begin(), short_timeouts.end(), boost::bind(&observer::short_timeout, _1));
|
||||||
|
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -431,13 +437,13 @@ bool rpc_manager::invoke(entry& e, udp::endpoint target_addr
|
||||||
int tid = (random() ^ (random() << 5)) & 0xffff;
|
int tid = (random() ^ (random() << 5)) & 0xffff;
|
||||||
io::write_uint16(tid, out);
|
io::write_uint16(tid, out);
|
||||||
e["t"] = transaction_id;
|
e["t"] = transaction_id;
|
||||||
|
|
||||||
o->set_target(target_addr);
|
o->set_target(target_addr);
|
||||||
o->set_transaction_id(tid);
|
o->set_transaction_id(tid);
|
||||||
|
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
m_log->log(dht_logger::rpc_manager, "[%p] invoking %s -> %s"
|
m_log->log(dht_logger::rpc_manager, "[%p] invoking %s -> %s"
|
||||||
, o->m_algorithm.get(), e["q"].string().c_str()
|
, o->algorithm(), e["q"].string().c_str()
|
||||||
, print_endpoint(target_addr).c_str());
|
, print_endpoint(target_addr).c_str());
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
|
@ -552,26 +552,26 @@ void traversal_observer::reply(msg const& m)
|
||||||
if (!r)
|
if (!r)
|
||||||
{
|
{
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
if (m_algorithm->get_node().observer())
|
if (get_observer())
|
||||||
{
|
{
|
||||||
m_algorithm->get_node().observer()->log(dht_logger::traversal
|
get_observer()->log(dht_logger::traversal
|
||||||
, "[%p] missing response dict"
|
, "[%p] missing response dict"
|
||||||
, m_algorithm.get());
|
, algorithm());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
if (m_algorithm->get_node().observer())
|
if (get_observer())
|
||||||
{
|
{
|
||||||
bdecode_node nid = r.dict_find_string("id");
|
bdecode_node nid = r.dict_find_string("id");
|
||||||
char hex_id[41];
|
char hex_id[41];
|
||||||
to_hex(nid.string_ptr(), 20, hex_id);
|
to_hex(nid.string_ptr(), 20, hex_id);
|
||||||
m_algorithm->get_node().observer()->log(dht_logger::traversal
|
get_observer()->log(dht_logger::traversal
|
||||||
, "[%p] RESPONSE id: %s invoke-count: %d addr: %s type: %s"
|
, "[%p] RESPONSE id: %s invoke-count: %d addr: %s type: %s"
|
||||||
, m_algorithm.get(), hex_id, m_algorithm->invoke_count()
|
, algorithm(), hex_id, algorithm()->invoke_count()
|
||||||
, print_endpoint(target_ep()).c_str(), m_algorithm->name());
|
, print_endpoint(target_ep()).c_str(), algorithm()->name());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
// look for nodes
|
// look for nodes
|
||||||
|
@ -586,7 +586,7 @@ void traversal_observer::reply(msg const& m)
|
||||||
node_id id;
|
node_id id;
|
||||||
std::copy(nodes, nodes + 20, id.begin());
|
std::copy(nodes, nodes + 20, id.begin());
|
||||||
nodes += 20;
|
nodes += 20;
|
||||||
m_algorithm->traverse(id, read_v4_endpoint<udp::endpoint>(nodes));
|
algorithm()->traverse(id, read_v4_endpoint<udp::endpoint>(nodes));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -594,10 +594,10 @@ void traversal_observer::reply(msg const& m)
|
||||||
if (!id || id.string_length() != 20)
|
if (!id || id.string_length() != 20)
|
||||||
{
|
{
|
||||||
#ifndef TORRENT_DISABLE_LOGGING
|
#ifndef TORRENT_DISABLE_LOGGING
|
||||||
if (m_algorithm->get_node().observer())
|
if (get_observer())
|
||||||
{
|
{
|
||||||
m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] invalid id in response"
|
get_observer()->log(dht_logger::traversal, "[%p] invalid id in response"
|
||||||
, m_algorithm.get());
|
, algorithm());
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
return;
|
return;
|
||||||
|
|
|
@ -2344,7 +2344,7 @@ retry:
|
||||||
}
|
}
|
||||||
|
|
||||||
// to test SSL connections, one can use this openssl command template:
|
// to test SSL connections, one can use this openssl command template:
|
||||||
//
|
//
|
||||||
// openssl s_client -cert <client-cert>.pem -key <client-private-key>.pem
|
// openssl s_client -cert <client-cert>.pem -key <client-private-key>.pem
|
||||||
// -CAfile <torrent-cert>.pem -debug -connect 127.0.0.1:4433 -tls1
|
// -CAfile <torrent-cert>.pem -debug -connect 127.0.0.1:4433 -tls1
|
||||||
// -servername <hex-encoded-info-hash>
|
// -servername <hex-encoded-info-hash>
|
||||||
|
@ -2851,7 +2851,7 @@ retry:
|
||||||
#endif
|
#endif
|
||||||
error_code ec;
|
error_code ec;
|
||||||
m_timer.expires_at(now + milliseconds(m_settings.get_int(settings_pack::tick_interval)), ec);
|
m_timer.expires_at(now + milliseconds(m_settings.get_int(settings_pack::tick_interval)), ec);
|
||||||
m_timer.async_wait(bind(&session_impl::on_tick, this, _1));
|
m_timer.async_wait(boost::bind(&session_impl::on_tick, this, _1));
|
||||||
|
|
||||||
m_download_rate.update_quotas(now - m_last_tick);
|
m_download_rate.update_quotas(now - m_last_tick);
|
||||||
m_upload_rate.update_quotas(now - m_last_tick);
|
m_upload_rate.update_quotas(now - m_last_tick);
|
||||||
|
|
Loading…
Reference in New Issue