RESTinio

Public Types | Public Member Functions | Private Types | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
restinio::http_server_t< Traits > Class Template Reference

Class for http-server. More...

`#include <http_server.hpp>`

## Public Types
using | traits_t = Traits
| An alias for Traits type. More...

## Public Member Functions
| http_server_t (const http_server_t &)=delete
| http_server_t (http_server_t &&)=delete
template<typename D >
| http_server_t (io_context_holder_t io_context, basic_server_settings_t< D, Traits > &&settings)
template<typename Configurator , typename = decltype( std::declval<Configurator>()( *(static_cast<server_settings_t<Traits>*>(nullptr))))>
| http_server_t (io_context_holder_t io_context, Configurator &&configurator)
virtual | ~http_server_t ()
| It is allowed to inherit from http_server_t. More...
asio_ns::io_context & | io_context () noexcept
| Get io_context on which server runs. More...
template<typename Server_Open_Ok_CB , typename Server_Open_Error_CB >
void | open_async (Server_Open_Ok_CB open_ok_cb, Server_Open_Error_CB open_err_cb)
| Starts server in async way. More...
void | open_sync ()
| Start server. More...
template<typename Server_Close_Ok_CB , typename Server_Close_Error_CB >
void | close_async (Server_Close_Ok_CB close_ok_cb, Server_Close_Error_CB close_err_cb)
| Closes server in async way. More...
void | close_sync ()
| Stop server. More...

## Private Types
enum class | running_state_t { not_running , running }
| State of server. More...
using | connection_settings_t = impl::connection_settings_t< Traits >
using | connection_factory_t = impl::connection_factory_t< Traits >
using | acceptor_t = impl::acceptor_t< Traits >
using | timer_manager_t = typename Traits::timer_manager_t
using | timer_manager_handle_t = std::shared_ptr< timer_manager_t >

## Private Member Functions
void | call_cleanup_functor () noexcept
| Call a cleanup functor if it is defined. More...

## Static Private Member Functions
template<typename Callback >
static void | call_nothrow_cb (Callback &&cb) noexcept
| Call callback and terminate the application if callback throws. More...

## Private Attributes
io_context_shared_ptr_t | m_io_context
| A wrapper for asio io_context where server is running. More...
cleanup_functor_t | m_cleanup_functor
| An optional user's cleanup functor. More...
std::shared_ptr< acceptor_t > | m_acceptor
| Acceptor for new connections. More...
timer_manager_handle_t | m_timer_manager
| Timer manager object. More...
running_state_t | m_running_state { running_state_t::not_running }
| Server state. More...

## Detailed Description

template<typename Traits = default_traits_t>
class restinio::http_server_t< Traits >

Class for http-server.

With the help of this class one can run a server. Server can be started and stopped in sync or async way.

Please note that it is responsibility of user to provide a working context for http_server. It means that user must call asio::io_context::run() on some work thread (or on several working threads).

Sync way for starting and stopping a http_server can be used only if http_server_t::open_sync() and http_server_t::close_sync() methods are called somewhere inside asio::io_context::run(). For example:

// Create and initialize object.
restinio::http_server_t< my_traits_t > server{
restinio::own_io_context(),
[&]( auto & settings ){
//
settings
.port( args.port() )
// .set_more_params( ... )
.request_handler(
[]( restinio::request_handle_t req ){
// Handle request.
} );
} };
// Post initial action to asio event loop.
asio::post( server.io_context(),
[&] {
// Starting the server in a sync way.
server.open_sync();
} );
// Running server.
server.io_context().run();
restinio::http_server_t
Class for http-server.
Definition: http_server.hpp:169
restinio::own_io_context
io_context_holder_t own_io_context()
Function which tells that http_server should create and use its own instance of io_context.
Definition: http_server.hpp:65
restinio::request_handle_t
std::shared_ptr< request_t > request_handle_t
An alias for handle for incoming request without additional extra-data.
Definition: request_handler.hpp:463

Async way for starting and stopping a http_server can be used if http_server_t::open_async() and http_server_t::open_async() can be called from any other thread. For example:

asio::io_context io_ctx;
restinio::http_server_t< my_traits_t > server{
restinio::external_io_context(io_ctx),
[&]( auto & settings ) { ... } };
// Launch thread on which server will work.
std::thread server_thread{ [&] {
io_ctx.run();
} };
// Start server in async way. Actual start will be performed
// on the context of server_thread.
server.open_async(
// Ok callback. Nothing to do.
[]{},
// Error callback. Rethrow an exception.
[]( auto ex_ptr ) {
std::rethrow_exception( ex_ptr );
} );
...
// Wait while server_thread finishes its work.
server_thread.join();
restinio::external_io_context
io_context_holder_t external_io_context(asio_ns::io_context &ctx)
Function which tells that http_server should use external instance of io_context and should not contr...
Definition: http_server.hpp:87

Examples
  sample/using_external_io_context/main.cpp, and sample/websocket_detailed/main.cpp.

Definition at line 168 of file http_server.hpp.

## Member Typedef Documentation

## ◆ acceptor_t

template<typename Traits = default_traits_t>

using restinio::http_server_t< Traits >::acceptor_t = impl::acceptor_t< Traits >
| private

Definition at line 172 of file http_server.hpp.

## ◆ connection_factory_t

template<typename Traits = default_traits_t>

using restinio::http_server_t< Traits >::connection_factory_t = impl::connection_factory_t< Traits >
| private

Definition at line 171 of file http_server.hpp.

## ◆ connection_settings_t

template<typename Traits = default_traits_t>

using restinio::http_server_t< Traits >::connection_settings_t = impl::connection_settings_t< Traits >
| private

Definition at line 170 of file http_server.hpp.

## ◆ timer_manager_handle_t

template<typename Traits = default_traits_t>

using restinio::http_server_t< Traits >::timer_manager_handle_t = std::shared_ptr< timer_manager_t >
| private

Definition at line 174 of file http_server.hpp.

## ◆ timer_manager_t

template<typename Traits = default_traits_t>

using restinio::http_server_t< Traits >::timer_manager_t = typename Traits::timer_manager_t
| private

Definition at line 173 of file http_server.hpp.

## ◆ traits_t

template<typename Traits = default_traits_t>

using restinio::http_server_t< Traits >::traits_t = Traits

An alias for Traits type.

Since
  v.0.5.0

Definition at line 182 of file http_server.hpp.

## Member Enumeration Documentation

## ◆ running_state_t

template<typename Traits = default_traits_t>

enum class restinio::http_server_t::running_state_t
| strong private

State of server.

Enumerator
not_running |
running |

Definition at line 373 of file http_server.hpp.

## Constructor & Destructor Documentation

## ◆ http_server_t() [1/4]

template<typename Traits = default_traits_t>

restinio::http_server_t< Traits >::http_server_t | ( | const http_server_t< Traits > & |  | ) |
| delete

## ◆ http_server_t() [2/4]

template<typename Traits = default_traits_t>

restinio::http_server_t< Traits >::http_server_t | ( | http_server_t< Traits > && |  | ) |
| delete

## ◆ http_server_t() [3/4]

template<typename Traits = default_traits_t>
template<typename D >

restinio::http_server_t< Traits >::http_server_t | ( | io_context_holder_t | io_context,
|  | basic_server_settings_t< D, Traits > && | settings
| ) |  |
| inline

Definition at line 189 of file http_server.hpp.

## ◆ http_server_t() [4/4]

template<typename Traits = default_traits_t>
template<typename Configurator , typename = decltype( std::declval<Configurator>()( *(static_cast<server_settings_t<Traits>*>(nullptr))))>

restinio::http_server_t< Traits >::http_server_t | ( | io_context_holder_t | io_context,
|  | Configurator && | configurator
| ) |  |
| inline

Definition at line 232 of file http_server.hpp.

## ◆ ~http_server_t()

template<typename Traits = default_traits_t>

virtual restinio::http_server_t< Traits >::~http_server_t | ( |  | ) |
| inline virtual

It is allowed to inherit from http_server_t.

Definition at line 242 of file http_server.hpp.

## Member Function Documentation

## ◆ call_cleanup_functor()

template<typename Traits = default_traits_t>

void restinio::http_server_t< Traits >::call_cleanup_functor | ( |  | ) |
| inline private noexcept

Call a cleanup functor if it is defined.

Note
  Cleanup functor can be called only once. Next call to call_cleanup_functor() will do nothing.

Attention
  Cleanup functor can't throw.

Definition at line 392 of file http_server.hpp.

## ◆ call_nothrow_cb()

template<typename Traits = default_traits_t>
template<typename Callback >

static void restinio::http_server_t< Traits >::call_nothrow_cb | ( | Callback && | cb | ) |
| inline static private noexcept

Call callback and terminate the application if callback throws.

Definition at line 403 of file http_server.hpp.

## ◆ close_async()

template<typename Traits = default_traits_t>
template<typename Server_Close_Ok_CB , typename Server_Close_Error_CB >

void restinio::http_server_t< Traits >::close_async | ( | Server_Close_Ok_CB | close_ok_cb,
|  | Server_Close_Error_CB | close_err_cb
| ) |  |
| inline

Closes server in async way.

Note
  It doesn't call io_context to stop (

See also
  stop_io_context()).

Attention
  close_ok_cb and close_err_cb should be noexcept functions/lambdas. This requirement is not enforced by static_assert in RESTinio's code to avoid problems in cases when `std::function` is used for these callbacks.

Definition at line 319 of file http_server.hpp.

## ◆ close_sync()

template<typename Traits = default_traits_t>

void restinio::http_server_t< Traits >::close_sync | ( |  | ) |
| inline

Stop server.

If server was stopped successfully then function returns, otherwise it throws.

Definition at line 348 of file http_server.hpp.

## ◆ io_context()

template<typename Traits = default_traits_t>

asio_ns::io_context & restinio::http_server_t< Traits >::io_context | ( |  | ) |
| inline noexcept

Get io_context on which server runs.

Definition at line 249 of file http_server.hpp.

## ◆ open_async()

template<typename Traits = default_traits_t>
template<typename Server_Open_Ok_CB , typename Server_Open_Error_CB >

void restinio::http_server_t< Traits >::open_async | ( | Server_Open_Ok_CB | open_ok_cb,
|  | Server_Open_Error_CB | open_err_cb
| ) |  |
| inline

Starts server in async way.

Note
  It is necessary to be sure that ioservice is running.

Attention
  open_ok_cb and open_err_cb should be noexcept functions/lambdas. This requirement is not enforced by static_assert in RESTinio's code to avoid problems in cases when `std::function` is used for these callbacks.

Definition at line 265 of file http_server.hpp.

## ◆ open_sync()

template<typename Traits = default_traits_t>

void restinio::http_server_t< Traits >::open_sync | ( |  | ) |
| inline

Start server.

If server was started successfully then function returns, otherwise it throws.

Definition at line 294 of file http_server.hpp.

## Member Data Documentation

## ◆ m_acceptor

template<typename Traits = default_traits_t>

std::shared_ptr< acceptor_t > restinio::http_server_t< Traits >::m_acceptor
| private

Acceptor for new connections.

Definition at line 367 of file http_server.hpp.

## ◆ m_cleanup_functor

template<typename Traits = default_traits_t>

cleanup_functor_t restinio::http_server_t< Traits >::m_cleanup_functor
| private

An optional user's cleanup functor.

Definition at line 364 of file http_server.hpp.

## ◆ m_io_context

template<typename Traits = default_traits_t>

io_context_shared_ptr_t restinio::http_server_t< Traits >::m_io_context
| private

A wrapper for asio io_context where server is running.

Definition at line 361 of file http_server.hpp.

## ◆ m_running_state

template<typename Traits = default_traits_t>

running_state_t restinio::http_server_t< Traits >::m_running_state { running_state_t::not_running }
| private

Server state.

Definition at line 380 of file http_server.hpp.

## ◆ m_timer_manager

template<typename Traits = default_traits_t>

timer_manager_handle_t restinio::http_server_t< Traits >::m_timer_manager
| private

Timer manager object.

Definition at line 370 of file http_server.hpp.

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

- restinio/http_server.hpp