Evo C++ Library v0.5.1
Classes | Public Types | Public Member Functions | Public Attributes | Protected Member Functions | Static Protected Member Functions | Protected Attributes | List of all members
AsyncServer< T > Class Template Reference

#include <evo/ioasync_server.h>

Inheritance diagram for AsyncServer< T >:
Inheritance graph
[legend]

Detailed Description

template<class T>
class evo::AsyncServer< T >

Async I/O server for receiving and handling requests.

This is a template class that uses a ProtocolServer (T) which implements the server request handling via async I/O event methods.

See Server section in: Asynchronous I/O

ProtocolServer Requirements

The ProtocolServer (T) must define this nested type:

The ProtocolServer (T) must implement these event methods:

The ProtocolServer (T) must define this constant:

Template Parameters
TProtocolServer type to use (see above)

Classes

struct  Stats
 

Public Types

typedef ProtocolServer::Handler::DeferredContext DeferredContext
 
typedef ProtocolServer::Handler::Global Global
 
typedef T ProtocolServer
 
typedef ProtocolServer::Handler::Shared Shared
 
typedef AsyncServer< ProtocolServerThis
 

Public Member Functions

 AsyncServer ()
 Constructor. More...
 
Globalget_global ()
 Get reference to global data used by all requests and all threads in this server. More...
 
bool run (IoSocket::Handle listener)
 Run server event handling and handle connections until shutdown. More...
 
bool run (Socket &listener)
 Run server and handle connections until shutdown. More...
 
bool runlocal ()
 Run the event-loop locally in current thread until all pending requests are handled (client only). More...
 
virtual void set_logger (LoggerBase *newlogger)
 Set logger to use. More...
 
void set_timeout (ulong read_timeout_ms=0, ulong write_timeout_ms=0)
 Set read/write timeouts to use. More...
 
bool set_timer (OnTimer &on_timer, ulong msec)
 Activate timer so it expires after given time elapses. More...
 
void shutdown ()
 Shut down server. More...
 

Public Attributes

LoggerPtr logger
 Logger for protocol and debug messages, set to enable logging – see set_logger() More...
 

Protected Member Functions

virtual bool check_client_active ()
 Called during client event-loop to check whether any client requests are active (client only). More...
 
void init ()
 Initialize event-loop. More...
 
void init_attach (AsyncBase &parent)
 Initialize and attach to a parent event-loop. More...
 
bool run_eventloop ()
 Run event loop and process all events and repeat until shutdown. More...
 
bool run_eventloop_once ()
 Run event loop with one pass and process all events. More...
 

Static Protected Member Functions

static struct timeval * get_timeout_ptr (struct timeval &out, ulong ms)
 Get timeval struct pointer from timeout in milliseconds. More...
 

Protected Attributes

AsyncBasechild_base_
 Pointer to child in AsyncBase chain (always an AsyncClient), NULL for none. More...
 
AsyncEventLoopevloop_
 Event loop pointer, either owned by this or a parent. More...
 
bool local_
 Whether event-loop is local (same thread), false if separate thread. More...
 
AsyncBaseparent_base_
 Pointer to parent in AsyncBase chain, NULL if this is the main parent (and owns evloop_ pointer) More...
 
ulong read_timeout_ms_
 Socket read timeout in milliseconds, 0 for none (never timeout) More...
 
ulong write_timeout_ms_
 Socket write timeout in milliseconds, 0 for none (never timeout) More...
 

Member Typedef Documentation

◆ DeferredContext

typedef ProtocolServer::Handler::DeferredContext DeferredContext

◆ Global

typedef ProtocolServer::Handler::Global Global

◆ ProtocolServer

typedef T ProtocolServer

◆ Shared

typedef ProtocolServer::Handler::Shared Shared

◆ This

Constructor & Destructor Documentation

◆ AsyncServer()

AsyncServer ( )
inline

Constructor.

Member Function Documentation

◆ check_client_active()

virtual bool check_client_active ( )
inlineprotectedvirtualinherited

Called during client event-loop to check whether any client requests are active (client only).

  • This is used to determine if all client requests were handled
  • Only AsyncClient should override this, AsyncServer should leave the default
Returns
Whether any client requests are active, true if a request is in progress (waiting for response or write is pending)

◆ get_global()

Global& get_global ( )
inline

Get reference to global data used by all requests and all threads in this server.

  • Use this to populate configuration data before running the server
Returns
Reference to global data

◆ get_timeout_ptr()

static struct timeval* get_timeout_ptr ( struct timeval &  out,
ulong  ms 
)
inlinestaticprotectedinherited

Get timeval struct pointer from timeout in milliseconds.

Parameters
outStruct to store timeout
msTime in milliseconds to use
Returns
Pointer to out, or NULL if ms is 0

◆ init()

void init ( )
inlineprotectedinherited

Initialize event-loop.

  • Ignored if already initialized

◆ init_attach()

void init_attach ( AsyncBase parent)
inlineprotectedinherited

Initialize and attach to a parent event-loop.

  • Ignored if already initialized
Parameters
parentParent AsyncBase to attach to

◆ run() [1/2]

bool run ( IoSocket::Handle  listener)
inline

Run server event handling and handle connections until shutdown.

  • This initializes (calls Shared::on_init()) and runs the event-loop to accept and handle connections
  • This keeps running until the listener socket is closed or shutdown() is called
  • Caution: Only 1 thread may handle connections at a time, otherwise results are undefined
Parameters
listenerListener socket handle to accept connections on
Returns
Whether successful, false if initialization failed (Shared::on_init() or internal error)

◆ run() [2/2]

bool run ( Socket listener)
inline

Run server and handle connections until shutdown.

  • This runs the event-loop to accept and handle connections
  • This keeps running until the listener socket is closed or shutdown() is called
  • Caution: Only 1 thread may handle connections at a time, otherwise results are undefined
Parameters
listenerListener socket to accept connections on
Returns
Whether successful, false if initialization failed (Shared::on_init() or internal error)

◆ run_eventloop()

bool run_eventloop ( )
inlineprotectedinherited

Run event loop and process all events and repeat until shutdown.

Returns
Whether successful, false on internal error

◆ run_eventloop_once()

bool run_eventloop_once ( )
inlineprotectedinherited

Run event loop with one pass and process all events.

  • This waits until some events are active then processes all active events and returns
Returns
Whether successful, false on internal error

◆ runlocal()

bool runlocal ( )
inlineinherited

Run the event-loop locally in current thread until all pending requests are handled (client only).

  • This blocks while client requests are pending
  • This returns false immediately if this does not own an event-loop (i.e. was attached to a parent) – only the top parent can run an event-loop
Returns
Whether successful, false if attached to another AsyncBase or on internal error

◆ set_logger()

virtual void set_logger ( LoggerBase newlogger)
inlinevirtualinherited

Set logger to use.

  • If set, a logger shows low-level details and error information from the framework
  • The logger pointer must remain valid as long as it's referenced here
Parameters
newloggerPointer to logger to set, NULL for none (no logging)

◆ set_timeout()

void set_timeout ( ulong  read_timeout_ms = 0,
ulong  write_timeout_ms = 0 
)
inlineinherited

Set read/write timeouts to use.

Parameters
read_timeout_msSocket read timeout in milliseconds, 0 for none (never timeout)
write_timeout_msSocket write timeout in milliseconds, 0 for none (never timeout)

◆ set_timer()

bool set_timer ( OnTimer on_timer,
ulong  msec 
)
inlineinherited

Activate timer so it expires after given time elapses.

Parameters
on_timerTimer to use
msecExpiration time in milliseconds
Returns
Whether successful, false on internal error

◆ shutdown()

void shutdown ( )
inline

Shut down server.

  • The server will stop accepting new requests and will finish handling current requests
  • This doesn't block and can be called from any thread or event handler

Member Data Documentation

◆ child_base_

AsyncBase* child_base_
protectedinherited

Pointer to child in AsyncBase chain (always an AsyncClient), NULL for none.

◆ evloop_

AsyncEventLoop* evloop_
protectedinherited

Event loop pointer, either owned by this or a parent.

◆ local_

bool local_
protectedinherited

Whether event-loop is local (same thread), false if separate thread.

◆ logger

LoggerPtr logger
inherited

Logger for protocol and debug messages, set to enable logging – see set_logger()

◆ parent_base_

AsyncBase* parent_base_
protectedinherited

Pointer to parent in AsyncBase chain, NULL if this is the main parent (and owns evloop_ pointer)

◆ read_timeout_ms_

ulong read_timeout_ms_
protectedinherited

Socket read timeout in milliseconds, 0 for none (never timeout)

◆ write_timeout_ms_

ulong write_timeout_ms_
protectedinherited

Socket write timeout in milliseconds, 0 for none (never timeout)


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