6#include <aegis2d/net/export.hpp>
14#include <system_error>
62 using MessageHandler = std::function<void(std::span<const std::byte> message)>;
70 std::uint64_t sent = 0;
72 std::uint64_t received = 0;
115 void send(std::span<const std::byte> message);
123 [[nodiscard]]
bool is_open() const noexcept;
135 [[nodiscard]]
Stats stats() const noexcept;
141 std::shared_ptr<Impl> impl_;
190 [[nodiscard]]
bool is_open() const noexcept;
198 std::shared_ptr<Impl> impl_;
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
TCP connection exchanging whole messages over the byte stream.
Definition tcp_connection.hpp:51
std::function< void(TcpCloseReason reason)> CloseHandler
Called once when the connection closes by itself; never after close().
Definition tcp_connection.hpp:65
TcpConnection(NetworkContext &context)
Creates an unconnected connection, to be connected with connect().
void send(std::span< const std::byte > message)
Sends a message, asynchronously. Thread-safe.
void connect(const Endpoint &remote, ConnectHandler handler)
Connects to a remote endpoint, asynchronously.
std::function< void(const std::error_code &error)> ConnectHandler
Called with the result of connect(): an empty error code once connected.
Definition tcp_connection.hpp:56
~TcpConnection()
Closes the connection.
TcpConnection(TcpConnection &&other) noexcept
Takes over the connection of other, which must not be used afterwards.
std::function< void(std::span< const std::byte > message)> MessageHandler
Called for each message received.
Definition tcp_connection.hpp:62
TcpConnection(const TcpConnection &)=delete
TcpConnection & operator=(TcpConnection &&other) noexcept
Closes this connection and takes over the one of other.
TcpConnection & operator=(const TcpConnection &)=delete
void close() noexcept
Closes the connection: nothing more is received, pending sends are dropped.
void start(MessageHandler on_message, CloseHandler on_close={})
Starts handing every message received to on_message, until the connection closes.
Listening TCP socket handing every accepted peer over as a TcpConnection.
Definition tcp_connection.hpp:150
TcpListener & operator=(TcpListener &&other) noexcept
Closes this listener and takes over the one of other.
void close() noexcept
Stops listening; connections already accepted stay open.
~TcpListener()
Stops listening; connections already accepted stay open.
std::function< void(TcpConnection connection)> AcceptHandler
Called for each connection accepted.
Definition tcp_connection.hpp:155
TcpListener & operator=(const TcpListener &)=delete
TcpListener(NetworkContext &context, std::uint16_t port)
Listens on every IPv4 interface.
TcpListener(NetworkContext &context, const Endpoint &local)
Listens on a given local endpoint.
TcpListener(const TcpListener &)=delete
TcpListener(TcpListener &&other) noexcept
Takes over the listener of other, which must not be used afterwards.
void start_accepting(AcceptHandler handler)
Starts handing every accepted connection to handler, until close().
IP address and port of a network peer.
Binary serialization, UDP/TCP sockets and the network thread.
Definition binary.hpp:14
constexpr std::size_t message_prefix_size
Size of the length prefix in front of each message: a little-endian u16.
Definition tcp_connection.hpp:24
constexpr std::size_t max_message_size
Largest message a TcpConnection sends or accepts, in bytes, prefix excluded.
Definition tcp_connection.hpp:21
TcpCloseReason
Why a TcpConnection closed by itself.
Definition tcp_connection.hpp:27
@ MessageTooLarge
The peer announced a message larger than max_message_size.
@ Error
The connection failed: reset by the peer, unreachable, failed send...
@ PeerClosed
The peer closed its side of the connection.
Definition endpoint.hpp:85
Event loop that runs the network operations.
Error thrown when a socket cannot be opened or bound.
Counters since the connection was opened.
Definition tcp_connection.hpp:68