RESTinio
Loading...
Searching...
No Matches
restinio::async_chain::fixed_size_chain_t< Size, Extra_Data_Factory > Class Template Reference

A holder of fixed-size chain of asynchronous handlers. More...

#include <fixed_size.hpp>

Classes

class  actual_controller_t
 Actual implementation of the controller interface. More...

Public Member Functions

 fixed_size_chain_t ()=delete
template<typename... Schedulers>
 fixed_size_chain_t (Schedulers &&...schedulers)
 Initializing constructor.
request_handling_status_t operator() (const actual_request_handle_t &req) const

Private Types

using unique_controller_t = unique_async_handling_controller_t< Extra_Data_Factory >
 Short alias for unique controller type.
using scheduler_holder_t = generic_async_request_scheduler_t< Extra_Data_Factory >
 Short alias for a request handler.
using schedulers_array_t = std::array< scheduler_holder_t, Size >
 Short alias for an array of request handlers.
using actual_request_handle_t
 Short alias to a smart pointer to the source request.
using actual_on_next_result_t
 Short alias for the result of controller's on_next method.

Private Member Functions

template<typename Head, typename... Tail>
void store_to (std::size_t index, Head &&head, Tail &&...tail)
 Helper method to initialize the array of schedulers.

Private Attributes

schedulers_array_t m_schedulers
 The array of schedulers.

Detailed Description

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
class restinio::async_chain::fixed_size_chain_t< Size, Extra_Data_Factory >

A holder of fixed-size chain of asynchronous handlers.

Note
An instance of that type is intended to be filled with actual schedulers at the creation time. After that new schedulers can't be added to the chain, and old handlers can't be removed from the chain.

Usage example for the case when there is no extra-data in a request object (please note that this is simplified example without actual asynchronous code, all schedulers work as synchronous handlers):

struct my_traits : public restinio::default_traits_t {
};
// The first handler in the chain.
{
... // Checks values of HTTP-fields and rejects invalid requests.
// Activation of the next handler.
next( std::move(controller) );
}
// The second handler in the chain.
{
... // Checks user's credentials and rejects requests from
// non-authentificated users.
// Activation of the next handler.
next( std::move(controller) );
}
// The last handler in the chain.
{
... // Actual processing.
}
.address(...)
.port(...)
.request_handler(
// Just enumerate all handlers.
headers_checker,
authentificator,
actual_handler )
);
A holder of fixed-size chain of asynchronous handlers.
constexpr schedule_result_t ok() noexcept
Helper function to be used if scheduling was successful.
Definition common.hpp:49
void next(unique_async_handling_controller_t< Extra_Data_Factory > controller)
Command to try to switch to the next handler in an async chain.
Definition common.hpp:327
schedule_result_t
Type for return value of a scheduler in a chain.
Definition common.hpp:25
std::unique_ptr< async_handling_controller_t< Extra_Data_Factory > > unique_async_handling_controller_t
Short alias for unique_ptr to async_handling_controller.
Definition common.hpp:84
run_on_thread_pool_settings_t< Traits > on_thread_pool(std::size_t pool_size)
A special marker for the case when http_server must be run on an thread pool.
void run(asio_ns::io_context &ioctx, run_on_this_thread_settings_t< Traits > &&settings)
Helper function for running http server until ctrl+c is hit.
traits_t< asio_timer_manager_t, null_logger_t > default_traits_t
Definition traits.hpp:413
details::autodetect_request_handler_type request_handler_t
Definition traits.hpp:251

An instance of fixed_size_chain_t can also be created manually and passed to server's settings by unique_ptr:

auto chain = std::make_unique<restinio::async_chain::fixed_size_chain_t<3>>(
headers_checker, authentificator, actual_handler);
...
restinio::run(
.address(...)
.port(...)
.request_handler(std::move(chain))
);

Usage example for the case when some extra-data is incorporated into a request object (please note that this is simplified example without actual asynchronous code, all schedulers work as synchronous handlers):

struct my_extra_data_factory {
// A data formed by checker of HTTP-fields.
struct request_specific_fields_t {...};
// A data formed by user-authentificator.
struct user_info_t {...};
// A data to be incorporated into a request object.
using data_t = std::tuple<request_specific_fields_t, user_info_t>;
void make_within(restinio::extra_data_buffer_t<data_t> buf) {
new(buf.get()) data_t{};
}
};
struct my_traits : public restinio::default_traits_t {
using extra_data_factory_t = my_extra_data_factory;
using request_handler_t = restinio::async_chain::fixed_size_chain_t<
3,
extra_data_factory>;
};
using my_unique_controller_t =
using my_request_handle_t = my_unique_controller_t::actual_request_handle_t;
// The first handler in the chain.
my_unique_controller_t controller )
{
const my_request_handle_t req = acceptor->request_handle();
... // Checks values of HTTP-fields and rejects invalid requests.
...
next( std::move(controller) );
}
// The second handler in the chain.
my_unique_controller_t controller )
{
const my_request_handle_t req = acceptor->request_handle();
... // Checks user's credentials and rejects requests from
// non-authentificated users.
...
next( std::move(controller) );
}
// The last handler in the chain.
my_unique_controller_t controller )
{
const my_request_handle_t req = acceptor->request_handle();
auto & field_values = std::get<my_extra_data_factory::request_specific_fields_t>(req->extra_data());
auto & user_info = std::get<my_extra_data_factory::user_info_t>(req->extra_data());
... // Actual processing.
}
.address(...)
.port(...)
.request_handler(
// Just enumerate all handlers.
headers_checker,
authentificator,
actual_handler )
);
Helper for holding a pointer to a buffer where a new object of type Extra_Data should be constructed.
void * get() const noexcept
Template Parameters
SizeThe exact number of schedulers in the chain.
Extra_Data_FactoryThe type of extra-data-factory specified in the server's traits.
Since
v.0.7.0
Examples
sample/async_chained_handlers/main.cpp.

Definition at line 179 of file fixed_size.hpp.

Member Typedef Documentation

◆ actual_on_next_result_t

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
using restinio::async_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::actual_on_next_result_t
private
Initial value:
on_next_result_t< Extra_Data_Factory > actual_on_next_result_t
Short alias for the result type of on_next method.
Definition common.hpp:162

Short alias for the result of controller's on_next method.

Definition at line 195 of file fixed_size.hpp.

◆ actual_request_handle_t

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
using restinio::async_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::actual_request_handle_t
private
Initial value:
generic_request_handle_t< typename Extra_Data_Factory::data_t > actual_request_handle_t
Short alias for request_handle type.
Definition common.hpp:154

Short alias to a smart pointer to the source request.

Definition at line 191 of file fixed_size.hpp.

◆ scheduler_holder_t

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
using restinio::async_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::scheduler_holder_t = generic_async_request_scheduler_t< Extra_Data_Factory >
private

Short alias for a request handler.

Definition at line 185 of file fixed_size.hpp.

◆ schedulers_array_t

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
using restinio::async_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::schedulers_array_t = std::array< scheduler_holder_t, Size >
private

Short alias for an array of request handlers.

Definition at line 188 of file fixed_size.hpp.

◆ unique_controller_t

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
using restinio::async_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::unique_controller_t = unique_async_handling_controller_t< Extra_Data_Factory >
private

Short alias for unique controller type.

Definition at line 182 of file fixed_size.hpp.

Constructor & Destructor Documentation

◆ fixed_size_chain_t() [1/2]

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
restinio::async_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::fixed_size_chain_t ( )
delete
Attention
The default constructor is disabled. It's because a chain should be initialized by handlers at the creation time. Because of that fixed_size_chain_t isn't a DefaultConstructible type.

◆ fixed_size_chain_t() [2/2]

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
template<typename... Schedulers>
restinio::async_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::fixed_size_chain_t ( Schedulers &&... schedulers)
inline

Initializing constructor.

Note
The number of parameters should match the value of Size template parameter.

Definition at line 293 of file fixed_size.hpp.

Member Function Documentation

◆ operator()()

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
request_handling_status_t restinio::async_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::operator() ( const actual_request_handle_t & req) const
inlinenodiscard

Initiates execution of the first scheduler in the chain.

Note
Always returns request_handling_status_t::accepted.

Definition at line 310 of file fixed_size.hpp.

◆ store_to()

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
template<typename Head, typename... Tail>
void restinio::async_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::store_to ( std::size_t index,
Head && head,
Tail &&... tail )
inlineprivate

Helper method to initialize the array of schedulers.

Definition at line 263 of file fixed_size.hpp.

Member Data Documentation

◆ m_schedulers

template<std::size_t Size, typename Extra_Data_Factory = no_extra_data_factory_t>
schedulers_array_t restinio::async_chain::fixed_size_chain_t< Size, Extra_Data_Factory >::m_schedulers
private

The array of schedulers.

Note
It's initialized in the constructor and then never changed.

Definition at line 256 of file fixed_size.hpp.


The documentation for this class was generated from the following file: