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

Listening TCP socket handing every accepted peer over as a TcpConnection. More...

#include <aegis2d/net/tcp_connection.hpp>

Public Types

using AcceptHandler = std::function< void(TcpConnection connection)>
 Called for each connection accepted.
 

Public Member Functions

 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 & operator= (const TcpListener &)=delete
 
 TcpListener (TcpListener &&other) noexcept
 Takes over the listener of other, which must not be used afterwards.
 
TcpListener & operator= (TcpListener &&other) noexcept
 Closes this listener and takes over the one of other.
 
 ~TcpListener ()
 Stops listening; connections already accepted stay open.
 
void start_accepting (AcceptHandler handler)
 Starts handing every accepted connection to handler, until close().
 
void close () noexcept
 Stops listening; connections already accepted stay open.
 
bool is_open () const noexcept
 Tells whether the listener is open.
 
Endpoint local_endpoint () const
 Local address and port, with the port the system picked if 0 was asked.
 

Detailed Description

Listening TCP socket handing every accepted peer over as a TcpConnection.

Once start_accepting() is called, each connection accepted is given to the handler, on the thread running the NetworkContext; the handler owns it from then on and calls TcpConnection::start() to read from it. Accepting goes on after errors. Create, close and destroy the listener on the thread that runs the context, or while the context is not running.

Member Typedef Documentation

◆ AcceptHandler

using aegis2d::net::TcpListener::AcceptHandler = std::function<void(TcpConnection connection)>

Called for each connection accepted.

An exception thrown by the handler leaves the listener accepting and propagates out of the context's poll(), run_for() or run().

Constructor & Destructor Documentation

◆ TcpListener() [1/4]

aegis2d::net::TcpListener::TcpListener ( NetworkContext &  context,
std::uint16_t  port 
)

Listens on every IPv4 interface.

Parameters
contextContext that runs the listener's operations and its connections'; it must outlive them.
portLocal port, or 0 for any free port.
Exceptions
SocketErrorif the port is already in use or cannot be bound.

◆ TcpListener() [2/4]

aegis2d::net::TcpListener::TcpListener ( NetworkContext &  context,
const Endpoint &  local 
)

Listens on a given local endpoint.

Parameters
contextContext that runs the listener's operations and its connections'; it must outlive them.
localLocal address and port, IPv4 or IPv6; port 0 picks a free port.
Exceptions
SocketErrorif the endpoint is already in use or cannot be bound.

◆ TcpListener() [3/4]

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

◆ TcpListener() [4/4]

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

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

◆ ~TcpListener()

aegis2d::net::TcpListener::~TcpListener ( )

Stops listening; connections already accepted stay open.

Member Function Documentation

◆ close()

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

Stops listening; connections already accepted stay open.

◆ is_open()

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

Tells whether the listener is open.

Returns
false after close().

◆ local_endpoint()

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

Local address and port, with the port the system picked if 0 was asked.

Returns
The bound endpoint.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

Closes this listener and takes over the one of other.

Returns
This listener.

◆ start_accepting()

void aegis2d::net::TcpListener::start_accepting ( AcceptHandler  handler)

Starts handing every accepted connection to handler, until close().

Parameters
handlerCalled on the context's thread for each connection.

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