Loading...
Searching...
No Matches
so_timer_manager.hpp
Go to the documentation of this file.
/*
restinio
*/
#pragma once
#include <restinio/asio_include.hpp>
#include <restinio/compiler_features.hpp>
#include <so_5/all.hpp>
namespace restinio
{
namespace so5
{
#if defined(SO_5_VERSION)
#if SO_5_VERSION < SO_5_VERSION_MAKE(6ull, 0ull, 0ull)
#define RESTINIO_USE_SO_5_5
#endif
#else
#define RESTINIO_USE_SO_5_5
#endif
//
// msg_check_timer_t
//
struct msg_check_timer_t final : public so_5::message_t
{
msg_check_timer_t( tcp_connection_ctx_weak_handle_t weak_handle )
: m_weak_handle{ std::move( weak_handle ) }
{}
tcp_connection_ctx_weak_handle_t m_weak_handle;
};
//
// so_timer_manager_t
//
#if defined(RESTINIO_USE_SO_5_5)
// The implementation of so_timer_manager for SO-5.5.
// SO-5.5 requires a reference to SObjectizer Environment for working with timers.
class so_timer_manager_t final
{
public:
so_timer_manager_t(
so_5::environment_t & env,
so_5::mbox_t mbox,
std::chrono::steady_clock::duration check_period )
: m_env{ env }
, m_mbox{ std::move( mbox ) }
, m_check_period{ check_period }
{}
class timer_guard_t final
{
public:
timer_guard_t(
so_5::environment_t & env,
so_5::mbox_t mbox,
std::chrono::steady_clock::duration check_period )
: m_env{ env }
, m_mbox{ std::move( mbox ) }
, m_check_period{ check_period }
{}
void
schedule( tcp_connection_ctx_weak_handle_t weak_handle )
{
if( !m_current_op_timer.is_active() )
{
m_current_op_timer = so_5::send_periodic< msg_check_timer_t >(
m_env,
m_mbox,
m_check_period,
m_check_period,
std::move(weak_handle) );
}
}
void
cancel() noexcept
{
RESTINIO_ENSURE_NOEXCEPT_CALL( m_current_op_timer.release() );
}
private:
so_5::environment_t & m_env;
const so_5::mbox_t m_mbox;
so_5::timer_id_t m_current_op_timer;
const std::chrono::steady_clock::duration m_check_period;
};
// Create guard for connection.
timer_guard_t
create_timer_guard()
{
return timer_guard_t{ m_env, m_mbox, m_check_period };
}
void start() const noexcept {}
void stop() const noexcept {}
struct factory_t
{
so_5::environment_t & m_env;
so_5::mbox_t m_mbox;
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 } )
: m_env{ env }
, m_mbox{ std::move( mbox ) }
, m_check_period{ check_period }
{}
auto
create( asio_ns::io_context & ) const
{
return std::make_shared< so_timer_manager_t >( m_env, m_mbox, m_check_period );
}
};
private:
so_5::environment_t & m_env;
so_5::mbox_t m_mbox;
const std::chrono::steady_clock::duration m_check_period;
};
#else
// The implementation of so_timer_manager for SO-5.6 and newer.
// There is no need to hold a reference to SObjectizer Environment for working
// with timers.
class so_timer_manager_t final
{
public:
so_timer_manager_t(
so_5::mbox_t mbox,
std::chrono::steady_clock::duration check_period )
: m_mbox{ std::move( mbox ) }
, m_check_period{ check_period }
{}
class timer_guard_t final
{
public:
timer_guard_t(
so_5::mbox_t mbox,
std::chrono::steady_clock::duration check_period )
: m_mbox{ std::move( mbox ) }
, m_check_period{ check_period }
{}
void
schedule( tcp_connection_ctx_weak_handle_t weak_handle )
{
if( !m_current_op_timer.is_active() )
{
m_current_op_timer = so_5::send_periodic< msg_check_timer_t >(
m_mbox,
m_check_period,
m_check_period,
std::move(weak_handle) );
}
}
void
cancel() noexcept
{
RESTINIO_ENSURE_NOEXCEPT_CALL( m_current_op_timer.release() );
}
private:
const so_5::mbox_t m_mbox;
so_5::timer_id_t m_current_op_timer;
const std::chrono::steady_clock::duration m_check_period;
};
// Create guard for connection.
timer_guard_t
create_timer_guard()
{
return timer_guard_t{ m_mbox, m_check_period };
}
void start() const noexcept {}
void stop() const noexcept {}
struct factory_t
{
so_5::mbox_t m_mbox;
const std::chrono::steady_clock::duration m_check_period;
factory_t(
so_5::mbox_t mbox,
std::chrono::steady_clock::duration check_period = std::chrono::seconds{ 1 } )
: m_mbox{ std::move( mbox ) }
, m_check_period{ check_period }
{}
// This constructor is just for compatibility with previous versions.
factory_t(
so_5::environment_t &,
so_5::mbox_t mbox,
std::chrono::steady_clock::duration check_period = std::chrono::seconds{ 1 } )
: factory_t{ std::move(mbox), check_period }
{}
auto
create( asio_ns::io_context & ) const
{
return std::make_shared< so_timer_manager_t >( m_mbox, m_check_period );
}
};
private:
so_5::mbox_t m_mbox;
const std::chrono::steady_clock::duration m_check_period;
};
#endif
//
// a_timeout_handler_t
//
class a_timeout_handler_t final
: public so_5::agent_t
{
typedef so_5::agent_t so_base_type_t;
public:
a_timeout_handler_t( context_t ctx )
: so_base_type_t{ std::move( ctx ) }
{
so_subscribe_self()
.event(
[]( const msg_check_timer_t & msg ){
if( auto h = msg.m_weak_handle.lock() )
h->check_timeout( h );
} );
}
};
} /* namespace so5 */
} /* namespace restinio */
asio_include.hpp
restinio::so5::a_timeout_handler_t
Agent that handles timeouts.
Definition: so_timer_manager.hpp:271
restinio::so5::a_timeout_handler_t::so_base_type_t
so_5::agent_t so_base_type_t
Definition: so_timer_manager.hpp:272
restinio::so5::a_timeout_handler_t::a_timeout_handler_t
a_timeout_handler_t(context_t ctx)
Definition: so_timer_manager.hpp:275
restinio::so5::so_timer_manager_t::timer_guard_t
Timer guard for async operations.
Definition: so_timer_manager.hpp:68
restinio::so5::so_timer_manager_t::timer_guard_t::m_check_period
const std::chrono::steady_clock::duration m_check_period
Definition: so_timer_manager.hpp:111
restinio::so5::so_timer_manager_t::timer_guard_t::timer_guard_t
timer_guard_t(so_5::environment_t &env, so_5::mbox_t mbox, std::chrono::steady_clock::duration check_period)
Definition: so_timer_manager.hpp:70
restinio::so5::so_timer_manager_t::timer_guard_t::schedule
void schedule(tcp_connection_ctx_weak_handle_t weak_handle)
Schedule timeout check invocation.
Definition: so_timer_manager.hpp:81
restinio::so5::so_timer_manager_t::timer_guard_t::cancel
void cancel() noexcept
Cancel timeout guard if any.
Definition: so_timer_manager.hpp:101
restinio::so5::so_timer_manager_t::timer_guard_t::m_mbox
const so_5::mbox_t m_mbox
Definition: so_timer_manager.hpp:108
restinio::so5::so_timer_manager_t::timer_guard_t::m_current_op_timer
so_5::timer_id_t m_current_op_timer
Definition: so_timer_manager.hpp:110
restinio::so5::so_timer_manager_t::timer_guard_t::m_env
so_5::environment_t & m_env
Definition: so_timer_manager.hpp:107
restinio::so5::so_timer_manager_t
Timer factory implementation using timers from SObjectizer.
Definition: so_timer_manager.hpp:55
restinio::so5::so_timer_manager_t::so_timer_manager_t
so_timer_manager_t(so_5::environment_t &env, so_5::mbox_t mbox, std::chrono::steady_clock::duration check_period)
Definition: so_timer_manager.hpp:57
restinio::so5::so_timer_manager_t::create_timer_guard
timer_guard_t create_timer_guard()
Definition: so_timer_manager.hpp:117
restinio::so5::so_timer_manager_t::m_mbox
so_5::mbox_t m_mbox
Definition: so_timer_manager.hpp:152
restinio::so5::so_timer_manager_t::m_env
so_5::environment_t & m_env
Definition: so_timer_manager.hpp:151
restinio::so5::so_timer_manager_t::m_check_period
const std::chrono::steady_clock::duration m_check_period
Definition: so_timer_manager.hpp:153
restinio::so5::so_timer_manager_t::start
void start() const noexcept
Start/stop timer manager.
Definition: so_timer_manager.hpp:124
restinio::so5::so_timer_manager_t::stop
void stop() const noexcept
Definition: so_timer_manager.hpp:125
compiler_features.hpp
Detection of compiler version and absence of various features.
RESTINIO_ENSURE_NOEXCEPT_CALL
#define RESTINIO_ENSURE_NOEXCEPT_CALL(expr)
A wrapper around static_assert for checking that an expression is noexcept and execution of that expr...
Definition: compiler_features.hpp:70
nonstd::optional_lite::std11::move
T & move(T &t)
Definition: optional.hpp:478
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
std
STL namespace.
restinio::so5::msg_check_timer_t
Check timer.
Definition: so_timer_manager.hpp:36
restinio::so5::msg_check_timer_t::m_weak_handle
tcp_connection_ctx_weak_handle_t m_weak_handle
Definition: so_timer_manager.hpp:41
restinio::so5::msg_check_timer_t::msg_check_timer_t
msg_check_timer_t(tcp_connection_ctx_weak_handle_t weak_handle)
Definition: so_timer_manager.hpp:37
restinio::so5::so_timer_manager_t::factory_t
Definition: so_timer_manager.hpp:129
restinio::so5::so_timer_manager_t::factory_t::m_check_period
const std::chrono::steady_clock::duration m_check_period
Definition: so_timer_manager.hpp:132
restinio::so5::so_timer_manager_t::factory_t::factory_t
factory_t(so_5::environment_t &env, so_5::mbox_t mbox, std::chrono::steady_clock::duration check_period=std::chrono::seconds{ 1 })
Definition: so_timer_manager.hpp:134
restinio::so5::so_timer_manager_t::factory_t::m_mbox
so_5::mbox_t m_mbox
Definition: so_timer_manager.hpp:131
restinio::so5::so_timer_manager_t::factory_t::m_env
so_5::environment_t & m_env
Definition: so_timer_manager.hpp:130
restinio::so5::so_timer_manager_t::factory_t::create
auto create(asio_ns::io_context &) const
Definition: so_timer_manager.hpp:144
const
#define const
Definition: zconf.h:230