libfly  6.2.2
C++20 utility library for Linux, macOS, and Windows
fly::net::TcpSocket< EndpointType > Class Template Reference

#include <tcp_socket.hpp>

Inheritance diagram for fly::net::TcpSocket< EndpointType >:
Collaboration diagram for fly::net::TcpSocket< EndpointType >:

Public Member Functions

 TcpSocket (std::shared_ptr< NetworkConfig > config) noexcept
 
 TcpSocket (std::shared_ptr< NetworkConfig > config, IOMode mode) noexcept
 
 TcpSocket (TcpSocket &&socket) noexcept
 
TcpSocketoperator= (TcpSocket &&socket) noexcept
 
std::optional< EndpointType > remote_endpoint () const
 
ConnectedState connect (const EndpointType &endpoint)
 
ConnectedState connect (std::string_view hostname, port_type port)
 
ConnectedState connect_async (const EndpointType &endpoint, ConnectCompletion &&callback)
 
ConnectedState connect_async (std::string_view hostname, port_type port, ConnectCompletion &&callback)
 
ConnectedState finish_connect ()
 
bool is_connecting () const
 
bool is_connected () const
 
std::size_t send (std::string_view message)
 
bool send_async (std::string_view message, SendCompletion &&callback)
 
std::string receive ()
 
bool receive_async (ReceiveCompletion &&callback)
 
void close ()
 
socket_type handle () const
 
bool is_open () const
 
- Public Member Functions inherited from fly::net::detail::BaseSocket< EndpointType >
bool is_open () const
 
socket_type handle () const
 
std::uint64_t socket_id () const
 
bool set_io_mode (fly::net::IOMode mode)
 
fly::net::IOMode io_mode () const
 
std::optional< EndpointType > local_endpoint () const
 
void close ()
 
bool bind (const EndpointType &endpoint, BindMode mode) const
 
bool bind (std::string_view hostname, port_type port, BindMode mode) const
 

Static Public Member Functions

static std::optional< address_type > hostname_to_address (std::string_view hostname)
 
- Static Public Member Functions inherited from fly::net::detail::BaseSocket< EndpointType >
static constexpr bool is_ipv4 ()
 
static constexpr bool is_ipv6 ()
 
static std::optional< address_type > hostname_to_address (std::string_view hostname)
 

Additional Inherited Members

- Public Types inherited from fly::net::detail::BaseSocket< EndpointType >
using endpoint_type = EndpointType
 
using address_type = typename EndpointType::address_type
 
- Protected Member Functions inherited from fly::net::detail::BaseSocket< EndpointType >
 BaseSocket (std::shared_ptr< fly::net::NetworkConfig > config, socket_type handle, fly::net::IOMode mode) noexcept
 
 BaseSocket (const std::shared_ptr< fly::net::SocketService > &service, std::shared_ptr< fly::net::NetworkConfig > config, socket_type handle) noexcept
 
 BaseSocket (BaseSocket &&socket) noexcept
 
virtual ~BaseSocket () noexcept
 
BaseSocketoperator= (BaseSocket &&socket) noexcept
 
std::shared_ptr< fly::net::SocketServicesocket_service () const
 
std::shared_ptr< fly::net::NetworkConfignetwork_config () const
 
std::size_t packet_size () const
 

Detailed Description

template<IPEndpoint EndpointType>
class fly::net::TcpSocket< EndpointType >

Class to represent a connection-oriented streaming network socket.

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

Constructor & Destructor Documentation

◆ TcpSocket() [1/3]

template<IPEndpoint EndpointType>
fly::net::TcpSocket< EndpointType >::TcpSocket ( std::shared_ptr< NetworkConfig config)
explicitnoexcept

Constructor. Open the socket in a synchronous IO processing mode.

Parameters
configReference to network configuration.

◆ TcpSocket() [2/3]

template<IPEndpoint EndpointType>
fly::net::TcpSocket< EndpointType >::TcpSocket ( std::shared_ptr< NetworkConfig config,
IOMode  mode 
)
noexcept

Constructor. Open the socket in the provided IO processing mode.

Parameters
configReference to network configuration.
modeIO processing mode to apply to the socket.

◆ TcpSocket() [3/3]

template<IPEndpoint EndpointType>
fly::net::TcpSocket< EndpointType >::TcpSocket ( TcpSocket< EndpointType > &&  socket)
noexcept

Move constructor. The provided socket is left in a disconnected, invalid state.

Parameters
socketThe socket instance to move.

Member Function Documentation

◆ close()

template<IPEndpoint EndpointType>
void fly::net::detail::BaseSocket< EndpointType >::close

If this socket is valid, close this socket.

◆ connect() [1/2]

template<IPEndpoint EndpointType>
ConnectedState fly::net::TcpSocket< EndpointType >::connect ( const EndpointType &  endpoint)

Connect to a remote socket. If this socket was opened in an asynchronous IO processing mode, the connection may not complete immediately. In that case, the connection must be completed via finish_connect() after the socket becomes writable.

Parameters
endpointThe remote endpoint to connect to.
Returns
The connection state (disconnected, connecting, or connected).

◆ connect() [2/2]

template<IPEndpoint EndpointType>
ConnectedState fly::net::TcpSocket< EndpointType >::connect ( std::string_view  hostname,
port_type  port 
)

Connect to a remote socket. If this socket was opened in an asynchronous IO processing mode, the connection may not complete immediately. In that case, the connection must be completed via finish_connect() after the socket becomes writable.

Parameters
hostnameThe hostname or IP address string to connect to.
portThe port to connect to.
Returns
The connection state (disconnected, connecting, or connected).

◆ connect_async() [1/2]

template<IPEndpoint EndpointType>
ConnectedState fly::net::TcpSocket< EndpointType >::connect_async ( const EndpointType &  endpoint,
ConnectCompletion &&  callback 
)

Asynchronously connect to a remote socket. May only be used if this socket was created through a socket service.

A connection attempt will be made immediately. If successful, the provided callback will not invoked; rather, the appropriate connection state will be returned.

If the immediate attempt fails because the operation would have blocked, the attempt will be completed asynchronously later. The provided callback will be invoked upon completion with the new connection state.

Parameters
endpointThe remote endpoint to connect to.
callbackThe callback to invoke if the operation completes asynchronously.
Returns
The connection state (disconnected, connecting, or connected).

◆ connect_async() [2/2]

template<IPEndpoint EndpointType>
ConnectedState fly::net::TcpSocket< EndpointType >::connect_async ( std::string_view  hostname,
port_type  port,
ConnectCompletion &&  callback 
)

Asynchronously connect to a remote socket. May only be used if this socket was created through a socket service.

A connection attempt will be made immediately. If successful, the provided callback will not invoked; rather, the appropriate connection state will be returned.

If the immediate attempt fails because the operation would have blocked, the attempt will be completed asynchronously later. The provided callback will be invoked upon completion with the new connection state.

Parameters
hostnameThe hostname or IP address string to connect to.
portThe port to connect to.
callbackThe callback to invoke if the operation completes asynchronously.
Returns
The connection state (disconnected, connecting, or connected).

◆ finish_connect()

template<IPEndpoint EndpointType>
ConnectedState fly::net::TcpSocket< EndpointType >::finish_connect

After an asynchronous socket in a connecting state becomes available for writing, verify the socket is healthy and update its state as connected.

Returns
The connection state (disconnected or connected).

◆ handle()

template<IPEndpoint EndpointType>
socket_type fly::net::detail::BaseSocket< EndpointType >::handle
Returns
This socket's native handle.

◆ hostname_to_address()

template<IPEndpoint EndpointType>
auto fly::net::detail::BaseSocket< EndpointType >::hostname_to_address
static

Convert a hostname or IP address string to an IP address.

Parameters
hostnameThe hostname or IP address string to convert.
Returns
If successful, the created IP address. Otherwise, an uninitialized value.

◆ is_connected()

template<IPEndpoint EndpointType>
bool fly::net::TcpSocket< EndpointType >::is_connected
Returns
True if this socket is connected to a remote endpoint.

◆ is_connecting()

template<IPEndpoint EndpointType>
bool fly::net::TcpSocket< EndpointType >::is_connecting
Returns
True if this socket is connecting to a remote endpoint.

◆ is_open()

template<IPEndpoint EndpointType>
bool fly::net::detail::BaseSocket< EndpointType >::is_open
Returns
True if the socket handle is opened.

◆ operator=()

template<IPEndpoint EndpointType>
TcpSocket< EndpointType > & fly::net::TcpSocket< EndpointType >::operator= ( TcpSocket< EndpointType > &&  socket)
noexcept

Move assignment operator. The provided socket is left in a disconnected, invalid state.

Parameters
socketThe socket instance to move.
Returns
A reference to this socket.

◆ receive()

template<IPEndpoint EndpointType>
std::string fly::net::TcpSocket< EndpointType >::receive

Receive a message from the connected remote socket. If an error occurs on the socket, the socket will be closed.

Returns
The message received.

◆ receive_async()

template<IPEndpoint EndpointType>
bool fly::net::TcpSocket< EndpointType >::receive_async ( ReceiveCompletion &&  callback)

Asynchronously receive a message from the connected remote socket. May only be used if this socket was created through a socket service.

Upon completion, the provided callback will be invoked with the message received. If an error occurs on the socket, the callback will still be invoked with any message partially received, but the socket will also be closed before the invocation.

Parameters
callbackThe callback to invoke when the operation has completed.
Returns
True if the socket service and the provided callback are valid.

◆ remote_endpoint()

template<IPEndpoint EndpointType>
std::optional< EndpointType > fly::net::TcpSocket< EndpointType >::remote_endpoint

Retrieve the remote endpoint to which this socket is connected.

Returns
If successful, the connected endpoint. Otherwise, an uninitialized value.

◆ send()

template<IPEndpoint EndpointType>
std::size_t fly::net::TcpSocket< EndpointType >::send ( std::string_view  message)

Transmit a message to the connected remote socket. If an error occurs on the socket, the socket will be closed.

Parameters
messageThe message to transmit.
Returns
The number of bytes transmitted.

◆ send_async()

template<IPEndpoint EndpointType>
bool fly::net::TcpSocket< EndpointType >::send_async ( std::string_view  message,
SendCompletion &&  callback 
)

Asynchronously transmit a message to the connected remote socket. May only be used if this socket was created through a socket service.

Upon completion, the provided callback will be invoked with the number of bytes that were transmitted. If an error occurs on the socket, the callback will still be invoked with the number of bytes successfully transmitted, but the socket will also be closed before the invocation.

Parameters
messageThe message to transmit.
callbackThe callback to invoke when the operation has completed.
Returns
True if the socket service and the provided callback are valid.

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