asio_timer_manager.hpp
Go to the documentation of this file.
/*
restinio
*/
#pragma once
#include <memory>
#include <chrono>
#include <restinio/asio_include.hpp>
#include <restinio/utils/suppress_exceptions.hpp>
#include <restinio/timer_common.hpp>
#include <restinio/compiler_features.hpp>
namespace restinio
{
//
// asio_timer_manager_t
//
class asio_timer_manager_t final
: public std::enable_shared_from_this< asio_timer_manager_t >
{
public:
asio_timer_manager_t(
asio_ns::io_context & io_context,
std::chrono::steady_clock::duration check_period )
: m_io_context{ io_context }
, m_check_period{ check_period }
{}
class timer_guard_t final
{
public:
timer_guard_t(
asio_ns::io_context & io_context,
std::chrono::steady_clock::duration check_period ) noexcept
: m_operation_timer{ io_context }
, m_check_period{ check_period }
{}
void
schedule( tcp_connection_ctx_weak_handle_t weak_handle )
{
m_operation_timer.expires_after( m_check_period );
m_operation_timer.async_wait(
[ weak_handle = std::move( weak_handle ) ]( const auto & ec ){
if( !ec )
{
if( auto h = weak_handle.lock() )
{
h->check_timeout( h );
}
}
} );
}
void
cancel() noexcept
{
restinio::utils::suppress_exceptions_quietly(
[this]{ m_operation_timer.cancel(); } );
}
private:
asio_ns::steady_timer m_operation_timer;
const std::chrono::steady_clock::duration m_check_period;
};
timer_guard_t
create_timer_guard() const
{
return timer_guard_t{ m_io_context, m_check_period };
}
void start() const noexcept {}
void stop() const noexcept {}
struct factory_t final
{
const std::chrono::steady_clock::duration
m_check_period{ std::chrono::seconds{ 1 } };
factory_t() noexcept {}
factory_t( std::chrono::steady_clock::duration check_period ) noexcept
: m_check_period{ check_period }
{}
auto
create( asio_ns::io_context & io_context ) const
{
return std::make_shared< asio_timer_manager_t >( io_context, m_check_period );
}
};
private:
asio_ns::io_context & m_io_context;
const std::chrono::steady_clock::duration m_check_period;
};
} /* namespace restinio */
asio_include.hpp
restinio::asio_timer_manager_t::timer_guard_t
Timer guard for async operations.
Definition: asio_timer_manager.hpp:42
restinio::asio_timer_manager_t::timer_guard_t::m_operation_timer
asio_ns::steady_timer m_operation_timer
Definition: asio_timer_manager.hpp:81
restinio::asio_timer_manager_t::timer_guard_t::timer_guard_t
timer_guard_t(asio_ns::io_context &io_context, std::chrono::steady_clock::duration check_period) noexcept
Definition: asio_timer_manager.hpp:44
restinio::asio_timer_manager_t::timer_guard_t::m_check_period
const std::chrono::steady_clock::duration m_check_period
Definition: asio_timer_manager.hpp:82
restinio::asio_timer_manager_t::timer_guard_t::cancel
void cancel() noexcept
Cancel timeout guard if any.
Definition: asio_timer_manager.hpp:74
restinio::asio_timer_manager_t::timer_guard_t::schedule
void schedule(tcp_connection_ctx_weak_handle_t weak_handle)
Schedule timeouts check invocation.
Definition: asio_timer_manager.hpp:53
restinio::asio_timer_manager_t
Timer factory implementation using asio timers.
Definition: asio_timer_manager.hpp:31
restinio::asio_timer_manager_t::asio_timer_manager_t
asio_timer_manager_t(asio_ns::io_context &io_context, std::chrono::steady_clock::duration check_period)
Definition: asio_timer_manager.hpp:33
restinio::asio_timer_manager_t::m_io_context
asio_ns::io_context & m_io_context
An instanse of io_context to work with.
Definition: asio_timer_manager.hpp:120
restinio::asio_timer_manager_t::create_timer_guard
timer_guard_t create_timer_guard() const
Create guard for connection.
Definition: asio_timer_manager.hpp:88
restinio::asio_timer_manager_t::m_check_period
const std::chrono::steady_clock::duration m_check_period
Check period for timer events.
Definition: asio_timer_manager.hpp:123
restinio::asio_timer_manager_t::stop
void stop() const noexcept
Definition: asio_timer_manager.hpp:96
restinio::asio_timer_manager_t::start
void start() const noexcept
Definition: asio_timer_manager.hpp:95
compiler_features.hpp
Detection of compiler version and absence of various features.
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:465
restinio::utils::suppress_exceptions_quietly
void suppress_exceptions_quietly(Lambda &&lambda) noexcept
Helper function for execution a block of code with suppression of any exceptions raised inside that b...
Definition: suppress_exceptions.hpp:133
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
restinio::asio_timer_manager_t::factory_t
Definition: asio_timer_manager.hpp:100
restinio::asio_timer_manager_t::factory_t::create
auto create(asio_ns::io_context &io_context) const
Create an instance of timer manager.
Definition: asio_timer_manager.hpp:112
restinio::asio_timer_manager_t::factory_t::factory_t
factory_t(std::chrono::steady_clock::duration check_period) noexcept
Definition: asio_timer_manager.hpp:106
restinio::asio_timer_manager_t::factory_t::factory_t
factory_t() noexcept
Definition: asio_timer_manager.hpp:105
restinio::asio_timer_manager_t::factory_t::m_check_period
const std::chrono::steady_clock::duration m_check_period
Check period for timer events.
Definition: asio_timer_manager.hpp:103
suppress_exceptions.hpp
Utilities for suppressing exceptions from some code block.
timer_common.hpp
const
#define const
Definition: zconf.h:230