diff --git a/bindings/python/src/datetime.cpp b/bindings/python/src/datetime.cpp
index 2ae33f2ec..35dcc488c 100644
--- a/bindings/python/src/datetime.cpp
+++ b/bindings/python/src/datetime.cpp
@@ -60,7 +60,7 @@ struct time_duration_to_python
struct time_point_to_python
{
- static PyObject* convert(lt::time_point pt)
+ static PyObject* convert(lt::time_point const pt)
{
using std::chrono::system_clock;
using std::chrono::duration_cast;
diff --git a/bindings/python/test.py b/bindings/python/test.py
index e95ea98f0..2aa068f87 100644
--- a/bindings/python/test.py
+++ b/bindings/python/test.py
@@ -90,8 +90,8 @@ class test_torrent_handle(unittest.TestCase):
self.setup()
st = self.h.status()
# last upload and download times are at session start time
- self.assertEqual(st.last_upload, sessionStart)
- self.assertEqual(st.last_download, sessionStart)
+ self.assertLessEqual(abs(st.last_upload - sessionStart), datetime.timedelta(seconds=1))
+ self.assertLessEqual(abs(st.last_download - sessionStart), datetime.timedelta(seconds=1))
def test_torrent_status(self):
self.setup()
diff --git a/include/libtorrent/announce_entry.hpp b/include/libtorrent/announce_entry.hpp
index b2b8ce39f..6411e6c20 100644
--- a/include/libtorrent/announce_entry.hpp
+++ b/include/libtorrent/announce_entry.hpp
@@ -83,10 +83,10 @@ namespace libtorrent
#endif
// the time of next tracker announce
- time_point next_announce = min_time();
+ time_point32 next_announce = time_point32::min();
// no announces before this time
- time_point min_announce = min_time();
+ time_point32 min_announce = time_point32::min();
// TODO: include the number of peers received from this tracker, at last
// announce
@@ -168,7 +168,7 @@ namespace libtorrent
// updates the failure counter and time-outs for re-trying.
// This is called when the tracker announce fails.
- void failed(time_duration tracker_backoff, int retry_interval = 0);
+ void failed(int backoff_ratio, seconds32 retry_interval = seconds32(0));
#ifndef TORRENT_NO_DEPRECATE
// deprecated in 1.0
diff --git a/include/libtorrent/aux_/session_impl.hpp b/include/libtorrent/aux_/session_impl.hpp
index d45784bb1..78665c1a4 100644
--- a/include/libtorrent/aux_/session_impl.hpp
+++ b/include/libtorrent/aux_/session_impl.hpp
@@ -1247,16 +1247,16 @@ namespace libtorrent
{
explicit tracker_logger(session_interface& ses);
void tracker_warning(tracker_request const& req
- , std::string const& str);
+ , std::string const& str) override;
void tracker_response(tracker_request const&
, libtorrent::address const& tracker_ip
, std::list
const& ip_list
- , struct tracker_response const& resp);
+ , struct tracker_response const& resp) override;
void tracker_request_error(tracker_request const& r
, int response_code, error_code const& ec, const std::string& str
- , int retry_interval);
- bool should_log() const;
- void debug_log(const char* fmt, ...) const TORRENT_FORMAT(2,3);
+ , seconds32 retry_interval) override;
+ bool should_log() const override;
+ void debug_log(const char* fmt, ...) const override TORRENT_FORMAT(2,3);
session_interface& m_ses;
private:
// explicitly disallow assignment, to silence msvc warning
diff --git a/include/libtorrent/aux_/time.hpp b/include/libtorrent/aux_/time.hpp
index fdca9eea5..407eacecf 100644
--- a/include/libtorrent/aux_/time.hpp
+++ b/include/libtorrent/aux_/time.hpp
@@ -41,6 +41,7 @@ namespace libtorrent { namespace aux
// returns the current time, as represented by time_point. The
// resolution of this timer is about 100 ms.
TORRENT_EXTRA_EXPORT time_point time_now();
+ TORRENT_EXTRA_EXPORT time_point32 time_now32();
TORRENT_EXTRA_EXPORT void update_time_now();
diff --git a/include/libtorrent/time.hpp b/include/libtorrent/time.hpp
index 3102d8e7a..7a8b7218b 100644
--- a/include/libtorrent/time.hpp
+++ b/include/libtorrent/time.hpp
@@ -53,12 +53,18 @@ namespace libtorrent {
using time_point = clock_type::time_point;
using time_duration = clock_type::duration;
+ // 32 bit versions of time_point and duration, with second resolution
+ using seconds32 = std::chrono::duration;
+ using minutes32 = std::chrono::duration>;
+ using time_point32 = std::chrono::time_point;
+
using std::chrono::seconds;
using std::chrono::milliseconds;
using std::chrono::microseconds;
using std::chrono::minutes;
using std::chrono::hours;
using std::chrono::duration_cast;
+ using std::chrono::time_point_cast;
// internal
inline time_point min_time() { return (time_point::min)(); }
diff --git a/include/libtorrent/torrent.hpp b/include/libtorrent/torrent.hpp
index 4a978592a..2781aae7c 100644
--- a/include/libtorrent/torrent.hpp
+++ b/include/libtorrent/torrent.hpp
@@ -95,8 +95,6 @@ namespace libtorrent
class bt_peer_connection;
struct listen_socket_t;
- using seconds32 = std::chrono::duration;
-
enum class waste_reason
{
piece_timed_out, piece_cancelled, piece_unknown, piece_seed
@@ -136,7 +134,7 @@ namespace libtorrent
, web_seed_entry::headers_t const& extra_headers_ = web_seed_entry::headers_t());
// if this is > now, we can't reconnect yet
- time_point retry = aux::time_now();
+ time_point32 retry = aux::time_now32();
// if the hostname of the web seed has been resolved,
// these are its IP addresses
@@ -480,7 +478,7 @@ namespace libtorrent
void stop_when_ready(bool b);
- time_point started() const { return m_started; }
+ time_point32 started() const { return m_started; }
void step_session_time(int seconds);
void do_pause(bool clear_disk_cache = true);
void do_resume();
@@ -500,7 +498,7 @@ namespace libtorrent
// save resume data every 15 minutes regardless, just to
// keep stats up to date
return m_need_save_resume_data ||
- aux::time_now() - m_last_saved_resume > minutes(15);
+ aux::time_now32() - m_last_saved_resume > minutes(15);
}
void set_need_save_resume()
@@ -673,7 +671,7 @@ namespace libtorrent
, struct tracker_response const& resp) override;
virtual void tracker_request_error(tracker_request const& r
, int response_code, error_code const& ec, const std::string& msg
- , int retry_interval) override;
+ , seconds32 retry_interval) override;
virtual void tracker_warning(tracker_request const& req
, std::string const& msg) override;
virtual void tracker_scrape_response(tracker_request const& req
@@ -1038,7 +1036,7 @@ namespace libtorrent
// that are not private
void lsd_announce();
- void update_last_upload() { m_last_upload = aux::time_now(); }
+ void update_last_upload() { m_last_upload = aux::time_now32(); }
void set_apply_ip_filter(bool b);
bool apply_ip_filter() const { return m_apply_ip_filter; }
@@ -1138,7 +1136,7 @@ namespace libtorrent
void update_peer_interest(bool was_finished);
void prioritize_udp_trackers();
- void update_tracker_timer(time_point now);
+ void update_tracker_timer(time_point32 now);
void on_tracker_announce(error_code const& ec);
@@ -1287,7 +1285,7 @@ namespace libtorrent
// the posix time this torrent was added and when
// it was completed. If the torrent isn't yet
// completed, m_completed_time is 0
- time_t m_added_time;
+ time_t m_added_time = time(nullptr);
time_t m_completed_time = 0;
// this was the last time _we_ saw a seed in this swarm
@@ -1318,7 +1316,7 @@ namespace libtorrent
// m_num_verified = m_verified.count()
std::uint32_t m_num_verified = 0;
- time_point m_last_saved_resume = aux::time_now();
+ time_point32 m_last_saved_resume = aux::time_now32();
// if this torrent is running, this was the time
// when it was started. This is used to have a
@@ -1328,15 +1326,15 @@ namespace libtorrent
// in session-time. see session_impl for details.
// the reference point is stepped forward every 4
// hours to keep the timestamps fit in 16 bits
- time_point m_started = aux::time_now();
+ time_point32 m_started = aux::time_now32();
// if we're a seed, this is the session time
// timestamp of when we became one
- time_point m_became_seed = aux::time_now();
+ time_point32 m_became_seed = aux::time_now32();
// if we're finished, this is the session time
// timestamp of when we finished
- time_point m_became_finished = aux::time_now();
+ time_point32 m_became_finished = aux::time_now32();
// when checking, this is the first piece we have not
// issued a hash job for
@@ -1385,7 +1383,7 @@ namespace libtorrent
// the session time timestamp of when we entered upload mode
// if we're currently in upload-mode
- time_point m_upload_mode_time = aux::time_now();
+ time_point32 m_upload_mode_time = aux::time_now32();
// true when this torrent should announce to
// trackers
@@ -1427,7 +1425,7 @@ namespace libtorrent
// paused. specified in seconds. This only track time _before_ we started
// the torrent this last time. When the torrent is paused, this counter is
// incremented to include this current session.
- seconds32 m_active_time;
+ seconds32 m_active_time{0};
// the index to the last tracker that worked
std::int8_t m_last_working_tracker = -1;
@@ -1436,7 +1434,7 @@ namespace libtorrent
// total time we've been finished with this torrent.
// does not count when the torrent is stopped or paused.
- seconds32 m_finished_time;
+ seconds32 m_finished_time{0};
// in case the piece picker hasn't been constructed
// when this settings is set, this variable will keep
@@ -1476,7 +1474,7 @@ namespace libtorrent
// accounts for the time prior to the current start of the torrent. When
// the torrent is paused, this counter is incremented to account for the
// additional seeding time.
- seconds32 m_seeding_time;
+ seconds32 m_seeding_time{0};
// ----
@@ -1564,7 +1562,7 @@ namespace libtorrent
// the timestamp of the last piece passed for this torrent specified in
// session_time. This is signed because it must be able to represent time
// before the session started
- time_point m_last_download = aux::time_now();
+ time_point32 m_last_download = aux::time_now32();
// the number of peer connections to seeds. This should be the same as
// counting the peer connections that say true for is_seed()
@@ -1577,7 +1575,7 @@ namespace libtorrent
// the timestamp of the last byte uploaded from this torrent specified in
// session_time. This is signed because it must be able to represent time
// before the session started.
- time_point m_last_upload = aux::time_now();
+ time_point32 m_last_upload = aux::time_now32();
// ----
diff --git a/include/libtorrent/tracker_manager.hpp b/include/libtorrent/tracker_manager.hpp
index 94711ed21..25e9ff5f9 100644
--- a/include/libtorrent/tracker_manager.hpp
+++ b/include/libtorrent/tracker_manager.hpp
@@ -197,10 +197,10 @@ namespace libtorrent
std::string warning_message;
// re-announce interval, in seconds
- int interval;
+ seconds32 interval;
// the lowest force-announce interval
- int min_interval;
+ seconds32 min_interval;
// the number of seeds in the swarm
int complete;
@@ -236,7 +236,7 @@ namespace libtorrent
, int response_code
, error_code const& ec
, const std::string& msg
- , int retry_interval) = 0;
+ , seconds32 retry_interval) = 0;
#ifndef TORRENT_DISABLE_LOGGING
virtual bool should_log() const = 0;
@@ -298,7 +298,7 @@ namespace libtorrent
tracker_request const& tracker_req() const { return m_req; }
void fail(error_code const& ec, int code = -1, char const* msg = ""
- , int interval = 0, int min_interval = 0);
+ , seconds32 interval = seconds32(0), seconds32 min_interval = seconds32(0));
virtual void start() = 0;
virtual void close() = 0;
address const& bind_interface() const { return m_req.bind_ip; }
@@ -318,7 +318,7 @@ namespace libtorrent
protected:
void fail_impl(error_code const& ec, int code = -1, std::string msg = std::string()
- , int interval = 0, int min_interval = 0);
+ , seconds32 interval = seconds32(0), seconds32 min_interval = seconds32(0));
std::weak_ptr m_requester;
diff --git a/include/libtorrent/udp_tracker_connection.hpp b/include/libtorrent/udp_tracker_connection.hpp
index 32a9a2e93..f5946c36c 100644
--- a/include/libtorrent/udp_tracker_connection.hpp
+++ b/include/libtorrent/udp_tracker_connection.hpp
@@ -95,7 +95,9 @@ namespace libtorrent
// wraps tracker_connection::fail
void fail(error_code const& ec, int code = -1
- , char const* msg = "", int interval = 0, int min_interval = 0);
+ , char const* msg = ""
+ , seconds32 interval = seconds32(0)
+ , seconds32 min_interval = seconds32(0));
void send_udp_connect();
void send_udp_announce();
diff --git a/simulation/test_torrent_status.cpp b/simulation/test_torrent_status.cpp
index 88b02a81d..40fa39f02 100644
--- a/simulation/test_torrent_status.cpp
+++ b/simulation/test_torrent_status.cpp
@@ -40,10 +40,21 @@ using namespace libtorrent;
using namespace sim;
namespace lt = libtorrent;
+time_point32 time_now()
+{
+ return lt::time_point_cast(lt::clock_type::now());
+}
+
+template
+bool eq(Tp1 const lhs, Tp2 const rhs)
+{
+ return std::abs(lt::duration_cast(lhs - rhs).count()) <= 1;
+}
+
// this is a test for torrent_status time counters are correct
TORRENT_TEST(status_timers)
{
- lt::time_point start_time;
+ lt::time_point32 start_time;
lt::torrent_handle handle;
bool ran_to_completion = false;
@@ -57,7 +68,7 @@ TORRENT_TEST(status_timers)
if (auto ta = alert_cast(a))
{
TEST_CHECK(!handle.is_valid());
- start_time = lt::clock_type::now();
+ start_time = time_now();
handle = ta->handle;
}
}
@@ -78,7 +89,7 @@ TORRENT_TEST(status_timers)
// once an hour, verify that the timers seem correct
if ((ticks % 3600) == 0)
{
- lt::time_point const now = lt::clock_type::now();
+ lt::time_point32 const now = time_now();
// finish is 1 tick after start
auto const since_finish = duration_cast(now - start_time) - lt::seconds(1);
torrent_status st = handle.status();
@@ -100,7 +111,7 @@ TORRENT_TEST(status_timers_last_upload)
{
bool ran_to_completion = false;
- lt::time_point start_time;
+ lt::time_point32 start_time;
lt::torrent_handle handle;
setup_swarm(2, swarm_test::upload
@@ -113,7 +124,7 @@ TORRENT_TEST(status_timers_last_upload)
if (auto ta = alert_cast(a))
{
TEST_CHECK(!handle.is_valid());
- start_time = lt::clock_type::now();
+ start_time = time_now();
handle = ta->handle;
torrent_status st = handle.status();
// test last upload and download state before wo go throgh
@@ -133,9 +144,9 @@ TORRENT_TEST(status_timers_last_upload)
torrent_status st = handle.status();
// uploadtime is 0 seconds behind now
- TEST_EQUAL(duration_cast(st.last_upload - lt::clock_type::now()).count(), 0);
+ TEST_CHECK(eq(st.last_upload, time_now()));
// does not download in seeding mode
- TEST_CHECK(st.last_download == start_time);
+ TEST_CHECK(eq(st.last_download, start_time));
return false;
});
TEST_CHECK(ran_to_completion);
@@ -145,9 +156,9 @@ TORRENT_TEST(status_timers_time_shift_with_active_torrent)
{
bool ran_to_completion = false;
- lt::time_point start_time;
+ lt::time_point32 start_time;
lt::torrent_handle handle;
- seconds expected_active_duration = seconds(0);
+ seconds expected_active_duration = seconds(1);
bool tick_is_in_active_range = false;
int tick_check_intervall = 1;
@@ -161,7 +172,7 @@ TORRENT_TEST(status_timers_time_shift_with_active_torrent)
if (auto ta = alert_cast(a))
{
TEST_CHECK(!handle.is_valid());
- start_time = lt::clock_type::now();
+ start_time = time_now();
handle = ta->handle;
torrent_status st = handle.status();
// test last upload and download state before wo go throgh
@@ -227,9 +238,9 @@ TORRENT_TEST(finish_time_shift_active)
{
bool ran_to_completion = false;
- lt::time_point start_time;
+ lt::time_point32 start_time;
lt::torrent_handle handle;
- seconds expected_active_duration = seconds(0);
+ seconds expected_active_duration = seconds(1);
bool tick_is_in_active_range = false;
setup_swarm(1, swarm_test::upload
@@ -242,7 +253,7 @@ TORRENT_TEST(finish_time_shift_active)
if (auto ta = alert_cast(a))
{
TEST_CHECK(!handle.is_valid());
- start_time = lt::clock_type::now();
+ start_time = time_now();
handle = ta->handle;
torrent_status st = handle.status();
// test last upload and download state before wo go throgh
@@ -301,9 +312,9 @@ TORRENT_TEST(finish_time_shift_paused)
{
bool ran_to_completion = false;
- lt::time_point start_time;
+ lt::time_point32 start_time;
lt::torrent_handle handle;
- seconds expected_active_duration = seconds(0);
+ seconds expected_active_duration = seconds(1);
bool tick_is_in_active_range = false;
setup_swarm(1, swarm_test::upload
@@ -316,13 +327,13 @@ TORRENT_TEST(finish_time_shift_paused)
if (auto ta = alert_cast(a))
{
TEST_CHECK(!handle.is_valid());
- start_time = lt::clock_type::now();
+ start_time = time_now();
handle = ta->handle;
torrent_status st = handle.status();
// test last upload and download state before wo go throgh
// torrent states
- TEST_CHECK(st.last_download == start_time);
- TEST_CHECK(st.last_upload == start_time);
+ TEST_CHECK(eq(st.last_download, start_time));
+ TEST_CHECK(eq(st.last_upload, start_time));
}
}
// terminate
diff --git a/simulation/test_tracker.cpp b/simulation/test_tracker.cpp
index 2b99aa5ba..6fdff1421 100644
--- a/simulation/test_tracker.cpp
+++ b/simulation/test_tracker.cpp
@@ -52,6 +52,12 @@ using chrono::duration_cast;
// seconds
const int duration = 10000;
+template
+bool eq(Tp1 const lhs, Tp2 const rhs)
+{
+ return std::abs(lt::duration_cast(lhs - rhs).count()) <= 1;
+}
+
void test_interval(int interval)
{
using sim::asio::ip::address_v4;
@@ -121,14 +127,13 @@ void test_interval(int interval)
lt::time_point last_alert = announce_alerts[0];
for (int i = 1; i < int(announces.size()); ++i)
{
- // make sure the interval is within 500 ms of what it's supposed to be
- // (this accounts for network latencies)
- std::int64_t const actual_interval_ms = duration_cast(announces[i] - last_announce).count();
- TEST_CHECK(std::abs(actual_interval_ms - interval * 1000) < 500);
+ // make sure the interval is within 1 second of what it's supposed to be
+ // (this accounts for network latencies, and the second-granularity
+ // timestamps)
+ TEST_CHECK(eq(duration_cast(announces[i] - last_announce), lt::seconds(interval)));
last_announce = announces[i];
- std::int64_t const alert_interval_ms = duration_cast(announce_alerts[i] - last_alert).count();
- TEST_CHECK(std::abs(alert_interval_ms - interval * 1000) < 500);
+ TEST_CHECK(eq(duration_cast(announce_alerts[i] - last_alert), lt::seconds(interval)));
last_alert = announce_alerts[i];
}
}
diff --git a/src/announce_entry.cpp b/src/announce_entry.cpp
index e0c64ee83..54bff6227 100644
--- a/src/announce_entry.cpp
+++ b/src/announce_entry.cpp
@@ -38,14 +38,13 @@ POSSIBILITY OF SUCH DAMAGE.
namespace libtorrent
{
- enum
- {
+ namespace {
// wait at least 5 seconds before retrying a failed tracker
- tracker_retry_delay_min = 5,
- // when tracker_failed_max trackers
- // has failed, wait 60 minutes instead
- tracker_retry_delay_max = 60 * 60
- };
+ seconds32 constexpr tracker_retry_delay_min{5};
+
+ // never wait more than 60 minutes to retry a tracker
+ minutes32 constexpr tracker_retry_delay_max{60};
+ }
announce_entry::announce_entry(std::string u)
: url(std::move(u))
@@ -81,21 +80,23 @@ namespace libtorrent
void announce_entry::reset()
{
start_sent = false;
- next_announce = min_time();
- min_announce = min_time();
+ next_announce = time_point32::min();
+ min_announce = time_point32::min();
}
- void announce_entry::failed(time_duration const tracker_backoff, int const retry_interval)
+ void announce_entry::failed(int const backoff_ratio, seconds32 const retry_interval)
{
++fails;
// the exponential back-off ends up being:
// 7, 15, 27, 45, 95, 127, 165, ... seconds
// with the default tracker_backoff of 250
- int const tracker_backoff_seconds = int(total_seconds(tracker_backoff));
- int const delay = std::max(std::min(tracker_retry_delay_min + int(fails) * int(fails)
- * tracker_retry_delay_min * tracker_backoff_seconds / 100
- , int(tracker_retry_delay_max)), retry_interval);
- next_announce = aux::time_now() + seconds(delay);
+ int const fail_square = int(fails) * int(fails);
+ seconds32 const delay = std::max(retry_interval
+ , std::min(duration_cast(tracker_retry_delay_max)
+ , tracker_retry_delay_min
+ + fail_square * tracker_retry_delay_min * backoff_ratio / 100
+ ));
+ next_announce = aux::time_now32() + delay;
updating = false;
}
diff --git a/src/http_tracker_connection.cpp b/src/http_tracker_connection.cpp
index 735c42967..5602195ef 100644
--- a/src/http_tracker_connection.cpp
+++ b/src/http_tracker_connection.cpp
@@ -161,7 +161,7 @@ namespace libtorrent
{
if (tracker_req().i2pconn->local_endpoint().empty())
{
- fail(errors::no_i2p_endpoint, -1, "Waiting for i2p acceptor from SAM bridge", 5);
+ fail(errors::no_i2p_endpoint, -1, "Waiting for i2p acceptor from SAM bridge", seconds32(5));
return;
}
else
@@ -427,10 +427,9 @@ namespace libtorrent
return resp;
}
- int interval = int(e.dict_find_int_value("interval", 0));
// if no interval is specified, default to 30 minutes
- if (interval == 0) interval = 1800;
- int min_interval = int(e.dict_find_int_value("min interval", 30));
+ seconds32 interval(e.dict_find_int_value("interval", 1800));
+ seconds32 const min_interval(e.dict_find_int_value("min interval", 30));
resp.interval = interval;
resp.min_interval = min_interval;
diff --git a/src/session_impl.cpp b/src/session_impl.cpp
index dce90285d..46ca74885 100644
--- a/src/session_impl.cpp
+++ b/src/session_impl.cpp
@@ -6923,7 +6923,7 @@ namespace aux {
"external ip: %s\n"
"we connected to: %s\n"
"peers:"
- , resp.interval
+ , resp.interval.count()
, print_address(resp.external_ip).c_str()
, print_address(tracker_ip).c_str());
@@ -6946,7 +6946,7 @@ namespace aux {
void tracker_logger::tracker_request_error(tracker_request const&
, int response_code, error_code const& ec, const std::string& str
- , int retry_interval)
+ , seconds32 const retry_interval)
{
TORRENT_UNUSED(retry_interval);
debug_log("*** tracker error: %d: %s %s"
diff --git a/src/time.cpp b/src/time.cpp
index 64e180cec..b7b3dd154 100644
--- a/src/time.cpp
+++ b/src/time.cpp
@@ -42,6 +42,7 @@ namespace libtorrent { namespace aux
std::atomic g_current_time(clock_type::now());
}
time_point time_now() { return aux::g_current_time.load(); }
+ time_point32 time_now32() { return time_point_cast(clock_type::now()); }
void update_time_now() { g_current_time.store(clock_type::now()); }
} }
diff --git a/src/torrent.cpp b/src/torrent.cpp
index 44521d22c..434178d0f 100644
--- a/src/torrent.cpp
+++ b/src/torrent.cpp
@@ -180,10 +180,7 @@ namespace libtorrent
#endif
, m_stats_counters(ses.stats_counters())
, m_storage_constructor(p.storage)
- , m_added_time(time(nullptr))
, m_info_hash(info_hash)
- , m_last_saved_resume(aux::time_now())
- , m_started(aux::time_now())
, m_error_file(torrent_status::error_file_none)
, m_sequence_number(seq)
, m_announce_to_trackers((p.flags & add_torrent_params::flag_paused) == 0)
@@ -193,15 +190,12 @@ namespace libtorrent
, m_storage_mode(p.storage_mode)
, m_announcing(false)
, m_added(false)
- , m_active_time(0)
- , m_finished_time(0)
, m_sequential_download(false)
, m_auto_sequential(false)
, m_seed_mode(false)
, m_super_seeding(false)
, m_stop_when_ready((p.flags & add_torrent_params::flag_stop_when_ready) != 0)
, m_need_save_resume_data((p.flags & add_torrent_params::flag_need_save_resume) != 0)
- , m_seeding_time(0)
, m_max_uploads((1<<24)-1)
, m_num_uploads(0)
, m_need_connect_boost(true)
@@ -963,7 +957,7 @@ namespace libtorrent
p->cancel_all_requests();
}
// this is used to try leaving upload only mode periodically
- m_upload_mode_time = aux::time_now();
+ m_upload_mode_time = aux::time_now32();
}
else if (m_peer_list)
{
@@ -1496,7 +1490,7 @@ namespace libtorrent
// this is needed for openssl < 1.0 to decrypt keys created by openssl 1.0+
OpenSSL_add_all_algorithms();
- std::uint64_t now = clock_type::now().time_since_epoch().count();
+ std::uint64_t const now = clock_type::now().time_since_epoch().count();
// assume 9 bits of entropy (i.e. about 1 millisecond)
RAND_add(&now, 8, 1.125);
RAND_add(&info_hash()[0], 20, 3);
@@ -2550,7 +2544,7 @@ namespace libtorrent
#ifndef TORRENT_DISABLE_LOGGING
debug_log("START DHT announce");
- m_dht_start_time = clock_type::now();
+ m_dht_start_time = aux::time_now();
#endif
// if we're a seed, we tell the DHT for better scrape stats
@@ -2690,7 +2684,7 @@ namespace libtorrent
req.num_want = (req.event == tracker_request::stopped)
? 0 : settings().get_int(settings_pack::num_want);
- time_point const now = clock_type::now();
+ time_point32 const now = aux::time_now32();
// the tier is kept as INT_MAX until we find the first
// tracker that works, then it's set to that tracker's
@@ -2760,7 +2754,7 @@ namespace libtorrent
if ((protocol == "http" || protocol == "https")
&& proxy_type == settings_pack::none)
{
- ae.next_announce = now + minutes(10);
+ ae.next_announce = now + minutes32(10);
if (m_ses.alerts().should_post()
|| req.triggered_manually)
{
@@ -2778,7 +2772,7 @@ namespace libtorrent
&& proxy_type != settings_pack::socks5_pw
&& proxy_type != settings_pack::i2p_proxy)
{
- ae.next_announce = now + minutes(10);
+ ae.next_announce = now + minutes32(10);
if (m_ses.alerts().should_post()
|| req.triggered_manually)
{
@@ -2822,8 +2816,8 @@ namespace libtorrent
}
ae.updating = true;
- ae.next_announce = now + seconds(20);
- ae.min_announce = now + seconds(10);
+ ae.next_announce = now + seconds32(20);
+ ae.min_announce = now + seconds32(10);
if (m_ses.alerts().should_post())
{
@@ -2965,11 +2959,10 @@ namespace libtorrent
m_ses.set_external_address(resp.external_ip
, aux::session_interface::source_tracker, tracker_ip);
- time_point now = aux::time_now();
+ time_point32 now = aux::time_now32();
- int interval = resp.interval;
- if (interval < settings().get_int(settings_pack::min_announce_interval))
- interval = settings().get_int(settings_pack::min_announce_interval);
+ auto interval = std::max(resp.interval, seconds32(
+ settings().get_int(settings_pack::min_announce_interval)));
announce_entry* ae = find_tracker(r.url);
if (ae)
@@ -2984,8 +2977,8 @@ namespace libtorrent
ae->verified = true;
ae->updating = false;
ae->fails = 0;
- ae->next_announce = now + seconds(interval);
- ae->min_announce = now + seconds(resp.min_interval);
+ ae->next_announce = now + interval;
+ ae->min_announce = now + resp.min_interval;
int tracker_index = int(ae - &m_trackers[0]);
m_last_working_tracker = std::int8_t(prioritize_tracker(tracker_index));
@@ -3022,7 +3015,7 @@ namespace libtorrent
"resolved to: %s\n"
"we connected to: %s\n"
"peers:"
- , interval
+ , interval.count()
, print_address(resp.external_ip).c_str()
, resolved_to.c_str()
, print_address(tracker_ip).c_str());
@@ -3283,7 +3276,8 @@ namespace libtorrent
{
for (auto& e : m_trackers)
{
- e.next_announce = std::max(t, e.min_announce) + seconds(1);
+ e.next_announce = std::max(time_point_cast(t)
+ , e.min_announce) + seconds(1);
e.triggered_manually = true;
}
}
@@ -3292,10 +3286,11 @@ namespace libtorrent
if (tracker_idx < 0 || tracker_idx >= int(m_trackers.size()))
return;
announce_entry& e = m_trackers[tracker_idx];
- e.next_announce = std::max(t, e.min_announce) + seconds(1);
+ e.next_announce = std::max(time_point_cast(t)
+ , e.min_announce) + seconds32(1);
e.triggered_manually = true;
}
- update_tracker_timer(clock_type::now());
+ update_tracker_timer(aux::time_now32());
}
#ifndef TORRENT_NO_DEPRECATE
@@ -3851,7 +3846,7 @@ namespace libtorrent
// is deallocated by the torrent once it starts seeding
}
- m_last_download = aux::time_now();
+ m_last_download = aux::time_now32();
if (m_share_mode)
recalc_share_mode();
@@ -5735,7 +5730,7 @@ namespace libtorrent
#endif
// unavailable, retry in 30 minutes
- web->retry = aux::time_now() + minutes(30);
+ web->retry = aux::time_now32() + minutes32(30);
return;
}
@@ -7245,7 +7240,7 @@ namespace libtorrent
set_state(torrent_status::finished);
set_queue_position(-1);
- m_became_finished = aux::time_now();
+ m_became_finished = aux::time_now32();
// we have to call completed() before we start
// disconnecting peers, since there's an assert
@@ -7362,11 +7357,11 @@ namespace libtorrent
maybe_done_flushing();
set_state(torrent_status::seeding);
- m_became_seed = aux::time_now();
+ m_became_seed = aux::time_now32();
if (!m_announcing) return;
- time_point now = aux::time_now();
+ time_point32 const now = aux::time_now32();
for (std::vector::iterator i = m_trackers.begin()
, end(m_trackers.end()); i != end; ++i)
{
@@ -8346,7 +8341,7 @@ namespace libtorrent
}
m_need_save_resume_data = false;
- m_last_saved_resume = aux::time_now();
+ m_last_saved_resume = aux::time_now32();
m_save_resume_flags = std::uint8_t(flags);
state_updated();
@@ -8678,7 +8673,7 @@ namespace libtorrent
if (alerts().should_post())
alerts().emplace_alert(get_handle());
- m_started = aux::time_now();
+ m_started = aux::time_now32();
if (is_seed()) m_became_seed = m_started;
if (is_finished()) m_became_finished = m_started;
@@ -8705,7 +8700,7 @@ namespace libtorrent
do_connect_boost();
}
- void torrent::update_tracker_timer(time_point const now)
+ void torrent::update_tracker_timer(time_point32 const now)
{
TORRENT_ASSERT(is_single_thread());
if (!m_announcing)
@@ -8716,7 +8711,7 @@ namespace libtorrent
return;
}
- time_point next_announce = max_time();
+ time_point32 next_announce = time_point32::max();
int tier = INT_MAX;
bool found_working = false;
@@ -8751,7 +8746,7 @@ namespace libtorrent
}
else
{
- time_point next_tracker_announce = (std::max)(t.next_announce, t.min_announce);
+ time_point32 next_tracker_announce = std::max(t.next_announce, t.min_announce);
if (next_tracker_announce < next_announce
&& (!found_working || t.is_working()))
next_announce = next_tracker_announce;
@@ -8858,7 +8853,7 @@ namespace libtorrent
m_announcing = false;
- time_point now = aux::time_now();
+ time_point32 const now = aux::time_now32();
for (auto& t : m_trackers)
{
t.next_announce = now;
@@ -8904,8 +8899,7 @@ namespace libtorrent
if(!m_upload_mode)
return seconds32(0);
- return duration_cast(
- aux::time_now() - m_upload_mode_time);
+ return aux::time_now32() - m_upload_mode_time;
}
void torrent::second_tick(int tick_interval_ms)
@@ -9890,7 +9884,7 @@ namespace libtorrent
if (i == m_web_seeds.end()) return;
if (i->removed) return;
if (retry == 0) retry = settings().get_int(settings_pack::urlseed_wait_retry);
- i->retry = aux::time_now() + seconds(retry);
+ i->retry = aux::time_now32() + seconds32(retry);
}
torrent_state torrent::get_peer_list_state()
@@ -10497,7 +10491,7 @@ namespace libtorrent
{
INVARIANT_CHECK;
- time_point now = aux::time_now();
+ time_point32 const now = aux::time_now32();
st->handle = get_handle();
st->info_hash = info_hash();
@@ -10793,7 +10787,7 @@ namespace libtorrent
void torrent::tracker_request_error(tracker_request const& r
, int response_code, error_code const& ec, std::string const& msg
- , int retry_interval)
+ , seconds32 const retry_interval)
{
TORRENT_ASSERT(is_single_thread());
@@ -10812,7 +10806,7 @@ namespace libtorrent
announce_entry* ae = find_tracker(r.url);
if (ae)
{
- ae->failed(seconds(settings().get_int(settings_pack::tracker_backoff))
+ ae->failed(settings().get_int(settings_pack::tracker_backoff)
, retry_interval);
ae->last_error = ec;
ae->message = msg;
@@ -10854,7 +10848,7 @@ namespace libtorrent
// announce to the next working tracker
if ((!m_abort && !is_paused()) || r.event == tracker_request::stopped)
announce_with_tracker(r.event);
- update_tracker_timer(aux::time_now());
+ update_tracker_timer(aux::time_now32());
}
#ifndef TORRENT_DISABLE_LOGGING
diff --git a/src/tracker_manager.cpp b/src/tracker_manager.cpp
index d22bbc341..97358ce9b 100644
--- a/src/tracker_manager.cpp
+++ b/src/tracker_manager.cpp
@@ -155,7 +155,7 @@ namespace libtorrent
}
void tracker_connection::fail(error_code const& ec, int code
- , char const* msg, int interval, int min_interval)
+ , char const* msg, seconds32 const interval, seconds32 const min_interval)
{
// we need to post the error to avoid deadlock
get_io_service().post(std::bind(&tracker_connection::fail_impl
@@ -163,11 +163,11 @@ namespace libtorrent
}
void tracker_connection::fail_impl(error_code const& ec, int code
- , std::string msg, int interval, int min_interval)
+ , std::string msg, seconds32 const interval, seconds32 const min_interval)
{
std::shared_ptr cb = requester();
if (cb) cb->tracker_request_error(m_req, code, ec, msg.c_str()
- , interval == 0 ? min_interval : interval);
+ , interval.count() == 0 ? min_interval : interval);
close();
}
@@ -284,7 +284,7 @@ namespace libtorrent
if (std::shared_ptr r = c.lock())
ios.post(std::bind(&request_callback::tracker_request_error, r, req
, -1, error_code(errors::unsupported_url_protocol)
- , "", 0));
+ , "", seconds32(0)));
}
bool tracker_manager::incoming_packet(udp::endpoint const& ep
diff --git a/src/udp_tracker_connection.cpp b/src/udp_tracker_connection.cpp
index 1a9e741a0..14abf7ab5 100644
--- a/src/udp_tracker_connection.cpp
+++ b/src/udp_tracker_connection.cpp
@@ -128,7 +128,7 @@ namespace libtorrent
}
void udp_tracker_connection::fail(error_code const& ec, int code
- , char const* msg, int interval, int min_interval)
+ , char const* msg, seconds32 const interval, seconds32 const min_interval)
{
// m_target failed. remove it from the endpoint list
auto const i = std::find(m_endpoints.begin()
@@ -593,8 +593,8 @@ namespace libtorrent
tracker_response resp;
- resp.interval = aux::read_int32(buf);
- resp.min_interval = 60;
+ resp.interval = seconds32(aux::read_int32(buf));
+ resp.min_interval = seconds32(60);
resp.incomplete = aux::read_int32(buf);
resp.complete = aux::read_int32(buf);
int const num_peers = int(buf.size()) / 6;
diff --git a/test/test_primitives.cpp b/test/test_primitives.cpp
index 9b6fa8e32..f152ce672 100644
--- a/test/test_primitives.cpp
+++ b/test/test_primitives.cpp
@@ -51,10 +51,10 @@ TORRENT_TEST(primitives)
// on failing announces
announce_entry ae("dummy");
int last = 0;
- auto const tracker_backoff = seconds(250);
+ auto const tracker_backoff = 250;
for (int i = 0; i < 10; ++i)
{
- ae.failed(tracker_backoff, 5);
+ ae.failed(tracker_backoff, seconds32(5));
#ifndef TORRENT_NO_DEPRECATE
int const delay = ae.next_announce_in();
#else
diff --git a/test/test_tracker.cpp b/test/test_tracker.cpp
index 2b2251bda..f44195914 100644
--- a/test/test_tracker.cpp
+++ b/test/test_tracker.cpp
@@ -183,8 +183,8 @@ TORRENT_TEST(parse_interval)
TEST_EQUAL(ec, error_code());
TEST_EQUAL(resp.peers.size(), 0);
TEST_EQUAL(resp.peers4.size(), 0);
- TEST_EQUAL(resp.interval, 1042);
- TEST_EQUAL(resp.min_interval, 10);
+ TEST_EQUAL(resp.interval.count(), 1042);
+ TEST_EQUAL(resp.min_interval.count(), 10);
}
TORRENT_TEST(parse_warning)