RESTinio
so_timer_manager.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
13
14#include <so_5/all.hpp>
15
16namespace restinio
17{
18
19namespace so5
20{
21
22#if defined(SO_5_VERSION)
23 #if SO_5_VERSION < SO_5_VERSION_MAKE(6ull, 0ull, 0ull)
24 #define RESTINIO_USE_SO_5_5
25 #endif
26#else
27 #define RESTINIO_USE_SO_5_5
28#endif
29
30//
31// msg_check_timer_t
32//
33
35struct msg_check_timer_t final : public so_5::message_t
36{
38 : m_weak_handle{ std::move( weak_handle ) }
39 {}
40
42};
43
44//
45// so_timer_manager_t
46//
47
48#if defined(RESTINIO_USE_SO_5_5)
49
50// The implementation of so_timer_manager for SO-5.5.
51// SO-5.5 requires a reference to SObjectizer Environment for working with timers.
52
55{
56 public:
58 so_5::environment_t & env,
59 so_5::mbox_t mbox,
60 std::chrono::steady_clock::duration check_period )
61 : m_env{ env }
62 , m_mbox{ std::move( mbox ) }
63 , m_check_period{ check_period }
64 {}
65
67 class timer_guard_t final
68 {
69 public:
71 so_5::environment_t & env,
72 so_5::mbox_t mbox,
73 std::chrono::steady_clock::duration check_period )
74 : m_env{ env }
75 , m_mbox{ std::move( mbox ) }
76 , m_check_period{ check_period }
77 {}
78
80 void
82 {
83 if( !m_current_op_timer.is_active() )
84 {
85 m_current_op_timer = so_5::send_periodic< msg_check_timer_t >(
86 m_env,
87 m_mbox,
90 std::move(weak_handle) );
91
92 }
93 }
94
96
100 void
101 cancel() noexcept
102 {
104 }
105
106 private:
107 so_5::environment_t & m_env;
108 const so_5::mbox_t m_mbox;
109
110 so_5::timer_id_t m_current_op_timer;
111 const std::chrono::steady_clock::duration m_check_period;
113 };
114
115 // Create guard for connection.
118 {
120 }
121
124 void start() const noexcept {}
125 void stop() const noexcept {}
127
129 {
130 so_5::environment_t & m_env;
131 so_5::mbox_t m_mbox;
132 const std::chrono::steady_clock::duration m_check_period;
133
135 so_5::environment_t & env,
136 so_5::mbox_t mbox,
137 std::chrono::steady_clock::duration check_period = std::chrono::seconds{ 1 } )
138 : m_env{ env }
139 , m_mbox{ std::move( mbox ) }
140 , m_check_period{ check_period }
141 {}
142
143 auto
144 create( asio_ns::io_context & ) const
145 {
146 return std::make_shared< so_timer_manager_t >( m_env, m_mbox, m_check_period );
147 }
148 };
149
150 private:
151 so_5::environment_t & m_env;
152 so_5::mbox_t m_mbox;
153 const std::chrono::steady_clock::duration m_check_period;
154};
155
156#else
157
158// The implementation of so_timer_manager for SO-5.6 and newer.
159// There is no need to hold a reference to SObjectizer Environment for working
160// with timers.
161
163class so_timer_manager_t final
164{
165 public:
167 so_5::mbox_t mbox,
168 std::chrono::steady_clock::duration check_period )
169 : m_mbox{ std::move( mbox ) }
170 , m_check_period{ check_period }
171 {}
172
174 class timer_guard_t final
175 {
176 public:
178 so_5::mbox_t mbox,
179 std::chrono::steady_clock::duration check_period )
180 : m_mbox{ std::move( mbox ) }
181 , m_check_period{ check_period }
182 {}
183
185 void
187 {
188 if( !m_current_op_timer.is_active() )
189 {
190 m_current_op_timer = so_5::send_periodic< msg_check_timer_t >(
191 m_mbox,
194 std::move(weak_handle) );
195
196 }
197 }
198
200
204 void
205 cancel() noexcept
206 {
208 }
209
210 private:
211 const so_5::mbox_t m_mbox;
212
213 so_5::timer_id_t m_current_op_timer;
214 const std::chrono::steady_clock::duration m_check_period;
216 };
217
218 // Create guard for connection.
219 timer_guard_t
221 {
222 return timer_guard_t{ m_mbox, m_check_period };
223 }
224
227 void start() const noexcept {}
228 void stop() const noexcept {}
230
231 struct factory_t
232 {
233 so_5::mbox_t m_mbox;
234 const std::chrono::steady_clock::duration m_check_period;
235
236 factory_t(
237 so_5::mbox_t mbox,
238 std::chrono::steady_clock::duration check_period = std::chrono::seconds{ 1 } )
239 : m_mbox{ std::move( mbox ) }
240 , m_check_period{ check_period }
241 {}
242
243 // This constructor is just for compatibility with previous versions.
244 factory_t(
245 so_5::environment_t &,
246 so_5::mbox_t mbox,
247 std::chrono::steady_clock::duration check_period = std::chrono::seconds{ 1 } )
248 : factory_t{ std::move(mbox), check_period }
249 {}
250
251 auto
252 create( asio_ns::io_context & ) const
253 {
254 return std::make_shared< so_timer_manager_t >( m_mbox, m_check_period );
255 }
256 };
257
258 private:
259 so_5::mbox_t m_mbox;
260 const std::chrono::steady_clock::duration m_check_period;
261};
262#endif
263
264//
265// a_timeout_handler_t
266//
267
270 : public so_5::agent_t
271{
272 typedef so_5::agent_t so_base_type_t;
273
274 public:
275 a_timeout_handler_t( context_t ctx )
276 : so_base_type_t{ std::move( ctx ) }
277 {
278 so_subscribe_self()
279 .event(
280 []( const msg_check_timer_t & msg ){
281 if( auto h = msg.m_weak_handle.lock() )
282 h->check_timeout( h );
283 } );
284 }
285};
286
287} /* namespace so5 */
288
289} /* namespace restinio */
290
Agent that handles timeouts.
const std::chrono::steady_clock::duration m_check_period
timer_guard_t(so_5::environment_t &env, so_5::mbox_t mbox, std::chrono::steady_clock::duration check_period)
void schedule(tcp_connection_ctx_weak_handle_t weak_handle)
Schedule timeout check invocation.
void cancel() noexcept
Cancel timeout guard if any.
Timer factory implementation using timers from SObjectizer.
so_timer_manager_t(so_5::environment_t &env, so_5::mbox_t mbox, std::chrono::steady_clock::duration check_period)
const std::chrono::steady_clock::duration m_check_period
void start() const noexcept
Start/stop timer manager.
Detection of compiler version and absence of various features.
#define RESTINIO_ENSURE_NOEXCEPT_CALL(expr)
A wrapper around static_assert for checking that an expression is noexcept and execution of that expr...
std::weak_ptr< tcp_connection_ctx_base_t > tcp_connection_ctx_weak_handle_t
Alias for http connection weak handle.
STL namespace.
tcp_connection_ctx_weak_handle_t m_weak_handle
msg_check_timer_t(tcp_connection_ctx_weak_handle_t weak_handle)
const std::chrono::steady_clock::duration m_check_period
factory_t(so_5::environment_t &env, so_5::mbox_t mbox, std::chrono::steady_clock::duration check_period=std::chrono::seconds{ 1 })
auto create(asio_ns::io_context &) const
#define const
Definition: zconf.h:230