From 306d015924fe98815b3b28ce894278f8aaa64ff9 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 4 Apr 2007 22:27:36 +0000 Subject: [PATCH] added a timer abstraction and replaced the use of boost.date_time. the timers used now are QueryPerformanceCounter on windows, mach_absolute_time on darwin and currently falls back on boost.date_time on other platforms. This has the advantage of being a monotonic clock, and also slightly more efficient --- examples/client_test.cpp | 14 +- include/libtorrent/alert.hpp | 6 +- include/libtorrent/aux_/session_impl.hpp | 2 +- include/libtorrent/bandwidth_manager.hpp | 6 +- include/libtorrent/bt_peer_connection.hpp | 2 +- include/libtorrent/file_pool.hpp | 8 +- include/libtorrent/http_connection.hpp | 12 +- include/libtorrent/kademlia/dht_tracker.hpp | 4 +- include/libtorrent/kademlia/node.hpp | 12 +- include/libtorrent/kademlia/routing_table.hpp | 7 +- include/libtorrent/kademlia/rpc_manager.hpp | 12 +- include/libtorrent/natpmp.hpp | 6 +- include/libtorrent/peer_connection.hpp | 12 +- include/libtorrent/policy.hpp | 15 +- include/libtorrent/socket.hpp | 7 +- include/libtorrent/time.hpp | 303 ++++++++++++++++++ include/libtorrent/torrent.hpp | 14 +- include/libtorrent/torrent_handle.hpp | 3 +- include/libtorrent/torrent_info.hpp | 8 +- include/libtorrent/tracker_manager.hpp | 7 +- include/libtorrent/upnp.hpp | 4 +- src/alert.cpp | 4 +- src/bandwidth_manager.cpp | 10 +- src/bt_peer_connection.cpp | 10 +- src/file_pool.cpp | 2 +- src/http_connection.cpp | 22 +- src/kademlia/dht_tracker.cpp | 13 +- src/kademlia/node.cpp | 15 +- src/kademlia/routing_table.cpp | 19 +- src/kademlia/rpc_manager.cpp | 19 +- src/logger.cpp | 2 +- src/metadata_transfer.cpp | 22 +- src/natpmp.cpp | 23 +- src/peer_connection.cpp | 116 ++++--- src/policy.cpp | 73 ++--- src/session_impl.cpp | 21 +- src/torrent.cpp | 43 +-- src/torrent_handle.cpp | 4 +- src/torrent_info.cpp | 25 +- src/tracker_manager.cpp | 21 +- src/upnp.cpp | 26 +- test/test_upnp.cpp | 2 - 42 files changed, 590 insertions(+), 366 deletions(-) create mode 100644 include/libtorrent/time.hpp diff --git a/examples/client_test.cpp b/examples/client_test.cpp index 7857d4b60..0d0c447a2 100644 --- a/examples/client_test.cpp +++ b/examples/client_test.cpp @@ -148,6 +148,8 @@ void clear_home() #endif +namespace pt = boost::posix_time; + std::string esc(char const* code) { #ifdef ANSI_TERMINAL_COLORS @@ -310,9 +312,6 @@ void print_peer_info(std::ostream& out, std::vector const typedef std::multimap handles_t; -using boost::posix_time::ptime; -using boost::posix_time::second_clock; -using boost::posix_time::seconds; using boost::bind; using boost::filesystem::path; using boost::filesystem::exists; @@ -579,7 +578,7 @@ int main(int ac, char* av[]) std::deque events; - ptime next_dir_scan = second_clock::universal_time(); + boost::posix_time::ptime next_dir_scan = boost::posix_time::second_clock::universal_time(); // the string is the filename of the .torrent file, but only if // it was added through the directory monitor. It is used to @@ -777,7 +776,7 @@ int main(int ac, char* av[]) // loop through the alert queue to see if anything has happened. std::auto_ptr a; a = ses.pop_alert(); - std::string now = to_simple_string(second_clock::local_time()); + std::string now = to_simple_string(boost::posix_time::second_clock::local_time()); while (a.get()) { std::stringstream event_string; @@ -999,11 +998,12 @@ int main(int ac, char* av[]) puts(out.str().c_str()); if (!monitor_dir.empty() - && next_dir_scan < second_clock::universal_time()) + && next_dir_scan < boost::posix_time::second_clock::universal_time()) { scan_dir(monitor_dir, ses, handles, preferred_ratio , compact_allocation_mode, save_path); - next_dir_scan = second_clock::universal_time() + seconds(poll_interval); + next_dir_scan = boost::posix_time::second_clock::universal_time() + + boost::posix_time::seconds(poll_interval); } } diff --git a/include/libtorrent/alert.hpp b/include/libtorrent/alert.hpp index a820ef225..759b66f29 100755 --- a/include/libtorrent/alert.hpp +++ b/include/libtorrent/alert.hpp @@ -49,12 +49,12 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include #ifdef _MSC_VER #pragma warning(pop) #endif +#include "libtorrent/time.hpp" #include "libtorrent/config.hpp" #define TORRENT_MAX_ALERT_TYPES 10 @@ -70,7 +70,7 @@ namespace libtorrent { virtual ~alert(); // a timestamp is automatically created in the constructor - boost::posix_time::ptime timestamp() const; + ptime timestamp() const; const std::string& msg() const; @@ -81,7 +81,7 @@ namespace libtorrent { private: std::string m_msg; severity_t m_severity; - boost::posix_time::ptime m_timestamp; + ptime m_timestamp; }; class TORRENT_EXPORT alert_manager diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp index 8ec94ce2d..4604ce616 100644 --- a/include/libtorrent/aux_/session_impl.hpp +++ b/include/libtorrent/aux_/session_impl.hpp @@ -382,7 +382,7 @@ namespace libtorrent file_pool m_files; void second_tick(asio::error_code const& e); - boost::posix_time::ptime m_last_tick; + ptime m_last_tick; #ifndef TORRENT_DISABLE_DHT boost::intrusive_ptr m_dht; diff --git a/include/libtorrent/bandwidth_manager.hpp b/include/libtorrent/bandwidth_manager.hpp index 32d1b510d..dbf93aa77 100644 --- a/include/libtorrent/bandwidth_manager.hpp +++ b/include/libtorrent/bandwidth_manager.hpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #define TORRENT_BANDWIDTH_MANAGER_HPP_INCLUDED #include "libtorrent/socket.hpp" + #include #include #include @@ -42,7 +43,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -namespace pt = boost::posix_time; using boost::weak_ptr; using boost::shared_ptr; using boost::intrusive_ptr; @@ -70,8 +70,8 @@ namespace libtorrent struct history_entry { history_entry(intrusive_ptr p, weak_ptr t - , int a, pt::ptime exp); - pt::ptime expires_at; + , int a, ptime exp); + ptime expires_at; int amount; intrusive_ptr peer; weak_ptr tor; diff --git a/include/libtorrent/bt_peer_connection.hpp b/include/libtorrent/bt_peer_connection.hpp index f68a5d1fa..45319aeb9 100755 --- a/include/libtorrent/bt_peer_connection.hpp +++ b/include/libtorrent/bt_peer_connection.hpp @@ -199,7 +199,7 @@ namespace libtorrent #ifndef NDEBUG void check_invariant() const; - boost::posix_time::ptime m_last_choke; + ptime m_last_choke; #endif private: diff --git a/include/libtorrent/file_pool.hpp b/include/libtorrent/file_pool.hpp index 8180a54fe..01f161e21 100644 --- a/include/libtorrent/file_pool.hpp +++ b/include/libtorrent/file_pool.hpp @@ -49,6 +49,7 @@ POSSIBILITY OF SUCH DAMAGE. #endif #include "libtorrent/file.hpp" +#include "libtorrent/time.hpp" namespace libtorrent { @@ -58,7 +59,6 @@ namespace libtorrent using boost::multi_index::ordered_unique; using boost::multi_index::indexed_by; using boost::multi_index::member; - namespace pt = boost::posix_time; namespace fs = boost::filesystem; struct TORRENT_EXPORT file_pool : boost::noncopyable @@ -76,11 +76,11 @@ namespace libtorrent { lru_file_entry(boost::shared_ptr const& f) : file_ptr(f) - , last_use(pt::second_clock::universal_time()) {} + , last_use(time_now()) {} mutable boost::shared_ptr file_ptr; fs::path file_path; void* key; - pt::ptime last_use; + ptime last_use; file::open_mode mode; }; @@ -88,7 +88,7 @@ namespace libtorrent lru_file_entry, indexed_by< ordered_unique > - , ordered_non_unique > , ordered_non_unique > diff --git a/include/libtorrent/http_connection.hpp b/include/libtorrent/http_connection.hpp index bdc4b14f2..162cb36fa 100644 --- a/include/libtorrent/http_connection.hpp +++ b/include/libtorrent/http_connection.hpp @@ -44,6 +44,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/socket.hpp" #include "libtorrent/http_tracker_connection.hpp" +#include "libtorrent/time.hpp" namespace libtorrent { @@ -63,7 +64,7 @@ struct http_connection : boost::enable_shared_from_this, boost: , m_resolver(ios) , m_handler(handler) , m_timer(ios) - , m_last_receive(boost::posix_time::second_clock::universal_time()) + , m_last_receive(time_now()) , m_bottled(bottled) , m_called(false) , m_rate_limit(0) @@ -81,11 +82,10 @@ struct http_connection : boost::enable_shared_from_this, boost: std::string sendbuffer; - void get(std::string const& url, boost::posix_time::time_duration timeout - = boost::posix_time::seconds(30)); + void get(std::string const& url, time_duration timeout = seconds(30)); void start(std::string const& hostname, std::string const& port - , boost::posix_time::time_duration timeout); + , time_duration timeout); void close(); private: @@ -107,8 +107,8 @@ private: http_parser m_parser; http_handler m_handler; deadline_timer m_timer; - boost::posix_time::time_duration m_timeout; - boost::posix_time::ptime m_last_receive; + time_duration m_timeout; + ptime m_last_receive; // bottled means that the handler is called once, when // everything is received (and buffered in memory). // non bottled means that once the headers have been diff --git a/include/libtorrent/kademlia/dht_tracker.hpp b/include/libtorrent/kademlia/dht_tracker.hpp index d1a3a8247..2c290639f 100644 --- a/include/libtorrent/kademlia/dht_tracker.hpp +++ b/include/libtorrent/kademlia/dht_tracker.hpp @@ -39,8 +39,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include -#include #include #include #include @@ -120,7 +118,7 @@ namespace libtorrent { namespace dht udp::endpoint m_remote_endpoint[2]; std::vector m_send_buf; - boost::posix_time::ptime m_last_refresh; + ptime m_last_refresh; deadline_timer m_timer; deadline_timer m_connection_timer; deadline_timer m_refresh_timer; diff --git a/include/libtorrent/kademlia/node.hpp b/include/libtorrent/kademlia/node.hpp index c44a21f33..62791427a 100644 --- a/include/libtorrent/kademlia/node.hpp +++ b/include/libtorrent/kademlia/node.hpp @@ -47,11 +47,11 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -#include -#include #include #include +#include "libtorrent/socket.hpp" + namespace libtorrent { namespace dht { @@ -67,7 +67,7 @@ TORRENT_DECLARE_LOG(node); struct peer_entry { tcp::endpoint addr; - boost::posix_time::ptime added; + ptime added; }; // this is a group. It contains a set of group members @@ -133,8 +133,8 @@ public: // the returned time is the delay until connection_timeout() // should be called again the next time - boost::posix_time::time_duration connection_timeout(); - boost::posix_time::time_duration refresh_timeout(); + time_duration connection_timeout(); + time_duration refresh_timeout(); // generates a new secret number used to generate write tokens void new_write_key(); @@ -172,7 +172,7 @@ private: rpc_manager m_rpc; table_t m_map; - boost::posix_time::ptime m_last_tracker_tick; + ptime m_last_tracker_tick; // secret random numbers used to create write tokens int m_secret[2]; diff --git a/include/libtorrent/kademlia/routing_table.hpp b/include/libtorrent/kademlia/routing_table.hpp index 3abc472b4..4947cb5fe 100644 --- a/include/libtorrent/kademlia/routing_table.hpp +++ b/include/libtorrent/kademlia/routing_table.hpp @@ -36,7 +36,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include #include #include @@ -51,8 +50,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -namespace pt = boost::posix_time; - namespace libtorrent { namespace dht { @@ -177,7 +174,7 @@ public: // if the given bucket is empty but there are nodes // in a bucket closer to us, or if the bucket is non-empty and // the time from the last activity is more than 15 minutes - boost::posix_time::ptime next_refresh(int bucket); + ptime next_refresh(int bucket); // fills the vector with the count nodes from our buckets that // are nearest to the given id. @@ -226,7 +223,7 @@ private: typedef boost::array, 160> table_t; table_t m_buckets; // timestamps of the last activity in each bucket - typedef boost::array table_activity_t; + typedef boost::array table_activity_t; table_activity_t m_bucket_activity; node_id m_id; // our own node id diff --git a/include/libtorrent/kademlia/rpc_manager.hpp b/include/libtorrent/kademlia/rpc_manager.hpp index 2603071fc..25eacebd6 100644 --- a/include/libtorrent/kademlia/rpc_manager.hpp +++ b/include/libtorrent/kademlia/rpc_manager.hpp @@ -36,7 +36,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include #include #include #include @@ -49,6 +48,8 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +#include "libtorrent/time.hpp" + namespace libtorrent { namespace dht { @@ -112,8 +113,7 @@ struct msg struct observer : boost::noncopyable { - observer() - : sent(boost::posix_time::microsec_clock::universal_time()) + observer(): sent(time_now()) {} virtual ~observer() {} @@ -136,7 +136,7 @@ struct observer : boost::noncopyable virtual void abort() = 0; udp::endpoint target_addr; - boost::posix_time::ptime sent; + ptime sent; }; class routing_table; @@ -153,7 +153,7 @@ public: // returns true if the node needs a refresh bool incoming(msg const&); - boost::posix_time::time_duration tick(); + time_duration tick(); void invoke(int message_id, udp::endpoint target , boost::shared_ptr o); @@ -191,7 +191,7 @@ private: send_fun m_send; node_id m_our_id; routing_table& m_table; - boost::posix_time::ptime m_timer; + ptime m_timer; node_id m_random_number; bool m_destructing; }; diff --git a/include/libtorrent/natpmp.hpp b/include/libtorrent/natpmp.hpp index 6347ec1e8..1c0ffd0be 100644 --- a/include/libtorrent/natpmp.hpp +++ b/include/libtorrent/natpmp.hpp @@ -33,8 +33,8 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_NATPMP_HPP #define TORRENT_NATPMP_HPP -#include -#include +#include "libtorrent/socket.hpp" + #include #if defined(TORRENT_LOGGING) || defined(TORRENT_VERBOSE_LOGGING) @@ -88,7 +88,7 @@ private: bool need_update; // the time the port mapping will expire - boost::posix_time::ptime expires; + ptime expires; // the local port for this mapping. If this is set // to 0, the mapping is not in use diff --git a/include/libtorrent/peer_connection.hpp b/include/libtorrent/peer_connection.hpp index 6ef32c951..f0f37484f 100755 --- a/include/libtorrent/peer_connection.hpp +++ b/include/libtorrent/peer_connection.hpp @@ -314,7 +314,7 @@ namespace libtorrent #ifndef NDEBUG void check_invariant() const; - boost::posix_time::ptime m_last_choke; + ptime m_last_choke; #endif virtual void get_peer_info(peer_info& p) const = 0; @@ -452,7 +452,7 @@ namespace libtorrent // the time when we last got a part of a // piece packet from this peer - boost::posix_time::ptime m_last_piece; + ptime m_last_piece; int m_packet_size; int m_recv_pos; @@ -479,8 +479,8 @@ namespace libtorrent int m_write_pos; // timeouts - boost::posix_time::ptime m_last_receive; - boost::posix_time::ptime m_last_sent; + ptime m_last_receive; + ptime m_last_sent; boost::shared_ptr m_socket; // this is the peer we're actually talking to @@ -588,11 +588,11 @@ namespace libtorrent // the time when this peer sent us a not_interested message // the last time. - boost::posix_time::ptime m_became_uninterested; + ptime m_became_uninterested; // the time when we sent a not_interested message to // this peer the last time. - boost::posix_time::ptime m_became_uninteresting; + ptime m_became_uninteresting; // this is true until this socket has become // writable for the first time (i.e. the diff --git a/include/libtorrent/policy.hpp b/include/libtorrent/policy.hpp index 94ad9b03e..f858641bc 100755 --- a/include/libtorrent/policy.hpp +++ b/include/libtorrent/policy.hpp @@ -40,8 +40,6 @@ POSSIBILITY OF SUCH DAMAGE. #pragma warning(push, 1) #endif -#include - #ifdef _MSC_VER #pragma warning(pop) #endif @@ -52,6 +50,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/size_type.hpp" #include "libtorrent/invariant_check.hpp" #include "libtorrent/config.hpp" +#include "libtorrent/time.hpp" namespace libtorrent { @@ -156,11 +155,11 @@ namespace libtorrent // the time when this peer was optimistically unchoked // the last time. - boost::posix_time::ptime last_optimistically_unchoked; + ptime last_optimistically_unchoked; // the time when the peer connected to us // or disconnected if it isn't connected right now - boost::posix_time::ptime connected; + ptime connected; // this is the accumulated amount of // uploaded and downloaded data to this @@ -222,14 +221,12 @@ namespace libtorrent { bool operator()(const peer& p) { - using namespace boost::posix_time; - - ptime not_tried_yet(boost::gregorian::date(1970,boost::gregorian::Jan,1)); + ptime not_tried_yet(min_time()); // this timeout has to be customizable! return p.connection == 0 && p.connected != not_tried_yet - && second_clock::universal_time() - p.connected > minutes(120); + && time_now() - p.connected > minutes(120); } }; @@ -249,7 +246,7 @@ namespace libtorrent // if there is a connection limit, // we disconnect one peer every minute in hope of // establishing a connection with a better peer - boost::posix_time::ptime m_last_optimistic_disconnect; + ptime m_last_optimistic_disconnect; }; } diff --git a/include/libtorrent/socket.hpp b/include/libtorrent/socket.hpp index 5df7a4579..c478a92ef 100755 --- a/include/libtorrent/socket.hpp +++ b/include/libtorrent/socket.hpp @@ -51,12 +51,15 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include +#include #ifdef __OBJC__ #undef Protocol #endif #include "libtorrent/io.hpp" +#include "libtorrent/time.hpp" #ifdef _MSC_VER #pragma warning(pop) @@ -64,6 +67,7 @@ POSSIBILITY OF SUCH DAMAGE. namespace libtorrent { + /* namespace asio = boost::asio; @@ -91,7 +95,8 @@ namespace libtorrent typedef asio::io_service io_service; using asio::async_write; - using asio::deadline_timer; + + typedef asio::basic_deadline_timer deadline_timer; namespace detail { diff --git a/include/libtorrent/time.hpp b/include/libtorrent/time.hpp new file mode 100644 index 000000000..1386082e6 --- /dev/null +++ b/include/libtorrent/time.hpp @@ -0,0 +1,303 @@ +/* + +Copyright (c) 2007, Arvid Norberg +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions +are met: + + * Redistributions of source code must retain the above copyright + notice, this list of conditions and the following disclaimer. + * Redistributions in binary form must reproduce the above copyright + notice, this list of conditions and the following disclaimer in + the documentation and/or other materials provided with the distribution. + * Neither the name of the author nor the names of its + contributors may be used to endorse or promote products derived + from this software without specific prior written permission. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +POSSIBILITY OF SUCH DAMAGE. + +*/ + +#ifndef TORRENT_TIME_HPP_INCLUDED +#define TORRENT_TIME_HPP_INCLUDED + +#if (!defined (__MACH__) && !defined (_WIN32)) || defined (TORRENT_USE_BOOST_DATE_TIME) + +#include + +namespace libtorrent +{ + typedef boost::posix_time::ptime ptime; + typedef boost::posix_time::time_duration time_duration; + inline ptime time_now() + { return boost::posix_time::microsec_clock::universal_time(); } + inline ptime min_time() + { return boost::posix_time::ptime(boost::posix_time::min_date_time); } + inline ptime max_time() + { return boost::posix_time::ptime(boost::posix_time::max_date_time); } + inline time_duration seconds(int s) { return boost::posix_time::seconds(s); } + inline time_duration milliseconds(int s) { return boost::posix_time::milliseconds(s); } + inline time_duration microsec(int s) { return boost::posix_time::microsec(s); } + inline time_duration minutes(int s) { return boost::posix_time::minutes(s); } + inline time_duration hours(int s) { return boost::posix_time::hours(s); } + + inline int total_seconds(time_duration td) + { return td.total_seconds(); } + inline int total_milliseconds(time_duration td) + { return td.total_milliseconds(); } + inline boost::int64_t total_microseconds(time_duration td) + { return td.total_microseconds(); } + +} + +#else + +#include +#include +#include + +namespace libtorrent +{ + // libtorrent time_duration type + struct time_duration + { + time_duration() {} + explicit time_duration(boost::int64_t d) : diff(d) {} + boost::int64_t diff; + }; + + inline bool is_negative(time_duration dt) { return dt.diff < 0; } + inline bool operator<(time_duration lhs, time_duration rhs) + { return lhs.diff < rhs.diff; } + inline bool operator>(time_duration lhs, time_duration rhs) + { return lhs.diff > rhs.diff; } + + // libtorrent time type + struct ptime + { + ptime() {} + explicit ptime(boost::int64_t t): time(t) {} + boost::int64_t time; + }; + + inline bool operator>(ptime lhs, ptime rhs) + { return lhs.time > rhs.time; } + inline bool operator>=(ptime lhs, ptime rhs) + { return lhs.time >= rhs.time; } + inline bool operator<=(ptime lhs, ptime rhs) + { return lhs.time <= rhs.time; } + inline bool operator<(ptime lhs, ptime rhs) + { return lhs.time < rhs.time; } + inline bool operator!=(ptime lhs, ptime rhs) + { return lhs.time != rhs.time;} + inline bool operator==(ptime lhs, ptime rhs) + { return lhs.time == rhs.time;} + inline time_duration operator-(ptime lhs, ptime rhs) + { return time_duration(lhs.time - rhs.time); } + inline ptime operator+(ptime lhs, time_duration rhs) + { return ptime(lhs.time + rhs.diff); } + inline ptime operator+(time_duration lhs, ptime rhs) + { return ptime(rhs.time + lhs.diff); } + inline ptime operator-(ptime lhs, time_duration rhs) + { return ptime(lhs.time - rhs.diff); } + + ptime time_now(); + inline ptime min_time() { return ptime(0); } + inline ptime max_time() { return ptime(std::numeric_limits::max()); } + int total_seconds(time_duration td); + int total_milliseconds(time_duration td); + boost::int64_t total_microseconds(time_duration td); +} + +// asio time_traits +namespace asio +{ + template<> + struct time_traits + { + typedef libtorrent::ptime time_type; + typedef libtorrent::time_duration duration_type; + static time_type now() + { return time_type(libtorrent::time_now()); } + static time_type add(time_type t, duration_type d) + { return time_type(t.time + d.diff);} + static duration_type subtract(time_type t1, time_type t2) + { return duration_type(t1 - t2); } + static bool less_than(time_type t1, time_type t2) + { return t1 < t2; } + static boost::posix_time::time_duration to_posix_duration( + duration_type d) + { return boost::posix_time::microseconds(libtorrent::total_microseconds(d)); } + }; +} + +#if defined(__MACH__) + +#include +#include + +// high precision timer for darwin intel and ppc + +namespace libtorrent +{ + namespace aux + { + inline boost::int64_t absolutetime_to_microseconds(boost::int64_t at) + { + static mach_timebase_info_data_t timebase_info = {0,0}; + if (timebase_info.denom == 0) + mach_timebase_info(&timebase_info); + // make sure we don't overflow + assert((at >= 0 && at >= at / 1000 * timebase_info.numer / timebase_info.denom) + || (at < 0 && at < at / 1000 * timebase_info.numer / timebase_info.denom)); + return at / 1000 * timebase_info.numer / timebase_info.denom; + } + + inline boost::int64_t microseconds_to_absolutetime(boost::int64_t ms) + { + static mach_timebase_info_data_t timebase_info = {0,0}; + if (timebase_info.denom == 0) + { + mach_timebase_info(&timebase_info); + assert(timebase_info.numer > 0); + assert(timebase_info.denom > 0); + } + // make sure we don't overflow + assert((ms >= 0 && ms <= ms * timebase_info.denom / timebase_info.numer * 1000) + || (ms < 0 && ms > ms * timebase_info.denom / timebase_info.numer * 1000)); + return ms * timebase_info.denom / timebase_info.numer * 1000; + } + } + + inline int total_seconds(time_duration td) + { + return aux::absolutetime_to_microseconds(td.diff) + / 1000000; + } + inline int total_milliseconds(time_duration td) + { + return aux::absolutetime_to_microseconds(td.diff) + / 1000; + } + inline boost::int64_t total_microseconds(time_duration td) + { + return aux::absolutetime_to_microseconds(td.diff); + } + + inline ptime time_now() { return ptime(mach_absolute_time()); } + + inline time_duration microsec(boost::int64_t s) + { + return time_duration(aux::microseconds_to_absolutetime(s)); + } + inline time_duration milliseconds(boost::int64_t s) + { + return time_duration(aux::microseconds_to_absolutetime(s * 1000)); + } + inline time_duration seconds(boost::int64_t s) + { + return time_duration(aux::microseconds_to_absolutetime(s * 1000000)); + } + inline time_duration minutes(boost::int64_t s) + { + return time_duration(aux::microseconds_to_absolutetime(s * 1000000 * 60)); + } + inline time_duration hours(boost::int64_t s) + { + return time_duration(aux::microseconds_to_absolutetime(s * 1000000 * 60 * 60)); + } + +} +#elif defined(_WIN32) + +#define WIN32_LEAN_AND_MEAN +#include + +namespace libtorrent +{ + namespace aux + { + inline boost::int64_t performance_counter_to_microseconds(boost::int64_t pc) + { + static LARGE_INTEGER performace_counter_frequency = {0,0}; + if (performace_counter_frequency.QuadPart == 0) + QueryPerformanceFrequency(&performace_counter_frequency); + + return pc * 1000000 / performace_counter_frequency.QuadPart; + } + + inline boost::int64_t microseconds_to_performance_counter(boost::int64_t ms) + { + static LARGE_INTEGER performace_counter_frequency = {0,0}; + if (performace_counter_frequency.QuadPart == 0) + QueryPerformanceFrequency(&performace_counter_frequency); + return ms * performace_counter_frequency.QuadPart / 1000000; + } + } + + inline int total_seconds(time_duration td) + { + return aux::performance_counter_to_microseconds(td.diff) + / 1000000; + } + inline int total_milliseconds(time_duration td) + { + return aux::performance_counter_to_microseconds(td.diff) + / 1000; + } + inline boost::int64_t total_microseconds(time_duration td) + { + return aux::performance_counter_to_microseconds(td.diff); + } + + inline ptime time_now() + { + LARGE_INTEGER now; + QueryPerformanceCounter(&now); + return ptime(now.QuadPart); + } + + inline time_duration microsec(boost::int64_t s) + { + return time_duration(aux::microseconds_to_performance_counter(s)); + } + inline time_duration milliseconds(boost::int64_t s) + { + return time_duration(aux::microseconds_to_performance_counter( + s * 1000)); + } + inline time_duration seconds(boost::int64_t s) + { + return time_duration(aux::microseconds_to_performance_counter( + s * 1000000)); + } + inline time_duration minutes(boost::int64_t s) + { + return time_duration(aux::microseconds_to_performance_counter( + s * 1000000 * 60)); + } + inline time_duration hours(boost::int64_t s) + { + return time_duration(aux::microseconds_to_performance_counter( + s * 1000000 * 60 * 60)); + } + +} + +#endif + +#endif +#endif + diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp index c5b1baf13..6f8660717 100755 --- a/include/libtorrent/torrent.hpp +++ b/include/libtorrent/torrent.hpp @@ -299,7 +299,7 @@ namespace libtorrent // returns the absolute time when the next tracker // announce will take place. - boost::posix_time::ptime next_announce() const; + ptime next_announce() const; // returns true if it is time for this torrent to make another // tracker request @@ -307,7 +307,7 @@ namespace libtorrent // forcefully sets next_announce to the current time void force_tracker_request(); - void force_tracker_request(boost::posix_time::ptime); + void force_tracker_request(ptime); // sets the username and password that will be sent to // the tracker @@ -523,7 +523,7 @@ namespace libtorrent boost::scoped_ptr m_storage; // the time of next tracker request - boost::posix_time::ptime m_next_request; + ptime m_next_request; // ----------------------------- // DATA FROM TRACKER RESPONSE @@ -705,20 +705,18 @@ namespace libtorrent #endif }; - inline boost::posix_time::ptime torrent::next_announce() const + inline ptime torrent::next_announce() const { return m_next_request; } inline void torrent::force_tracker_request() { - using boost::posix_time::second_clock; - m_next_request = second_clock::universal_time(); + m_next_request = time_now(); } - inline void torrent::force_tracker_request(boost::posix_time::ptime t) + inline void torrent::force_tracker_request(ptime t) { - namespace time = boost::posix_time; m_next_request = t; } diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index 337ca7fbd..2a797e0d0 100755 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -49,6 +49,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/peer_info.hpp" #include "libtorrent/piece_picker.hpp" #include "libtorrent/torrent_info.hpp" +#include "libtorrent/time.hpp" #include "libtorrent/config.hpp" namespace libtorrent @@ -288,7 +289,7 @@ namespace libtorrent // This overrides the default announce interval, and no // announce will take place until the given time has // timed out. - void force_reannounce(boost::posix_time::time_duration) const; + void force_reannounce(time_duration) const; // returns the name of this torrent, in case it doesn't // have metadata it returns the name assigned to it diff --git a/include/libtorrent/torrent_info.hpp b/include/libtorrent/torrent_info.hpp index c7eebf7b1..be2c42eef 100755 --- a/include/libtorrent/torrent_info.hpp +++ b/include/libtorrent/torrent_info.hpp @@ -57,9 +57,12 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/size_type.hpp" #include "libtorrent/peer_request.hpp" #include "libtorrent/config.hpp" +#include "libtorrent/time.hpp" namespace libtorrent { + namespace pt = boost::posix_time; + namespace gr = boost::gregorian; struct TORRENT_EXPORT file_entry { @@ -154,8 +157,7 @@ namespace libtorrent return m_piece_hash[index]; } - boost::optional - creation_date() const; + boost::optional creation_date() const; const std::string& creator() const { return m_created_by; } @@ -211,7 +213,7 @@ namespace libtorrent // if a creation date is found in the torrent file // this will be set to that, otherwise it'll be // 1970, Jan 1 - boost::posix_time::ptime m_creation_date; + pt::ptime m_creation_date; // if a comment is found in the torrent file // this will be set to that comment diff --git a/include/libtorrent/tracker_manager.hpp b/include/libtorrent/tracker_manager.hpp index 5bac57e6e..4e38f934d 100755 --- a/include/libtorrent/tracker_manager.hpp +++ b/include/libtorrent/tracker_manager.hpp @@ -61,6 +61,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/peer_id.hpp" #include "libtorrent/peer.hpp" #include "libtorrent/config.hpp" +#include "libtorrent/time.hpp" namespace libtorrent { @@ -177,11 +178,11 @@ namespace libtorrent asio::strand& m_strand; // used for timeouts // this is set when the request has been sent - boost::posix_time::ptime m_start_time; + ptime m_start_time; // this is set every time something is received - boost::posix_time::ptime m_read_time; + ptime m_read_time; // the asio async operation - asio::deadline_timer m_timeout; + deadline_timer m_timeout; int m_completion_timeout; int m_read_timeout; diff --git a/include/libtorrent/upnp.hpp b/include/libtorrent/upnp.hpp index bbfcb3efc..628514d67 100644 --- a/include/libtorrent/upnp.hpp +++ b/include/libtorrent/upnp.hpp @@ -35,7 +35,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/socket.hpp" #include "libtorrent/http_connection.hpp" -#include + #include #include #include @@ -107,7 +107,7 @@ private: {} // the time the port mapping will expire - boost::posix_time::ptime expires; + ptime expires; bool need_update; diff --git a/src/alert.cpp b/src/alert.cpp index 066458cdf..b990db7e1 100755 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -39,7 +39,7 @@ namespace libtorrent { alert::alert(severity_t severity, const std::string& msg) : m_msg(msg) , m_severity(severity) - , m_timestamp(boost::posix_time::second_clock::universal_time()) + , m_timestamp(time_now()) { } @@ -47,7 +47,7 @@ namespace libtorrent { { } - boost::posix_time::ptime alert::timestamp() const + ptime alert::timestamp() const { return m_timestamp; } diff --git a/src/bandwidth_manager.cpp b/src/bandwidth_manager.cpp index f4b73df68..96ca46f03 100644 --- a/src/bandwidth_manager.cpp +++ b/src/bandwidth_manager.cpp @@ -35,6 +35,8 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/invariant_check.hpp" #include "libtorrent/bandwidth_manager.hpp" #include "libtorrent/peer_connection.hpp" +#include "libtorrent/time.hpp" + #if defined TORRENT_LOGGING || defined TORRENT_VERBOSE_LOGGING #include "libtorrent/aux_/session_impl.hpp" #endif @@ -43,11 +45,11 @@ namespace libtorrent { namespace { - const pt::time_duration window_size = pt::seconds(1); + const time_duration window_size = seconds(1); } history_entry::history_entry(intrusive_ptr p - , weak_ptr t, int a, pt::ptime exp) + , weak_ptr t, int a, ptime exp) : expires_at(exp), amount(a), peer(p), tor(t) {} @@ -145,7 +147,7 @@ namespace libtorrent assert(!m_history.empty()); - pt::ptime now(pt::microsec_clock::universal_time()); + ptime now(time_now()); while (!m_history.empty() && m_history.back().expires_at <= now) { history_entry e = m_history.back(); @@ -182,7 +184,7 @@ namespace libtorrent // (*m_ses->m_logger) << "hand out bw [" << m_channel << "]\n"; #endif - pt::ptime now(pt::microsec_clock::universal_time()); + ptime now(time_now()); mutex_t::scoped_lock l(m_mutex); int limit = m_limit; diff --git a/src/bt_peer_connection.cpp b/src/bt_peer_connection.cpp index 75547ee48..af847c976 100755 --- a/src/bt_peer_connection.cpp +++ b/src/bt_peer_connection.cpp @@ -167,7 +167,7 @@ namespace libtorrent INVARIANT_CHECK; #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " ==> DHT_PORT [ " << listen_port << " ]\n"; #endif buffer::interval packet = allocate_send_buffer(7); @@ -302,7 +302,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " ==> HANDSHAKE\n"; #endif setup_send(); @@ -353,7 +353,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== KEEPALIVE\n"; #endif incoming_keepalive(); @@ -826,7 +826,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " ==> BITFIELD "; for (int i = 0; i < (int)get_bitfield().size(); ++i) @@ -863,7 +863,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " ==> EXTENSIONS\n"; #endif assert(m_supports_extensions); diff --git a/src/file_pool.cpp b/src/file_pool.cpp index 2b71fd4c0..6b3d27422 100644 --- a/src/file_pool.cpp +++ b/src/file_pool.cpp @@ -51,7 +51,7 @@ namespace libtorrent if (i != pt.end()) { lru_file_entry e = *i; - e.last_use = pt::second_clock::universal_time(); + e.last_use = time_now(); // if you hit this assert, you probably have more than one // storage/torrent using the same file at the same time! diff --git a/src/http_connection.cpp b/src/http_connection.cpp index 3fad5fbad..4cc656030 100644 --- a/src/http_connection.cpp +++ b/src/http_connection.cpp @@ -30,19 +30,17 @@ POSSIBILITY OF SUCH DAMAGE. */ -#include "http_connection.hpp" -#include +#include "libtorrent/http_connection.hpp" + #include #include -using boost::posix_time::second_clock; -using boost::posix_time::millisec; using boost::bind; namespace libtorrent { -void http_connection::get(std::string const& url, boost::posix_time::time_duration timeout) +void http_connection::get(std::string const& url, time_duration timeout) { std::string protocol; std::string hostname; @@ -59,7 +57,7 @@ void http_connection::get(std::string const& url, boost::posix_time::time_durati } void http_connection::start(std::string const& hostname, std::string const& port - , boost::posix_time::time_duration timeout) + , time_duration timeout) { m_timeout = timeout; m_timer.expires_from_now(m_timeout); @@ -91,7 +89,7 @@ void http_connection::on_timeout(boost::weak_ptr p if (!c) return; if (c->m_bottled && c->m_called) return; - if (c->m_last_receive + c->m_timeout < second_clock::universal_time()) + if (c->m_last_receive + c->m_timeout < time_now()) { c->m_called = true; c->m_handler(asio::error::timed_out, c->m_parser, 0, 0); @@ -131,7 +129,7 @@ void http_connection::on_connect(asio::error_code const& e { if (!e) { - m_last_receive = second_clock::universal_time(); + m_last_receive = time_now(); asio::async_write(m_sock, asio::buffer(sendbuffer) , bind(&http_connection::on_write, shared_from_this(), _1)); } @@ -223,7 +221,7 @@ void http_connection::on_read(asio::error_code const& e m_handler(e, m_parser, &m_recvbuffer[0] + m_parser.body_start() , m_read_pos - m_parser.body_start()); m_read_pos = 0; - m_last_receive = second_clock::universal_time(); + m_last_receive = time_now(); } else if (m_bottled && m_parser.finished()) { @@ -238,7 +236,7 @@ void http_connection::on_read(asio::error_code const& e assert(!m_bottled); m_handler(e, m_parser, &m_recvbuffer[0], m_read_pos); m_read_pos = 0; - m_last_receive = second_clock::universal_time(); + m_last_receive = time_now(); } if (int(m_recvbuffer.size()) == m_read_pos) @@ -287,7 +285,7 @@ void http_connection::on_assign_bandwidth(asio::error_code const& e) , shared_from_this(), _1, _2)); m_limiter_timer_active = true; - m_limiter_timer.expires_from_now(millisec(250)); + m_limiter_timer.expires_from_now(milliseconds(250)); m_limiter_timer.async_wait(bind(&http_connection::on_assign_bandwidth , shared_from_this(), _1)); } @@ -297,7 +295,7 @@ void http_connection::rate_limit(int limit) if (!m_limiter_timer_active) { m_limiter_timer_active = true; - m_limiter_timer.expires_from_now(millisec(250)); + m_limiter_timer.expires_from_now(milliseconds(250)); m_limiter_timer.async_wait(bind(&http_connection::on_assign_bandwidth , shared_from_this(), _1)); } diff --git a/src/kademlia/dht_tracker.cpp b/src/kademlia/dht_tracker.cpp index f5aaf4ffc..501d68630 100644 --- a/src/kademlia/dht_tracker.cpp +++ b/src/kademlia/dht_tracker.cpp @@ -37,8 +37,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include -#include #include #include #include @@ -54,14 +52,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/io.hpp" #include "libtorrent/version.hpp" -using boost::posix_time::ptime; -using boost::posix_time::time_duration; -using boost::posix_time::second_clock; -using boost::posix_time::microsec_clock; -using boost::posix_time::seconds; -using boost::posix_time::minutes; -using boost::posix_time::hours; -using boost::posix_time::milliseconds; using boost::ref; using boost::lexical_cast; using libtorrent::dht::node_impl; @@ -157,7 +147,7 @@ namespace libtorrent { namespace dht , m_dht(bind(&dht_tracker::send_packet, this, _1), settings , read_id(bootstrap)) , m_buffer(0) - , m_last_refresh(second_clock::universal_time() - hours(1)) + , m_last_refresh(time_now() - hours(1)) , m_timer(ios) , m_connection_timer(ios) , m_refresh_timer(ios) @@ -307,6 +297,7 @@ namespace libtorrent { namespace dht { first = false; using boost::posix_time::to_simple_string; + using boost::posix_time::second_clock; pc << "\n\n ***** starting log at " << to_simple_string( second_clock::universal_time()) << " *****\n\n" << "minute:active nodes:passive nodes" diff --git a/src/kademlia/node.cpp b/src/kademlia/node.cpp index 4de002b95..124a7000e 100644 --- a/src/kademlia/node.cpp +++ b/src/kademlia/node.cpp @@ -52,11 +52,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/kademlia/find_data.hpp" using boost::bind; -using boost::posix_time::second_clock; -using boost::posix_time::seconds; -using boost::posix_time::minutes; -using boost::posix_time::ptime; -using boost::posix_time::time_duration; namespace libtorrent { namespace dht { @@ -101,7 +96,7 @@ void purge_peers(std::set& peers) , end(peers.end()); i != end;) { // the peer has timed out - if (i->added + minutes(int(announce_interval * 1.5f)) < second_clock::universal_time()) + if (i->added + minutes(int(announce_interval * 1.5f)) < time_now()) { #ifdef TORRENT_DHT_VERBOSE_LOGGING TORRENT_LOG(node) << "peer timed out at: " << i->addr.address(); @@ -122,7 +117,7 @@ node_impl::node_impl(boost::function const& f , m_table(m_id, 8, settings) , m_rpc(bind(&node_impl::incoming_request, this, _1) , m_id, m_table, f) - , m_last_tracker_tick(boost::posix_time::second_clock::universal_time()) + , m_last_tracker_tick(time_now()) { m_secret[0] = std::rand(); m_secret[1] = std::rand(); @@ -379,7 +374,7 @@ void node_impl::announce(sha1_hash const& info_hash, int listen_port time_duration node_impl::refresh_timeout() { int refresh = -1; - ptime now = second_clock::universal_time(); + ptime now = time_now(); ptime next = now + minutes(15); try { @@ -414,7 +409,7 @@ time_duration node_impl::connection_timeout() time_duration d = m_rpc.tick(); try { - ptime now(second_clock::universal_time()); + ptime now(time_now()); if (now - m_last_tracker_tick < minutes(10)) return d; m_last_tracker_tick = now; @@ -457,7 +452,7 @@ void node_impl::on_announce(msg const& m, msg& reply) torrent_entry& v = m_map[m.info_hash]; peer_entry e; e.addr = tcp::endpoint(m.addr.address(), m.addr.port()); - e.added = second_clock::universal_time(); + e.added = time_now(); std::set::iterator i = v.peers.find(e); if (i != v.peers.end()) v.peers.erase(i++); v.peers.insert(i, e); diff --git a/src/kademlia/routing_table.cpp b/src/kademlia/routing_table.cpp index c09176765..77c7dd826 100644 --- a/src/kademlia/routing_table.cpp +++ b/src/kademlia/routing_table.cpp @@ -39,7 +39,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include #include "libtorrent/kademlia/routing_table.hpp" #include "libtorrent/kademlia/node_id.hpp" @@ -48,13 +47,6 @@ POSSIBILITY OF SUCH DAMAGE. using boost::bind; using boost::uint8_t; -using boost::posix_time::second_clock; -using boost::posix_time::minutes; -using boost::posix_time::seconds; -using boost::posix_time::hours; - -namespace pt = boost::posix_time; - namespace libtorrent { namespace dht { @@ -71,7 +63,7 @@ routing_table::routing_table(node_id const& id, int bucket_size // distribute the refresh times for the buckets in an // attempt do even out the network load for (int i = 0; i < 160; ++i) - m_bucket_activity[i] = second_clock::universal_time() - seconds(15*60 - i*5); + m_bucket_activity[i] = time_now() - seconds(15*60 - i*5); } boost::tuple routing_table::size() const @@ -127,7 +119,6 @@ void routing_table::print_state(std::ostream& os) const { int bucket_index = int(i - m_buckets.begin()); os << "bucket " << bucket_index << " " - << to_simple_string(m_bucket_activity[bucket_index]) << " " << (bucket_index >= m_lowest_active_bucket?"active":"inactive") << "\n"; for (bucket_t::const_iterator j = i->first.begin() @@ -141,17 +132,17 @@ void routing_table::print_state(std::ostream& os) const void routing_table::touch_bucket(int bucket) { - m_bucket_activity[bucket] = second_clock::universal_time(); + m_bucket_activity[bucket] = time_now(); } -boost::posix_time::ptime routing_table::next_refresh(int bucket) +ptime routing_table::next_refresh(int bucket) { assert(bucket < 160); assert(bucket >= 0); // lower than or equal to since a refresh of bucket 0 will // effectively refresh the lowest active bucket as well if (bucket <= m_lowest_active_bucket && bucket > 0) - return second_clock::universal_time() + minutes(15); + return time_now() + minutes(15); return m_bucket_activity[bucket] + minutes(15); } @@ -252,7 +243,7 @@ bool routing_table::node_seen(node_id const& id, udp::endpoint addr) bool ret = need_bootstrap(); - m_bucket_activity[bucket_index] = second_clock::universal_time(); + m_bucket_activity[bucket_index] = time_now(); if (i != b.end()) { diff --git a/src/kademlia/rpc_manager.cpp b/src/kademlia/rpc_manager.cpp index 723bc4e78..8dd60f9bd 100644 --- a/src/kademlia/rpc_manager.cpp +++ b/src/kademlia/rpc_manager.cpp @@ -31,9 +31,8 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "libtorrent/pch.hpp" +#include "libtorrent/socket.hpp" -#include -#include #include #include @@ -45,11 +44,6 @@ POSSIBILITY OF SUCH DAMAGE. #include -using boost::posix_time::ptime; -using boost::posix_time::time_duration; -using boost::posix_time::microsec_clock; -using boost::posix_time::seconds; -using boost::posix_time::milliseconds; using boost::shared_ptr; using boost::bind; @@ -72,7 +66,7 @@ rpc_manager::rpc_manager(fun const& f, node_id const& our_id , m_send(sf) , m_our_id(our_id) , m_table(table) - , m_timer(boost::posix_time::microsec_clock::universal_time()) + , m_timer(time_now()) , m_random_number(generate_id()) , m_destructing(false) { @@ -201,8 +195,6 @@ time_duration rpc_manager::tick() { INVARIANT_CHECK; - using boost::posix_time::microsec_clock; - const int timeout_ms = 10 * 1000; // look for observers that has timed out @@ -220,8 +212,7 @@ time_duration rpc_manager::tick() boost::shared_ptr o = m_transactions[m_oldest_transaction_id]; if (!o) continue; - time_duration diff = o->sent + milliseconds(timeout_ms) - - microsec_clock::universal_time(); + time_duration diff = o->sent + milliseconds(timeout_ms) - time_now(); if (diff > seconds(0)) { if (diff < seconds(1)) return seconds(1); @@ -317,7 +308,7 @@ void rpc_manager::invoke(int message_id, udp::endpoint target_addr o->send(m); - o->sent = boost::posix_time::microsec_clock::universal_time(); + o->sent = time_now(); o->target_addr = target_addr; #ifdef TORRENT_DHT_VERBOSE_LOGGING @@ -385,7 +376,7 @@ void rpc_manager::reply_with_ping(msg& m, msg const& reply_to) boost::shared_ptr o(new dummy_observer); assert(!m_transactions[m_next_transaction_id]); - o->sent = boost::posix_time::microsec_clock::universal_time(); + o->sent = time_now(); o->target_addr = m.addr; m_send(m); diff --git a/src/logger.cpp b/src/logger.cpp index c8f6f7a74..62f581500 100644 --- a/src/logger.cpp +++ b/src/logger.cpp @@ -73,7 +73,7 @@ namespace libtorrent { namespace void log_timestamp() { using namespace boost::posix_time; - std::string now(to_simple_string(second_clock::universal_time())); + std::string now(to_simple_string(second_clock::local_time())); m_file << now << ": "; } diff --git a/src/metadata_transfer.cpp b/src/metadata_transfer.cpp index 12743d93e..42a5d2a03 100644 --- a/src/metadata_transfer.cpp +++ b/src/metadata_transfer.cpp @@ -40,7 +40,6 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include #ifdef _MSC_VER #pragma warning(pop) @@ -58,8 +57,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/extensions.hpp" #include "libtorrent/extensions/metadata_transfer.hpp" -using boost::posix_time::second_clock; - namespace libtorrent { namespace { int div_round_up(int numerator, int denominator) @@ -245,12 +242,8 @@ namespace libtorrent { namespace : m_waiting_metadata_request(false) , m_message_index(0) , m_metadata_progress(0) - , m_no_metadata( - boost::gregorian::date(1970, boost::date_time::Jan, 1) - , boost::posix_time::seconds(0)) - , m_metadata_request( - boost::gregorian::date(1970, boost::date_time::Jan, 1) - , boost::posix_time::seconds(0)) + , m_no_metadata(min_time()) + , m_metadata_request(min_time()) , m_torrent(t) , m_pc(pc) , m_tp(tp) @@ -413,7 +406,7 @@ namespace libtorrent { namespace } break; case 2: // have no data - m_no_metadata = second_clock::universal_time(); + m_no_metadata = time_now(); if (m_waiting_metadata_request) m_tp.cancel_metadata_request(m_last_metadata_request); m_waiting_metadata_request = false; @@ -439,14 +432,13 @@ namespace libtorrent { namespace m_last_metadata_request = m_tp.metadata_request(); write_metadata_request(m_last_metadata_request); m_waiting_metadata_request = true; - m_metadata_request = second_clock::universal_time(); + m_metadata_request = time_now(); } } bool has_metadata() const { - using namespace boost::posix_time; - return second_clock::universal_time() - m_no_metadata > minutes(5); + return time_now() - m_no_metadata > minutes(5); } private: @@ -469,11 +461,11 @@ namespace libtorrent { namespace // this is set to the current time each time we get a // "I don't have metadata" message. - boost::posix_time::ptime m_no_metadata; + ptime m_no_metadata; // this is set to the time when we last sent // a request for metadata to this peer - boost::posix_time::ptime m_metadata_request; + ptime m_metadata_request; // if we're waiting for a metadata request // this was the request we sent diff --git a/src/natpmp.cpp b/src/natpmp.cpp index 7c0ae99f9..3be47aece 100644 --- a/src/natpmp.cpp +++ b/src/natpmp.cpp @@ -35,12 +35,13 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include #include +#include + +using boost::posix_time::microsec_clock; using boost::bind; using namespace libtorrent; -using boost::posix_time::microsec_clock; enum { num_mappings = 2 }; @@ -169,7 +170,6 @@ void natpmp::update_mapping(int i, int port) void natpmp::send_map_request(int i) try { using namespace libtorrent::detail; - using boost::posix_time::milliseconds; assert(m_currently_mapping == -1 || m_currently_mapping == i); @@ -205,15 +205,13 @@ catch (std::exception& e) void natpmp::resend_request(int i, asio::error_code const& e) { - using boost::posix_time::hours; if (e) return; if (m_currently_mapping != i) return; if (m_retry_count >= 9) { m_mappings[i].need_update = false; // try again in two hours - m_mappings[i].expires - = boost::posix_time::second_clock::universal_time() + hours(2); + m_mappings[i].expires = time_now() + hours(2); return; } send_map_request(i); @@ -223,7 +221,6 @@ void natpmp::on_reply(asio::error_code const& e , std::size_t bytes_transferred) { using namespace libtorrent::detail; - using boost::posix_time::seconds; if (e) return; try @@ -289,8 +286,7 @@ void natpmp::on_reply(asio::error_code const& e } else { - m.expires = boost::posix_time::second_clock::universal_time() - + seconds(int(lifetime * 0.7f)); + m.expires = time_now() + seconds(int(lifetime * 0.7f)); m.external_port = public_port; } @@ -324,10 +320,8 @@ void natpmp::on_reply(asio::error_code const& e } catch (std::exception& e) { - using boost::posix_time::hours; // try again in two hours - m_mappings[m_currently_mapping].expires - = boost::posix_time::second_clock::universal_time() + hours(2); + m_mappings[m_currently_mapping].expires = time_now() + hours(2); m_callback(0, 0, e.what()); } int i = m_currently_mapping; @@ -340,9 +334,8 @@ void natpmp::on_reply(asio::error_code const& e void natpmp::update_expiration_timer() { - using boost::posix_time::seconds; - boost::posix_time::ptime now = boost::posix_time::second_clock::universal_time(); - boost::posix_time::ptime min_expire = now + seconds(3600); + ptime now = time_now(); + ptime min_expire = now + seconds(3600); int min_index = -1; for (int i = 0; i < num_mappings; ++i) if (m_mappings[i].expires < min_expire diff --git a/src/peer_connection.cpp b/src/peer_connection.cpp index 56f62e8ba..1688a6fa4 100755 --- a/src/peer_connection.cpp +++ b/src/peer_connection.cpp @@ -81,20 +81,19 @@ namespace libtorrent , tcp::endpoint const& proxy) : #ifndef NDEBUG - m_last_choke(boost::posix_time::second_clock::universal_time() - - hours(1)) + m_last_choke(time_now() - hours(1)) , #endif m_ses(ses) , m_max_out_request_queue(m_ses.settings().max_out_request_queue) , m_timeout(m_ses.settings().peer_timeout) - , m_last_piece(second_clock::universal_time()) + , m_last_piece(time_now()) , m_packet_size(0) , m_recv_pos(0) , m_current_send_buffer(0) , m_write_pos(0) - , m_last_receive(second_clock::universal_time()) - , m_last_sent(second_clock::universal_time()) + , m_last_receive(time_now()) + , m_last_sent(time_now()) , m_socket(s) , m_remote(remote) , m_remote_proxy(proxy) @@ -112,8 +111,8 @@ namespace libtorrent , m_assume_fifo(false) , m_num_invalid_requests(0) , m_disconnecting(false) - , m_became_uninterested(second_clock::universal_time()) - , m_became_uninteresting(second_clock::universal_time()) + , m_became_uninterested(time_now()) + , m_became_uninteresting(time_now()) , m_connecting(true) , m_queued(true) , m_writing(false) @@ -148,20 +147,19 @@ namespace libtorrent , boost::shared_ptr s) : #ifndef NDEBUG - m_last_choke(boost::posix_time::second_clock::universal_time() - - hours(1)) + m_last_choke(time_now() - hours(1)) , #endif m_ses(ses) , m_max_out_request_queue(m_ses.settings().max_out_request_queue) , m_timeout(m_ses.settings().peer_timeout) - , m_last_piece(second_clock::universal_time()) + , m_last_piece(time_now()) , m_packet_size(0) , m_recv_pos(0) , m_current_send_buffer(0) , m_write_pos(0) - , m_last_receive(second_clock::universal_time()) - , m_last_sent(second_clock::universal_time()) + , m_last_receive(time_now()) + , m_last_sent(time_now()) , m_socket(s) , m_active(false) , m_peer_interested(false) @@ -176,8 +174,8 @@ namespace libtorrent , m_assume_fifo(false) , m_num_invalid_requests(0) , m_disconnecting(false) - , m_became_uninterested(second_clock::universal_time()) - , m_became_uninteresting(second_clock::universal_time()) + , m_became_uninterested(time_now()) + , m_became_uninteresting(time_now()) , m_connecting(false) , m_queued(false) , m_writing(false) @@ -297,7 +295,7 @@ namespace libtorrent using namespace boost::posix_time; if (m_logger) { - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " *** CONNECTION CLOSED\n"; } #endif @@ -315,7 +313,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " ==> HAVE [ piece: " << index << "]\n"; #endif write_have(index); @@ -516,7 +514,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== KEEPALIVE\n"; #endif } @@ -542,7 +540,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== CHOKE\n"; #endif m_peer_choked = true; @@ -592,7 +590,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== UNCHOKE\n"; #endif m_peer_choked = false; @@ -620,7 +618,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== INTERESTED\n"; #endif m_peer_interested = true; @@ -643,7 +641,7 @@ namespace libtorrent } #endif - m_became_uninterested = second_clock::universal_time(); + m_became_uninterested = time_now(); // clear the request queue if the client isn't interested m_requests.clear(); @@ -651,7 +649,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== NOT_INTERESTED\n"; #endif @@ -683,7 +681,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== HAVE [ piece: " << index << "]\n"; #endif @@ -750,7 +748,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== BITFIELD "; for (int i = 0; i < int(bitfield.size()); ++i) @@ -846,7 +844,7 @@ namespace libtorrent // we shouldn't get a request #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== UNEXPECTED_REQUEST [ " "piece: " << r.piece << " | " "s: " << r.start << " | " @@ -866,7 +864,7 @@ namespace libtorrent // is making too many of them. #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== TOO MANY REQUESTS [ " "piece: " << r.piece << " | " "s: " << r.start << " | " @@ -892,7 +890,7 @@ namespace libtorrent { #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== REQUEST [ piece: " << r.piece << " | s: " << r.start << " | l: " << r.length << " ]\n"; #endif // if we have choked the client @@ -907,7 +905,7 @@ namespace libtorrent { #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== INVALID_REQUEST [ " "piece: " << r.piece << " | " "s: " << r.start << " | " @@ -934,7 +932,7 @@ namespace libtorrent void peer_connection::incoming_piece_fragment() { - m_last_piece = second_clock::universal_time(); + m_last_piece = time_now(); } #ifndef NDEBUG @@ -993,7 +991,7 @@ namespace libtorrent #endif #ifdef TORRENT_VERBOSE_LOGGING - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== PIECE [ piece: " << p.piece << " | " "s: " << p.start << " | " "l: " << p.length << " | " @@ -1005,7 +1003,7 @@ namespace libtorrent { #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== INVALID_PIECE [ piece: " << p.piece << " | " "start: " << p.start << " | " "length: " << p.length << " ]\n"; @@ -1051,7 +1049,7 @@ namespace libtorrent i != b; ++i) { #ifdef TORRENT_VERBOSE_LOGGING - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " *** SKIPPED_PIECE [ piece: " << i->piece_index << " | " "b: " << i->block_index << " ] ***\n"; #endif @@ -1248,7 +1246,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== CANCEL [ piece: " << r.piece << " | s: " << r.start << " | l: " << r.length << " ]\n"; #endif @@ -1263,7 +1261,7 @@ namespace libtorrent { #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " *** GOT CANCEL NOT IN THE QUEUE\n"; #endif } @@ -1279,7 +1277,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " <== DHT_PORT [ p: " << listen_port << " ]\n"; #endif #ifndef TORRENT_DISABLE_DHT @@ -1356,7 +1354,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " ==> CANCEL [ piece: " << block.piece_index << " | s: " << block_offset << " | l: " << block_size << " | " << block.block_index << " ]\n"; #endif @@ -1372,12 +1370,12 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " ==> CHOKE\n"; #endif #ifndef NDEBUG using namespace boost::posix_time; - m_last_choke = second_clock::universal_time(); + m_last_choke = time_now(); #endif m_num_invalid_requests = 0; m_requests.clear(); @@ -1393,7 +1391,7 @@ namespace libtorrent // this condition cannot be guaranteed since if peers disconnect // a new one will be unchoked ignoring when it was last choked using namespace boost::posix_time; - //assert(second_clock::universal_time() - m_last_choke > seconds(9)); + //assert(time_now() - m_last_choke > seconds(9)); #endif if (!m_choked) return; @@ -1402,7 +1400,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " ==> UNCHOKE\n"; #endif } @@ -1417,7 +1415,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " ==> INTERESTED\n"; #endif } @@ -1430,11 +1428,11 @@ namespace libtorrent write_not_interested(); m_interesting = false; - m_became_uninteresting = second_clock::universal_time(); + m_became_uninteresting = time_now(); #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " ==> NOT_INTERESTED\n"; #endif } @@ -1472,7 +1470,7 @@ namespace libtorrent m_download_queue.push_back(block); /* #ifdef TORRENT_VERBOSE_LOGGING - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " *** REQUEST-QUEUE** [ " "piece: " << block.piece_index << " | " "block: " << block.block_index << " ]\n"; @@ -1491,7 +1489,7 @@ namespace libtorrent m_download_queue.push_back(block); /* #ifdef TORRENT_VERBOSE_LOGGING - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " *** REQUEST-QUEUE** [ " "piece: " << block.piece_index << " | " "block: " << block.block_index << " ]\n"; @@ -1524,7 +1522,7 @@ namespace libtorrent using namespace boost::posix_time; #ifdef TORRENT_VERBOSE_LOGGING - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " ==> REQUEST [ " "piece: " << r.piece << " | " "s: " << r.start << " | " @@ -1533,7 +1531,7 @@ namespace libtorrent "qs: " << m_desired_queue_size << " ]\n"; #endif } - m_last_piece = second_clock::universal_time(); + m_last_piece = time_now(); } @@ -1642,7 +1640,7 @@ namespace libtorrent { INVARIANT_CHECK; - ptime now(second_clock::universal_time()); + ptime now(time_now()); boost::shared_ptr t = m_torrent.lock(); assert(t); @@ -1688,9 +1686,9 @@ namespace libtorrent // in this case we'll clear our download queue and // re-request the blocks. #ifdef TORRENT_VERBOSE_LOGGING - (*m_logger) << to_simple_string(now) + (*m_logger) << to_simple_string(second_clock::local_time()) << " *** PIECE_REQUESTS TIMED OUT [ " << (int)m_download_queue.size() - << " " << to_simple_string(now - m_last_piece) << "] ***\n"; + << " " << total_seconds(now - m_last_piece) << "] ***\n"; #endif if (t->is_seed()) @@ -1829,7 +1827,7 @@ namespace libtorrent #ifdef TORRENT_VERBOSE_LOGGING using namespace boost::posix_time; - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << " ==> PIECE [ piece: " << r.piece << " | s: " << r.start << " | l: " << r.length << " ]\n"; #endif @@ -2070,7 +2068,7 @@ namespace libtorrent assert(m_packet_size > 0); assert(bytes_transferred > 0); - m_last_receive = second_clock::universal_time(); + m_last_receive = time_now(); m_recv_pos += bytes_transferred; assert(m_recv_pos <= int(m_recv_buffer.size())); @@ -2185,7 +2183,7 @@ namespace libtorrent } if (m_disconnecting) return; - m_last_receive = second_clock::universal_time(); + m_last_receive = time_now(); // this means the connection just succeeded @@ -2251,7 +2249,7 @@ namespace libtorrent m_write_pos = 0; } - m_last_sent = second_clock::universal_time(); + m_last_sent = time_now(); on_sent(error, bytes_transferred); fill_send_buffer(); @@ -2352,7 +2350,7 @@ namespace libtorrent using namespace boost::posix_time; - ptime now(second_clock::universal_time()); + ptime now(time_now()); // if the socket is still connecting, don't // consider it timed out. Because Windows XP SP2 @@ -2362,7 +2360,7 @@ namespace libtorrent // if the peer hasn't said a thing for a certain // time, it is considered to have timed out time_duration d; - d = second_clock::universal_time() - m_last_receive; + d = time_now() - m_last_receive; if (d > seconds(m_timeout)) return true; // if the peer hasn't become interested and we haven't @@ -2389,9 +2387,9 @@ namespace libtorrent { INVARIANT_CHECK; - boost::posix_time::time_duration d; - d = second_clock::universal_time() - m_last_sent; - if (d.total_seconds() < m_timeout / 2) return; + time_duration d; + d = time_now() - m_last_sent; + if (total_seconds(d) < m_timeout / 2) return; if (m_connecting) return; diff --git a/src/policy.cpp b/src/policy.cpp index c7e27b04e..2b04b0e09 100755 --- a/src/policy.cpp +++ b/src/policy.cpp @@ -38,7 +38,6 @@ POSSIBILITY OF SUCH DAMAGE. #pragma warning(push, 1) #endif -#include #include #ifdef _MSC_VER @@ -52,6 +51,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/socket.hpp" #include "libtorrent/alert_types.hpp" #include "libtorrent/invariant_check.hpp" +#include "libtorrent/time.hpp" #include "libtorrent/aux_/session_impl.hpp" namespace libtorrent @@ -59,7 +59,6 @@ namespace libtorrent class peer_connection; } -using namespace boost::posix_time; using boost::bind; namespace @@ -376,7 +375,7 @@ namespace libtorrent : m_torrent(t) , m_num_unchoked(0) , m_available_free_upload(0) - , m_last_optimistic_disconnect(boost::gregorian::date(1970,boost::gregorian::Jan,1)) + , m_last_optimistic_disconnect(min_time()) { assert(t); } // finds the peer that has the worst download rate // and returns it. May return 0 if all peers are @@ -435,11 +434,8 @@ namespace libtorrent if (m_num_unchoked == m_torrent->num_peers()) return 0; - using namespace boost::posix_time; - using namespace boost::gregorian; - peer* unchoke_peer = 0; - ptime min_time(date(9999,Jan,1)); + ptime min_time = libtorrent::min_time(); float max_down_speed = 0.f; // TODO: make this selection better @@ -468,8 +464,7 @@ namespace libtorrent peer *disconnect_peer = 0; double slowest_transfer_rate = std::numeric_limits::max(); - boost::posix_time::ptime local_time - = second_clock::universal_time(); + ptime local_time = time_now(); for (std::vector::iterator i = m_peers.begin(); i != m_peers.end(); ++i) @@ -483,13 +478,11 @@ namespace libtorrent double transferred_amount = (double)c->statistics().total_payload_download(); - boost::posix_time::time_duration connected_time + time_duration connected_time = local_time - i->connected; double connected_time_in_seconds - = connected_time.seconds() - + connected_time.minutes()*60.0 - + connected_time.hours()*60.0*60.0; + = total_seconds(connected_time); double transfer_rate = transferred_amount / (connected_time_in_seconds+1); @@ -505,8 +498,8 @@ namespace libtorrent policy::peer *policy::find_connect_candidate() { - boost::posix_time::ptime local_time = second_clock::universal_time(); - boost::posix_time::ptime ptime(local_time); + ptime now = time_now(); + ptime ptime(now); policy::peer* candidate = 0; for (std::vector::iterator i = m_peers.begin(); @@ -517,9 +510,9 @@ namespace libtorrent if (i->type == peer::not_connectable) continue; if (i->seed && m_torrent->is_seed()) continue; - assert(i->connected <= local_time); + assert(i->connected <= now); - boost::posix_time::ptime next_connect = i->connected; + libtorrent::ptime next_connect = i->connected; if (next_connect <= ptime) { @@ -528,7 +521,7 @@ namespace libtorrent } } - assert(ptime <= local_time); + assert(ptime <= now); return candidate; } @@ -541,13 +534,10 @@ namespace libtorrent // first choice candidate. // it is a candidate we owe nothing to and which has been unchoked // the longest. - using namespace boost::posix_time; - using namespace boost::gregorian; - peer* candidate = 0; // not valid when candidate == 0 - ptime last_unchoke = ptime(date(1970, Jan, 1)); + ptime last_unchoke = min_time(); // second choice candidate. // if there is no first choice candidate, this candidate will be chosen. @@ -596,8 +586,7 @@ namespace libtorrent INVARIANT_CHECK; peer* candidate = 0; - boost::posix_time::ptime last_unchoke - = second_clock::universal_time(); + ptime last_unchoke = time_now(); for (std::vector::iterator i = m_peers.begin(); i != m_peers.end(); ++i) @@ -623,8 +612,7 @@ namespace libtorrent { assert(p->connection->is_choked()); p->connection->send_unchoke(); - p->last_optimistically_unchoked - = second_clock::universal_time(); + p->last_optimistically_unchoked = time_now(); ++m_num_unchoked; } return p != 0; @@ -649,8 +637,6 @@ namespace libtorrent if (m_torrent->is_paused()) return; - using namespace boost::posix_time; - // remove old disconnected peers from the list m_peers.erase( std::remove_if(m_peers.begin() @@ -682,8 +668,8 @@ namespace libtorrent { // every minute, disconnect the worst peer in hope of finding a better peer - boost::posix_time::ptime local_time = second_clock::universal_time(); - if (m_last_optimistic_disconnect + boost::posix_time::seconds(120) <= local_time + ptime local_time = time_now(); + if (m_last_optimistic_disconnect + seconds(120) <= local_time && find_connect_candidate()) { m_last_optimistic_disconnect = local_time; @@ -693,7 +679,7 @@ namespace libtorrent else { // don't do a disconnect earlier than 1 minute after some peer was connected - m_last_optimistic_disconnect = second_clock::universal_time(); + m_last_optimistic_disconnect = time_now(); } while (num_connected_peers > max_connections) @@ -968,9 +954,6 @@ namespace libtorrent } else { - using namespace boost::posix_time; - using namespace boost::gregorian; - // we don't have ny info about this peer. // add a new entry assert((c.proxy() == tcp::endpoint() && c.remote() == c.get_socket()->remote_endpoint()) @@ -987,8 +970,8 @@ namespace libtorrent i->prev_amount_upload = 0; i->connection = &c; assert(i->connection); - i->connected = second_clock::universal_time(); - m_last_optimistic_disconnect = second_clock::universal_time(); + i->connected = time_now(); + m_last_optimistic_disconnect = time_now(); } void policy::peer_from_tracker(const tcp::endpoint& remote, const peer_id& pid @@ -1020,9 +1003,6 @@ namespace libtorrent if (i == m_peers.end()) { - using namespace boost::posix_time; - using namespace boost::gregorian; - // we don't have any info about this peer. // add a new entry peer p(remote, peer::connectable); @@ -1206,7 +1186,7 @@ namespace libtorrent assert(p->connection->is_choked()); p->connection->send_unchoke(); - p->last_optimistically_unchoked = second_clock::universal_time(); + p->last_optimistically_unchoked = time_now(); ++m_num_unchoked; return true; } @@ -1248,7 +1228,7 @@ namespace libtorrent p->prev_amount_upload = 0; p->connected = m_last_optimistic_disconnect = - second_clock::universal_time(); + time_now(); return true; } catch (std::exception& e) @@ -1287,7 +1267,7 @@ namespace libtorrent if (i == m_peers.end()) return; assert(i->connection == &c); - i->connected = second_clock::universal_time(); + i->connected = time_now(); if (!i->connection->is_choked() && !m_torrent->is_aborted()) { unchoked = true; @@ -1296,7 +1276,7 @@ namespace libtorrent if (c.failed()) { if (++i->failcount > 3) erase = true; - i->connected = second_clock::universal_time(); + i->connected = time_now(); } // if the share ratio is 0 (infinite), the @@ -1414,15 +1394,14 @@ namespace libtorrent , type(t) , failcount(0) , seed(false) - , last_optimistically_unchoked( - boost::gregorian::date(1970,boost::gregorian::Jan,1)) - , connected(boost::gregorian::date(1970,boost::gregorian::Jan,1)) + , last_optimistically_unchoked(min_time()) + , connected(min_time()) , prev_amount_upload(0) , prev_amount_download(0) , banned(false) , connection(0) { - assert(connected < second_clock::universal_time()); + assert(connected < time_now()); } size_type policy::peer::total_download() const diff --git a/src/session_impl.cpp b/src/session_impl.cpp index c967313d5..3157c1db5 100755 --- a/src/session_impl.cpp +++ b/src/session_impl.cpp @@ -489,7 +489,7 @@ namespace libtorrent { namespace detail , m_half_open_limit(-1) , m_incoming_connection(false) , m_files(40) - , m_last_tick(microsec_clock::universal_time()) + , m_last_tick(time_now()) #ifndef TORRENT_DISABLE_DHT , m_dht_same_port(true) , m_external_udp_port(0) @@ -509,7 +509,7 @@ namespace libtorrent { namespace detail m_logger = create_log("main_session", listen_port(), false); using boost::posix_time::second_clock; using boost::posix_time::to_simple_string; - (*m_logger) << to_simple_string(second_clock::universal_time()) << "\n"; + (*m_logger) << to_simple_string(second_clock::local_time()) << "\n"; m_stats_logger = create_log("session_stats", listen_port(), false); (*m_stats_logger) << @@ -920,9 +920,8 @@ namespace libtorrent { namespace detail } if (m_abort) return; - float tick_interval = (microsec_clock::universal_time() - - m_last_tick).total_milliseconds() / 1000.f; - m_last_tick = microsec_clock::universal_time(); + float tick_interval = total_microseconds(time_now() - m_last_tick) / 1000000.f; + m_last_tick = time_now(); m_timer.expires_from_now(seconds(1)); m_timer.async_wait(m_strand.wrap( @@ -1061,7 +1060,7 @@ namespace libtorrent { namespace detail open_listen_port(); } - boost::posix_time::ptime timer = second_clock::universal_time(); + ptime timer = time_now(); do { @@ -1115,14 +1114,14 @@ namespace libtorrent { namespace detail } } - ptime start(microsec_clock::universal_time()); + ptime start(time_now()); l.unlock(); - while (microsec_clock::universal_time() - start < seconds( + while (time_now() - start < seconds( m_settings.stop_tracker_timeout) && !m_tracker_manager.empty()) { - tracker_timer.expires_from_now(boost::posix_time::milliseconds(100)); + tracker_timer.expires_from_now(milliseconds(100)); tracker_timer.async_wait(m_strand.wrap( bind(&io_service::stop, &m_io_service))); @@ -1491,7 +1490,7 @@ namespace libtorrent { namespace detail m_logger = create_log("main_session", listen_port(), false); using boost::posix_time::second_clock; using boost::posix_time::to_simple_string; - (*m_logger) << to_simple_string(second_clock::universal_time()) << "\n"; + (*m_logger) << to_simple_string(second_clock::local_time()) << "\n"; #endif return m_listen_socket; @@ -1517,7 +1516,7 @@ namespace libtorrent { namespace detail if (!t) return; #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) - (*m_logger) << to_simple_string(second_clock::universal_time()) + (*m_logger) << to_simple_string(second_clock::local_time()) << ": added peer from local discovery: " << peer << "\n"; #endif t->get_policy().peer_from_tracker(peer, peer_id(0)); diff --git a/src/torrent.cpp b/src/torrent.cpp index f48c8d8c5..11e5aa6c8 100755 --- a/src/torrent.cpp +++ b/src/torrent.cpp @@ -91,8 +91,8 @@ namespace libtorrent { namespace { - using boost::posix_time::ptime; - using boost::posix_time::time_duration; + using ptime; + using time_duration; using boost::posix_time::microsec_clock; std::vector > checkpoints; } @@ -238,7 +238,7 @@ namespace libtorrent , m_event(tracker_request::started) , m_block_size(0) , m_storage(0) - , m_next_request(second_clock::universal_time()) + , m_next_request(time_now()) , m_duration(1800) , m_complete(-1) , m_incomplete(-1) @@ -322,7 +322,7 @@ namespace libtorrent , m_event(tracker_request::started) , m_block_size(0) , m_storage(0) - , m_next_request(second_clock::universal_time()) + , m_next_request(time_now()) , m_duration(1800) , m_complete(-1) , m_incomplete(-1) @@ -468,7 +468,7 @@ namespace libtorrent { if (e) return; - m_announce_timer.expires_from_now(boost::posix_time::minutes(30)); + m_announce_timer.expires_from_now(minutes(30)); m_announce_timer.async_wait(m_ses.m_strand.wrap( bind(&torrent::on_announce, this, _1))); @@ -528,8 +528,7 @@ namespace libtorrent m_just_paused = false; return true; } - return !m_paused && - m_next_request < second_clock::universal_time(); + return !m_paused && m_next_request < time_now(); } void torrent::tracker_warning(std::string const& msg) @@ -565,7 +564,10 @@ namespace libtorrent m_currently_trying_tracker = 0; m_duration = interval; - m_next_request = second_clock::universal_time() + boost::posix_time::seconds(m_duration); + m_next_request = time_now() + seconds(m_duration); + std::cerr << "next announce: " << next_announce().time + << " time: " << time_now().time + << " duration: " << libtorrent::seconds(m_duration).diff << std::endl; if (complete >= 0) m_complete = complete; if (incomplete >= 0) m_incomplete = incomplete; @@ -1264,9 +1266,7 @@ namespace libtorrent assert(!m_trackers.empty()); - m_next_request - = second_clock::universal_time() - + boost::posix_time::seconds(tracker_retry_delay_max); + m_next_request = time_now() + seconds(tracker_retry_delay_max); tracker_request req; req.info_hash = m_torrent_file.info_hash(); @@ -1335,7 +1335,7 @@ namespace libtorrent INVARIANT_CHECK; #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) - std::string now(to_simple_string(second_clock::universal_time())); + std::string now(to_simple_string(second_clock::local_time())); (*m_ses.m_logger) << now << " resolving: " << url << "\n"; #endif @@ -1373,7 +1373,7 @@ namespace libtorrent INVARIANT_CHECK; #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) - std::string now(to_simple_string(second_clock::universal_time())); + std::string now(to_simple_string(second_clock::local_time())); (*m_ses.m_logger) << now << " completed resolve proxy hostname for: " << url << "\n"; #endif @@ -1427,7 +1427,7 @@ namespace libtorrent INVARIANT_CHECK; #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) - std::string now(to_simple_string(second_clock::universal_time())); + std::string now(to_simple_string(second_clock::local_time())); (*m_ses.m_logger) << now << " completed resolve: " << url << "\n"; #endif @@ -2158,13 +2158,13 @@ namespace libtorrent ++m_failed_trackers; // if we've looped the tracker list, wait a bit before retrying m_currently_trying_tracker = 0; - m_next_request = second_clock::universal_time() + seconds(delay); + m_next_request = time_now() + seconds(delay); #ifndef TORRENT_DISABLE_DHT // only start the announce if we want to announce with the dht if (should_announce_dht()) { - m_announce_timer.expires_from_now(boost::posix_time::seconds(1)); + m_announce_timer.expires_from_now(seconds(1)); m_announce_timer.async_wait(m_ses.m_strand.wrap( bind(&torrent::on_announce, this, _1))); } @@ -2174,7 +2174,7 @@ namespace libtorrent else { // don't delay before trying the next tracker - m_next_request = second_clock::universal_time(); + m_next_request = time_now(); } } @@ -2712,10 +2712,11 @@ namespace libtorrent st.download_payload_rate = m_stat.download_payload_rate(); st.upload_payload_rate = m_stat.upload_payload_rate(); - st.next_announce = next_announce() - - second_clock::universal_time(); - if (st.next_announce.is_negative()) st.next_announce - = boost::posix_time::seconds(0); + st.next_announce = boost::posix_time::seconds( + total_seconds(next_announce() - time_now())); + if (st.next_announce.is_negative()) + st.next_announce = boost::posix_time::seconds(0); + st.announce_interval = boost::posix_time::seconds(m_duration); if (m_last_working_tracker >= 0) diff --git a/src/torrent_handle.cpp b/src/torrent_handle.cpp index 03034011f..97d58d154 100755 --- a/src/torrent_handle.cpp +++ b/src/torrent_handle.cpp @@ -640,7 +640,7 @@ namespace libtorrent } void torrent_handle::force_reannounce( - boost::posix_time::time_duration duration) const + time_duration duration) const { INVARIANT_CHECK; @@ -651,7 +651,7 @@ namespace libtorrent if (!t) throw_invalid_handle(); using boost::posix_time::second_clock; - t->force_tracker_request(second_clock::universal_time() + t->force_tracker_request(time_now() + duration); } diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index 7aacdc355..8f419091d 100755 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -59,6 +59,9 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/hasher.hpp" #include "libtorrent/entry.hpp" +namespace pt = boost::posix_time; +namespace gr = boost::gregorian; + using namespace libtorrent; using namespace boost::filesystem; @@ -220,7 +223,7 @@ namespace libtorrent // standard constructor that parses a torrent file torrent_info::torrent_info(const entry& torrent_file) - : m_creation_date(date(not_a_date_time)) + : m_creation_date(pt::ptime(pt::not_a_date_time)) , m_multifile(false) , m_private(false) , m_extra_info(entry::dictionary_t) @@ -244,7 +247,7 @@ namespace libtorrent , m_total_size(0) , m_info_hash(info_hash) , m_name() - , m_creation_date(second_clock::universal_time()) + , m_creation_date(pt::second_clock::universal_time()) , m_multifile(false) , m_private(false) , m_extra_info(entry::dictionary_t) @@ -256,7 +259,7 @@ namespace libtorrent , m_total_size(0) , m_info_hash(0) , m_name() - , m_creation_date(second_clock::universal_time()) + , m_creation_date(pt::second_clock::universal_time()) , m_multifile(false) , m_private(false) , m_extra_info(entry::dictionary_t) @@ -455,8 +458,8 @@ namespace libtorrent // extract creation date try { - m_creation_date = ptime(date(1970, Jan, 1)) - + seconds(long(torrent_file["creation date"].integer())); + m_creation_date = pt::ptime(gr::date(1970, gr::Jan, 1)) + + pt::seconds(long(torrent_file["creation date"].integer())); } catch (type_error) {} @@ -494,14 +497,14 @@ namespace libtorrent parse_info_section(torrent_file["info"]); } - boost::optional + boost::optional torrent_info::creation_date() const { - if (m_creation_date != ptime(date(not_a_date_time))) + if (m_creation_date != pt::ptime(gr::date(pt::not_a_date_time))) { - return boost::optional(m_creation_date); + return boost::optional(m_creation_date); } - return boost::optional(); + return boost::optional(); } void torrent_info::add_tracker(std::string const& url, int tier) @@ -696,7 +699,7 @@ namespace libtorrent dict["comment"] = m_comment; dict["creation date"] = - (m_creation_date - ptime(date(1970, Jan, 1))).total_seconds(); + (m_creation_date - pt::ptime(gr::date(1970, gr::Jan, 1))).total_seconds(); if (!m_created_by.empty()) dict["created by"] = m_created_by; @@ -750,7 +753,7 @@ namespace libtorrent } if (!m_comment.empty()) os << "comment: " << m_comment << "\n"; - if (m_creation_date != ptime(date(not_a_date_time))) + if (m_creation_date != pt::ptime(gr::date(pt::not_a_date_time))) os << "creation date: " << to_simple_string(m_creation_date) << "\n"; os << "private: " << (m_private?"yes":"no") << "\n"; os << "number of pieces: " << num_pieces() << "\n"; diff --git a/src/tracker_manager.cpp b/src/tracker_manager.cpp index ed3f6b620..aaabf99ca 100755 --- a/src/tracker_manager.cpp +++ b/src/tracker_manager.cpp @@ -81,11 +81,6 @@ namespace namespace libtorrent { - using boost::posix_time::second_clock; - using boost::posix_time::seconds; - using boost::posix_time::ptime; - using boost::posix_time::time_duration; - // returns -1 if gzip header is invalid or the header size in bytes int gzip_header(const char* buf, int size) { @@ -318,8 +313,8 @@ namespace libtorrent timeout_handler::timeout_handler(asio::strand& str) : m_strand(str) - , m_start_time(second_clock::universal_time()) - , m_read_time(second_clock::universal_time()) + , m_start_time(time_now()) + , m_read_time(time_now()) , m_timeout(str.io_service()) , m_completion_timeout(0) , m_read_timeout(0) @@ -330,8 +325,8 @@ namespace libtorrent { m_completion_timeout = completion_timeout; m_read_timeout = read_timeout; - m_start_time = second_clock::universal_time(); - m_read_time = second_clock::universal_time(); + m_start_time = time_now(); + m_read_time = time_now(); m_timeout.expires_at(std::min( m_read_time + seconds(m_read_timeout) @@ -342,7 +337,7 @@ namespace libtorrent void timeout_handler::restart_read_timeout() { - m_read_time = second_clock::universal_time(); + m_read_time = time_now(); } void timeout_handler::cancel() @@ -356,14 +351,14 @@ namespace libtorrent if (error) return; if (m_completion_timeout == 0) return; - ptime now(second_clock::universal_time()); + ptime now(time_now()); time_duration receive_timeout = now - m_read_time; time_duration completion_timeout = now - m_start_time; if (m_read_timeout - < receive_timeout.total_seconds() + < total_seconds(receive_timeout) || m_completion_timeout - < completion_timeout.total_seconds()) + < total_seconds(completion_timeout)) { on_timeout(); return; diff --git a/src/upnp.cpp b/src/upnp.cpp index 82283c797..e54242ac3 100644 --- a/src/upnp.cpp +++ b/src/upnp.cpp @@ -32,6 +32,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/pch.hpp" +#include "libtorrent/socket.hpp" #include "libtorrent/upnp.hpp" #include "libtorrent/io.hpp" #include "libtorrent/http_tracker_connection.hpp" @@ -44,13 +45,9 @@ POSSIBILITY OF SUCH DAMAGE. #include #include +using boost::posix_time::microsec_clock; using boost::bind; using namespace libtorrent; -using boost::posix_time::microsec_clock; -using boost::posix_time::milliseconds; -using boost::posix_time::seconds; -using boost::posix_time::second_clock; -using boost::posix_time::ptime; namespace { @@ -728,11 +725,11 @@ void upnp::on_upnp_map_response(asio::error_code const& e m_callback(tcp, udp, ""); if (d.lease_duration > 0) { - d.mapping[mapping].expires = second_clock::universal_time() + d.mapping[mapping].expires = time_now() + seconds(int(d.lease_duration * 0.75f)); ptime next_expire = m_refresh_timer.expires_at(); if (next_expire == ptime(boost::date_time::not_a_date_time) - || next_expire < second_clock::universal_time() + || next_expire < time_now() || next_expire > d.mapping[mapping].expires) { m_refresh_timer.expires_at(d.mapping[mapping].expires); @@ -741,7 +738,7 @@ void upnp::on_upnp_map_response(asio::error_code const& e } else { - d.mapping[mapping].expires = ptime(boost::date_time::not_a_date_time); + d.mapping[mapping].expires = max_time(); } } @@ -805,8 +802,8 @@ void upnp::on_expire(asio::error_code const& e) { if (e) return; - ptime now = second_clock::universal_time(); - ptime next_expire = ptime(boost::date_time::not_a_date_time); + ptime now = time_now(); + ptime next_expire = max_time(); boost::mutex::scoped_lock l(m_mutex); for (std::set::iterator i = m_devices.begin() @@ -815,22 +812,21 @@ void upnp::on_expire(asio::error_code const& e) rootdevice& d = const_cast(*i); for (int m = 0; m < num_mappings; ++m) { - if (d.mapping[m].expires != ptime(boost::date_time::not_a_date_time)) + if (d.mapping[m].expires != max_time()) continue; if (d.mapping[m].expires < now) { - d.mapping[m].expires = ptime(boost::date_time::not_a_date_time); + d.mapping[m].expires = max_time(); map_port(d, m); } - else if (next_expire == ptime(boost::date_time::not_a_date_time) - || d.mapping[m].expires < next_expire) + else if (d.mapping[m].expires < next_expire) { next_expire = d.mapping[m].expires; } } } - if (next_expire != ptime(boost::date_time::not_a_date_time)) + if (next_expire != max_time()) { m_refresh_timer.expires_at(next_expire); m_refresh_timer.async_wait(bind(&upnp::on_expire, this, _1)); diff --git a/test/test_upnp.cpp b/test/test_upnp.cpp index ef4f87568..82a19df59 100644 --- a/test/test_upnp.cpp +++ b/test/test_upnp.cpp @@ -1,10 +1,8 @@ #include "libtorrent/upnp.hpp" #include "libtorrent/socket.hpp" -#include #include #include -using boost::posix_time::seconds; using namespace libtorrent; void callback(int tcp, int udp, std::string const& err)