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/timer_common.hpp>
17 
18 namespace restinio
19 {
20 
21 //
22 // asio_timer_manager_t
23 //
24 
25 //! Timer factory implementation using asio timers.
26 class asio_timer_manager_t final
27  : public std::enable_shared_from_this< asio_timer_manager_t >
28 {
29  public:
31  asio_ns::io_context & io_context,
32  std::chrono::steady_clock::duration check_period )
35  {}
36 
37  //! Timer guard for async operations.
38  class timer_guard_t final
39  {
40  public:
42  asio_ns::io_context & io_context,
43  std::chrono::steady_clock::duration check_period )
46  {}
47 
48  //! Schedule timeouts check invocation.
49  void
50  schedule( tcp_connection_ctx_weak_handle_t weak_handle )
51  {
52  m_operation_timer.expires_after( m_check_period );
53  m_operation_timer.async_wait(
54  [ weak_handle = std::move( weak_handle ) ]( const auto & ec ){
55  if( !ec )
56  {
57  if( auto h = weak_handle.lock() )
58  {
59  h->check_timeout( h );
60  }
61  }
62  } );
63  }
64 
65  //! Cancel timeout guard if any.
66  void
68  {
69  m_operation_timer.cancel();
70  }
71 
72  private:
75  //! \}
76  };
77 
78  //! Create guard for connection.
79  timer_guard_t
81  {
82  return timer_guard_t{ m_io_context, m_check_period };
83  }
84 
85  //! @name Start/stop timer manager.
86  ///@{
87  void start() const noexcept {}
88  void stop() const noexcept {}
89  ///@}
90 
91  struct factory_t final
92  {
93  //! Check period for timer events.
95  m_check_period{ std::chrono::seconds{ 1 } };
96 
97  factory_t() noexcept {}
98  factory_t( std::chrono::steady_clock::duration check_period ) noexcept
100  {}
101 
102  //! Create an instance of timer manager.
103  auto
104  create( asio_ns::io_context & io_context ) const
105  {
106  return std::make_shared< asio_timer_manager_t >( io_context, m_check_period );
107  }
108  };
109 
110  private:
111  //! An instanse of io_context to work with.
113 
114  //! Check period for timer events.
116 };
117 
118 } /* namespace restinio */
asio_timer_manager_t(asio_ns::io_context &io_context, std::chrono::steady_clock::duration check_period)
void cancel()
Cancel timeout guard if any.
asio_ns::io_context & m_io_context
An instanse of io_context to work with.
timer_guard_t(asio_ns::io_context &io_context, std::chrono::steady_clock::duration check_period)
factory_t(std::chrono::steady_clock::duration check_period) noexcept
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 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