Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
udp_socket.hpp
Go to the documentation of this file.
1
3#pragma once
6#include <aegis2d/net/export.hpp>
9
10#include <cstddef>
11#include <cstdint>
12#include <functional>
13#include <memory>
14#include <span>
15#include <stdexcept>
16
17namespace aegis2d::net {
18
29class AEGIS2D_NET_EXPORT UdpSocket {
30public:
36 std::function<void(const Endpoint& from, std::span<const std::byte> datagram)>;
37
39 struct Stats {
41 std::uint64_t sent = 0;
43 std::uint64_t received = 0;
45 std::uint64_t dropped = 0;
47 std::uint64_t send_errors = 0;
48 };
49
54 UdpSocket(NetworkContext& context, std::uint16_t port);
55
60 UdpSocket(NetworkContext& context, const Endpoint& local);
61
62 UdpSocket(const UdpSocket&) = delete;
63 UdpSocket& operator=(const UdpSocket&) = delete;
65 UdpSocket(UdpSocket&& other) noexcept;
68 UdpSocket& operator=(UdpSocket&& other) noexcept;
71
75
82 void send_to(const Endpoint& to, std::span<const std::byte> datagram);
83
85 void close() noexcept;
86
89 [[nodiscard]] bool is_open() const noexcept;
90
93 [[nodiscard]] Endpoint local_endpoint() const;
94
97 [[nodiscard]] Stats stats() const noexcept;
98
99private:
100 struct Impl;
101 std::shared_ptr<Impl> impl_;
102};
103
104} // namespace aegis2d::net
IPv4 or IPv6 address and port of a peer.
Definition endpoint.hpp:22
Runs the completion of network operations: received datagrams, finished sends.
Definition context.hpp:23
UDP socket bound to a local endpoint, receiving and sending whole datagrams.
Definition udp_socket.hpp:29
UdpSocket(NetworkContext &context, const Endpoint &local)
Opens a socket bound to a given local endpoint.
std::function< void(const Endpoint &from, std::span< const std::byte > datagram)> ReceiveHandler
Called for each datagram received.
Definition udp_socket.hpp:36
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.
~UdpSocket()
Closes the socket.
UdpSocket & operator=(const UdpSocket &)=delete
UdpSocket & operator=(UdpSocket &&other) noexcept
Closes this socket and takes over the one of other.
void close() noexcept
Closes the socket: no more datagrams are received, pending sends are dropped.
UdpSocket(UdpSocket &&other) noexcept
Takes over the socket of other, which must not be used afterwards.
UdpSocket(NetworkContext &context, std::uint16_t port)
Opens an IPv4 socket on every local interface.
UdpSocket(const UdpSocket &)=delete
IP address and port of a network peer.
Binary serialization, UDP/TCP sockets and the network thread.
Definition binary.hpp:14
Definition endpoint.hpp:85
Event loop that runs the network operations.
Header carried by every UDP datagram, and the packet types.
Error thrown when a socket cannot be opened or bound.
Counters since the socket was opened.
Definition udp_socket.hpp:39