sample/connection_state/main.cpp
#include <iostream>
#include <restinio/all.hpp>
// Class for connection state listener.
class connection_listener_t
{
// Because method of connection_listener_t can be called from
// different threads at the same time we have to protect connection
// listener.
std::mutex m_lock;
static const char *
cause_to_str( const restinio::connection_state::notice_t & notice ) noexcept
{
using namespace restinio::connection_state;
const auto cause = notice.cause();
if( restinio::holds_alternative< accepted_t >( cause ) )
return "accepted";
else if( restinio::holds_alternative< closed_t >( cause ) )
return "closed";
else if( restinio::holds_alternative< upgraded_to_websocket_t >( cause ) )
return "upgraded_to_websocket";
else
return "unknown";
}
public:
// This method will be called by RESTinio.
void state_changed(
const restinio::connection_state::notice_t & notice ) noexcept
{
std::lock_guard<std::mutex> l{ m_lock };
fmt::print( "connection-notice: {} (from {}) => {}\n",
notice.connection_id(),
notice.remote_endpoint(),
cause_to_str( notice ) );
}
};
// This is the request handler.
restinio::request_handling_status_t handler( const restinio::request_handle_t& req )
{
if( restinio::http_method_get() == req->header().method() &&
req->header().request_target() == "/" )
{
req->create_response()
.append_header( restinio::http_field::server, "RESTinio hello world server" )
.append_header_date_field()
.append_header( restinio::http_field::content_type, "text/plain; charset=utf-8" )
.set_body( fmt::format( "{} (from {}): Hello world!",
req->connection_id(),
req->remote_endpoint() ) )
.done();
return restinio::request_accepted();
}
return restinio::request_rejected();
}
int main()
{
struct my_traits_t : public restinio::default_traits_t
{
// Change the default connection state listener to our one.
using connection_state_listener_t = connection_listener_t;
};
try
{
restinio::run(
restinio::on_thread_pool< my_traits_t >( std::thread::hardware_concurrency() )
.port( 8080 )
.address( "localhost" )
// Connection listener must be set manually.
.connection_state_listener(
std::make_shared< connection_listener_t >() )
.request_handler( handler ) );
}
catch( const std::exception & ex )
{
std::cerr << "Error: " << ex.what() << std::endl;
return 1;
}
return 0;
}
all.hpp
restinio::connection_state::notice_t
An object with info about connection to be passed to state listener.
Definition: connection_state_listener.hpp:225
restinio::connection_state::notice_t::cause
RESTINIO_NODISCARD cause_t cause() const noexcept
Get the cause for the notification.
Definition: connection_state_listener.hpp:259
restinio::connection_state::notice_t::connection_id
RESTINIO_NODISCARD connection_id_t connection_id() const noexcept
Get the connection id.
Definition: connection_state_listener.hpp:244
restinio::connection_state::notice_t::remote_endpoint
RESTINIO_NODISCARD endpoint_t remote_endpoint() const noexcept
Get the remote endpoint for the connection.
Definition: connection_state_listener.hpp:249
restinio::connection_state
Definition: connection_state_listener.hpp:23
restinio::request_rejected
RESTINIO_NODISCARD constexpr request_handling_status_t request_rejected() noexcept
Definition: common_types.hpp:53
restinio::run
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.
Definition: http_server_run.hpp:216
restinio::request_handling_status_t
request_handling_status_t
Request handling status.
Definition: common_types.hpp:26
restinio::request_accepted
RESTINIO_NODISCARD constexpr request_handling_status_t request_accepted() noexcept
Definition: common_types.hpp:46
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
restinio::traits_t
Definition: traits.hpp:147