3es  0.7
tes::ConnectionMonitor Class Referenceabstract

A utility class for monitoring new connections for a Server. More...

#include <3esconnectionmonitor.h>

Public Types

enum  Mode { None, Synchronous, Asynchronous }
 

Public Member Functions

virtual bool start (Mode mode)=0
 Starts the monitor listening in the specified mode. More...
 
virtual void stop ()=0
 Stops listening for further connections. More...
 
virtual void join ()=0
 Called to join the monitor thread. More...
 
virtual bool isRunning () const =0
 Returns true if the connection monitor has start. More...
 
virtual Mode mode () const =0
 Returns the current running mode. More...
 
virtual int waitForConnection (unsigned timeoutMs)=0
 Wait up to timeoutMs milliseconds for a connection. More...
 
virtual void monitorConnections ()=0
 Accepts new connections and checks for expired connections, but effects neither in the Server. More...
 
virtual void setConnectionCallback (void(*callback)(Server &, Connection &, void *), void *user)=0
 Sets the callback invoked for each new connection. More...
 
virtual void setConnectionCallback (const std::function< void(Server &, Connection &)> &callback)=0
 An overload of setConnectionCallback() using the C++11 funtion object. More...
 
virtual void commitConnections ()=0
 Migrates new connections to the owning Server and removes expired connections. More...
 

Protected Member Functions

virtual ~ConnectionMonitor ()
 Protected virtual destructor.
 

Detailed Description

A utility class for monitoring new connections for a Server.

The monitor manages listening for new connections and expiring old ones. Doing so can be time consuming, so the monitor supports running its own monitor thread. It also supports synchronous operation in case connection monitoring is unnecessary, or unable to be pushed off thread.

Asynchronous mode is activated by calling start() and stopped with stop(). Calls to join() will block until the monitor thread has completed, but should only be called after stop() has been called. The commitConnections() method must still be called by the main thread (synchronously) to control when connections are activated and deactivated.

Synchronous mode is supported by calling monitorConnections() to accept new connections. This must be followed by a call to commitConnections() to commit the changes to the owning Server.

Synchronous Usage
float dt = 0;
Server *server = Server::create()
server->connectionMonitor()->start(tes::ConnectionMonitor::Synchronous);
for (;;)
{
// Prepare frame.
//...
server->updateFrame(dt);
server->connectionMonitor()->monitorConnections();
server->connectionMonitor()->commitConnections();
// Loop end...
}
Asynchronous Usage
float dt = 0;
Server *server = Server::create()
server->connectionMonitor()->start(tes::ConnectionMonitor::Asynchronous);
for (;;)
{
// Prepare frame.
//...
server->updateFrame(dt);
server->connectionMonitor()->commitConnections();
// Loop end...
}
server->connectionMonitor()->stop(); // Safe even if Synchronous
server->connectionMonitor()->join(); // Safe even if Synchronous

Member Function Documentation

◆ commitConnections()

virtual void tes::ConnectionMonitor::commitConnections ( )
pure virtual

Migrates new connections to the owning Server and removes expired connections.

For each new connection, the callback set in setConnectionCallback() is invoked, passing the server, connection and user argument.

Parameters
callbackWhen given, called for each new connection.
userUser argument passed to callback.

◆ isRunning()

virtual bool tes::ConnectionMonitor::isRunning ( ) const
pure virtual

Returns true if the connection monitor has start.

Returns
True if running.

◆ join()

virtual void tes::ConnectionMonitor::join ( )
pure virtual

Called to join the monitor thread.

Returns immediately if not running.

◆ mode()

virtual Mode tes::ConnectionMonitor::mode ( ) const
pure virtual

Returns the current running mode.

Asynchronous mode is set as soon as start(Asynchronous) is called and drops to None after calling stop() once the thread has stopped.

Synchronous mode is set as soon as start(Synchronous) is called and drops to None on calling stop().

The mode is None if not running in either mode.

◆ monitorConnections()

virtual void tes::ConnectionMonitor::monitorConnections ( )
pure virtual

Accepts new connections and checks for expired connections, but effects neither in the Server.

This is either called on the main thread for synchronous operation, or internally in asynchronous mode.

◆ setConnectionCallback() [1/2]

virtual void tes::ConnectionMonitor::setConnectionCallback ( void(*)(Server &, Connection &, void *)  callback,
void *  user 
)
pure virtual

Sets the callback invoked for each new connection.

This is invoked from commitConnections() for each new connection. The arguments passed to the callback are:

  • server : the Server object.
  • connection : the new Connection object.
  • user : the user argument given here.

Write only.

Parameters
callbackThe callback function pointer.
userA user pointer passed to the callback whenever it is invoked.

◆ setConnectionCallback() [2/2]

virtual void tes::ConnectionMonitor::setConnectionCallback ( const std::function< void(Server &, Connection &)> &  callback)
pure virtual

An overload of setConnectionCallback() using the C++11 funtion object.

Both methods are provided to cater for potential ABI issues.

No user argument is supported as the flexibility of std::function obviates the for such.

Parameters
callbackThe function to invoke for each new connection.

◆ start()

virtual bool tes::ConnectionMonitor::start ( Mode  mode)
pure virtual

Starts the monitor listening in the specified mode.

The listening thread is started if mode is Asynchronous.

Parameters
modeThe listening mode. Mode Node is ignored.
Returns
True if listening is running in the specified mode. This includes both newly started and if it was already running in that mode. False is returned if mode is None, or differs from the running mode.

◆ stop()

virtual void tes::ConnectionMonitor::stop ( )
pure virtual

Stops listening for further connections.

This requests termination of the monitor thread if running.

Safe to call if not running.

◆ waitForConnection()

virtual int tes::ConnectionMonitor::waitForConnection ( unsigned  timeoutMs)
pure virtual

Wait up to timeoutMs milliseconds for a connection.

Returns immediately if we already have a connection.

Parameters
timeoutMsThe time out to wait in milliseconds.
Returns
The number of connections on returning. These may need to be committed.

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