CppServer  1.0.4.0
C++ Server Library
timer.h
Go to the documentation of this file.
1 
9 #ifndef CPPSERVER_ASIO_TIMER_H
10 #define CPPSERVER_ASIO_TIMER_H
11 
12 #include "service.h"
13 
14 #include "time/time.h"
15 #include "time/timespan.h"
16 
17 #include <cassert>
18 #include <functional>
19 
20 namespace CppServer {
21 namespace Asio {
22 
24 
29 class Timer : public std::enable_shared_from_this<Timer>
30 {
31 public:
33 
36  Timer(const std::shared_ptr<Service>& service);
38 
42  Timer(const std::shared_ptr<Service>& service, const CppCommon::UtcTime& time);
44 
48  Timer(const std::shared_ptr<Service>& service, const CppCommon::Timespan& timespan);
50 
54  Timer(const std::shared_ptr<Service>& service, const std::function<void(bool)>& action);
56 
61  Timer(const std::shared_ptr<Service>& service, const std::function<void(bool)>& action, const CppCommon::UtcTime& time);
63 
68  Timer(const std::shared_ptr<Service>& service, const std::function<void(bool)>& action, const CppCommon::Timespan& timespan);
69  Timer(const Timer&) = delete;
70  Timer(Timer&&) = delete;
71  virtual ~Timer() = default;
72 
73  Timer& operator=(const Timer&) = delete;
74  Timer& operator=(Timer&&) = delete;
75 
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; }
82 
84  CppCommon::UtcTime expire_time() const;
86  CppCommon::Timespan expire_timespan() const;
87 
89 
93  virtual bool Setup(const CppCommon::UtcTime& time);
95 
99  virtual bool Setup(const CppCommon::Timespan& timespan);
101 
105  virtual bool Setup(const std::function<void(bool)>& action);
107 
112  virtual bool Setup(const std::function<void(bool)>& action, const CppCommon::UtcTime& time);
114 
119  virtual bool Setup(const std::function<void(bool)>& action, const CppCommon::Timespan& timespan);
120 
122 
125  virtual bool WaitAsync();
127 
130  virtual bool WaitSync();
131 
133 
136  virtual bool Cancel();
137 
138 protected:
140  virtual void onTimer(bool canceled) {}
141 
143 
148  virtual void onError(int error, const std::string& category, const std::string& message) {}
149 
150 private:
151  // Asio service
152  std::shared_ptr<Service> _service;
153  // Asio IO service
154  std::shared_ptr<asio::io_service> _io_service;
155  // Asio service strand for serialized handler execution
156  asio::io_service::strand _strand;
157  bool _strand_required;
158  // Deadline timer
159  asio::system_timer _timer;
160  // Action function
161  std::function<void(bool)> _action;
162 
164  void SendError(std::error_code ec);
166  void SendTimer(bool canceled);
167 };
168 
171 } // namespace Asio
172 } // namespace CppServer
173 
174 #endif // CPPSERVER_ASIO_TIMER_H
std::shared_ptr< asio::io_service > & io_service() noexcept
Get the Asio IO service.
Definition: timer.h:79
Timer(const Timer &)=delete
Timer & operator=(const Timer &)=delete
virtual bool Cancel()
Cancel any wait operation on the timer.
Definition: timer.cpp:217
virtual ~Timer()=default
std::shared_ptr< Service > & service() noexcept
Get the Asio service.
Definition: timer.h:77
virtual bool WaitSync()
Wait for the timer (synchronous)
Definition: timer.cpp:195
CppCommon::UtcTime expire_time() const
Get the timer's expiry time as an absolute time.
Definition: timer.cpp:98
Timer(const std::shared_ptr< Service > &service)
Initialize timer with a given Asio service.
Definition: timer.cpp:14
virtual void onTimer(bool canceled)
Handle timer notification.
Definition: timer.h:140
CppCommon::Timespan expire_timespan() const
Get the timer's expiry time relative to now.
Definition: timer.cpp:103
asio::io_service::strand & strand() noexcept
Get the Asio service strand for serialized handler execution.
Definition: timer.h:81
Timer(Timer &&)=delete
virtual bool Setup(const CppCommon::UtcTime &time)
Setup the timer with absolute expiry time.
Definition: timer.cpp:108
Timer & operator=(Timer &&)=delete
virtual bool WaitAsync()
Wait for the timer (asynchronous)
Definition: timer.cpp:168
virtual void onError(int error, const std::string &category, const std::string &message)
Handle error notification.
Definition: timer.h:148
C++ Server project definitions.
Definition: asio.h:56
Asio service definition.