RESTinio
Loading...
Searching...
No Matches
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.

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.
void start ()
 Start the HTTP-server.

Private Attributes

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

Friends

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)
 Creates an instance of HTTP-server and launches it on a separate thread or thread pool.

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 1028 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 1044 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 1062 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.
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.
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 1096 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 1119 of file http_server_run.hpp.

◆ 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.
// The settings for the HTTP-server.
.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.
}
Derived & address(std::string addr) &
Definition settings.hpp:642
A fluent style interface for setting http server params.
io_context_holder_t own_io_context()
Function which tells that http_server should create and use its own instance of io_context.

Or, if user-defined traits should be used:

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

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 1199 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 1041 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 1038 of file http_server_run.hpp.


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