RESTinio
asio_timer_manager.hpp
Go to the documentation of this file.
1/*
2 restinio
3*/
4
9#pragma once
10
11#include <memory>
12#include <chrono>
13
15
17
20
21namespace restinio
22{
23
24//
25// asio_timer_manager_t
26//
27
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 )
36 : m_io_context{ io_context }
37 , m_check_period{ check_period }
38 {}
39
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
47 : m_operation_timer{ io_context }
48 , m_check_period{ check_period }
49 {}
50
52 void
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
69
73 void
74 cancel() noexcept
75 {
77 [this]{ m_operation_timer.cancel(); } );
78 }
79
80 private:
81 asio_ns::steady_timer m_operation_timer;
82 const std::chrono::steady_clock::duration m_check_period;
84 };
85
89 {
91 }
92
95 void start() const noexcept {}
96 void stop() const noexcept {}
98
99 struct factory_t final
100 {
102 const std::chrono::steady_clock::duration
103 m_check_period{ std::chrono::seconds{ 1 } };
104
105 factory_t() noexcept {}
106 factory_t( std::chrono::steady_clock::duration check_period ) noexcept
107 : m_check_period{ check_period }
108 {}
109
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:
120 asio_ns::io_context & m_io_context;
121
123 const std::chrono::steady_clock::duration m_check_period;
124};
125
126} /* namespace restinio */
timer_guard_t(asio_ns::io_context &io_context, std::chrono::steady_clock::duration check_period) noexcept
const std::chrono::steady_clock::duration m_check_period
void cancel() noexcept
Cancel timeout guard if any.
void schedule(tcp_connection_ctx_weak_handle_t weak_handle)
Schedule timeouts check invocation.
Timer factory implementation using asio timers.
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.
timer_guard_t create_timer_guard() const
Create guard for connection.
const std::chrono::steady_clock::duration m_check_period
Check period for timer events.
Detection of compiler version and absence of various features.
void suppress_exceptions_quietly(Lambda &&lambda) noexcept
Helper function for execution a block of code with suppression of any exceptions raised inside that b...
std::weak_ptr< tcp_connection_ctx_base_t > tcp_connection_ctx_weak_handle_t
Alias for http connection weak handle.
auto create(asio_ns::io_context &io_context) const
Create an instance of timer manager.
factory_t(std::chrono::steady_clock::duration check_period) noexcept
const std::chrono::steady_clock::duration m_check_period
Check period for timer events.
Utilities for suppressing exceptions from some code block.
#define const
Definition: zconf.h:230