cppnet
C++ network utilities for asynchronous servers.
Loading...
Searching...
No Matches
Classes | Public Types | Public Member Functions | Protected Member Functions | List of all members
net::service::async_tcp_service< TCPStreamHandler, Size > Class Template Reference

A ServiceLike Async TCP Service. More...

#include <net/service/async_tcp_service.hpp>

Classes

struct  read_context
 A read context. More...
 

Public Types

template<typename T >
using socket_address = io::socket::socket_address< T >
 Templated socket address type.
 
using async_context = service::async_context
 The async context type.
 
using async_scope = async_context::async_scope
 The async scope type.
 
using multiplexer_type = async_context::multiplexer_type
 The io multiplexer type.
 
using socket_handle = io::socket::socket_handle
 The socket handle type.
 
using socket_dialog = io::socket::socket_dialog< multiplexer_type >
 The socket dialog type.
 
enum  signals
 An enum of all valid async context signals.
 

Public Member Functions

auto signal_handler (int signum) noexcept -> void
 handle signals.
 
auto start (async_context &ctx) noexcept -> std::error_code
 Start the service on the context.
 
auto submit_recv (async_context &ctx, const socket_dialog &socket, std::shared_ptr< read_context > rctx) -> void
 Submits an asynchronous socket recv.
 

Protected Member Functions

 async_tcp_service ()=default
 Default constructor.
 
template<typename T >
 async_tcp_service (socket_address< T > address) noexcept
 Socket address constructor.
 

Detailed Description

template<typename TCPStreamHandler, std::size_t Size = 64 * 1024UL>
class net::service::async_tcp_service< TCPStreamHandler, Size >

A ServiceLike Async TCP Service.

Template Parameters
StreamHandlerThe StreamHandler type that derives from async_tcp_service.
SizeThe socket read buffer size. (Default 64KiB).
Note
The default constructor of async_tcp_service is protected so async_tcp_service can't be constructed without a stream handler (which would be UB).

async_tcp_service is a CRTP base class compliant with the ServiceLike concept. It must be used with an inheriting CRTP specialization that defines what the service should do with bytes it reads off the wire. StreamHandler must define an operator() overload that eventually calls reader to restart the read loop. It also optionally specifies an initialize member that can be used to configure the service socket, and a stop member that can be used to gracefully drain and stop TCP connections upon receiving a terminate signal. See noop_service below for an example of how to specialize async_tcp_service.

struct noop_service : public async_tcp_service<noop_service>
{
template <typename T>
{}
// Optional. initialize() is called by service.start() and is
// used to set optional socket and file descriptor options before
// The TCP server starts to accept connections.
auto initialize(const socket_handle &socket) -> std::error_code
{
return {};
}
// Optional. stop() is called each time the service receives a
// terminate signal. Terminate signals can be received more than
// once.
auto stop() -> void {}
auto operator()(async_context &ctx, const socket_dialog &socket,
std::shared_ptr<read_context> rctx,
std::span<const std::byte> buf) -> void
{
reader(ctx, socket, std::move(rctx));
}
};
A ServiceLike Async TCP Service.
Definition async_tcp_service.hpp:73
io::socket::socket_dialog< multiplexer_type > socket_dialog
The socket dialog type.
Definition async_tcp_service.hpp:86
io::socket::socket_address< T > socket_address
Templated socket address type.
Definition async_tcp_service.hpp:76
io::socket::socket_handle socket_handle
The socket handle type.
Definition async_tcp_service.hpp:84
service::async_context async_context
The async context type.
Definition async_tcp_service.hpp:78

Constructor & Destructor Documentation

◆ async_tcp_service()

template<typename TCPStreamHandler , std::size_t Size = 64 * 1024UL>
template<typename T >
net::service::async_tcp_service< TCPStreamHandler, Size >::async_tcp_service ( socket_address< T address)
explicitprotectednoexcept

Socket address constructor.

Template Parameters
TThe socket address type.
Parameters
addressThe service address to bind.

Member Function Documentation

◆ signal_handler()

template<typename TCPStreamHandler , std::size_t Size = 64 * 1024UL>
auto net::service::async_tcp_service< TCPStreamHandler, Size >::signal_handler ( int  signum) -> void
noexcept

handle signals.

Parameters
signumThe signal number to handle.

◆ start()

template<typename TCPStreamHandler , std::size_t Size = 64 * 1024UL>
auto net::service::async_tcp_service< TCPStreamHandler, Size >::start ( async_context ctx) -> std::error_code
noexcept

Start the service on the context.

Parameters
ctxThe async context to start the service on.
Returns
An error code indicating success.

◆ submit_recv()

template<typename TCPStreamHandler , std::size_t Size = 64 * 1024UL>
auto net::service::async_tcp_service< TCPStreamHandler, Size >::submit_recv ( async_context ctx,
const socket_dialog socket,
std::shared_ptr< read_context rctx 
) -> void

Submits an asynchronous socket recv.

Parameters
ctxThe async context to start the reader on.
socketthe socket to read data from.
rctxA shared pointer to a mutable read buffer.

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