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
AsyncClient< T, Q > Class Template Reference

#include <evo/ioasync_client.h>

Inheritance diagram for AsyncClient< T, Q >:
Inheritance graph
[legend]

Detailed Description

template<class T, class Q>
class evo::AsyncClient< T, Q >

Template class for an async I/O client.

Template Parameters
TProtocol handler type, which must inherit from AsyncClient
QResponse queue type that works with the protocol used
  • Must have null() and set() methods, and must default construct as null

Classes

struct  OnConnect
 Client connected event. More...
 
struct  OnError
 Client error event. More...
 
struct  PreQueueItem
 Request data for prequeue. More...
 
struct  RequestWriter
 Used by the protocol implementation to write a request to an AsyncClient. More...
 

Public Types

typedef T ProtocolHandler
 Derived protocol hander type (must inherit from AsyncClient) More...
 
typedef Q QueueItem
 Response queue item type. More...
 
enum  State { sNONE = 0, sCONNECTING, sCONNECTED }
 Client state. More...
 
typedef AsyncClient< T, Q > This
 This type More...
 

Public Member Functions

 AsyncClient (SizeT max_queue_size, SizeT max_read_size)
 Constructor. More...
 
 ~AsyncClient ()
 Destructor. More...
 
Thisattach_to (AsyncBase &parent)
 Attach to a parent AsyncClient or AsyncServer and use the same event-loop as the parent. More...
 
void close ()
 Close connection. More...
 
bool connect_ip (const char *host, ushort port, int family=AF_INET)
 Start IP connection. More...
 
ulong get_id () const
 Get current client ID. More...
 
State get_state () const
 Get current state. 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...
 
Thisset_on_connect (OnConnect *cb)
 Set general handler to call when a connection is established. More...
 
Thisset_on_error (OnError *cb)
 Set general handler to call when an unexpected error occurs. 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...
 

Public Attributes

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

Protected Member Functions

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...
 
AtomicBufferQueue< QueueItemqueue_
 Queue where each item represents an expected response from server. 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

◆ ProtocolHandler

typedef T ProtocolHandler

Derived protocol hander type (must inherit from AsyncClient)

◆ QueueItem

typedef Q QueueItem

Response queue item type.

◆ This

typedef AsyncClient<T,Q> This

This type

Member Enumeration Documentation

◆ State

enum State

Client state.

Enumerator
sNONE 

No connection.

sCONNECTING 

Connection in progress.

sCONNECTED 

Connected.

Constructor & Destructor Documentation

◆ AsyncClient()

AsyncClient ( SizeT  max_queue_size,
SizeT  max_read_size 
)
inline

Constructor.

Parameters
max_queue_sizeMax size for pending response queue
max_read_sizeMax read buffer size, 0 for unlimited – this is used to limit the read buffer size

◆ ~AsyncClient()

~AsyncClient ( )
inline

Destructor.

Member Function Documentation

◆ attach_to()

This& attach_to ( AsyncBase parent)
inline

Attach to a parent AsyncClient or AsyncServer and use the same event-loop as the parent.

  • This must be called before any connect*() method, otherwise this is ignored
Parameters
parentParent to attach to
Returns
This

◆ close()

void close ( )
inline

Close connection.

  • This must not be called from an event handler

◆ connect_ip()

bool connect_ip ( const char *  host,
ushort  port,
int  family = AF_INET 
)
inline

Start IP connection.

  • This is non-blocking and returns immediately while connecting

◆ get_id()

ulong get_id ( ) const
inline

Get current client ID.

Returns
Client ID

◆ get_state()

State get_state ( ) const
inline

Get current state.

Returns
Current state

◆ 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_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_on_connect()

This& set_on_connect ( OnConnect cb)
inline

Set general handler to call when a connection is established.

Parameters
cbOnConnect event to use, NULL for none
Returns
This

◆ set_on_error()

This& set_on_error ( OnError cb)
inline

Set general handler to call when an unexpected error occurs.

Parameters
cbOnError event to use, NULL for none
Returns
This

◆ 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

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)

◆ queue_

AtomicBufferQueue<QueueItem> queue_
protected

Queue where each item represents an expected response from server.

◆ 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: