CppServer  1.0.4.0
C++ Server Library
Public Member Functions | Protected Member Functions | List of all members
CppServer::Asio::Timer Class Reference

Timer. More...

#include <timer.h>

Inheritance diagram for CppServer::Asio::Timer:

Public Member Functions

 Timer (const std::shared_ptr< Service > &service)
 Initialize timer with a given Asio service. More...
 
 Timer (const std::shared_ptr< Service > &service, const CppCommon::UtcTime &time)
 Initialize timer with a given Asio service and absolute expiry time. More...
 
 Timer (const std::shared_ptr< Service > &service, const CppCommon::Timespan &timespan)
 Initialize timer with a given Asio service and expiry time relative to now. More...
 
 Timer (const std::shared_ptr< Service > &service, const std::function< void(bool)> &action)
 Initialize timer with a given Asio service and action function. More...
 
 Timer (const std::shared_ptr< Service > &service, const std::function< void(bool)> &action, const CppCommon::UtcTime &time)
 Initialize timer with a given Asio service, action function and absolute expiry time. More...
 
 Timer (const std::shared_ptr< Service > &service, const std::function< void(bool)> &action, const CppCommon::Timespan &timespan)
 Initialize timer with a given Asio service, action function and expiry time relative to now. More...
 
 Timer (const Timer &)=delete
 
 Timer (Timer &&)=delete
 
virtual ~Timer ()=default
 
Timeroperator= (const Timer &)=delete
 
Timeroperator= (Timer &&)=delete
 
std::shared_ptr< Service > & service () noexcept
 Get the Asio service. More...
 
std::shared_ptr< asio::io_service > & io_service () noexcept
 Get the Asio IO service. More...
 
asio::io_service::strand & strand () noexcept
 Get the Asio service strand for serialized handler execution. More...
 
CppCommon::UtcTime expire_time () const
 Get the timer's expiry time as an absolute time. More...
 
CppCommon::Timespan expire_timespan () const
 Get the timer's expiry time relative to now. More...
 
virtual bool Setup (const CppCommon::UtcTime &time)
 Setup the timer with absolute expiry time. More...
 
virtual bool Setup (const CppCommon::Timespan &timespan)
 Setup the timer with expiry time relative to now. More...
 
virtual bool Setup (const std::function< void(bool)> &action)
 Setup the timer with an action function. More...
 
virtual bool Setup (const std::function< void(bool)> &action, const CppCommon::UtcTime &time)
 Setup the timer with an action function and absolute expiry time. More...
 
virtual bool Setup (const std::function< void(bool)> &action, const CppCommon::Timespan &timespan)
 Setup the timer with an action function and expiry time relative to now. More...
 
virtual bool WaitAsync ()
 Wait for the timer (asynchronous) More...
 
virtual bool WaitSync ()
 Wait for the timer (synchronous) More...
 
virtual bool Cancel ()
 Cancel any wait operation on the timer. More...
 

Protected Member Functions

virtual void onTimer (bool canceled)
 Handle timer notification. More...
 
virtual void onError (int error, const std::string &category, const std::string &message)
 Handle error notification. More...
 

Detailed Description

Timer.

Timer is used to plan and perform delayed operation.

Thread-safe.

Examples
asio_timer.cpp.

Definition at line 29 of file timer.h.

Constructor & Destructor Documentation

◆ Timer() [1/8]

CppServer::Asio::Timer::Timer ( const std::shared_ptr< Service > &  service)

Initialize timer with a given Asio service.

Parameters
service- Asio service
Examples
asio_timer.cpp.

Definition at line 14 of file timer.cpp.

◆ Timer() [2/8]

CppServer::Asio::Timer::Timer ( const std::shared_ptr< Service > &  service,
const CppCommon::UtcTime &  time 
)

Initialize timer with a given Asio service and absolute expiry time.

Parameters
service- Asio service
time- Absolute time

Definition at line 26 of file timer.cpp.

◆ Timer() [3/8]

CppServer::Asio::Timer::Timer ( const std::shared_ptr< Service > &  service,
const CppCommon::Timespan &  timespan 
)

Initialize timer with a given Asio service and expiry time relative to now.

Parameters
service- Asio service
timespan- Relative timespan

Definition at line 38 of file timer.cpp.

◆ Timer() [4/8]

CppServer::Asio::Timer::Timer ( const std::shared_ptr< Service > &  service,
const std::function< void(bool)> &  action 
)

Initialize timer with a given Asio service and action function.

Parameters
service- Asio service
action- Action function

Definition at line 50 of file timer.cpp.

◆ Timer() [5/8]

CppServer::Asio::Timer::Timer ( const std::shared_ptr< Service > &  service,
const std::function< void(bool)> &  action,
const CppCommon::UtcTime &  time 
)

Initialize timer with a given Asio service, action function and absolute expiry time.

Parameters
service- Asio service
action- Action function
time- Absolute time

Definition at line 66 of file timer.cpp.

◆ Timer() [6/8]

CppServer::Asio::Timer::Timer ( const std::shared_ptr< Service > &  service,
const std::function< void(bool)> &  action,
const CppCommon::Timespan &  timespan 
)

Initialize timer with a given Asio service, action function and expiry time relative to now.

Parameters
service- Asio service
action- Action function
timespan- Relative timespan

Definition at line 82 of file timer.cpp.

◆ Timer() [7/8]

CppServer::Asio::Timer::Timer ( const Timer )
delete

◆ Timer() [8/8]

CppServer::Asio::Timer::Timer ( Timer &&  )
delete

◆ ~Timer()

virtual CppServer::Asio::Timer::~Timer ( )
virtualdefault

Member Function Documentation

◆ Cancel()

bool CppServer::Asio::Timer::Cancel ( )
virtual

Cancel any wait operation on the timer.

Returns
'true' if the timer was successfully canceled, 'false' if any error occurred

Definition at line 217 of file timer.cpp.

◆ expire_time()

CppCommon::UtcTime CppServer::Asio::Timer::expire_time ( ) const

Get the timer's expiry time as an absolute time.

Definition at line 98 of file timer.cpp.

◆ expire_timespan()

CppCommon::Timespan CppServer::Asio::Timer::expire_timespan ( ) const

Get the timer's expiry time relative to now.

Definition at line 103 of file timer.cpp.

◆ io_service()

std::shared_ptr<asio::io_service>& CppServer::Asio::Timer::io_service ( )
inlinenoexcept

Get the Asio IO service.

Definition at line 79 of file timer.h.

◆ onError()

virtual void CppServer::Asio::Timer::onError ( int  error,
const std::string &  category,
const std::string &  message 
)
inlineprotectedvirtual

Handle error notification.

Parameters
error- Error code
category- Error category
message- Error message
Examples
asio_timer.cpp.

Definition at line 148 of file timer.h.

◆ onTimer()

virtual void CppServer::Asio::Timer::onTimer ( bool  canceled)
inlineprotectedvirtual

Handle timer notification.

Examples
asio_timer.cpp.

Definition at line 140 of file timer.h.

◆ operator=() [1/2]

Timer& CppServer::Asio::Timer::operator= ( const Timer )
delete

◆ operator=() [2/2]

Timer& CppServer::Asio::Timer::operator= ( Timer &&  )
delete

◆ service()

std::shared_ptr<Service>& CppServer::Asio::Timer::service ( )
inlinenoexcept

Get the Asio service.

Definition at line 77 of file timer.h.

◆ Setup() [1/5]

bool CppServer::Asio::Timer::Setup ( const CppCommon::Timespan &  timespan)
virtual

Setup the timer with expiry time relative to now.

Parameters
timespan- Relative timespan
Returns
'true' if the timer was successfully setup, 'false' if the timer failed to setup

Definition at line 123 of file timer.cpp.

◆ Setup() [2/5]

bool CppServer::Asio::Timer::Setup ( const CppCommon::UtcTime &  time)
virtual

Setup the timer with absolute expiry time.

Parameters
time- Absolute time
Returns
'true' if the timer was successfully setup, 'false' if the timer failed to setup

Definition at line 108 of file timer.cpp.

◆ Setup() [3/5]

bool CppServer::Asio::Timer::Setup ( const std::function< void(bool)> &  action)
virtual

Setup the timer with an action function.

Parameters
action- Action function
Returns
'true' if the timer was successfully setup, 'false' if the timer failed to setup

Definition at line 138 of file timer.cpp.

◆ Setup() [4/5]

bool CppServer::Asio::Timer::Setup ( const std::function< void(bool)> &  action,
const CppCommon::Timespan &  timespan 
)
virtual

Setup the timer with an action function and expiry time relative to now.

Parameters
action- Action function
timespan- Relative timespan
Returns
'true' if the timer was successfully setup, 'false' if the timer failed to setup

Definition at line 158 of file timer.cpp.

◆ Setup() [5/5]

bool CppServer::Asio::Timer::Setup ( const std::function< void(bool)> &  action,
const CppCommon::UtcTime &  time 
)
virtual

Setup the timer with an action function and absolute expiry time.

Parameters
action- Action function
time- Absolute time
Returns
'true' if the timer was successfully setup, 'false' if the timer failed to setup

Definition at line 148 of file timer.cpp.

◆ strand()

asio::io_service::strand& CppServer::Asio::Timer::strand ( )
inlinenoexcept

Get the Asio service strand for serialized handler execution.

Definition at line 81 of file timer.h.

◆ WaitAsync()

bool CppServer::Asio::Timer::WaitAsync ( )
virtual

Wait for the timer (asynchronous)

Returns
'true' if the timer was successfully expired, 'false' if any error occurred

Definition at line 168 of file timer.cpp.

◆ WaitSync()

bool CppServer::Asio::Timer::WaitSync ( )
virtual

Wait for the timer (synchronous)

Returns
'true' if the timer was successfully expired, 'false' if any error occurred

Definition at line 195 of file timer.cpp.


The documentation for this class was generated from the following files: