clean up some time_t and srand() uses

This commit is contained in:
Arvid Norberg 2014-08-16 20:55:44 +00:00
parent c7d95fee10
commit 51f2aafb43
5 changed files with 15 additions and 17 deletions

View File

@ -41,6 +41,7 @@ POSSIBILITY OF SUCH DAMAGE.
#include "libtorrent/io_service.hpp" #include "libtorrent/io_service.hpp"
#include "libtorrent/resolver_interface.hpp" #include "libtorrent/resolver_interface.hpp"
#include "libtorrent/address.hpp" #include "libtorrent/address.hpp"
#include "libtorrent/time.hpp"
namespace libtorrent namespace libtorrent
{ {
@ -59,7 +60,7 @@ private:
struct dns_cache_entry struct dns_cache_entry
{ {
time_t last_seen; ptime last_seen;
std::vector<address> addresses; std::vector<address> addresses;
}; };
@ -71,8 +72,8 @@ private:
// max number of cached entries // max number of cached entries
int m_max_size; int m_max_size;
// timeout (in seconds) of cache entries // timeout of cache entries
int m_timeout; time_duration m_timeout;
}; };
} }

View File

@ -96,8 +96,6 @@ int distance_exp(node_id const& n1, node_id const& n2)
return 0; 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) node_id generate_id_impl(address const& ip_, boost::uint32_t r)
{ {
boost::uint8_t* ip = 0; boost::uint8_t* ip = 0;

View File

@ -48,7 +48,6 @@ POSSIBILITY OF SUCH DAMAGE.
#include <libtorrent/hasher.hpp> #include <libtorrent/hasher.hpp>
#include <libtorrent/session_settings.hpp> // for dht_settings #include <libtorrent/session_settings.hpp> // for dht_settings
#include <libtorrent/time.hpp> #include <libtorrent/time.hpp>
#include <time.h> // time()
#ifdef TORRENT_DHT_VERBOSE_LOGGING #ifdef TORRENT_DHT_VERBOSE_LOGGING
#include <fstream> #include <fstream>
@ -174,8 +173,6 @@ rpc_manager::rpc_manager(node_id const& our_id
, m_allocated_observers(0) , m_allocated_observers(0)
, m_destructing(false) , m_destructing(false)
{ {
std::srand((unsigned int)time(0));
#ifdef TORRENT_DHT_VERBOSE_LOGGING #ifdef TORRENT_DHT_VERBOSE_LOGGING
TORRENT_LOG(rpc) << "Constructing"; TORRENT_LOG(rpc) << "Constructing";

View File

@ -43,7 +43,7 @@ namespace libtorrent
: m_ios(ios) : m_ios(ios)
, m_resolver(ios) , m_resolver(ios)
, m_max_size(700) , m_max_size(700)
, m_timeout(1200) , m_timeout(seconds(1200))
{} {}
void resolver::on_lookup(error_code const& ec, tcp::resolver::iterator i 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]; dns_cache_entry& ce = m_cache[hostname];
time_t now = time(NULL); ptime now = time_now();
ce.last_seen = now; ce.last_seen = now;
ce.addresses.clear(); ce.addresses.clear();
while (i != tcp::resolver::iterator()) while (i != tcp::resolver::iterator())
@ -81,8 +81,8 @@ namespace libtorrent
{ {
cache_t::iterator e = i; cache_t::iterator e = i;
++i; ++i;
if (i->second.last_seen < oldest->second.last_seen) if (e->second.last_seen < oldest->second.last_seen)
oldest = i; oldest = e;
} }
// remove the oldest entry // remove the oldest entry
@ -99,7 +99,7 @@ namespace libtorrent
{ {
// keep cache entries valid for m_timeout seconds // keep cache entries valid for m_timeout seconds
if ((flags & resolver_interface::prefer_cache) 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; error_code ec;
m_ios.post(boost::bind(h, ec, i->second.addresses)); m_ios.post(boost::bind(h, ec, i->second.addresses));

View File

@ -196,9 +196,9 @@ namespace libtorrent { namespace
struct metadata_piece struct metadata_piece
{ {
metadata_piece(): num_requests(0), last_request(0) {} metadata_piece(): num_requests(0), last_request(min_time()) {}
int num_requests; int num_requests;
time_t last_request; ptime last_request;
boost::weak_ptr<ut_metadata_peer_plugin> source; boost::weak_ptr<ut_metadata_peer_plugin> source;
bool operator<(metadata_piece const& rhs) const bool operator<(metadata_piece const& rhs) const
{ return num_requests < rhs.num_requests; } { return num_requests < rhs.num_requests; }
@ -523,8 +523,10 @@ namespace libtorrent { namespace
int piece = i - m_requested_metadata.begin(); int piece = i - m_requested_metadata.begin();
// don't request the same block more than once every 3 seconds // don't request the same block more than once every 3 seconds
time_t now = time(0); ptime now = time_now();
if (now - m_requested_metadata[piece].last_request < 3) return -1; 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; ++m_requested_metadata[piece].num_requests;