From 0052fa6c19bf497db93892ef48bcfa6ab8e6e142 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Fri, 22 May 2015 02:42:26 +0000 Subject: [PATCH] some cleanup to dht observers (don't expose internals) --- include/libtorrent/kademlia/observer.hpp | 12 ++++++--- src/kademlia/find_data.cpp | 10 +++---- src/kademlia/get_item.cpp | 6 ++--- src/kademlia/get_peers.cpp | 34 ++++++++++++------------ src/kademlia/node.cpp | 8 +++--- src/kademlia/rpc_manager.cpp | 22 +++++++++------ src/kademlia/traversal_algorithm.cpp | 22 +++++++-------- src/session_impl.cpp | 4 +-- 8 files changed, 64 insertions(+), 54 deletions(-) diff --git a/include/libtorrent/kademlia/observer.hpp b/include/libtorrent/kademlia/observer.hpp index a426f88ba..ad2389079 100644 --- a/include/libtorrent/kademlia/observer.hpp +++ b/include/libtorrent/kademlia/observer.hpp @@ -48,6 +48,7 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { namespace dht { +struct dht_observer; struct observer; struct msg; struct traversal_algorithm; @@ -96,13 +97,17 @@ struct observer : boost::noncopyable // this is called when no reply has been received within // some timeout void timeout(); - + // if this is called the destructor should // not invoke any new messages, and should // only clean up. It means the rpc-manager // is being destructed void abort(); + dht_observer* get_observer() const; + + traversal_algorithm* algorithm() const { return m_algorithm.get(); } + time_point sent() const { return m_sent; } void set_target(udp::endpoint const& ep); @@ -129,13 +134,12 @@ struct observer : boost::noncopyable flag_done = 128 }; -#ifdef TORRENT_DISABLE_LOGGING - // TODO: 3 make this private and unconditional protected: -#endif void done(); +private: + time_point m_sent; const boost::intrusive_ptr m_algorithm; diff --git a/src/kademlia/find_data.cpp b/src/kademlia/find_data.cpp index def9184b2..b0fba5653 100644 --- a/src/kademlia/find_data.cpp +++ b/src/kademlia/find_data.cpp @@ -53,8 +53,8 @@ void find_data_observer::reply(msg const& m) if (!r) { #ifndef TORRENT_DISABLE_LOGGING - m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] missing response dict" - , m_algorithm.get()); + get_observer()->log(dht_logger::traversal, "[%p] missing response dict" + , algorithm()); #endif return; } @@ -63,15 +63,15 @@ void find_data_observer::reply(msg const& m) if (!id || id.string_length() != 20) { #ifndef TORRENT_DISABLE_LOGGING - m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] invalid id in response" - , m_algorithm.get()); + get_observer()->log(dht_logger::traversal, "[%p] invalid id in response" + , algorithm()); #endif return; } bdecode_node token = r.dict_find_string("token"); if (token) { - static_cast(m_algorithm.get())->got_write_token( + static_cast(algorithm())->got_write_token( node_id(id.string_ptr()), token.string_value()); } diff --git a/src/kademlia/get_item.cpp b/src/kademlia/get_item.cpp index 5c09d2625..1570026c2 100644 --- a/src/kademlia/get_item.cpp +++ b/src/kademlia/get_item.cpp @@ -258,8 +258,8 @@ void get_item_observer::reply(msg const& m) if (!r) { #ifndef TORRENT_DISABLE_LOGGING - m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] missing response dict" - , m_algorithm.get()); + get_observer()->log(dht_logger::traversal, "[%p] missing response dict" + , algorithm()); #endif return; } @@ -281,7 +281,7 @@ void get_item_observer::reply(msg const& m) bdecode_node v = r.dict_find("v"); if (v) { - static_cast(m_algorithm.get())->got_data(v, pk, seq, sig); + static_cast(algorithm())->got_data(v, pk, seq, sig); } find_data_observer::reply(m); diff --git a/src/kademlia/get_peers.cpp b/src/kademlia/get_peers.cpp index c8c638250..b0d578e75 100644 --- a/src/kademlia/get_peers.cpp +++ b/src/kademlia/get_peers.cpp @@ -51,8 +51,8 @@ void get_peers_observer::reply(msg const& m) if (!r) { #ifndef TORRENT_DISABLE_LOGGING - m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] missing response dict" - , m_algorithm.get()); + get_observer()->log(dht_logger::traversal, "[%p] missing response dict" + , algorithm()); #endif return; } @@ -72,14 +72,14 @@ void get_peers_observer::reply(msg const& m) bdecode_node id = r.dict_find_string("id"); 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" - , m_algorithm.get() - , m_algorithm->invoke_count() - , m_algorithm->branch_factor() + , algorithm() + , algorithm()->invoke_count() + , algorithm()->branch_factor() , print_endpoint(m.addr).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)); } #endif @@ -94,19 +94,19 @@ void get_peers_observer::reply(msg const& m) bdecode_node id = r.dict_find_string("id"); 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" - , m_algorithm.get() - , m_algorithm->invoke_count() - , m_algorithm->branch_factor() + , algorithm() + , algorithm()->invoke_count() + , algorithm()->branch_factor() , print_endpoint(m.addr).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())); } #endif } - static_cast(m_algorithm.get())->got_peers(peer_list); + static_cast(algorithm())->got_peers(peer_list); } find_data_observer::reply(m); @@ -308,8 +308,8 @@ void obfuscated_get_peers_observer::reply(msg const& m) if (!r) { #ifndef TORRENT_DISABLE_LOGGING - m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] missing response dict" - , m_algorithm.get()); + get_observer()->log(dht_logger::traversal, "[%p] missing response dict" + , algorithm()); #endif return; } @@ -318,8 +318,8 @@ void obfuscated_get_peers_observer::reply(msg const& m) if (!id || id.string_length() != 20) { #ifndef TORRENT_DISABLE_LOGGING - m_algorithm->get_node().observer()->log(dht_logger::traversal, "[%p] invalid id in response" - , m_algorithm.get()); + get_observer()->log(dht_logger::traversal, "[%p] invalid id in response" + , algorithm()); #endif return; } diff --git a/src/kademlia/node.cpp b/src/kademlia/node.cpp index 8f4f1ac6c..befd7e367 100644 --- a/src/kademlia/node.cpp +++ b/src/kademlia/node.cpp @@ -456,11 +456,11 @@ struct ping_observer : observer if (!r) { #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" - , m_algorithm.get()); + , algorithm()); } #endif return; @@ -478,7 +478,7 @@ struct ping_observer : observer node_id id; std::copy(nodes, nodes + 20, id.begin()); nodes += 20; - m_algorithm.get()->get_node().m_table.heard_about(id + algorithm()->get_node().m_table.heard_about(id , detail::read_v4_endpoint(nodes)); } } diff --git a/src/kademlia/rpc_manager.cpp b/src/kademlia/rpc_manager.cpp index 26610eca2..957c2760c 100644 --- a/src/kademlia/rpc_manager.cpp +++ b/src/kademlia/rpc_manager.cpp @@ -79,12 +79,18 @@ void intrusive_ptr_release(observer const* o) TORRENT_ASSERT(o->m_refs > 0); if (--o->m_refs == 0) { - boost::intrusive_ptr ta = o->m_algorithm; + boost::intrusive_ptr ta = o->algorithm(); (const_cast(o))->~observer(); ta->free_observer(const_cast(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) { m_sent = clock_type::now(); @@ -336,7 +342,7 @@ bool rpc_manager::incoming(msg const& m, node_id* id #ifndef TORRENT_DISABLE_LOGGING 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()); #endif o->reply(m); @@ -376,7 +382,7 @@ time_duration rpc_manager::tick() { #ifndef TORRENT_DISABLE_LOGGING 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()); #endif m_transactions.erase(i++); @@ -390,7 +396,7 @@ time_duration rpc_manager::tick() { #ifndef TORRENT_DISABLE_LOGGING 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()); #endif ++i; @@ -402,10 +408,10 @@ time_duration rpc_manager::tick() ret = std::min(seconds(timeout) - diff, ret); ++i; } - + 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)); - + return ret; } @@ -431,13 +437,13 @@ bool rpc_manager::invoke(entry& e, udp::endpoint target_addr int tid = (random() ^ (random() << 5)) & 0xffff; io::write_uint16(tid, out); e["t"] = transaction_id; - + o->set_target(target_addr); o->set_transaction_id(tid); #ifndef TORRENT_DISABLE_LOGGING 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()); #endif diff --git a/src/kademlia/traversal_algorithm.cpp b/src/kademlia/traversal_algorithm.cpp index 881f17fa7..12777943d 100644 --- a/src/kademlia/traversal_algorithm.cpp +++ b/src/kademlia/traversal_algorithm.cpp @@ -552,26 +552,26 @@ void traversal_observer::reply(msg const& m) if (!r) { #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" - , m_algorithm.get()); + , algorithm()); } #endif return; } #ifndef TORRENT_DISABLE_LOGGING - if (m_algorithm->get_node().observer()) + if (get_observer()) { bdecode_node nid = r.dict_find_string("id"); char hex_id[41]; 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" - , m_algorithm.get(), hex_id, m_algorithm->invoke_count() - , print_endpoint(target_ep()).c_str(), m_algorithm->name()); + , algorithm(), hex_id, algorithm()->invoke_count() + , print_endpoint(target_ep()).c_str(), algorithm()->name()); } #endif // look for nodes @@ -586,7 +586,7 @@ void traversal_observer::reply(msg const& m) node_id id; std::copy(nodes, nodes + 20, id.begin()); nodes += 20; - m_algorithm->traverse(id, read_v4_endpoint(nodes)); + algorithm()->traverse(id, read_v4_endpoint(nodes)); } } @@ -594,10 +594,10 @@ void traversal_observer::reply(msg const& m) if (!id || id.string_length() != 20) { #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" - , m_algorithm.get()); + get_observer()->log(dht_logger::traversal, "[%p] invalid id in response" + , algorithm()); } #endif return; diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 95551746e..ba467b3e5 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -2344,7 +2344,7 @@ retry: } // to test SSL connections, one can use this openssl command template: - // + // // openssl s_client -cert .pem -key .pem // -CAfile .pem -debug -connect 127.0.0.1:4433 -tls1 // -servername @@ -2851,7 +2851,7 @@ retry: #endif error_code 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_upload_rate.update_quotas(now - m_last_tick);