From 51f2aafb438d4716fc75d411f83f6d0b55c841c6 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Sat, 16 Aug 2014 20:55:44 +0000 Subject: [PATCH] clean up some time_t and srand() uses --- include/libtorrent/resolver.hpp | 7 ++++--- src/kademlia/node_id.cpp | 2 -- src/kademlia/rpc_manager.cpp | 3 --- src/resolver.cpp | 10 +++++----- src/ut_metadata.cpp | 10 ++++++---- 5 files changed, 15 insertions(+), 17 deletions(-) diff --git a/include/libtorrent/resolver.hpp b/include/libtorrent/resolver.hpp index bbb0cc3b2..515e42dd5 100644 --- a/include/libtorrent/resolver.hpp +++ b/include/libtorrent/resolver.hpp @@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/io_service.hpp" #include "libtorrent/resolver_interface.hpp" #include "libtorrent/address.hpp" +#include "libtorrent/time.hpp" namespace libtorrent { @@ -59,7 +60,7 @@ private: struct dns_cache_entry { - time_t last_seen; + ptime last_seen; std::vector
addresses; }; @@ -71,8 +72,8 @@ private: // max number of cached entries int m_max_size; - // timeout (in seconds) of cache entries - int m_timeout; + // timeout of cache entries + time_duration m_timeout; }; } diff --git a/src/kademlia/node_id.cpp b/src/kademlia/node_id.cpp index 5499a956b..585d81277 100644 --- a/src/kademlia/node_id.cpp +++ b/src/kademlia/node_id.cpp @@ -96,8 +96,6 @@ int distance_exp(node_id const& n1, node_id const& n2) return 0; } -struct static_ { static_() { std::srand((unsigned int)std::time(0)); } } static__; - node_id generate_id_impl(address const& ip_, boost::uint32_t r) { boost::uint8_t* ip = 0; diff --git a/src/kademlia/rpc_manager.cpp b/src/kademlia/rpc_manager.cpp index 6d3a37e72..41c80a625 100644 --- a/src/kademlia/rpc_manager.cpp +++ b/src/kademlia/rpc_manager.cpp @@ -48,7 +48,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include // for dht_settings #include -#include // time() #ifdef TORRENT_DHT_VERBOSE_LOGGING #include @@ -174,8 +173,6 @@ rpc_manager::rpc_manager(node_id const& our_id , m_allocated_observers(0) , m_destructing(false) { - std::srand((unsigned int)time(0)); - #ifdef TORRENT_DHT_VERBOSE_LOGGING TORRENT_LOG(rpc) << "Constructing"; diff --git a/src/resolver.cpp b/src/resolver.cpp index 156cdb5db..bb53557a6 100644 --- a/src/resolver.cpp +++ b/src/resolver.cpp @@ -43,7 +43,7 @@ namespace libtorrent : m_ios(ios) , m_resolver(ios) , m_max_size(700) - , m_timeout(1200) + , m_timeout(seconds(1200)) {} void resolver::on_lookup(error_code const& ec, tcp::resolver::iterator i @@ -60,7 +60,7 @@ namespace libtorrent } dns_cache_entry& ce = m_cache[hostname]; - time_t now = time(NULL); + ptime now = time_now(); ce.last_seen = now; ce.addresses.clear(); while (i != tcp::resolver::iterator()) @@ -81,8 +81,8 @@ namespace libtorrent { cache_t::iterator e = i; ++i; - if (i->second.last_seen < oldest->second.last_seen) - oldest = i; + if (e->second.last_seen < oldest->second.last_seen) + oldest = e; } // remove the oldest entry @@ -99,7 +99,7 @@ namespace libtorrent { // keep cache entries valid for m_timeout seconds if ((flags & resolver_interface::prefer_cache) - || i->second.last_seen + m_timeout >= time(NULL)) + || i->second.last_seen + m_timeout >= time_now()) { error_code ec; m_ios.post(boost::bind(h, ec, i->second.addresses)); diff --git a/src/ut_metadata.cpp b/src/ut_metadata.cpp index 7cd5ba94c..2fad627a7 100644 --- a/src/ut_metadata.cpp +++ b/src/ut_metadata.cpp @@ -196,9 +196,9 @@ namespace libtorrent { namespace struct metadata_piece { - metadata_piece(): num_requests(0), last_request(0) {} + metadata_piece(): num_requests(0), last_request(min_time()) {} int num_requests; - time_t last_request; + ptime last_request; boost::weak_ptr source; bool operator<(metadata_piece const& rhs) const { return num_requests < rhs.num_requests; } @@ -523,8 +523,10 @@ namespace libtorrent { namespace int piece = i - m_requested_metadata.begin(); // don't request the same block more than once every 3 seconds - time_t now = time(0); - if (now - m_requested_metadata[piece].last_request < 3) return -1; + ptime now = time_now(); + if (m_requested_metadata[piece].last_request != min_time() + && total_seconds(now - m_requested_metadata[piece].last_request) < 3) + return -1; ++m_requested_metadata[piece].num_requests;