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// ------------------------------------------------------------------------------------------
754752template <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);
0 commit comments