|
Aegis2D
Modular C++26 2D game engine for networked games
|
TCP connection exchanging whole messages over the byte stream. More...
#include <aegis2d/net/tcp_connection.hpp>
Classes | |
| struct | Stats |
| Counters since the connection was opened. More... | |
Public Types | |
| using | ConnectHandler = std::function< void(const std::error_code &error)> |
| Called with the result of connect(): an empty error code once connected. | |
| using | MessageHandler = std::function< void(std::span< const std::byte > message)> |
| Called for each message received. | |
| using | CloseHandler = std::function< void(TcpCloseReason reason)> |
| Called once when the connection closes by itself; never after close(). | |
Public Member Functions | |
| TcpConnection (NetworkContext &context) | |
| Creates an unconnected connection, to be connected with connect(). | |
| TcpConnection (const TcpConnection &)=delete | |
| TcpConnection & | operator= (const TcpConnection &)=delete |
| TcpConnection (TcpConnection &&other) noexcept | |
Takes over the connection of other, which must not be used afterwards. | |
| TcpConnection & | operator= (TcpConnection &&other) noexcept |
Closes this connection and takes over the one of other. | |
| ~TcpConnection () | |
| Closes the connection. | |
| void | connect (const Endpoint &remote, ConnectHandler handler) |
| Connects to a remote endpoint, asynchronously. | |
| void | start (MessageHandler on_message, CloseHandler on_close={}) |
Starts handing every message received to on_message, until the connection closes. | |
| void | send (std::span< const std::byte > message) |
| Sends a message, asynchronously. Thread-safe. | |
| void | close () noexcept |
| Closes the connection: nothing more is received, pending sends are dropped. | |
| bool | is_open () const noexcept |
| Tells whether the connection is established. Thread-safe. | |
| Endpoint | remote_endpoint () const |
| Address and port of the peer, kept after the connection closes. | |
| Endpoint | local_endpoint () const |
| Local address and port. | |
| Stats | stats () const noexcept |
| Counters since the connection was opened. Thread-safe. | |
Friends | |
| class | TcpListener |
TCP connection exchanging whole messages over the byte stream.
TCP delivers a stream, not messages: one read can return part of a message, or several. Each message is therefore sent behind its length, a little-endian u16, and the connection reads the prefix, then exactly that many bytes, so the handler always receives the message whole, however the stream was split.
A connection is accepted by a TcpListener (server), or created unconnected and given a remote endpoint with connect() (client). start() then hands every message received to a handler, on the thread running the NetworkContext, and calls a second handler once if the connection closes by itself (peer gone, error, oversized message).
send() may be called from any thread: it copies the message and hands it to the context; messages leave in the order they were sent. Create, connect, start, close and destroy the connection on the thread that runs the context, or while the context is not running.
| using aegis2d::net::TcpConnection::CloseHandler = std::function<void(TcpCloseReason reason)> |
Called once when the connection closes by itself; never after close().
| using aegis2d::net::TcpConnection::ConnectHandler = std::function<void(const std::error_code& error)> |
Called with the result of connect(): an empty error code once connected.
Errors are in the standard categories: compare them with std::errc (std::errc::connection_refused...) and show error.message() to the player.
| using aegis2d::net::TcpConnection::MessageHandler = std::function<void(std::span<const std::byte> message)> |
Called for each message received.
message is only valid during the call. An exception thrown by the handler leaves the connection reading and propagates out of the context's poll(), run_for() or run().
|
explicit |
Creates an unconnected connection, to be connected with connect().
| context | Context that runs the connection's operations; it must outlive the connection. |
|
delete |
|
noexcept |
Takes over the connection of other, which must not be used afterwards.
| aegis2d::net::TcpConnection::~TcpConnection | ( | ) |
Closes the connection.
|
noexcept |
Closes the connection: nothing more is received, pending sends are dropped.
The close handler is not called.
| void aegis2d::net::TcpConnection::connect | ( | const Endpoint & | remote, |
| ConnectHandler | handler | ||
| ) |
Connects to a remote endpoint, asynchronously.
The system may take a long time to give up on a silent host: close() stops the attempt, without calling handler. A failed attempt leaves the connection closed; retry with a new one. Windows retries a refused connection before reporting it, so a closed port is only reported after about two seconds there.
| remote | Address and port of the listener. |
| handler | Called on the context's thread once connected, or with the error. |
| std::logic_error | if the connection was already connected or closed. |
|
noexcept |
Tells whether the connection is established. Thread-safe.
true between a successful connect (or accept) and the close. | Endpoint aegis2d::net::TcpConnection::local_endpoint | ( | ) | const |
Local address and port.
|
delete |
|
noexcept |
Closes this connection and takes over the one of other.
| Endpoint aegis2d::net::TcpConnection::remote_endpoint | ( | ) | const |
Address and port of the peer, kept after the connection closes.
| void aegis2d::net::TcpConnection::send | ( | std::span< const std::byte > | message | ) |
Sends a message, asynchronously. Thread-safe.
The message leaves when the context next runs its handlers, after the messages sent before it. Messages sent before the connection is established, or after it closed, are dropped.
| message | Bytes to send, without prefix; they are copied, the caller may reuse the buffer. |
| std::length_error | if message is larger than max_message_size. |
| void aegis2d::net::TcpConnection::start | ( | MessageHandler | on_message, |
| CloseHandler | on_close = {} |
||
| ) |
Starts handing every message received to on_message, until the connection closes.
| on_message | Called on the context's thread for each message. |
| on_close | Called on the context's thread if the connection closes by itself; may be empty. |
| std::logic_error | if the connection is not established, or already started. |
|
noexcept |
Counters since the connection was opened. Thread-safe.
|
friend |