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

Runs a NetworkContext on a thread of its own, until stopped. More...

#include <aegis2d/net/network_thread.hpp>

Public Member Functions

 NetworkThread (NetworkContext &context)
 Starts a thread running context.
 
 NetworkThread (const NetworkThread &)=delete
 
NetworkThread & operator= (const NetworkThread &)=delete
 
 NetworkThread (NetworkThread &&)=delete
 
NetworkThread & operator= (NetworkThread &&)=delete
 
 ~NetworkThread ()
 Stops the context and waits for the thread to finish.
 
void stop ()
 Stops the context and waits for the thread to finish.
 
bool running () const noexcept
 Tells whether the thread is running.
 
bool is_network_thread () const noexcept
 Tells whether the caller is the network thread.
 
std::uint64_t errors () const noexcept
 Number of exceptions that escaped a handler and were caught. Thread-safe.
 

Detailed Description

Runs a NetworkContext on a thread of its own, until stopped.

Sockets then receive, send and call their handlers on this thread, so the game thread never waits on the network. The two threads exchange messages through two core::SpscQueue, one in each direction:

  • socket handlers push what they receive into the incoming queue; the game drains it once per tick;
  • the game pushes what it sends into the outgoing queue, then calls NetworkContext::post() with a task that drains it into the sockets.

An exception escaping a handler is logged and counted, and the thread goes on running the context: one faulty message never stops the network.

Create the sockets and start them (start_receiving(), start_accepting()) before the thread, and declare the thread after them, so that it stops before they are destroyed. While it runs, only touch the sockets from their handlers or from posted tasks, apart from what they document as thread-safe (send_to(), send()).

Constructor & Destructor Documentation

◆ NetworkThread() [1/3]

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

Starts a thread running context.

Parameters
contextContext to run; it must outlive the thread. A stopped context is not restarted: call NetworkContext::restart() first.

◆ NetworkThread() [2/3]

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

◆ NetworkThread() [3/3]

aegis2d::net::NetworkThread::NetworkThread ( NetworkThread &&  )
delete

◆ ~NetworkThread()

aegis2d::net::NetworkThread::~NetworkThread ( )

Stops the context and waits for the thread to finish.

Member Function Documentation

◆ errors()

std::uint64_t aegis2d::net::NetworkThread::errors ( ) const
noexcept

Number of exceptions that escaped a handler and were caught. Thread-safe.

Returns
The count since the thread started.

◆ is_network_thread()

bool aegis2d::net::NetworkThread::is_network_thread ( ) const
noexcept

Tells whether the caller is the network thread.

Returns
true inside socket handlers and posted tasks run by this thread.

◆ operator=() [1/2]

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

◆ operator=() [2/2]

NetworkThread & aegis2d::net::NetworkThread::operator= ( NetworkThread &&  )
delete

◆ running()

bool aegis2d::net::NetworkThread::running ( ) const
noexcept

Tells whether the thread is running.

Returns
false once stop() returned.

◆ stop()

void aegis2d::net::NetworkThread::stop ( )

Stops the context and waits for the thread to finish.

Handlers already running finish first; the others are not run. Calling it again does nothing. Must not be called from the network thread itself.

Exceptions
std::logic_errorif called from the network thread.

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