RESTinio
asio_timer_manager.hpp
Go to the documentation of this file.
1 /*
2  restinio
3 */
4 
5 /*!
6  Timer factory implementation using asio timers.
7 */
8 
9 #pragma once
10 
11 #include <memory>
12 #include <chrono>
13 
14 #include <restinio/asio_include.hpp>
15 
16 #include <restinio/utils/suppress_exceptions.hpp>
17 
18 #include <restinio/timer_common.hpp>
19 #include <restinio/compiler_features.hpp>
20 
21 namespace restinio
22 {
23 
24 //
25 // asio_timer_manager_t
26 //
27 
28 //! Timer factory implementation using asio timers.
29 class asio_timer_manager_t final
30  : public std::enable_shared_from_this< asio_timer_manager_t >
31 {
32  public:
34  asio_ns::io_context & io_context,
35  std::chrono::steady_clock::duration check_period )
38  {}
39 
40  //! Timer guard for async operations.
41  class timer_guard_t final
42  {
43  public:
45  asio_ns::io_context & io_context,
46  std::chrono::steady_clock::duration check_period ) noexcept
49  {}
50 
51  //! Schedule timeouts check invocation.
52  void
53  schedule( tcp_connection_ctx_weak_handle_t weak_handle )
54  {
55  m_operation_timer.expires_after( m_check_period );
56  m_operation_timer.async_wait(
57  [ weak_handle = std::move( weak_handle ) ]( const auto & ec ){
58  if( !ec )
59  {
60  if( auto h = weak_handle.lock() )
61  {
62  h->check_timeout( h );
63  }
64  }
65  } );
66  }
67 
68  //! Cancel timeout guard if any.
69  /*!
70  * @note
71  * Since v.0.6.0 this method is noexcept.
72  */
73  void
74  cancel() noexcept
75  {
76  restinio::utils::suppress_exceptions_quietly(
77  [this]{ m_operation_timer.cancel(); } );
78  }
79 
80  private:
83  //! \}
84  };
85 
86  //! Create guard for connection.
87  timer_guard_t
89  {
90  return timer_guard_t{ m_io_context, m_check_period };
91  }
92 
93  //! @name Start/stop timer manager.
94  ///@{
95  void start() const noexcept {}
96  void stop() const noexcept {}
97  ///@}
98 
99  struct factory_t final
100  {
101  //! Check period for timer events.
103  m_check_period{ std::chrono::seconds{ 1 } };
104 
105  factory_t() noexcept {}
106  factory_t( std::chrono::steady_clock::duration check_period ) noexcept
108  {}
109 
110  //! Create an instance of timer manager.
111  auto
112  create( asio_ns::io_context & io_context ) const
113  {
114  return std::make_shared< asio_timer_manager_t >( io_context, m_check_period );
115  }
116  };
117 
118  private:
119  //! An instanse of io_context to work with.
121 
122  //! Check period for timer events.
124 };
125 
126 } /* namespace restinio */
asio_timer_manager_t(asio_ns::io_context &io_context, std::chrono::steady_clock::duration check_period)
asio_ns::io_context & m_io_context
An instanse of io_context to work with.
factory_t(std::chrono::steady_clock::duration check_period) noexcept
void cancel() noexcept
Cancel timeout guard if any.
const std::chrono::steady_clock::duration m_check_period
Check period for timer events.
const std::chrono::steady_clock::duration m_check_period
timer_guard_t(asio_ns::io_context &io_context, std::chrono::steady_clock::duration check_period) noexcept
timer_guard_t create_timer_guard() const
Create guard for connection.
auto create(asio_ns::io_context &io_context) const
Create an instance of timer manager.
void schedule(tcp_connection_ctx_weak_handle_t weak_handle)
Schedule timeouts check invocation.
const std::chrono::steady_clock::duration m_check_period
Check period for timer events.
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