From 46bf2970fb192e9474dab0c2148d6a83b72a5944 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Thu, 14 Jun 2007 21:47:00 +0000 Subject: [PATCH] fixes to remote peer download rate etimation --- bindings/python/Jamfile | 1 + examples/client_test.cpp | 2 +- include/libtorrent/peer_connection.hpp | 4 +++ src/peer_connection.cpp | 43 ++++++++++++++++---------- 4 files changed, 32 insertions(+), 18 deletions(-) diff --git a/bindings/python/Jamfile b/bindings/python/Jamfile index afad3a1a7..00eb2cb1b 100755 --- a/bindings/python/Jamfile +++ b/bindings/python/Jamfile @@ -25,5 +25,6 @@ python-extension libtorrent /torrent//torrent /boost/python//boost_python : src + : static ; diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 2d33d70bb..9050d5588 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -336,7 +336,7 @@ void print_peer_info(std::ostream& out, std::vector const out << " " << i->country[0] << i->country[1]; } #endif - out << (i->remote_dl_rate > 0 ? add_suffix(i->remote_dl_rate) + "/s ": " ") << " "; + out << " " << (i->remote_dl_rate > 0 ? add_suffix(i->remote_dl_rate) + "/s ": " ") << " "; if (i->flags & peer_info::handshake) { diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 54b912ebe..b25503a9a 100755 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -690,6 +690,10 @@ namespace libtorrent // approximate peer download rate int m_remote_dl_rate; + + // a timestamp when the remote download rate + // was last updated + ptime m_remote_dl_update; #ifndef NDEBUG public: diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index c2fc912a2..6400e4a91 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -115,6 +115,7 @@ namespace libtorrent , m_connection_ticket(-1) , m_remote_bytes_dled(0) , m_remote_dl_rate(0) + , m_remote_dl_update(time_now()) #ifndef NDEBUG , m_in_constructor(true) #endif @@ -187,6 +188,7 @@ namespace libtorrent , m_speed(slow) , m_remote_bytes_dled(0) , m_remote_dl_rate(0) + , m_remote_dl_update(time_now()) #ifndef NDEBUG , m_in_constructor(true) #endif @@ -732,8 +734,15 @@ namespace libtorrent && t->picker().piece_priority(index) != 0) t->get_policy().peer_is_interesting(*this); - // update bytes downloaded since last timer - m_remote_bytes_dled += t->torrent_file().piece_size(index); + // this will disregard all have messages we get within + // the first two seconds. Since some clients implements + // lazy bitfields, these will not be reliable to use + // for an estimated peer download rate. + if (!peer_info_struct() || time_now() - peer_info_struct()->connected > seconds(2)) + { + // update bytes downloaded since last timer + m_remote_bytes_dled += t->torrent_file().piece_size(index); + } } if (is_seed()) @@ -1891,24 +1900,24 @@ namespace libtorrent , m_upload_limit)); } - // note the 1/60 multiplication here - this is per second of the - // minute; but since timers are not exact this makes this - // calculation REALLY approximate. - - float factor = 0.6666666666667f; + // update once every minute + if (now - m_remote_dl_update >= seconds(60)) + { + float factor = 0.6666666666667f; - if(m_remote_dl_rate == 0) { - factor = 0.0f; + if (m_remote_dl_rate == 0) + { + factor = 0.0f; + } + + m_remote_dl_rate = + (m_remote_dl_rate * factor) + + ((m_remote_bytes_dled * (1.0f-factor)) / 60.f); + + m_remote_bytes_dled = 0; + m_remote_dl_update = now; } - m_remote_dl_rate = - (m_remote_dl_rate * factor) + - ((m_remote_bytes_dled * (1.0f-factor)) * (1.f/60.f)); - - m_remote_bytes_dled = 0; - - - // fill_send_buffer(); /* size_type diff = share_diff();