restinio::running_server_instance_t< Http_Server > Class Template Reference

A helper class used in an implementation of run_async function. More...

`#include <http_server_run.hpp>`

## Public Member Functions

void | stop () noexcept
void | wait () noexcept
| Wait for the shutdown of HTTP-server. More...

## Private Member Functions

| running_server_instance_t (io_context_holder_t io_context, server_settings_t< typename Http_Server::traits_t > &&settings, std::size_t thread_pool_size)
| Initializing constructor. More...
void | start ()
| Start the HTTP-server. More...

## Private Attributes

Http_Server | m_server
| Actual server instance. More...
on_pool_runner_t< Http_Server > | m_runner
| The runner of the server. More...

## Friends

template<typename Traits >
running_server_handle_t< Traits > | run_async (io_context_holder_t, server_settings_t< Traits > &&, std::size_t thread_pool_size)
| Creates an instance of HTTP-server and launches it on a separate thread or thread pool. More...

## Detailed Description

template<typename Http_Server>
class restinio::running_server_instance_t< Http_Server >

A helper class used in an implementation of run_async function.

An instance of that class holds an HTTP-server and thread pool on that this HTTP-server is launched.

The HTTP-server will automatically be stopped in the destructor. However, a user can stop the HTTP-server manually by using stop() and wait() methods.

Since
  v.0.6.7

Definition at line 941 of file http_server_run.hpp.

## Constructor & Destructor Documentation

## running_server_instance_t()

template<typename Http_Server >

restinio::running_server_instance_t< Http_Server >::running_server_instance_t | ( | io_context_holder_t | io_context,
|  | server_settings_t< typename Http_Server::traits_t > && | settings,
|  | std::size_t | thread_pool_size
| ) |  |

inlineprivate

Initializing constructor.

Definition at line 957 of file http_server_run.hpp.

## Member Function Documentation

## start()

template<typename Http_Server >

void restinio::running_server_instance_t< Http_Server >::start | ( |  | ) |

inlineprivate

Start the HTTP-server.

Returns when HTTP-server started or some startup failure detected. It means that the caller thread will be blocked until HTTP-server calls on_ok or on_error callback.

Throws an exception on an error.

Definition at line 975 of file http_server_run.hpp.

## stop()

template<typename Http_Server >

void restinio::running_server_instance_t< Http_Server >::stop | ( |  | ) |

inlinenoexcept

Stop the HTTP-server.

This method initiates shutdown procedure that can take some time. But stop() returns without the waiting for the completeness of the shutdown. To wait for the completeness use wait() method:

auto server = restinio::run_async(...);
...
server->stop(); // Returns without the waiting.
... // Some other actions.
server->wait(); // Returns only when HTTP-server stopped.
restinio::run_async
RESTINIO_NODISCARD running_server_handle_t< Traits > run_async(io_context_holder_t io_context, server_settings_t< Traits > &&settings, std::size_t thread_pool_size)
Creates an instance of HTTP-server and launches it on a separate thread or thread pool.
Definition: http_server_run.hpp:1112

Attention
  The current version doesn't guarantee that stop() can be called safely several times. Please take care of that and call stop() only once.

Definition at line 1009 of file http_server_run.hpp.

## wait()

template<typename Http_Server >

void restinio::running_server_instance_t< Http_Server >::wait | ( |  | ) |

inlinenoexcept

Wait for the shutdown of HTTP-server.

Note
  Method stop() should be called before the call to wait():
  auto server = restinio::run_async(...);
  ...
  server->stop(); // Initiates the shutdown and returns without the waiting.
  server->wait(); // Returns only when HTTP-server stopped.

Attention
  The current version doesn't guarantee that wait() can be called safely several times. Please take care of that and call wait() only once.

Definition at line 1032 of file http_server_run.hpp.

## Friends And Related Function Documentation

## run_async

template<typename Http_Server >
template<typename Traits >

running_server_handle_t< Traits > run_async | ( | io_context_holder_t | io_context,
|  | server_settings_t< Traits > && | settings,
|  | std::size_t | thread_pool_size
| ) |  |

friend

Creates an instance of HTTP-server and launches it on a separate thread or thread pool.

Usage example:

int main() {
auto server = restinio::run_async(
// Asio's io_context to be used.
// HTTP-server will use own Asio's io_context object.
restinio::own_io_context(),
// The settings for the HTTP-server.
restinio::server_settings_t{}
.address("127.0.0.1")
.port(8080)
.request_handler(...),
// The size of thread-pool for the HTTP-server.
16);
// If we are here and run_async doesn't throw then HTTP-server
// is started.
... // Some other actions.
// No need to stop HTTP-server manually. It will be automatically
// stopped in the destructor of `server` object.
}
restinio::basic_server_settings_t::address
Derived & address(std::string addr) &
Definition: settings.hpp:643
restinio::server_settings_t
A fluent style interface for setting http server params.
Definition: settings.hpp:1768
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

Or, if user-defined traits should be used:

int main() {
struct my_traits : public restinio::default_traits_t {
...
};
auto server = restinio::run_async<my_traits>(
restinio::own_io_context(),
restinio::server_settings_t<my_traits>{}
.address(...)
.port(...)
.request_handler(...),
// Use just one thread for the HTTP-server.
1u);
... // Some other actions.
}
restinio::traits_t
Definition: traits.hpp:147

run_async() returns control when HTTP-server is started or some startup failure is detected. But if a failure is detected then an exception is thrown. So if run_async() returns successfuly then HTTP-server is working.

The started HTTP-server will be automatically stopped at the destruction of the returned value. Because of that the returned value should be stored for the time while HTTP-server is needed.

The started HTTP-server can be stopped manually by calling stop() and wait() methods:

auto server = restinio::run_async(...);
...
server->stop(); // Returns without the waiting.
... // Some other actions.
server->wait(); // Returns only when HTTP-server stopped.

Since
  v.0.6.7

Definition at line 1112 of file http_server_run.hpp.

## Member Data Documentation

## m_runner

template<typename Http_Server >

on_pool_runner_t< Http_Server > restinio::running_server_instance_t< Http_Server >::m_runner

private

The runner of the server.

Definition at line 954 of file http_server_run.hpp.

## m_server

template<typename Http_Server >

Http_Server restinio::running_server_instance_t< Http_Server >::m_server

private

Actual server instance.

Definition at line 951 of file http_server_run.hpp.

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

- restinio/http_server_run.hpp