libfly  6.2.2
C++20 utility library for Linux, macOS, and Windows
fly::net::SocketService Class Reference

#include <socket_service.hpp>

Inheritance diagram for fly::net::SocketService:
Collaboration diagram for fly::net::SocketService:

Public Member Functions

 ~SocketService () noexcept
 
template<Socket SocketType>
std::shared_ptr< SocketType > create_socket ()
 
void remove_socket (socket_type handle)
 
template<Socket SocketType, SocketNotification< SocketType > Callback>
void notify_when_writable (const std::shared_ptr< SocketType > &socket, Callback &&callback)
 
template<Socket SocketType, SocketNotification< SocketType > Callback>
void notify_when_readable (const std::shared_ptr< SocketType > &socket, Callback &&callback)
 
template<Socket SocketType, SocketNotification< SocketType > Callback>
auto wrap_callback (const std::shared_ptr< SocketType > &socket, Callback &&callback) -> Notification
 

Static Public Member Functions

static std::shared_ptr< SocketServicecreate (std::shared_ptr< fly::task::SequencedTaskRunner > task_runner, std::shared_ptr< NetworkConfig > config)
 

Detailed Description

Class to monitor asynchronous socket handles for IO readiness. Sockets handles are monitored on a per-IO basis.

Author
Timothy Flynn (trfly.nosp@m.nn89.nosp@m.@pm.m.nosp@m.e)
Version
March 6, 2021

Constructor & Destructor Documentation

◆ ~SocketService()

fly::net::SocketService::~SocketService ( )
noexcept

Destructor. Deinitialize the socket service.

Member Function Documentation

◆ create()

std::shared_ptr< SocketService > fly::net::SocketService::create ( std::shared_ptr< fly::task::SequencedTaskRunner task_runner,
std::shared_ptr< NetworkConfig config 
)
static

Create a socket service.

Parameters
task_runnerTask runner for posting socket service tasks onto.
configReference to network configuration.
Returns
The created socket service.

◆ create_socket()

template<Socket SocketType>
std::shared_ptr< SocketType > fly::net::SocketService::create_socket

Create an asynchronous socket armed with this socket service for performing IO operations.

Returns
The created socket.

◆ notify_when_readable()

template<Socket SocketType, SocketNotification< SocketType > Callback>
void fly::net::SocketService::notify_when_readable ( const std::shared_ptr< SocketType > &  socket,
Callback &&  callback 
)

Monitor a socket handle for readiness to be read from.

The provided callback may be any callable type which accepts a single argument, a strong pointer to the socket being monitored. The callback is protected by the provided strong socket pointer. When the monitor is queued, the strong pointer is stored as a weak pointer until the socket becomes ready for reading. It is then converted back to a strong pointer to invoke the callback; if the lock fails, the callback is dropped.

Note: The provided callback will be triggered directly on the sequence that is monitoring all sockets. Thus, the callback should not perform any blocking operations.

Template Parameters
SocketTypeType of the socket to monitor.
CallbackType of the callback to invoke when the socket is ready for reading.
Parameters
socketThe socket to monitor.
callbackThe callback to invoke when the socket is ready for reading.

◆ notify_when_writable()

template<Socket SocketType, SocketNotification< SocketType > Callback>
void fly::net::SocketService::notify_when_writable ( const std::shared_ptr< SocketType > &  socket,
Callback &&  callback 
)

Monitor a socket handle for readiness to be written to.

The provided callback may be any callable type which accepts a single argument, a strong pointer to the socket being monitored. The callback is protected by the provided strong socket pointer. When the monitor is queued, the strong pointer is stored as a weak pointer until the socket becomes ready for writing. It is then converted back to a strong pointer to invoke the callback; if the lock fails, the callback is dropped.

Note: The provided callback will be triggered directly on the sequence that is monitoring all sockets. Thus, the callback should not perform any blocking operations.

Template Parameters
SocketTypeType of the socket to monitor.
CallbackType of the callback to invoke when the socket is ready for writing.
Parameters
socketThe socket to monitor.
callbackThe callback to invoke when the socket is ready for writing.

◆ remove_socket()

void fly::net::SocketService::remove_socket ( socket_type  handle)

Remove a socket handle from the service if it is being monitored. This is not guaranteed to cancel a pending IO readiness notification. If the service is ready to notify a socket about IO readiness, that notification will still occur.

Parameters
handleThe socket handle to remove.

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