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

Runs the completion of network operations: received datagrams, finished sends. More...

#include <aegis2d/net/context.hpp>

Public Member Functions

 NetworkContext ()
 Creates an idle context.
 
 NetworkContext (const NetworkContext &)=delete
 
NetworkContext & operator= (const NetworkContext &)=delete
 
 NetworkContext (NetworkContext &&)=delete
 
NetworkContext & operator= (NetworkContext &&)=delete
 
 ~NetworkContext ()
 Destroys the context; handlers not run yet are dropped.
 
std::size_t poll ()
 Runs the handlers that are ready, without waiting.
 
std::size_t run_for (std::chrono::milliseconds duration)
 Runs handlers as they become ready, for at most duration.
 
void run ()
 Runs handlers until stop() is called, waiting when nothing is ready.
 
void stop () noexcept
 Makes run(), run_for() and poll() return as soon as possible. Thread-safe.
 
bool stopped () const noexcept
 Tells whether stop() was called since the last restart().
 
void restart ()
 Lets the context run again after stop().
 
void post (std::function< void()> task)
 Runs a task on the thread running the context. Thread-safe.
 

Friends

class TcpConnection
 
class TcpListener
 
class UdpSocket
 

Detailed Description

Runs the completion of network operations: received datagrams, finished sends.

Sockets and connections are created on a context, and their handlers are called by whichever thread calls poll(), run_for() or run() on it. The network thread (#101) calls run(); tests and tools can drive it by hand with poll() and run_for().

The context must outlive its sockets.

Constructor & Destructor Documentation

◆ NetworkContext() [1/3]

aegis2d::net::NetworkContext::NetworkContext ( )

Creates an idle context.

◆ NetworkContext() [2/3]

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

◆ NetworkContext() [3/3]

aegis2d::net::NetworkContext::NetworkContext ( NetworkContext &&  )
delete

◆ ~NetworkContext()

aegis2d::net::NetworkContext::~NetworkContext ( )

Destroys the context; handlers not run yet are dropped.

Member Function Documentation

◆ operator=() [1/2]

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

◆ operator=() [2/2]

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

◆ poll()

std::size_t aegis2d::net::NetworkContext::poll ( )

Runs the handlers that are ready, without waiting.

Returns
Number of handlers run.

◆ post()

void aegis2d::net::NetworkContext::post ( std::function< void()>  task)

Runs a task on the thread running the context. Thread-safe.

The game thread uses it to hand work to the network thread, for instance "send what is in the outgoing queue". Tasks run in the order they were posted. An exception thrown by task propagates out of poll(), run_for() or run().

Parameters
taskCalled once, the next time the context runs its handlers.

◆ restart()

void aegis2d::net::NetworkContext::restart ( )

Lets the context run again after stop().

◆ run()

void aegis2d::net::NetworkContext::run ( )

Runs handlers until stop() is called, waiting when nothing is ready.

◆ run_for()

std::size_t aegis2d::net::NetworkContext::run_for ( std::chrono::milliseconds  duration)

Runs handlers as they become ready, for at most duration.

Returns earlier if stop() is called or if nothing is pending.

Parameters
durationLongest time to wait.
Returns
Number of handlers run.

◆ stop()

void aegis2d::net::NetworkContext::stop ( )
noexcept

Makes run(), run_for() and poll() return as soon as possible. Thread-safe.

They then run nothing until restart() is called.

◆ stopped()

bool aegis2d::net::NetworkContext::stopped ( ) const
noexcept

Tells whether stop() was called since the last restart().

Returns
true while the context is stopped.

Friends And Related Symbol Documentation

◆ TcpConnection

friend class TcpConnection
friend

◆ TcpListener

friend class TcpListener
friend

◆ UdpSocket

friend class UdpSocket
friend

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