RESTinio

so_timer_manager.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
11#include <restinio/asio_include.hpp>
12#include <restinio/compiler_features.hpp>
13
14#include <so_5/all.hpp>
15
16 namespace restinio
17{
18
19 namespace 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
35 struct msg_check_timer_t final : public so_5::message_t
36{
37 msg_check_timer_t( tcp_connection_ctx_weak_handle_t weak_handle )
38 : m_weak_handle{ std::move( weak_handle ) }
39 {}
40
41 tcp_connection_ctx_weak_handle_t m_weak_handle;
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
54 class so_timer_manager_t final
55{
56 public:
57 so_timer_manager_t(
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:
70 timer_guard_t(
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
81 schedule( tcp_connection_ctx_weak_handle_t weak_handle )
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,
88 m_check_period,
89 m_check_period,
90 std::move(weak_handle) );
91
92 }
93 }
94
96
100 void
101 cancel() noexcept
102 {
103 RESTINIO_ENSURE_NOEXCEPT_CALL( m_current_op_timer.release() );
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.
116 timer_guard_t
117 create_timer_guard()
118 {
119 return timer_guard_t{ m_env, m_mbox, m_check_period };
120 }
121
124 void start() const noexcept {}
125 void stop() const noexcept {}
127
128 struct factory_t
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
134 factory_t(
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
163 class so_timer_manager_t final
164{
165 public:
166 so_timer_manager_t(
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:
177 timer_guard_t(
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
186 schedule( tcp_connection_ctx_weak_handle_t weak_handle )
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,
192 m_check_period,
193 m_check_period,
194 std::move(weak_handle) );
195
196 }
197 }
198
200
204 void
205 cancel() noexcept
206 {
207 RESTINIO_ENSURE_NOEXCEPT_CALL( m_current_op_timer.release() );
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
220 create_timer_guard()
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
269 class a_timeout_handler_t final
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
asio_include.hpp
restinio::so5::a_timeout_handler_t
Agent that handles timeouts.
Definition: so_timer_manager.hpp:271
restinio::so5::a_timeout_handler_t::so_base_type_t
so_5::agent_t so_base_type_t
Definition: so_timer_manager.hpp:272
restinio::so5::a_timeout_handler_t::a_timeout_handler_t
a_timeout_handler_t(context_t ctx)
Definition: so_timer_manager.hpp:275
restinio::so5::so_timer_manager_t::timer_guard_t
Timer guard for async operations.
Definition: so_timer_manager.hpp:68
restinio::so5::so_timer_manager_t::timer_guard_t::m_check_period
const std::chrono::steady_clock::duration m_check_period
Definition: so_timer_manager.hpp:111
restinio::so5::so_timer_manager_t::timer_guard_t::timer_guard_t
timer_guard_t(so_5::environment_t &env, so_5::mbox_t mbox, std::chrono::steady_clock::duration check_period)
Definition: so_timer_manager.hpp:70
restinio::so5::so_timer_manager_t::timer_guard_t::schedule
void schedule(tcp_connection_ctx_weak_handle_t weak_handle)
Schedule timeout check invocation.
Definition: so_timer_manager.hpp:81
restinio::so5::so_timer_manager_t::timer_guard_t::cancel
void cancel() noexcept
Cancel timeout guard if any.
Definition: so_timer_manager.hpp:101
restinio::so5::so_timer_manager_t::timer_guard_t::m_mbox
const so_5::mbox_t m_mbox
Definition: so_timer_manager.hpp:108
restinio::so5::so_timer_manager_t::timer_guard_t::m_current_op_timer
so_5::timer_id_t m_current_op_timer
Definition: so_timer_manager.hpp:110
restinio::so5::so_timer_manager_t::timer_guard_t::m_env
so_5::environment_t & m_env
Definition: so_timer_manager.hpp:107
restinio::so5::so_timer_manager_t
Timer factory implementation using timers from SObjectizer.
Definition: so_timer_manager.hpp:55
restinio::so5::so_timer_manager_t::so_timer_manager_t
so_timer_manager_t(so_5::environment_t &env, so_5::mbox_t mbox, std::chrono::steady_clock::duration check_period)
Definition: so_timer_manager.hpp:57
restinio::so5::so_timer_manager_t::create_timer_guard
timer_guard_t create_timer_guard()
Definition: so_timer_manager.hpp:117
restinio::so5::so_timer_manager_t::m_mbox
so_5::mbox_t m_mbox
Definition: so_timer_manager.hpp:152
restinio::so5::so_timer_manager_t::m_env
so_5::environment_t & m_env
Definition: so_timer_manager.hpp:151
restinio::so5::so_timer_manager_t::m_check_period
const std::chrono::steady_clock::duration m_check_period
Definition: so_timer_manager.hpp:153
restinio::so5::so_timer_manager_t::start
void start() const noexcept
Start/stop timer manager.
Definition: so_timer_manager.hpp:124
restinio::so5::so_timer_manager_t::stop
void stop() const noexcept
Definition: so_timer_manager.hpp:125
compiler_features.hpp
Detection of compiler version and absence of various features.
RESTINIO_ENSURE_NOEXCEPT_CALL
#define RESTINIO_ENSURE_NOEXCEPT_CALL(expr)
A wrapper around static_assert for checking that an expression is noexcept and execution of that expr...
Definition: compiler_features.hpp:60
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
restinio
Definition: asio_include.hpp:21
restinio::tcp_connection_ctx_weak_handle_t
std::weak_ptr< tcp_connection_ctx_base_t > tcp_connection_ctx_weak_handle_t
Alias for http connection weak handle.
Definition: tcp_connection_ctx_base.hpp:65
std
STL namespace.
restinio::so5::msg_check_timer_t
Check timer.
Definition: so_timer_manager.hpp:36
restinio::so5::msg_check_timer_t::m_weak_handle
tcp_connection_ctx_weak_handle_t m_weak_handle
Definition: so_timer_manager.hpp:41
restinio::so5::msg_check_timer_t::msg_check_timer_t
msg_check_timer_t(tcp_connection_ctx_weak_handle_t weak_handle)
Definition: so_timer_manager.hpp:37
restinio::so5::so_timer_manager_t::factory_t
Definition: so_timer_manager.hpp:129
restinio::so5::so_timer_manager_t::factory_t::m_check_period
const std::chrono::steady_clock::duration m_check_period
Definition: so_timer_manager.hpp:132
restinio::so5::so_timer_manager_t::factory_t::factory_t
factory_t(so_5::environment_t &env, so_5::mbox_t mbox, std::chrono::steady_clock::duration check_period=std::chrono::seconds{ 1 })
Definition: so_timer_manager.hpp:134
restinio::so5::so_timer_manager_t::factory_t::m_mbox
so_5::mbox_t m_mbox
Definition: so_timer_manager.hpp:131
restinio::so5::so_timer_manager_t::factory_t::m_env
so_5::environment_t & m_env
Definition: so_timer_manager.hpp:130
restinio::so5::so_timer_manager_t::factory_t::create
auto create(asio_ns::io_context &) const
Definition: so_timer_manager.hpp:144
const
#define const
Definition: zconf.h:230