|
Aegis2D
Modular C++26 2D game engine for networked games
|
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. | |
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:
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()).
|
explicit |
Starts a thread running context.
| context | Context to run; it must outlive the thread. A stopped context is not restarted: call NetworkContext::restart() first. |
|
delete |
|
delete |
| aegis2d::net::NetworkThread::~NetworkThread | ( | ) |
Stops the context and waits for the thread to finish.
|
noexcept |
Number of exceptions that escaped a handler and were caught. Thread-safe.
|
noexcept |
Tells whether the caller is the network thread.
true inside socket handlers and posted tasks run by this thread.
|
delete |
|
delete |
|
noexcept |
Tells whether the thread is running.
false once stop() returned. | 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.
| std::logic_error | if called from the network thread. |