RESTinio
so_timer_manager.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  Timers implementation with sobjectizer timers.
7 */
8 
9 #pragma once
10 
11 #include <restinio/asio_include.hpp>
12 
13 #include <so_5/all.hpp>
14 
15 namespace restinio
16 {
17 
18 namespace so5
19 {
20 
21 //
22 // msg_ckeck_timer_t
23 //
24 
25 //! Check timer.
26 struct msg_ckeck_timer_t final : public so_5::message_t
27 {
28  msg_ckeck_timer_t( tcp_connection_ctx_weak_handle_t weak_handle )
30  {}
31 
33 };
34 
35 //
36 // so_timer_manager_t
37 //
38 
39 //! Timer factory implementation using asio timers.
40 class so_timer_manager_t final
41 {
42  public:
44  so_5::environment_t & env,
45  so_5::mbox_t mbox,
46  std::chrono::steady_clock::duration check_period )
47  : m_env{ env }
48  , m_mbox{ std::move( mbox ) }
50  {}
51 
52  //! Timer guard for async operations.
53  class timer_guard_t final
54  {
55  public:
57  so_5::environment_t & env,
58  so_5::mbox_t mbox,
59  std::chrono::steady_clock::duration check_period )
60  : m_env{ env }
61  , m_mbox{ std::move( mbox ) }
63  {}
64 
65  //! Schedule timeout check invocation.
66  void
67  schedule( tcp_connection_ctx_weak_handle_t weak_handle )
68  {
69  if( !m_current_op_timer.is_active() )
70  {
71  auto msg =
72  std::make_unique< msg_ckeck_timer_t >( std::move( weak_handle ) );
73 
74  m_current_op_timer =
75  m_env.schedule_timer(
76  std::move( msg ),
77  m_mbox,
78  m_check_period,
79  m_check_period );
80  }
81  }
82 
83  // Cancel timeout guard if any.
84  void
86  {
87  m_current_op_timer.release();
88  }
89 
90  private:
92  const so_5::mbox_t m_mbox;
93 
96  //! \}
97  };
98 
99  // Create guard for connection.
100  timer_guard_t
102  {
103  return timer_guard_t{ m_env, m_mbox, m_check_period };
104  }
105 
106  //! Start/stop timer manager.
107  //! \{
108  void start() const {}
109  void stop() const {}
110  //! \}
111 
112  struct factory_t
113  {
117 
119  so_5::environment_t & env,
120  so_5::mbox_t mbox,
121  std::chrono::steady_clock::duration check_period = std::chrono::seconds{ 1 } )
122  : m_env{ env }
123  , m_mbox{ std::move( mbox ) }
125  {}
126 
127  auto
129  {
130  return std::make_shared< so_timer_manager_t >( m_env, m_mbox, m_check_period );
131  }
132  };
133 
134  private:
138 };
139 
140 //
141 // a_timeout_handler_t
142 //
143 
144 //! Agent that handles timeouts.
145 class a_timeout_handler_t final
146  : public so_5::agent_t
147 {
149 
150  public:
151  a_timeout_handler_t( context_t ctx )
152  : so_base_type_t{ std::move( ctx ) }
153  {
154  so_subscribe_self()
155  .event(
156  []( const msg_ckeck_timer_t & msg ){
157  if( auto h = msg.m_weak_handle.lock() )
158  h->check_timeout( h );
159  } );
160  }
161 };
162 
163 } /* namespace so5 */
164 
165 } /* namespace restinio */
timer_guard_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
const std::chrono::steady_clock::duration m_check_period
so_timer_manager_t(so_5::environment_t &env, so_5::mbox_t mbox, std::chrono::steady_clock::duration check_period)
msg_ckeck_timer_t(tcp_connection_ctx_weak_handle_t weak_handle)
void start() const
Start/stop timer manager.
void schedule(tcp_connection_ctx_weak_handle_t weak_handle)
Schedule timeout check invocation.
auto create(asio_ns::io_context &) const
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 })
tcp_connection_ctx_weak_handle_t m_weak_handle
std::enable_if< std::is_same< Parameter_Container, query_string_params_t >::value||std::is_same< Parameter_Container, router::route_params_t >::value, optional_t< Value_Type > >::type opt_value(const Parameter_Container &params, string_view_t key)
Gets the value of a parameter specified by key wrapped in optional_t<Value_Type> if parameter exists ...
Definition: value_or.hpp:64