|
Aegis2D
Modular C++26 2D game engine for networked games
|
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. | |
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.
| 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().
| aegis2d::net::UdpSocket::UdpSocket | ( | NetworkContext & | context, |
| std::uint16_t | port | ||
| ) |
Opens an IPv4 socket on every local interface.
| context | Context that runs the socket's operations; it must outlive the socket. |
| port | Local port, or 0 for any free port (clients). |
| SocketError | if the port is already in use or cannot be bound. |
| aegis2d::net::UdpSocket::UdpSocket | ( | NetworkContext & | context, |
| const Endpoint & | local | ||
| ) |
Opens a socket bound to a given local endpoint.
| context | Context that runs the socket's operations; it must outlive the socket. |
| local | Local address and port, IPv4 or IPv6; port 0 picks a free port. |
| SocketError | if the endpoint is already in use or cannot be bound. |
|
delete |
|
noexcept |
Takes over the socket of other, which must not be used afterwards.
| aegis2d::net::UdpSocket::~UdpSocket | ( | ) |
Closes the socket.
|
noexcept |
Closes the socket: no more datagrams are received, pending sends are dropped.
|
noexcept |
Tells whether the socket is open.
false after close(). | Endpoint aegis2d::net::UdpSocket::local_endpoint | ( | ) | const |
Local address and port, with the port the system picked if 0 was asked.
Closes this socket and takes over the one of other.
| 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.
| to | Destination. |
| datagram | Bytes to send; they are copied, the caller may reuse the buffer. |
| std::length_error | if datagram is larger than max_datagram_size. |
| void aegis2d::net::UdpSocket::start_receiving | ( | ReceiveHandler | handler | ) |
Starts handing every datagram received to handler, until close().
| handler | Called on the context's thread for each datagram. |
|
noexcept |
Counters since the socket was opened. Thread-safe.