Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::net::TcpConnection Class Reference

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
 

Detailed Description

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.

Member Typedef Documentation

◆ CloseHandler

using aegis2d::net::TcpConnection::CloseHandler = std::function<void(TcpCloseReason reason)>

Called once when the connection closes by itself; never after close().

◆ ConnectHandler

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.

◆ MessageHandler

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().

Constructor & Destructor Documentation

◆ TcpConnection() [1/3]

aegis2d::net::TcpConnection::TcpConnection ( NetworkContext &  context)
explicit

Creates an unconnected connection, to be connected with connect().

Parameters
contextContext that runs the connection's operations; it must outlive the connection.

◆ TcpConnection() [2/3]

aegis2d::net::TcpConnection::TcpConnection ( const TcpConnection &  )
delete

◆ TcpConnection() [3/3]

aegis2d::net::TcpConnection::TcpConnection ( TcpConnection &&  other)
noexcept

Takes over the connection of other, which must not be used afterwards.

◆ ~TcpConnection()

aegis2d::net::TcpConnection::~TcpConnection ( )

Closes the connection.

Member Function Documentation

◆ close()

void aegis2d::net::TcpConnection::close ( )
noexcept

Closes the connection: nothing more is received, pending sends are dropped.

The close handler is not called.

◆ connect()

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.

Parameters
remoteAddress and port of the listener.
handlerCalled on the context's thread once connected, or with the error.
Exceptions
std::logic_errorif the connection was already connected or closed.

◆ is_open()

bool aegis2d::net::TcpConnection::is_open ( ) const
noexcept

Tells whether the connection is established. Thread-safe.

Returns
true between a successful connect (or accept) and the close.

◆ local_endpoint()

Endpoint aegis2d::net::TcpConnection::local_endpoint ( ) const

Local address and port.

Returns
The local endpoint, or the unspecified endpoint when not connected.

◆ operator=() [1/2]

TcpConnection & aegis2d::net::TcpConnection::operator= ( const TcpConnection &  )
delete

◆ operator=() [2/2]

TcpConnection & aegis2d::net::TcpConnection::operator= ( TcpConnection &&  other)
noexcept

Closes this connection and takes over the one of other.

Returns
This connection.

◆ remote_endpoint()

Endpoint aegis2d::net::TcpConnection::remote_endpoint ( ) const

Address and port of the peer, kept after the connection closes.

Returns
The peer's endpoint, or the unspecified endpoint before it is connected.

◆ send()

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.

Parameters
messageBytes to send, without prefix; they are copied, the caller may reuse the buffer.
Exceptions
std::length_errorif message is larger than max_message_size.

◆ start()

void aegis2d::net::TcpConnection::start ( MessageHandler  on_message,
CloseHandler  on_close = {} 
)

Starts handing every message received to on_message, until the connection closes.

Parameters
on_messageCalled on the context's thread for each message.
on_closeCalled on the context's thread if the connection closes by itself; may be empty.
Exceptions
std::logic_errorif the connection is not established, or already started.

◆ stats()

Stats aegis2d::net::TcpConnection::stats ( ) const
noexcept

Counters since the connection was opened. Thread-safe.

Returns
The counters.

Friends And Related Symbol Documentation

◆ TcpListener

friend class TcpListener
friend

The documentation for this class was generated from the following file: