9#ifndef CPPSERVER_ASIO_TIMER_H
10#define CPPSERVER_ASIO_TIMER_H
15#include "time/timespan.h"
29class Timer :
public std::enable_shared_from_this<Timer>
42 Timer(
const std::shared_ptr<Service>&
service,
const CppCommon::UtcTime&
time);
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);
93 virtual bool Setup(
const CppCommon::UtcTime&
time);
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);
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);
Asio allocate handler wrapper.
Timer(const Timer &)=delete
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
virtual bool Cancel()
Cancel any wait operation on the timer.
Timer & operator=(Timer &&)=delete
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
Timer & operator=(const Timer &)=delete
virtual bool WaitSync()
Wait for the timer (synchronous)
CppCommon::UtcTime expire_time() const
Get the timer's expiry time as an absolute time.
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.