9 #ifndef CPPSERVER_ASIO_TIMER_H
10 #define CPPSERVER_ASIO_TIMER_H
14 #include "time/time.h"
15 #include "time/timespan.h"
29 class Timer :
public std::enable_shared_from_this<Timer>
42 Timer(
const std::shared_ptr<Service>&
service,
const CppCommon::UtcTime& time);
48 Timer(
const std::shared_ptr<Service>&
service,
const CppCommon::Timespan& timespan);
54 Timer(
const std::shared_ptr<Service>&
service,
const std::function<
void(
bool)>& action);
61 Timer(
const std::shared_ptr<Service>&
service,
const std::function<
void(
bool)>& action,
const CppCommon::UtcTime& time);
68 Timer(
const std::shared_ptr<Service>&
service,
const std::function<
void(
bool)>& action,
const CppCommon::Timespan& timespan);
77 std::shared_ptr<Service>&
service() noexcept {
return _service; }
79 std::shared_ptr<asio::io_service>&
io_service() noexcept {
return _io_service; }
81 asio::io_service::strand&
strand() noexcept {
return _strand; }
93 virtual bool Setup(
const CppCommon::UtcTime& time);
99 virtual bool Setup(
const CppCommon::Timespan& timespan);
105 virtual bool Setup(
const std::function<
void(
bool)>& action);
112 virtual bool Setup(
const std::function<
void(
bool)>& action,
const CppCommon::UtcTime& time);
119 virtual bool Setup(
const std::function<
void(
bool)>& action,
const CppCommon::Timespan& timespan);
148 virtual void onError(
int error,
const std::string& category,
const std::string& message) {}
152 std::shared_ptr<Service> _service;
154 std::shared_ptr<asio::io_service> _io_service;
156 asio::io_service::strand _strand;
157 bool _strand_required;
159 asio::system_timer _timer;
161 std::function<void(
bool)> _action;
164 void SendError(std::error_code ec);
166 void SendTimer(
bool canceled);
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
Timer(const Timer &)=delete
Timer & operator=(const Timer &)=delete
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.
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
virtual bool Setup(const CppCommon::UtcTime &time)
Setup the timer with absolute expiry time.
Timer & operator=(Timer &&)=delete
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.