Skip to content

Commit 4efde0f

Browse files
committed
Merge pull request #10320
4486925 Transition asio::deadline_timer to asio::steady_timer (Lee *!* Clagett)
2 parents 3bfa59d + 4486925 commit 4efde0f

19 files changed

+67
-67
lines changed

contrib/epee/include/net/abstract_tcp_server2.h

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ namespace net_utils
404404
try_connect_result_t try_connect(connection_ptr new_connection_l, const std::string& adr, const std::string& port, boost::asio::ip::tcp::socket &sock_, const boost::asio::ip::tcp::endpoint &remote_endpoint, const std::string &bind_ip, uint32_t conn_timeout, epee::net_utils::ssl_support_t ssl_support);
405405
bool connect(const std::string& adr, const std::string& port, uint32_t conn_timeot, t_connection_context& cn, const std::string& bind_ip = "0.0.0.0", epee::net_utils::ssl_support_t ssl_support = epee::net_utils::ssl_support_t::e_ssl_support_autodetect);
406406
template<class t_callback>
407-
bool connect_async(const std::string& adr, const std::string& port, uint32_t conn_timeot, const t_callback &cb, const std::string& bind_ip = "0.0.0.0", epee::net_utils::ssl_support_t ssl_support = epee::net_utils::ssl_support_t::e_ssl_support_autodetect, t_connection_context&& initial = t_connection_context{});
407+
bool connect_async(const std::string& adr, const std::string& port, std::chrono::milliseconds conn_timeout, const t_callback &cb, const std::string& bind_ip = "0.0.0.0", epee::net_utils::ssl_support_t ssl_support = epee::net_utils::ssl_support_t::e_ssl_support_autodetect, t_connection_context&& initial = t_connection_context{});
408408

409409
boost::asio::ssl::context& get_ssl_context() noexcept
410410
{
@@ -445,42 +445,43 @@ namespace net_utils
445445
idle_callback_conext_base(boost::asio::io_context& io_serice):
446446
m_timer(io_serice)
447447
{}
448-
boost::asio::deadline_timer m_timer;
448+
boost::asio::steady_timer m_timer;
449449
};
450450

451451
template <class t_handler>
452452
struct idle_callback_conext: public idle_callback_conext_base
453453
{
454-
idle_callback_conext(boost::asio::io_context& io_serice, t_handler& h, uint64_t period):
454+
idle_callback_conext(boost::asio::io_context& io_serice, t_handler& h, std::chrono::milliseconds period):
455455
idle_callback_conext_base(io_serice),
456-
m_handler(h)
457-
{this->m_period = period;}
456+
m_handler(h),
457+
m_period(period)
458+
{}
458459

459460
t_handler m_handler;
460461
virtual bool call_handler()
461462
{
462463
return m_handler();
463464
}
464-
uint64_t m_period;
465+
const std::chrono::milliseconds m_period;
465466
};
466467

467468
template<class t_handler>
468-
bool add_idle_handler(t_handler t_callback, uint64_t timeout_ms)
469+
bool add_idle_handler(t_handler t_callback, const std::chrono::milliseconds timeout)
469470
{
470-
boost::shared_ptr<idle_callback_conext<t_handler>> ptr(new idle_callback_conext<t_handler>(io_context_, t_callback, timeout_ms));
471+
std::shared_ptr<idle_callback_conext<t_handler>> ptr(std::make_shared<idle_callback_conext<t_handler>>(io_context_, t_callback, timeout));
471472
//needed call handler here ?...
472-
ptr->m_timer.expires_from_now(boost::posix_time::milliseconds(ptr->m_period));
473+
ptr->m_timer.expires_after(ptr->m_period);
473474
ptr->m_timer.async_wait(boost::bind(&boosted_tcp_server<t_protocol_handler>::global_timer_handler<t_handler>, this, ptr));
474475
return true;
475476
}
476477

477478
template<class t_handler>
478-
bool global_timer_handler(/*const boost::system::error_code& err, */boost::shared_ptr<idle_callback_conext<t_handler>> ptr)
479+
bool global_timer_handler(/*const boost::system::error_code& err, */std::shared_ptr<idle_callback_conext<t_handler>> ptr)
479480
{
480481
//if handler return false - he don't want to be called anymore
481482
if(!ptr->call_handler())
482483
return true;
483-
ptr->m_timer.expires_from_now(boost::posix_time::milliseconds(ptr->m_period));
484+
ptr->m_timer.expires_after(ptr->m_period);
484485
ptr->m_timer.async_wait(boost::bind(&boosted_tcp_server<t_protocol_handler>::global_timer_handler<t_handler>, this, ptr));
485486
return true;
486487
}

contrib/epee/include/net/abstract_tcp_server2.inl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
#include <boost/chrono.hpp>
3939
#include <boost/utility/value_init.hpp>
4040
#include <boost/asio/bind_executor.hpp>
41-
#include <boost/asio/deadline_timer.hpp>
41+
#include <boost/asio/steady_timer.hpp>
4242
#include <boost/date_time/posix_time/posix_time.hpp> // TODO
4343
#include <boost/thread/condition_variable.hpp> // TODO
4444
#include <boost/make_shared.hpp>
@@ -1894,7 +1894,7 @@ namespace net_utils
18941894
}
18951895
//---------------------------------------------------------------------------------
18961896
template<class t_protocol_handler> template<class t_callback>
1897-
bool boosted_tcp_server<t_protocol_handler>::connect_async(const std::string& adr, const std::string& port, uint32_t conn_timeout, const t_callback &cb, const std::string& bind_ip, epee::net_utils::ssl_support_t ssl_support, t_connection_context&& initial)
1897+
bool boosted_tcp_server<t_protocol_handler>::connect_async(const std::string& adr, const std::string& port, const std::chrono::milliseconds conn_timeout, const t_callback &cb, const std::string& bind_ip, epee::net_utils::ssl_support_t ssl_support, t_connection_context&& initial)
18981898
{
18991899
TRY_ENTRY();
19001900
connection_ptr new_connection_l(new connection<t_protocol_handler>(io_context_, m_state, m_connection_type, ssl_support, std::move(initial)) );
@@ -1975,14 +1975,14 @@ namespace net_utils
19751975
}
19761976
}
19771977

1978-
boost::shared_ptr<boost::asio::deadline_timer> sh_deadline(new boost::asio::deadline_timer(io_context_));
1978+
std::shared_ptr<boost::asio::steady_timer> sh_deadline(std::make_shared<boost::asio::steady_timer>(io_context_));
19791979
//start deadline
1980-
sh_deadline->expires_from_now(boost::posix_time::milliseconds(conn_timeout));
1980+
sh_deadline->expires_after(conn_timeout);
19811981
sh_deadline->async_wait([=](const boost::system::error_code& error)
19821982
{
19831983
if(error != boost::asio::error::operation_aborted)
19841984
{
1985-
_dbg3("Failed to connect to " << adr << ':' << port << ", because of timeout (" << conn_timeout << ")");
1985+
_dbg3("Failed to connect to " << adr << ':' << port << ", because of timeout (" << conn_timeout.count() << ")");
19861986
new_connection_l->socket().close();
19871987
}
19881988
});

contrib/epee/include/net/levin_base.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#ifndef _LEVIN_BASE_H_
3030
#define _LEVIN_BASE_H_
3131

32+
#include <chrono>
3233
#include <cstdint>
3334

3435
#include "byte_stream.h"
@@ -72,7 +73,7 @@ namespace levin
7273
#pragma pack(pop)
7374

7475

75-
#define LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED 0
76+
constexpr const std::chrono::milliseconds LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED{0};
7677
#define LEVIN_INITIAL_MAX_PACKET_SIZE 256*1024 // 256 KiB before handshake
7778
#define LEVIN_DEFAULT_MAX_PACKET_SIZE 100000000 //100MB by default after handshake
7879

contrib/epee/include/net/levin_protocol_handler_async.h

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
//
2626

2727
#pragma once
28-
#include <boost/asio/deadline_timer.hpp>
28+
#include <boost/asio/steady_timer.hpp>
2929
#include <boost/uuid/uuid_generators.hpp>
3030
#include <boost/unordered_map.hpp>
3131
#include <boost/smart_ptr/make_shared.hpp>
@@ -100,10 +100,10 @@ class async_protocol_handler_config
100100
typedef t_connection_context connection_context;
101101
uint64_t m_initial_max_packet_size;
102102
uint64_t m_max_packet_size;
103-
uint64_t m_invoke_timeout;
103+
std::chrono::milliseconds m_invoke_timeout;
104104

105105
template<class callback_t>
106-
int invoke_async(int command, message_writer in_msg, boost::uuids::uuid connection_id, const callback_t &cb, size_t timeout = LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED);
106+
int invoke_async(int command, message_writer in_msg, boost::uuids::uuid connection_id, const callback_t &cb, std::chrono::milliseconds timeout = LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED);
107107

108108
int send(epee::byte_slice message, const boost::uuids::uuid& connection_id);
109109
bool close(boost::uuids::uuid connection_id);
@@ -190,19 +190,19 @@ class async_protocol_handler
190190
template <class callback_t>
191191
struct anvoke_handler: invoke_response_handler_base
192192
{
193-
anvoke_handler(const callback_t& cb, uint64_t timeout, async_protocol_handler& con, int command)
193+
anvoke_handler(const callback_t& cb, const std::chrono::milliseconds timeout, async_protocol_handler& con, int command)
194194
:m_cb(cb), m_timeout(timeout), m_con(con), m_timer(con.m_pservice_endpoint->get_io_context()), m_timer_started(false),
195195
m_cancel_timer_called(false), m_timer_cancelled(false), m_command(command)
196196
{
197197
if(m_con.start_outer_call())
198198
{
199-
MDEBUG(con.get_context_ref() << "anvoke_handler, timeout: " << timeout);
200-
m_timer.expires_from_now(boost::posix_time::milliseconds(timeout));
199+
MDEBUG(con.get_context_ref() << "anvoke_handler, timeout: " << timeout.count());
200+
m_timer.expires_after(timeout);
201201
m_timer.async_wait([&con, command, cb, timeout](const boost::system::error_code& ec)
202202
{
203203
if(ec == boost::asio::error::operation_aborted)
204204
return;
205-
MINFO(con.get_context_ref() << "Timeout on invoke operation happened, command: " << command << " timeout: " << timeout);
205+
MINFO(con.get_context_ref() << "Timeout on invoke operation happened, command: " << command << " timeout: " << timeout.count());
206206
epee::span<const uint8_t> fake;
207207
cb(LEVIN_ERROR_CONNECTION_TIMEDOUT, fake, con.get_context_ref());
208208
con.close();
@@ -215,11 +215,11 @@ class async_protocol_handler
215215
{}
216216
callback_t m_cb;
217217
async_protocol_handler& m_con;
218-
boost::asio::deadline_timer m_timer;
218+
boost::asio::steady_timer m_timer;
219219
bool m_timer_started;
220220
bool m_cancel_timer_called;
221221
bool m_timer_cancelled;
222-
uint64_t m_timeout;
222+
const std::chrono::milliseconds m_timeout;
223223
int m_command;
224224
virtual bool handle(int res, const epee::span<const uint8_t> buff, typename async_protocol_handler::connection_context& context)
225225
{
@@ -247,26 +247,24 @@ class async_protocol_handler
247247
if(!m_cancel_timer_called)
248248
{
249249
m_cancel_timer_called = true;
250-
boost::system::error_code ignored_ec;
251-
m_timer_cancelled = 1 == m_timer.cancel(ignored_ec);
250+
m_timer_cancelled = 1 == m_timer.cancel();
252251
}
253252
return m_timer_cancelled;
254253
}
255254
virtual void reset_timer()
256255
{
257-
boost::system::error_code ignored_ec;
258-
if (!m_cancel_timer_called && m_timer.cancel(ignored_ec) > 0)
256+
if (!m_cancel_timer_called && m_timer.cancel() > 0)
259257
{
260258
callback_t& cb = m_cb;
261-
uint64_t timeout = m_timeout;
259+
const auto timeout = m_timeout;
262260
async_protocol_handler& con = m_con;
263261
int command = m_command;
264-
m_timer.expires_from_now(boost::posix_time::milliseconds(m_timeout));
262+
m_timer.expires_after(m_timeout);
265263
m_timer.async_wait([&con, cb, command, timeout](const boost::system::error_code& ec)
266264
{
267265
if(ec == boost::asio::error::operation_aborted)
268266
return;
269-
MINFO(con.get_context_ref() << "Timeout on invoke operation happened, command: " << command << " timeout: " << timeout);
267+
MINFO(con.get_context_ref() << "Timeout on invoke operation happened, command: " << command << " timeout: " << timeout.count());
270268
epee::span<const uint8_t> fake;
271269
cb(LEVIN_ERROR_CONNECTION_TIMEDOUT, fake, con.get_context_ref());
272270
con.close();
@@ -279,7 +277,7 @@ class async_protocol_handler
279277
std::list<boost::shared_ptr<invoke_response_handler_base> > m_invoke_response_handlers;
280278

281279
template<class callback_t>
282-
bool add_invoke_response_handler(const callback_t &cb, uint64_t timeout, async_protocol_handler& con, int command)
280+
bool add_invoke_response_handler(const callback_t &cb, const std::chrono::milliseconds timeout, async_protocol_handler& con, int command)
283281
{
284282
CRITICAL_REGION_LOCAL(m_invoke_response_handlers_lock);
285283
if (m_protocol_released)
@@ -604,7 +602,7 @@ class async_protocol_handler
604602
}
605603

606604
template<class callback_t>
607-
bool async_invoke(int command, message_writer in_msg, const callback_t &cb, size_t timeout = LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED)
605+
bool async_invoke(int command, message_writer in_msg, const callback_t &cb, std::chrono::milliseconds timeout = LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED)
608606
{
609607
misc_utils::auto_scope_leave_caller scope_exit_handler = misc_utils::create_scope_leave_handler(
610608
boost::bind(&async_protocol_handler::finish_outer_call, this));
@@ -752,7 +750,7 @@ int async_protocol_handler_config<t_connection_context>::find_and_lock_connectio
752750
}
753751
//------------------------------------------------------------------------------------------
754752
template<class t_connection_context> template<class callback_t>
755-
int async_protocol_handler_config<t_connection_context>::invoke_async(int command, message_writer in_msg, boost::uuids::uuid connection_id, const callback_t &cb, size_t timeout)
753+
int async_protocol_handler_config<t_connection_context>::invoke_async(int command, message_writer in_msg, boost::uuids::uuid connection_id, const callback_t &cb, const std::chrono::milliseconds timeout)
756754
{
757755
async_protocol_handler<t_connection_context>* aph;
758756
int r = find_and_lock_connection(connection_id, aph);

contrib/epee/include/net/network_throttle.hpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@
5656
#include <boost/uuid/random_generator.hpp>
5757
#include <boost/chrono.hpp>
5858
#include <boost/utility/value_init.hpp>
59-
#include <boost/asio/deadline_timer.hpp>
6059
#include <boost/date_time/posix_time/posix_time.hpp>
6160
#include "misc_language.h"
6261
#include <sstream>

contrib/epee/include/storages/levin_abstract_invoke2.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ namespace epee
5757
namespace net_utils
5858
{
5959
template<class t_result, class t_arg, class callback_t, class t_transport>
60-
bool async_invoke_remote_command2(const epee::net_utils::connection_context_base &context, int command, const t_arg& out_struct, t_transport& transport, const callback_t &cb, size_t inv_timeout = LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED)
60+
bool async_invoke_remote_command2(const epee::net_utils::connection_context_base &context, int command, const t_arg& out_struct, t_transport& transport, const callback_t &cb, const std::chrono::milliseconds inv_timeout = levin::LEVIN_DEFAULT_TIMEOUT_PRECONFIGURED)
6161
{
6262
const boost::uuids::uuid &conn_id = context.m_connection_id;
6363
typename serialization::portable_storage stg;

src/debug_utilities/object_sizes.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ int main(int argc, char* argv[])
7272
SL(boost::thread);
7373
SL(boost::asio::io_context);
7474
SL(boost::asio::executor_work_guard<boost::asio::io_context::executor_type>);
75-
SL(boost::asio::deadline_timer);
75+
SL(boost::asio::steady_timer);
7676

7777
SL(cryptonote::DB_ERROR);
7878
SL(cryptonote::mdb_txn_safe);

src/device_trezor/trezor/transport.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ namespace trezor{
571571
return ping_int();
572572
}
573573

574-
bool UdpTransport::ping_int(boost::posix_time::time_duration timeout){
574+
bool UdpTransport::ping_int(boost::asio::steady_timer::duration timeout){
575575
require_socket();
576576
try {
577577
std::string req = "PINGPING";
@@ -619,7 +619,7 @@ namespace trezor{
619619
m_socket.reset(new udp::socket(m_io_service));
620620
m_socket->open(udp::v4());
621621

622-
m_deadline.expires_at(boost::posix_time::pos_infin);
622+
m_deadline.expires_after(boost::asio::steady_timer::duration::max());
623623
check_deadline();
624624

625625
m_proto->session_begin(*this);
@@ -701,14 +701,14 @@ namespace trezor{
701701
return static_cast<size_t>(len);
702702
}
703703

704-
ssize_t UdpTransport::receive(void * buff, size_t size, boost::system::error_code * error_code, bool no_throw, boost::posix_time::time_duration timeout){
704+
ssize_t UdpTransport::receive(void * buff, size_t size, boost::system::error_code * error_code, bool no_throw, const boost::asio::steady_timer::duration timeout){
705705
boost::system::error_code ec;
706706
boost::asio::mutable_buffer buffer = boost::asio::buffer(buff, size);
707707

708708
require_socket();
709709

710710
// Set a deadline for the asynchronous operation.
711-
m_deadline.expires_from_now(timeout);
711+
m_deadline.expires_after(timeout);
712712

713713
// Set up the variables that receive the result of the asynchronous
714714
// operation. The error code is set to would_block to signal that the
@@ -766,7 +766,7 @@ namespace trezor{
766766
// Check whether the deadline has passed. We compare the deadline against
767767
// the current time since a new asynchronous operation may have moved the
768768
// deadline before this actor had a chance to run.
769-
if (m_deadline.expires_at() <= boost::asio::deadline_timer::traits_type::now())
769+
if (m_deadline.expiry() <= boost::asio::steady_timer::clock_type::now())
770770
{
771771
// The deadline has passed. The outstanding asynchronous operation needs
772772
// to be cancelled so that the blocked receive() function will return.
@@ -778,7 +778,7 @@ namespace trezor{
778778

779779
// There is no longer an active deadline. The expiry is set to positive
780780
// infinity so that the actor takes no action until a new deadline is set.
781-
m_deadline.expires_at(boost::posix_time::pos_infin);
781+
m_deadline.expires_after(boost::asio::steady_timer::duration::max());
782782
}
783783

784784
// Put the actor back to sleep.

src/device_trezor/trezor/transport.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232

3333

3434
#include <boost/asio.hpp>
35-
#include <boost/asio/deadline_timer.hpp>
35+
#include <boost/asio/steady_timer.hpp>
3636
#include <boost/array.hpp>
3737
#include <boost/utility/string_ref.hpp>
3838

@@ -233,19 +233,19 @@ namespace trezor {
233233

234234
private:
235235
void require_socket();
236-
ssize_t receive(void * buff, size_t size, boost::system::error_code * error_code=nullptr, bool no_throw=false, boost::posix_time::time_duration timeout=boost::posix_time::seconds(10));
236+
ssize_t receive(void * buff, size_t size, boost::system::error_code * error_code=nullptr, bool no_throw=false, boost::asio::steady_timer::duration timeout = std::chrono::seconds(10));
237237
void check_deadline();
238238
static void handle_receive(const boost::system::error_code& ec, std::size_t length,
239239
boost::system::error_code* out_ec, std::size_t* out_length);
240-
bool ping_int(boost::posix_time::time_duration timeout=boost::posix_time::milliseconds(1500));
240+
bool ping_int(boost::asio::steady_timer::duration = std::chrono::milliseconds(1500));
241241

242242
std::shared_ptr<Protocol> m_proto;
243243
std::string m_device_host;
244244
int m_device_port;
245245

246246
std::unique_ptr<udp::socket> m_socket;
247247
boost::asio::io_context m_io_service;
248-
boost::asio::deadline_timer m_deadline;
248+
boost::asio::steady_timer m_deadline;
249249
udp::endpoint m_endpoint;
250250
};
251251

src/p2p/net_node.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ namespace nodetool
230230
m_config.m_net_config.packet_max_size = P2P_DEFAULT_PACKET_MAX_SIZE;
231231
m_config.m_net_config.config_id = 0;
232232
m_config.m_net_config.connection_timeout = P2P_DEFAULT_CONNECTION_TIMEOUT;
233-
m_config.m_net_config.ping_connection_timeout = P2P_DEFAULT_PING_CONNECTION_TIMEOUT;
233+
m_config.m_net_config.ping_connection_timeout = std::chrono::milliseconds{P2P_DEFAULT_PING_CONNECTION_TIMEOUT};
234234
m_config.m_net_config.send_peerlist_sz = P2P_DEFAULT_PEERS_IN_HANDSHAKE;
235235
m_config.m_support_flags = 0; // only set in public zone
236236
}

0 commit comments

Comments
 (0)