16 _io_service(_service->GetAsioService()),
17 _strand(*_io_service),
18 _strand_required(_service->IsStrandRequired()),
21 assert((
service !=
nullptr) &&
"Asio service is invalid!");
23 throw CppCommon::ArgumentException(
"Asio service is invalid!");
26 Timer::Timer(
const std::shared_ptr<Service>& service,
const CppCommon::UtcTime& time)
28 _io_service(_service->GetAsioService()),
29 _strand(*_io_service),
30 _strand_required(_service->IsStrandRequired()),
31 _timer(*_io_service, time.chrono())
33 assert((
service !=
nullptr) &&
"Asio service is invalid!");
35 throw CppCommon::ArgumentException(
"Asio service is invalid!");
38 Timer::Timer(
const std::shared_ptr<Service>& service,
const CppCommon::Timespan& timespan)
40 _io_service(_service->GetAsioService()),
41 _strand(*_io_service),
42 _strand_required(_service->IsStrandRequired()),
43 _timer(*_io_service, timespan.chrono())
45 assert((
service !=
nullptr) &&
"Asio service is invalid!");
47 throw CppCommon::ArgumentException(
"Asio service is invalid!");
50 Timer::Timer(
const std::shared_ptr<Service>& service,
const std::function<
void(
bool)>& action)
52 _io_service(_service->GetAsioService()),
53 _strand(*_io_service),
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!");
66 Timer::Timer(
const std::shared_ptr<Service>& service,
const std::function<
void(
bool)>& action,
const CppCommon::UtcTime& time)
68 _io_service(_service->GetAsioService()),
69 _strand(*_io_service),
70 _strand_required(_service->IsStrandRequired()),
71 _timer(*_io_service, 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!");
82 Timer::Timer(
const std::shared_ptr<Service>& service,
const std::function<
void(
bool)>& action,
const CppCommon::Timespan& timespan)
84 _io_service(_service->GetAsioService()),
85 _strand(*_io_service),
86 _strand_required(_service->IsStrandRequired()),
87 _timer(*_io_service, 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.expires_at());
105 return CppCommon::Timespan(_timer.expires_from_now());
111 _timer.expires_at(time.chrono(), ec);
126 _timer.expires_from_now(timespan.chrono(), ec);
140 assert((action) &&
"Action function is invalid!");
148 bool Timer::Setup(
const std::function<
void(
bool)>& action,
const CppCommon::UtcTime& time)
150 assert((action) &&
"Action function is invalid!");
158 bool 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)
232 void Timer::SendError(std::error_code ec)
235 if (ec == asio::error::operation_aborted)
238 onError(ec.value(), ec.category().name(), ec.message());
241 void 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.