diff --git a/include/libtorrent/kademlia/dht_tracker.hpp b/include/libtorrent/kademlia/dht_tracker.hpp index 22c257ddc..6b4f98092 100644 --- a/include/libtorrent/kademlia/dht_tracker.hpp +++ b/include/libtorrent/kademlia/dht_tracker.hpp @@ -90,6 +90,7 @@ namespace libtorrent { namespace dht , sha1_hash const&)> f); void dht_status(session_status& s); + void network_stats(int& sent, int& received); // translate bittorrent kademlia message into the generic kademlia message // used by the library @@ -134,6 +135,10 @@ namespace libtorrent { namespace dht // used to resolve hostnames for nodes udp::resolver m_host_resolver; + // sent and received bytes since queried last time + int m_sent_bytes; + int m_received_bytes; + // used to ignore abusive dht nodes struct node_ban_entry { diff --git a/include/libtorrent/stat.hpp b/include/libtorrent/stat.hpp index d99593a1e..671b868de 100644 --- a/include/libtorrent/stat.hpp +++ b/include/libtorrent/stat.hpp @@ -146,16 +146,12 @@ namespace libtorrent { TORRENT_ASSERT(bytes >= 0); m_stat[download_dht_protocol].add(bytes); - // assuming IPv4 and UDP headers - m_stat[download_ip_protocol].add(28); } void sent_dht_bytes(int bytes) { TORRENT_ASSERT(bytes >= 0); m_stat[upload_dht_protocol].add(bytes); - // assuming IPv4 and UDP headers - m_stat[upload_ip_protocol].add(28); } void received_tracker_bytes(int bytes) diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index cbe452dae..43b309ff2 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -179,6 +179,7 @@ namespace libtorrent { namespace dht { std::vector initial_nodes; + mutex_t::scoped_lock l(m_mutex); if (bootstrap.type() == entry::dictionary_t) { try @@ -221,12 +222,22 @@ namespace libtorrent { namespace dht void dht_tracker::dht_status(session_status& s) { + mutex_t::scoped_lock l(m_mutex); boost::tie(s.dht_nodes, s.dht_node_cache) = m_dht.size(); s.dht_torrents = m_dht.data_size(); s.dht_global_nodes = m_dht.num_global_nodes(); m_dht.status(s); } + void dht_tracker::network_stats(int& sent, int& received) + { + mutex_t::scoped_lock l(m_mutex); + sent = m_sent_bytes; + received = m_received_bytes; + m_sent_bytes = 0; + m_received_bytes = 0; + } + void dht_tracker::connection_timeout(error_code const& e) { mutex_t::scoped_lock l(m_mutex); @@ -355,12 +366,14 @@ namespace libtorrent { namespace dht , boost::function const& , sha1_hash const&)> f) { + mutex_t::scoped_lock l(m_mutex); m_dht.announce(ih, listen_port, f); } void dht_tracker::on_unreachable(udp::endpoint const& ep) { + mutex_t::scoped_lock l(m_mutex); m_dht.unreachable(ep); } @@ -368,10 +381,9 @@ namespace libtorrent { namespace dht // used by the library void dht_tracker::on_receive(udp::endpoint const& ep, char const* buf, int bytes_transferred) { - { - libtorrent::aux::session_impl::mutex_t::scoped_lock l(m_ses.m_mutex); - m_ses.m_stat.received_dht_bytes(bytes_transferred); - } + mutex_t::scoped_lock l(m_mutex); + // account for IP and UDP overhead + m_received_bytes += bytes_transferred + 28; node_ban_entry* match = 0; node_ban_entry* min = m_ban_nodes; @@ -759,6 +771,7 @@ namespace libtorrent { namespace dht entry dht_tracker::state() const { + mutex_t::scoped_lock l(m_mutex); entry ret(entry::dictionary_t); { entry nodes(entry::list_t); @@ -790,11 +803,13 @@ namespace libtorrent { namespace dht void dht_tracker::add_node(udp::endpoint node) { + mutex_t::scoped_lock l(m_mutex); m_dht.add_node(node); } void dht_tracker::add_node(std::pair const& node) { + mutex_t::scoped_lock l(m_mutex); udp::resolver::query q(node.first, lexical_cast(node.second)); m_host_resolver.async_resolve(q, bind(&dht_tracker::on_name_lookup, self(), _1, _2)); @@ -809,6 +824,7 @@ namespace libtorrent { namespace dht void dht_tracker::add_router_node(std::pair const& node) { + mutex_t::scoped_lock l(m_mutex); udp::resolver::query q(node.first, lexical_cast(node.second)); m_host_resolver.async_resolve(q, bind(&dht_tracker::on_router_name_lookup, self(), _1, _2)); @@ -1003,10 +1019,8 @@ namespace libtorrent { namespace dht error_code ec; m_sock.send(m.addr, &m_send_buf[0], (int)m_send_buf.size(), ec); - { - libtorrent::aux::session_impl::mutex_t::scoped_lock l(m_ses.m_mutex); - m_ses.m_stat.sent_dht_bytes(m_send_buf.size()); - } + // account for IP and UDP overhead + m_sent_bytes += m_send_buf.size() + 28; #ifdef TORRENT_DHT_VERBOSE_LOGGING m_total_out_bytes += m_send_buf.size(); diff --git a/src/session_impl.cpp b/src/session_impl.cpp index 6e2c6d8c4..f6dec9a89 100644 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -1108,6 +1108,15 @@ namespace aux { ++i; } + if (m_dht) + { + int dht_down; + int dht_up; + m_dht->network_stats(dht_up, dht_down); + m_stat.sent_dht_bytes(dht_up); + m_stat.received_dht_bytes(dht_down); + } + // drain the IP overhead from the bandwidth limiters m_download_channel.drain( m_stat.download_ip_overhead()