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

#include <udp_socket.hpp>

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

Public Member Functions

 UdpSocket (std::shared_ptr< NetworkConfig > config) noexcept
 
 UdpSocket (std::shared_ptr< NetworkConfig > config, IOMode mode) noexcept
 
 UdpSocket (UdpSocket &&socket) noexcept
 
UdpSocketoperator= (UdpSocket &&socket) noexcept
 
std::size_t send (const EndpointType &endpoint, std::string_view message)
 
std::size_t send (std::string_view hostname, port_type port, std::string_view message)
 
bool send_async (const EndpointType &endpoint, std::string_view message, SendCompletion &&callback)
 
bool send_async (std::string_view hostname, port_type port, std::string_view message, SendCompletion &&callback)
 
std::string receive ()
 
bool receive_async (ReceiveCompletion &&callback)
 
void close ()
 
socket_type handle () 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::UdpSocket< EndpointType >

Class to represent a connectionless datagram network socket.

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

Constructor & Destructor Documentation

◆ UdpSocket() [1/3]

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

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

Parameters
configReference to network configuration.

◆ UdpSocket() [2/3]

template<IPEndpoint EndpointType>
fly::net::UdpSocket< EndpointType >::UdpSocket ( 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.

◆ UdpSocket() [3/3]

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

Move constructor. The provided socket is left in an 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.

◆ 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.

◆ operator=()

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

Move assignment operator. The provided socket is left in an invalid state.

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

◆ receive()

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

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

Returns
The message received.

◆ receive_async()

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

Asynchronously receive a message from an unspecified remote endpoint. 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.

◆ send() [1/2]

template<IPEndpoint EndpointType>
size_t fly::net::UdpSocket< EndpointType >::send ( const EndpointType &  endpoint,
std::string_view  message 
)

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

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

◆ send() [2/2]

template<IPEndpoint EndpointType>
size_t fly::net::UdpSocket< EndpointType >::send ( std::string_view  hostname,
port_type  port,
std::string_view  message 
)

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

Parameters
hostnameThe hostname or IP address string to transmit to.
portThe port to transmit to.
messageThe message to transmit.
Returns
The number of bytes transmitted.

◆ send_async() [1/2]

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

Asynchronously transmit a message to a specific remote endpoint. 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
endpointThe remote endpoint to transmit to.
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.

◆ send_async() [2/2]

template<IPEndpoint EndpointType>
bool fly::net::UdpSocket< EndpointType >::send_async ( std::string_view  hostname,
port_type  port,
std::string_view  message,
SendCompletion &&  callback 
)

Asynchronously transmit a message to a specific remote endpoint. 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
hostnameThe hostname or IP address string to transmit to.
portThe port to transmit to.
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: