16 _io_context(_service->GetAsioContext()),
17 _strand(*_io_context),
18 _strand_required(_service->IsStrandRequired()),
21 assert((
service !=
nullptr) &&
"Asio service is invalid!");
23 throw CppCommon::ArgumentException(
"Asio service is invalid!");
28 _io_context(_service->GetAsioContext()),
29 _strand(*_io_context),
30 _strand_required(_service->IsStrandRequired()),
31 _timer(*_io_context, time.chrono())
33 assert((
service !=
nullptr) &&
"Asio service is invalid!");
35 throw CppCommon::ArgumentException(
"Asio service is invalid!");
40 _io_context(_service->GetAsioContext()),
41 _strand(*_io_context),
42 _strand_required(_service->IsStrandRequired()),
43 _timer(*_io_context, timespan.chrono())
45 assert((
service !=
nullptr) &&
"Asio service is invalid!");
47 throw CppCommon::ArgumentException(
"Asio service is invalid!");
52 _io_context(_service->GetAsioContext()),
53 _strand(*_io_context),
54 _strand_required(_service->IsStrandRequired()),
58 assert((
service !=
nullptr) &&
"Asio service is invalid!");
60 throw CppCommon::ArgumentException(
"Asio service is invalid!");
61 assert((action) &&
"Action function is invalid!");
63 throw CppCommon::ArgumentException(
"Action function is invalid!");
66Timer::Timer(
const std::shared_ptr<Service>&
service,
const std::function<
void(
bool)>& action,
const CppCommon::UtcTime& time)
68 _io_context(_service->GetAsioContext()),
69 _strand(*_io_context),
70 _strand_required(_service->IsStrandRequired()),
71 _timer(*_io_context, time.chrono()),
74 assert((
service !=
nullptr) &&
"Asio service is invalid!");
76 throw CppCommon::ArgumentException(
"Asio service is invalid!");
77 assert((action) &&
"Action function is invalid!");
79 throw CppCommon::ArgumentException(
"Action function is invalid!");
82Timer::Timer(
const std::shared_ptr<Service>&
service,
const std::function<
void(
bool)>& action,
const CppCommon::Timespan& timespan)
84 _io_context(_service->GetAsioContext()),
85 _strand(*_io_context),
86 _strand_required(_service->IsStrandRequired()),
87 _timer(*_io_context, timespan.chrono()),
90 assert((
service !=
nullptr) &&
"Asio service is invalid!");
92 throw CppCommon::ArgumentException(
"Asio service is invalid!");
93 assert((action) &&
"Action function is invalid!");
95 throw CppCommon::ArgumentException(
"Action function is invalid!");
100 return CppCommon::UtcTime(_timer.expiry());
105 return CppCommon::Timespan(_timer.expiry() - asio::system_timer::clock_type::now());
112 _timer.expires_at(time.chrono());
114 catch (
const asio::system_error& ex)
116 SendError(ex.code());
127 _timer.expires_after(timespan.chrono());
129 catch (
const asio::system_error& ex)
131 SendError(ex.code());
140 assert((action) &&
"Action function is invalid!");
148bool Timer::Setup(
const std::function<
void(
bool)>& action,
const CppCommon::UtcTime& time)
150 assert((action) &&
"Action function is invalid!");
158bool Timer::Setup(
const std::function<
void(
bool)>& action,
const CppCommon::Timespan& timespan)
160 assert((action) &&
"Action function is invalid!");
165 return Setup(timespan);
170 auto self(this->shared_from_this());
171 auto async_wait_handler = [
this, self](
const std::error_code& ec)
174 if (ec == asio::error::operation_aborted)
187 if (_strand_required)
188 _timer.async_wait(bind_executor(_strand, async_wait_handler));
190 _timer.async_wait(async_wait_handler);
201 if (ec == asio::error::operation_aborted)
223 catch (
const asio::system_error& ex)
225 SendError(ex.code());
232void Timer::SendError(std::error_code ec)
235 if (ec == asio::error::operation_aborted)
238 onError(ec.value(), ec.category().name(), ec.message());
241void Timer::SendTimer(
bool canceled)
virtual bool Cancel()
Cancel any wait operation on the timer.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
virtual bool WaitSync()
Wait for the timer (synchronous).
CppCommon::UtcTime expire_time() const
Get the timer's expiry time as an absolute time.
Timer(const std::shared_ptr< Service > &service)
Initialize timer with a given Asio service.
virtual void onTimer(bool canceled)
Handle timer notification.
CppCommon::Timespan expire_timespan() const
Get the timer's expiry time relative to now.
virtual bool Setup(const CppCommon::UtcTime &time)
Setup the timer with absolute expiry time.
virtual bool WaitAsync()
Wait for the timer (asynchronous).
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
C++ Server project definitions.