Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
tcp_connection.hpp
Go to the documentation of this file.
1
3#pragma once
6#include <aegis2d/net/export.hpp>
8
9#include <cstddef>
10#include <cstdint>
11#include <functional>
12#include <memory>
13#include <span>
14#include <system_error>
15
16namespace aegis2d::net {
17
21inline constexpr std::size_t max_message_size = 4096;
22
24inline constexpr std::size_t message_prefix_size = 2;
25
27enum class TcpCloseReason : std::uint8_t {
31 Error,
34};
35
51class AEGIS2D_NET_EXPORT TcpConnection {
52public:
56 using ConnectHandler = std::function<void(const std::error_code& error)>;
57
62 using MessageHandler = std::function<void(std::span<const std::byte> message)>;
63
65 using CloseHandler = std::function<void(TcpCloseReason reason)>;
66
68 struct Stats {
70 std::uint64_t sent = 0;
72 std::uint64_t received = 0;
73 };
74
78 explicit TcpConnection(NetworkContext& context);
79
80 TcpConnection(const TcpConnection&) = delete;
83 TcpConnection(TcpConnection&& other) noexcept;
89
98 void connect(const Endpoint& remote, ConnectHandler handler);
99
106 void start(MessageHandler on_message, CloseHandler on_close = {});
107
115 void send(std::span<const std::byte> message);
116
119 void close() noexcept;
120
123 [[nodiscard]] bool is_open() const noexcept;
124
127 [[nodiscard]] Endpoint remote_endpoint() const;
128
131 [[nodiscard]] Endpoint local_endpoint() const;
132
135 [[nodiscard]] Stats stats() const noexcept;
136
137private:
138 friend class TcpListener;
139 struct Impl;
140 explicit TcpConnection(std::shared_ptr<Impl> impl) noexcept;
141 std::shared_ptr<Impl> impl_;
142};
143
150class AEGIS2D_NET_EXPORT TcpListener {
151public:
155 using AcceptHandler = std::function<void(TcpConnection connection)>;
156
162 TcpListener(NetworkContext& context, std::uint16_t port);
163
169 TcpListener(NetworkContext& context, const Endpoint& local);
170
171 TcpListener(const TcpListener&) = delete;
174 TcpListener(TcpListener&& other) noexcept;
180
184
186 void close() noexcept;
187
190 [[nodiscard]] bool is_open() const noexcept;
191
194 [[nodiscard]] Endpoint local_endpoint() const;
195
196private:
197 struct Impl;
198 std::shared_ptr<Impl> impl_;
199};
200
201} // 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
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