From a5586d154d123bddf29c413242594ae383235654 Mon Sep 17 00:00:00 2001 From: Arvid Norberg Date: Wed, 25 Nov 2009 06:55:34 +0000 Subject: [PATCH] more header dependency optimizations --- CMakeLists.txt | 1 + Jamfile | 1 + include/libtorrent/Makefile.am | 4 +- include/libtorrent/alert.hpp | 2 +- include/libtorrent/bandwidth_manager.hpp | 2 +- include/libtorrent/deadline_timer.hpp | 2 + include/libtorrent/file_pool.hpp | 2 +- include/libtorrent/kademlia/logging.hpp | 2 +- include/libtorrent/kademlia/observer.hpp | 2 +- include/libtorrent/kademlia/routing_table.hpp | 2 +- include/libtorrent/kademlia/rpc_manager.hpp | 2 +- include/libtorrent/policy.hpp | 1 - include/libtorrent/ptime.hpp | 136 +++++++++ include/libtorrent/time.hpp | 287 ++---------------- include/libtorrent/torrent_handle.hpp | 2 +- include/libtorrent/torrent_info.hpp | 11 +- src/Makefile.am | 1 + src/alert.cpp | 1 + src/bandwidth_manager.cpp | 1 + src/disk_io_thread.cpp | 2 - src/file_pool.cpp | 1 + src/kademlia/routing_table.cpp | 1 + src/kademlia/rpc_manager.cpp | 1 + src/session.cpp | 8 - src/time.cpp | 186 ++++++++++++ src/torrent_info.cpp | 10 + 26 files changed, 387 insertions(+), 284 deletions(-) create mode 100644 include/libtorrent/ptime.hpp create mode 100644 src/time.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index b3e8f5b06..0ffba7c06 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -38,6 +38,7 @@ set(sources socks5_stream stat storage + time torrent torrent_handle torrent_info diff --git a/Jamfile b/Jamfile index 943e272a0..033fa8ca5 100755 --- a/Jamfile +++ b/Jamfile @@ -363,6 +363,7 @@ SOURCES = torrent torrent_handle torrent_info + time tracker_manager http_tracker_connection udp_tracker_connection diff --git a/include/libtorrent/Makefile.am b/include/libtorrent/Makefile.am index 36193a1ea..74d1f1f82 100644 --- a/include/libtorrent/Makefile.am +++ b/include/libtorrent/Makefile.am @@ -30,6 +30,7 @@ nobase_include_HEADERS = \ disk_io_thread.hpp \ entry.hpp \ enum_net.hpp \ + error.hpp \ error_code.hpp \ escape_string.hpp \ extensions.hpp \ @@ -54,6 +55,7 @@ nobase_include_HEADERS = \ lazy_entry.hpp \ lsd.hpp \ magnet_uri.hpp \ + max.hpp \ natpmp.hpp \ parse_url.hpp \ pch.hpp \ @@ -67,7 +69,7 @@ nobase_include_HEADERS = \ piece_picker.hpp \ policy.hpp \ proxy_base.hpp \ - random_sample.hpp \ + ptime.hpp \ session.hpp \ session_settings.hpp \ session_status.hpp \ diff --git a/include/libtorrent/alert.hpp b/include/libtorrent/alert.hpp index a1a1fb3a3..60ac1029a 100644 --- a/include/libtorrent/alert.hpp +++ b/include/libtorrent/alert.hpp @@ -52,7 +52,7 @@ POSSIBILITY OF SUCH DAMAGE. #pragma warning(pop) #endif -#include "libtorrent/time.hpp" +#include "libtorrent/ptime.hpp" #include "libtorrent/config.hpp" #include "libtorrent/assert.hpp" #include "libtorrent/thread.hpp" diff --git a/include/libtorrent/bandwidth_manager.hpp b/include/libtorrent/bandwidth_manager.hpp index 348465c24..234667011 100644 --- a/include/libtorrent/bandwidth_manager.hpp +++ b/include/libtorrent/bandwidth_manager.hpp @@ -47,7 +47,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/bandwidth_queue_entry.hpp" #include "libtorrent/thread.hpp" #include "libtorrent/bandwidth_socket.hpp" -#include "libtorrent/time.hpp" +#include "libtorrent/ptime.hpp" using boost::intrusive_ptr; diff --git a/include/libtorrent/deadline_timer.hpp b/include/libtorrent/deadline_timer.hpp index 4f0ca2cbe..0263a67ae 100644 --- a/include/libtorrent/deadline_timer.hpp +++ b/include/libtorrent/deadline_timer.hpp @@ -45,6 +45,8 @@ POSSIBILITY OF SUCH DAMAGE. #include #endif +#include + #ifdef __OBJC__ #undef Protocol #endif diff --git a/include/libtorrent/file_pool.hpp b/include/libtorrent/file_pool.hpp index 3219eae66..6dc1d4c26 100644 --- a/include/libtorrent/file_pool.hpp +++ b/include/libtorrent/file_pool.hpp @@ -45,7 +45,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include "libtorrent/file.hpp" -#include "libtorrent/time.hpp" +#include "libtorrent/ptime.hpp" #include "libtorrent/thread.hpp" namespace libtorrent diff --git a/include/libtorrent/kademlia/logging.hpp b/include/libtorrent/kademlia/logging.hpp index a84a0753c..9b17ee6b1 100644 --- a/include/libtorrent/kademlia/logging.hpp +++ b/include/libtorrent/kademlia/logging.hpp @@ -39,7 +39,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -#include "libtorrent/time.hpp" +#include "libtorrent/ptime.hpp" namespace libtorrent { namespace dht { diff --git a/include/libtorrent/kademlia/observer.hpp b/include/libtorrent/kademlia/observer.hpp index 3164b59af..03e78ec87 100644 --- a/include/libtorrent/kademlia/observer.hpp +++ b/include/libtorrent/kademlia/observer.hpp @@ -37,7 +37,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include +#include #include namespace libtorrent { diff --git a/include/libtorrent/kademlia/routing_table.hpp b/include/libtorrent/kademlia/routing_table.hpp index 1ce190108..5749c5649 100644 --- a/include/libtorrent/kademlia/routing_table.hpp +++ b/include/libtorrent/kademlia/routing_table.hpp @@ -50,7 +50,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include -#include +#include namespace libtorrent { diff --git a/include/libtorrent/kademlia/rpc_manager.hpp b/include/libtorrent/kademlia/rpc_manager.hpp index ce7351049..ba59a2a96 100644 --- a/include/libtorrent/kademlia/rpc_manager.hpp +++ b/include/libtorrent/kademlia/rpc_manager.hpp @@ -44,7 +44,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -#include "libtorrent/time.hpp" +#include "libtorrent/ptime.hpp" namespace libtorrent { namespace dht { diff --git a/include/libtorrent/policy.hpp b/include/libtorrent/policy.hpp index 987aedc11..bafcbbdb0 100644 --- a/include/libtorrent/policy.hpp +++ b/include/libtorrent/policy.hpp @@ -43,7 +43,6 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/size_type.hpp" #include "libtorrent/invariant_check.hpp" #include "libtorrent/config.hpp" -#include "libtorrent/time.hpp" namespace libtorrent { diff --git a/include/libtorrent/ptime.hpp b/include/libtorrent/ptime.hpp new file mode 100644 index 000000000..68842c1e8 --- /dev/null +++ b/include/libtorrent/ptime.hpp @@ -0,0 +1,136 @@ +/* + +Copyright (c) 2009, 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_PTIME_HPP_INCLUDED +#define TORRENT_PTIME_HPP_INCLUDED + +#include "libtorrent/config.hpp" + +#if defined TORRENT_USE_BOOST_DATE_TIME + +#include +#include + +namespace libtorrent +{ + typedef boost::posix_time::ptime ptime; + typedef boost::posix_time::time_duration time_duration; +} + +#else // TORRENT_USE_BOOST_DATE_TIME + +#include + +namespace libtorrent +{ + // libtorrent time_duration type + struct time_duration + { + time_duration() {} + time_duration operator/(int rhs) const { return time_duration(diff / rhs); } + explicit time_duration(boost::int64_t d) : diff(d) {} + time_duration& operator-=(time_duration const& c) { diff -= c.diff; return *this; } + time_duration& operator+=(time_duration const& c) { diff += c.diff; return *this; } + time_duration& operator*=(int v) { diff *= v; return *this; } + time_duration operator+(time_duration const& c) { return time_duration(diff + c.diff); } + time_duration operator-(time_duration const& c) { return time_duration(diff - c.diff); } + boost::int64_t diff; + }; + + // libtorrent time type + struct ptime + { + ptime() {} + explicit ptime(boost::uint64_t t): time(t) {} + ptime& operator+=(time_duration rhs) { time += rhs.diff; return *this; } + ptime& operator-=(time_duration rhs) { time -= rhs.diff; return *this; } + boost::uint64_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;} + + ptime time_now_hires(); + ptime min_time(); + ptime max_time(); + + char const* time_now_string(); + std::string log_time(); + + 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; } + 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; } + inline bool operator>=(time_duration lhs, time_duration rhs) + { return lhs.diff >= rhs.diff; } + inline time_duration operator*(time_duration lhs, int rhs) + { return time_duration(boost::int64_t(lhs.diff * rhs)); } + inline time_duration operator*(int lhs, time_duration rhs) + { return time_duration(boost::int64_t(lhs * rhs.diff)); } + + 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); } + + namespace aux + { + extern TORRENT_EXPORT ptime g_current_time; + } + + inline ptime const& time_now() { return aux::g_current_time; } +} + +#endif // TORRENT_USE_BOOST_DATE_TIME + + +#endif + diff --git a/include/libtorrent/time.hpp b/include/libtorrent/time.hpp index 7bacbda63..cdc3d51cc 100644 --- a/include/libtorrent/time.hpp +++ b/include/libtorrent/time.hpp @@ -33,233 +33,59 @@ POSSIBILITY OF SUCH DAMAGE. #ifndef TORRENT_TIME_HPP_INCLUDED #define TORRENT_TIME_HPP_INCLUDED -#include #include #include "libtorrent/config.hpp" - -#ifndef _WIN32 -#include -#endif +#include "libtorrent/ptime.hpp" +#include +#include namespace libtorrent { - inline char const* time_now_string() - { - time_t t = std::time(0); - tm* timeinfo = std::localtime(&t); - static char str[200]; - std::strftime(str, 200, "%b %d %X", timeinfo); - return str; - } - + char const* time_now_string(); std::string log_time(); -} + + ptime time_now_hires(); + ptime min_time(); + ptime max_time(); #if defined TORRENT_USE_BOOST_DATE_TIME -#include -#include -#include -#include "libtorrent/assert.hpp" + time_duration seconds(int s); + time_duration milliseconds(int s); + time_duration microsec(int s); + time_duration minutes(int s); + time_duration hours(int s); -namespace libtorrent -{ - typedef boost::posix_time::ptime ptime; - typedef boost::posix_time::time_duration time_duration; - inline ptime time_now_hires() - { return boost::date_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 // TORRENT_USE_BOOST_DATE_TIME - -#include -#include -#include "libtorrent/assert.hpp" - -namespace libtorrent -{ - // libtorrent time_duration type - struct time_duration - { - time_duration() {} - time_duration operator/(int rhs) const { return time_duration(diff / rhs); } - explicit time_duration(boost::int64_t d) : diff(d) {} - time_duration& operator-=(time_duration const& c) { diff -= c.diff; return *this; } - time_duration& operator+=(time_duration const& c) { diff += c.diff; return *this; } - time_duration& operator*=(int v) { diff *= v; return *this; } - time_duration operator+(time_duration const& c) { return time_duration(diff + c.diff); } - time_duration operator-(time_duration const& c) { return time_duration(diff - c.diff); } - 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; } - 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; } - inline bool operator>=(time_duration lhs, time_duration rhs) - { return lhs.diff >= rhs.diff; } - inline time_duration operator*(time_duration lhs, int rhs) - { return time_duration(boost::int64_t(lhs.diff * rhs)); } - inline time_duration operator*(int lhs, time_duration rhs) - { return time_duration(boost::int64_t(lhs * rhs.diff)); } - - // libtorrent time type - struct ptime - { - ptime() {} - explicit ptime(boost::uint64_t t): time(t) {} - ptime& operator+=(time_duration rhs) { time += rhs.diff; return *this; } - ptime& operator-=(time_duration rhs) { time -= rhs.diff; return *this; } - boost::uint64_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_hires(); - 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); -} -#if defined TORRENT_USE_ABSOLUTE_TIME +#elif defined TORRENT_USE_ABSOLUTE_TIME -#include -#include -#include "libtorrent/assert.hpp" - -// high precision timer for darwin intel and ppc - -namespace libtorrent -{ inline int total_seconds(time_duration td) - { - return td.diff / 1000000; - } + { return td.diff / 1000000; } inline int total_milliseconds(time_duration td) - { - return td.diff / 1000; - } + { return td.diff / 1000; } inline boost::int64_t total_microseconds(time_duration td) - { - return td.diff; - } - - inline ptime time_now_hires() - { - static mach_timebase_info_data_t timebase_info = {0,0}; - if (timebase_info.denom == 0) - mach_timebase_info(&timebase_info); - boost::uint64_t at = mach_absolute_time(); - // make sure we don't overflow - TORRENT_ASSERT((at >= 0 && at >= at / 1000 * timebase_info.numer / timebase_info.denom) - || (at < 0 && at < at / 1000 * timebase_info.numer / timebase_info.denom)); - return ptime(at / 1000 * timebase_info.numer / timebase_info.denom); - } + { return td.diff; } inline time_duration microsec(boost::int64_t s) - { - return time_duration(s); - } + { return time_duration(s); } inline time_duration milliseconds(boost::int64_t s) - { - return time_duration(s * 1000); - } + { return time_duration(s * 1000); } inline time_duration seconds(boost::int64_t s) - { - return time_duration(s * 1000000); - } + { return time_duration(s * 1000000); } inline time_duration minutes(boost::int64_t s) - { - return time_duration(s * 1000000 * 60); - } + { return time_duration(s * 1000000 * 60); } inline time_duration hours(boost::int64_t s) - { - return time_duration(s * 1000000 * 60 * 60); - } + { return time_duration(s * 1000000 * 60 * 60); } -} #elif defined TORRENT_USE_QUERY_PERFORMANCE_TIMER -#ifndef WIN32_LEAN_AND_MEAN -#define WIN32_LEAN_AND_MEAN -#endif -#include -#include "libtorrent/assert.hpp" - -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); - -#ifdef TORRENT_DEBUG - // make sure we don't overflow - boost::int64_t ret = (pc * 1000 / performace_counter_frequency.QuadPart) * 1000; - TORRENT_ASSERT((pc >= 0 && pc >= ret) || (pc < 0 && pc < ret)); -#endif - return (pc * 1000 / performace_counter_frequency.QuadPart) * 1000; - } - - 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); -#ifdef TORRENT_DEBUG - // make sure we don't overflow - boost::int64_t ret = (ms / 1000) * performace_counter_frequency.QuadPart / 1000; - TORRENT_ASSERT((ms >= 0 && ms <= ret) - || (ms < 0 && ms > ret)); -#endif - return (ms / 1000) * performace_counter_frequency.QuadPart / 1000; - } + boost::int64_t performance_counter_to_microseconds(boost::int64_t pc); + boost::int64_t microseconds_to_performance_counter(boost::int64_t ms); } inline int total_seconds(time_duration td) @@ -277,13 +103,6 @@ namespace libtorrent return aux::performance_counter_to_microseconds(td.diff); } - inline ptime time_now_hires() - { - 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)); @@ -309,70 +128,28 @@ namespace libtorrent s * 1000000 * 60 * 60)); } -} - #elif defined TORRENT_USE_CLOCK_GETTIME -#include -#include "libtorrent/assert.hpp" - -namespace libtorrent -{ inline int total_seconds(time_duration td) - { - return td.diff / 1000000; - } + { return td.diff / 1000000; } inline int total_milliseconds(time_duration td) - { - return td.diff / 1000; - } + { return td.diff / 1000; } inline boost::int64_t total_microseconds(time_duration td) - { - return td.diff; - } - - inline ptime time_now_hires() - { - timespec ts; - clock_gettime(CLOCK_MONOTONIC, &ts); - return ptime(boost::uint64_t(ts.tv_sec) * 1000000 + ts.tv_nsec / 1000); - } + { return td.diff; } inline time_duration microsec(boost::int64_t s) - { - return time_duration(s); - } + { return time_duration(s); } inline time_duration milliseconds(boost::int64_t s) - { - return time_duration(s * 1000); - } + { return time_duration(s * 1000); } inline time_duration seconds(boost::int64_t s) - { - return time_duration(s * 1000000); - } + { return time_duration(s * 1000000); } inline time_duration minutes(boost::int64_t s) - { - return time_duration(s * 1000000 * 60); - } + { return time_duration(s * 1000000 * 60); } inline time_duration hours(boost::int64_t s) - { - return time_duration(s * 1000000 * 60 * 60); - } - -} + { return time_duration(s * 1000000 * 60 * 60); } #endif // TORRENT_USE_CLOCK_GETTIME -#endif // TORRENT_USE_BOOST_DATE_TIME - -namespace libtorrent -{ - namespace aux - { - extern TORRENT_EXPORT ptime g_current_time; - } - - inline ptime const& time_now() { return aux::g_current_time; } } #endif // TORRENT_TIME_HPP_INCLUDED diff --git a/include/libtorrent/torrent_handle.hpp b/include/libtorrent/torrent_handle.hpp index be24976af..12345093d 100644 --- a/include/libtorrent/torrent_handle.hpp +++ b/include/libtorrent/torrent_handle.hpp @@ -50,7 +50,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/ptime.hpp" #include "libtorrent/config.hpp" #include "libtorrent/storage.hpp" #include "libtorrent/address.hpp" diff --git a/include/libtorrent/torrent_info.hpp b/include/libtorrent/torrent_info.hpp index 4927dda63..acd8371bd 100644 --- a/include/libtorrent/torrent_info.hpp +++ b/include/libtorrent/torrent_info.hpp @@ -54,7 +54,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/lazy_entry.hpp" #include "libtorrent/peer_id.hpp" #include "libtorrent/size_type.hpp" -#include "libtorrent/time.hpp" +#include "libtorrent/ptime.hpp" #include "libtorrent/intrusive_ptr_base.hpp" #include "libtorrent/assert.hpp" #include "libtorrent/file_storage.hpp" @@ -133,14 +133,7 @@ namespace libtorrent next_announce = min_time(); } - void failed() - { - ++fails; - int delay = (std::min)(tracker_retry_delay_min + int(fails) * int(fails) * tracker_retry_delay_min - , int(tracker_retry_delay_max)); - next_announce = time_now() + seconds(delay); - updating = false; - } + void failed(); bool can_announce(ptime now) const { diff --git a/src/Makefile.am b/src/Makefile.am index 03e5d458d..c945c2aea 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -70,6 +70,7 @@ libtorrent_rasterbar_la_SOURCES = \ torrent.cpp \ torrent_handle.cpp \ torrent_info.cpp \ + time.cpp \ tracker_manager.cpp \ udp_socket.cpp \ udp_tracker_connection.cpp \ diff --git a/src/alert.cpp b/src/alert.cpp index 4e2ff9d46..b4a446a56 100644 --- a/src/alert.cpp +++ b/src/alert.cpp @@ -34,6 +34,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/alert.hpp" #include "libtorrent/io_service.hpp" +#include "libtorrent/time.hpp" #include namespace libtorrent { diff --git a/src/bandwidth_manager.cpp b/src/bandwidth_manager.cpp index 899aca164..d04f1b0a0 100644 --- a/src/bandwidth_manager.cpp +++ b/src/bandwidth_manager.cpp @@ -31,6 +31,7 @@ POSSIBILITY OF SUCH DAMAGE. */ #include "libtorrent/bandwidth_manager.hpp" +#include "libtorrent/time.hpp" namespace libtorrent { diff --git a/src/disk_io_thread.cpp b/src/disk_io_thread.cpp index 1da5c9d00..af5f10699 100644 --- a/src/disk_io_thread.cpp +++ b/src/disk_io_thread.cpp @@ -44,9 +44,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include -#ifdef TORRENT_DISK_STATS #include "libtorrent/time.hpp" -#endif #if TORRENT_USE_MLOCK && !defined TORRENT_WINDOWS #include diff --git a/src/file_pool.cpp b/src/file_pool.cpp index c8e492a4d..8dd21eebe 100644 --- a/src/file_pool.cpp +++ b/src/file_pool.cpp @@ -33,6 +33,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include "libtorrent/pch.hpp" +#include "libtorrent/assert.hpp" #include "libtorrent/file_pool.hpp" #include "libtorrent/error_code.hpp" diff --git a/src/kademlia/routing_table.cpp b/src/kademlia/routing_table.cpp index 8ea09848c..f0e91a695 100644 --- a/src/kademlia/routing_table.cpp +++ b/src/kademlia/routing_table.cpp @@ -43,6 +43,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/session_status.hpp" #include "libtorrent/kademlia/node_id.hpp" #include "libtorrent/session_settings.hpp" +#include "libtorrent/time.hpp" using boost::bind; using boost::uint8_t; diff --git a/src/kademlia/rpc_manager.cpp b/src/kademlia/rpc_manager.cpp index c47caaee2..a5c1cfed9 100644 --- a/src/kademlia/rpc_manager.cpp +++ b/src/kademlia/rpc_manager.cpp @@ -50,6 +50,7 @@ POSSIBILITY OF SUCH DAMAGE. #include #include #include +#include #include diff --git a/src/session.cpp b/src/session.cpp index 0ab900706..264e0efa5 100644 --- a/src/session.cpp +++ b/src/session.cpp @@ -88,14 +88,6 @@ namespace libtorrent TORRENT_EXPORT void TORRENT_LINK_TEST_NAME() {} - std::string log_time() - { - static const ptime start = time_now_hires(); - char ret[200]; - std::sprintf(ret, "%d", total_milliseconds(time_now() - start)); - return ret; - } - // this function returns a session_settings object // which will optimize libtorrent for minimum memory // usage, with no consideration of performance. diff --git a/src/time.cpp b/src/time.cpp new file mode 100644 index 000000000..3a4718c31 --- /dev/null +++ b/src/time.cpp @@ -0,0 +1,186 @@ +/* + +Copyright (c) 2009, 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. + +*/ + +#include +#include +#include +#include "libtorrent/config.hpp" +#include "libtorrent/time.hpp" + +#ifndef _WIN32 +#include +#endif + +namespace libtorrent +{ + char const* time_now_string() + { + time_t t = std::time(0); + tm* timeinfo = std::localtime(&t); + static char str[200]; + std::strftime(str, 200, "%b %d %X", timeinfo); + return str; + } + + std::string log_time() + { + static const ptime start = time_now_hires(); + char ret[200]; + std::sprintf(ret, "%d", total_milliseconds(time_now() - start)); + return ret; + } +} + +#if defined TORRENT_USE_BOOST_DATE_TIME + +#include + +namespace libtorrent +{ + ptime time_now_hires() + { return boost::date_time::microsec_clock::universal_time(); } + ptime min_time() + { return boost::posix_time::ptime(boost::posix_time::min_date_time); } + ptime max_time() + { return boost::posix_time::ptime(boost::posix_time::max_date_time); } + time_duration seconds(int s) { return boost::posix_time::seconds(s); } + time_duration milliseconds(int s) { return boost::posix_time::milliseconds(s); } + time_duration microsec(int s) { return boost::posix_time::microsec(s); } + time_duration minutes(int s) { return boost::posix_time::minutes(s); } + time_duration hours(int s) { return boost::posix_time::hours(s); } + + int total_seconds(time_duration td) + { return td.total_seconds(); } + int total_milliseconds(time_duration td) + { return td.total_milliseconds(); } + boost::int64_t total_microseconds(time_duration td) + { return td.total_microseconds(); } +} + +#else // TORRENT_USE_BOOST_DATE_TIME + +namespace libtorrent +{ + ptime min_time() { return ptime(0); } + ptime max_time() { return ptime((std::numeric_limits::max)()); } +} + +#if defined TORRENT_USE_ABSOLUTE_TIME + +#include +#include +#include "libtorrent/assert.hpp" + +// high precision timer for darwin intel and ppc + +namespace libtorrent +{ + ptime time_now_hires() + { + static mach_timebase_info_data_t timebase_info = {0,0}; + if (timebase_info.denom == 0) + mach_timebase_info(&timebase_info); + boost::uint64_t at = mach_absolute_time(); + // make sure we don't overflow + TORRENT_ASSERT((at >= 0 && at >= at / 1000 * timebase_info.numer / timebase_info.denom) + || (at < 0 && at < at / 1000 * timebase_info.numer / timebase_info.denom)); + return ptime(at / 1000 * timebase_info.numer / timebase_info.denom); + } +} +#elif defined TORRENT_USE_QUERY_PERFORMANCE_TIMER + +#ifndef WIN32_LEAN_AND_MEAN +#define WIN32_LEAN_AND_MEAN +#endif +#include +#include "libtorrent/assert.hpp" + +namespace libtorrent +{ + namespace aux + { + 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); + +#ifdef TORRENT_DEBUG + // make sure we don't overflow + boost::int64_t ret = (pc * 1000 / performace_counter_frequency.QuadPart) * 1000; + TORRENT_ASSERT((pc >= 0 && pc >= ret) || (pc < 0 && pc < ret)); +#endif + return (pc * 1000 / performace_counter_frequency.QuadPart) * 1000; + } + + 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); +#ifdef TORRENT_DEBUG + // make sure we don't overflow + boost::int64_t ret = (ms / 1000) * performace_counter_frequency.QuadPart / 1000; + TORRENT_ASSERT((ms >= 0 && ms <= ret) + || (ms < 0 && ms > ret)); +#endif + return (ms / 1000) * performace_counter_frequency.QuadPart / 1000; + } + } + + ptime time_now_hires() + { + LARGE_INTEGER now; + QueryPerformanceCounter(&now); + return ptime(now.QuadPart); + } +} + +#elif defined TORRENT_USE_CLOCK_GETTIME + +#include +#include "libtorrent/assert.hpp" + +namespace libtorrent +{ + ptime time_now_hires() + { + timespec ts; + clock_gettime(CLOCK_MONOTONIC, &ts); + return ptime(boost::uint64_t(ts.tv_sec) * 1000000 + ts.tv_nsec / 1000); + } +} + +#endif // TORRENT_USE_CLOCK_GETTIME + +#endif // TORRENT_USE_BOOST_DATE_TIME + diff --git a/src/torrent_info.cpp b/src/torrent_info.cpp index b5b414be1..d11ac2dc5 100644 --- a/src/torrent_info.cpp +++ b/src/torrent_info.cpp @@ -63,6 +63,7 @@ POSSIBILITY OF SUCH DAMAGE. #include "libtorrent/entry.hpp" #include "libtorrent/file.hpp" #include "libtorrent/utf8.hpp" +#include "libtorrent/time.hpp" #if TORRENT_USE_I2P #include "libtorrent/parse_url.hpp" @@ -379,6 +380,15 @@ namespace libtorrent return 0; } + void announce_entry::failed() + { + ++fails; + int delay = (std::min)(tracker_retry_delay_min + int(fails) * int(fails) * tracker_retry_delay_min + , int(tracker_retry_delay_max)); + next_announce = time_now() + seconds(delay); + updating = false; + } + #ifndef TORRENT_NO_DEPRECATE // standard constructor that parses a torrent file torrent_info::torrent_info(entry const& torrent_file)