Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::net::UdpSocket Class Reference

UDP socket bound to a local endpoint, receiving and sending whole datagrams. More...

#include <aegis2d/net/udp_socket.hpp>

Classes

struct  Stats
 Counters since the socket was opened. More...
 

Public Types

using ReceiveHandler = std::function< void(const Endpoint &from, std::span< const std::byte > datagram)>
 Called for each datagram received.
 

Public Member Functions

 UdpSocket (NetworkContext &context, std::uint16_t port)
 Opens an IPv4 socket on every local interface.
 
 UdpSocket (NetworkContext &context, const Endpoint &local)
 Opens a socket bound to a given local endpoint.
 
 UdpSocket (const UdpSocket &)=delete
 
UdpSocket & operator= (const UdpSocket &)=delete
 
 UdpSocket (UdpSocket &&other) noexcept
 Takes over the socket of other, which must not be used afterwards.
 
UdpSocket & operator= (UdpSocket &&other) noexcept
 Closes this socket and takes over the one of other.
 
 ~UdpSocket ()
 Closes the socket.
 
void start_receiving (ReceiveHandler handler)
 Starts handing every datagram received to handler, until close().
 
void send_to (const Endpoint &to, std::span< const std::byte > datagram)
 Sends a datagram, asynchronously. Thread-safe.
 
void close () noexcept
 Closes the socket: no more datagrams are received, pending sends are dropped.
 
bool is_open () const noexcept
 Tells whether the socket is open.
 
Endpoint local_endpoint () const
 Local address and port, with the port the system picked if 0 was asked.
 
Stats stats () const noexcept
 Counters since the socket was opened. Thread-safe.
 

Detailed Description

UDP socket bound to a local endpoint, receiving and sending whole datagrams.

Once start_receiving() is called, every datagram received is handed to the handler with its sender, on the thread running the NetworkContext. Datagrams larger than max_datagram_size (1200 bytes) are dropped and counted, and receiving goes on after any error, including the "port unreachable" that Windows reports on the next receive when an earlier send reached a closed port.

send_to() may be called from any thread: it copies the datagram and hands it to the context. Create, close and destroy the socket on the thread that runs the context, or while the context is not running.

Member Typedef Documentation

◆ ReceiveHandler

using aegis2d::net::UdpSocket::ReceiveHandler = std::function<void(const Endpoint& from, std::span<const std::byte> datagram)>

Called for each datagram received.

datagram is only valid during the call. An exception thrown by the handler leaves the socket receiving and propagates out of the context's poll(), run_for() or run().

Constructor & Destructor Documentation

◆ UdpSocket() [1/4]

aegis2d::net::UdpSocket::UdpSocket ( NetworkContext &  context,
std::uint16_t  port 
)

Opens an IPv4 socket on every local interface.

Parameters
contextContext that runs the socket's operations; it must outlive the socket.
portLocal port, or 0 for any free port (clients).
Exceptions
SocketErrorif the port is already in use or cannot be bound.

◆ UdpSocket() [2/4]

aegis2d::net::UdpSocket::UdpSocket ( NetworkContext &  context,
const Endpoint &  local 
)

Opens a socket bound to a given local endpoint.

Parameters
contextContext that runs the socket's operations; it must outlive the socket.
localLocal address and port, IPv4 or IPv6; port 0 picks a free port.
Exceptions
SocketErrorif the endpoint is already in use or cannot be bound.

◆ UdpSocket() [3/4]

aegis2d::net::UdpSocket::UdpSocket ( const UdpSocket &  )
delete

◆ UdpSocket() [4/4]

aegis2d::net::UdpSocket::UdpSocket ( UdpSocket &&  other)
noexcept

Takes over the socket of other, which must not be used afterwards.

◆ ~UdpSocket()

aegis2d::net::UdpSocket::~UdpSocket ( )

Closes the socket.

Member Function Documentation

◆ close()

void aegis2d::net::UdpSocket::close ( )
noexcept

Closes the socket: no more datagrams are received, pending sends are dropped.

◆ is_open()

bool aegis2d::net::UdpSocket::is_open ( ) const
noexcept

Tells whether the socket is open.

Returns
false after close().

◆ local_endpoint()

Endpoint aegis2d::net::UdpSocket::local_endpoint ( ) const

Local address and port, with the port the system picked if 0 was asked.

Returns
The bound endpoint.

◆ operator=() [1/2]

UdpSocket & aegis2d::net::UdpSocket::operator= ( const UdpSocket &  )
delete

◆ operator=() [2/2]

UdpSocket & aegis2d::net::UdpSocket::operator= ( UdpSocket &&  other)
noexcept

Closes this socket and takes over the one of other.

Returns
This socket.

◆ send_to()

void aegis2d::net::UdpSocket::send_to ( const Endpoint &  to,
std::span< const std::byte >  datagram 
)

Sends a datagram, asynchronously. Thread-safe.

The datagram leaves when the context next runs its handlers: stopping the context right after the call, or closing the socket, drops it.

Parameters
toDestination.
datagramBytes to send; they are copied, the caller may reuse the buffer.
Exceptions
std::length_errorif datagram is larger than max_datagram_size.

◆ start_receiving()

void aegis2d::net::UdpSocket::start_receiving ( ReceiveHandler  handler)

Starts handing every datagram received to handler, until close().

Parameters
handlerCalled on the context's thread for each datagram.

◆ stats()

Stats aegis2d::net::UdpSocket::stats ( ) const
noexcept

Counters since the socket was opened. Thread-safe.

Returns
The counters.

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