Evo C++ Library v0.5.1
Classes | Public Member Functions | List of all members
AsyncBuffers Class Reference

#include <evo/ioasync_base.h>

Detailed Description

Holds data for async I/O buffers (used internally with AsyncServer and protocol implementations).

Classes

class  BulkWrite
 Use to group multiple writes for efficiency. More...
 

Public Member Functions

 AsyncBuffers ()
 Constructor (used internally). More...
 
void attach (struct bufferevent *bev)
 Attach to active buffers (used internally). More...
 
void attach_read ()
 Attach to current write buffers for reading too (used internally). More...
 
void attach_write (struct bufferevent *bev)
 Attach to active buffers for writing (used internally). More...
 
bool read_fixed (SubString &data, SizeT size, SizeT max_size=0)
 Read fixed size data from read buffer. More...
 
template<class T >
bool read_fixed_helper (T &parent, SizeT &fixed_size, SizeT size, SizeT max_size=0, void *context=NULL)
 Helper for reading fixed size data from read buffer from a ProtocolHandler on_read() event. More...
 
void read_flush ()
 Flush and consume next line from read buffer. More...
 
bool read_line (SubString &data)
 Read next line from read buffer. More...
 
void read_reset (size_t max_size, size_t min_size=0)
 Reset read buffer thresholds. More...
 
size_t read_size ()
 Get read buffer data size in bytes. More...
 
void reset ()
 Reset buffer pointers (used internally). More...
 
void write (const char *data, size_t size)
 
void write_clear ()
 
void write_reserve (size_t size)
 
size_t write_size () const
 

Constructor & Destructor Documentation

◆ AsyncBuffers()

AsyncBuffers ( )
inline

Constructor (used internally).

Member Function Documentation

◆ attach()

void attach ( struct bufferevent *  bev)
inline

Attach to active buffers (used internally).

Parameters
bevBuffers to attach to

◆ attach_read()

void attach_read ( )
inline

Attach to current write buffers for reading too (used internally).

◆ attach_write()

void attach_write ( struct bufferevent *  bev)
inline

Attach to active buffers for writing (used internally).

Parameters
bevBuffers to attach to

◆ read_fixed()

bool read_fixed ( SubString data,
SizeT  size,
SizeT  max_size = 0 
)
inline

Read fixed size data from read buffer.

  • This references buffered data directly (no copy)
  • If not enough data available, call again on next read event – see AsyncServer
    • If the first call fails, this will adjust internal read thresholds so the next read event has enough data
  • On success, must call read_flush() to actually consume the data (removing it from read buffer), then if it took 2 calls (previous read_fixed() call failed), must call read_reset() to reset read thresholds
Parameters
dataSet to reference data from read buffer on success [out]
sizeData size to read
max_sizeMax read buffer size, 0 for no limit, must be at least enough for size
Returns
Whether successful, false if not enough received yet so call again on next read event

◆ read_fixed_helper()

bool read_fixed_helper ( T &  parent,
SizeT fixed_size,
SizeT  size,
SizeT  max_size = 0,
void *  context = NULL 
)
inline

Helper for reading fixed size data from read buffer from a ProtocolHandler on_read() event.

  • This helps properly implement a pattern where an on_read() event needs to read fixed size data
    • This is a bit tricky because I/O is asynchronous and we may or may not need to wait for more data, creating 2 code paths
    • The idea here is to use parent.on_fixed_read() to handle both code paths
    • See AsyncServer for more on this
  • If enough data has been received then this:
    • Reads the data with read_fixed(), calls parent.on_read_fixed() to consume it, then calls read_flush() to flush this data
    • The above is repeated in a loop as long as enough data is available, and parent.on_read_fixed() sets next_size to read another chunk of fixed data
  • If enough data is not available, this sets fixed_size and returns true
  • If this returns true and:
    • fixed_size > 0: the calling on_read() function should immediately return true to wait for more data
    • else: the data has been read and processed, and the calling on_read() function may continue reading and processing data
  • If a call to parent.on_read_fixed() returns false, this will immediately return false, and the calling on_read() function should also immediately return false to close the connection
Template Parameters
TParent ProtocolHandler type with on_read() and on_read_fixed() event methods (inferred from parent)
Parameters
parentParent ProtocolHandler calling this from on_read() event method
fixed_sizeReference to fixed_size param passed to on_read()
sizeData size to read
max_sizeMax read buffer size, 0 for no limit, must be at least enough for size
contextContext pointer for deferred server reply (used with servers), NULL for clients – passed to on_read_fixed()
Returns
Whether successful, true and fixed_size > 0 if need to wait for more data, otherwise true if data processed, false to close connection immediately
Example

This example reads a line with a data size, then does a fixed-size read on that data size.

bool on_read(size_t& fixed_size, AsyncBuffers& buffers, void* context) {
SubString line;
while (buffers.read_line(line)) {
uint data_size = line.numu().value();
buffers.read_flush();
if (data_size > 0) {
if (!buffers.read_fixed_helper(*this, fixed_size, data_size, 0, context))
return false;
if (fixed_size > 0)
return true; // wait for more data
}
}
}

◆ read_flush()

void read_flush ( )
inline

Flush and consume next line from read buffer.

◆ read_line()

bool read_line ( SubString data)
inline

Read next line from read buffer.

  • This references buffered data directly (no copy)
  • On success, must call read_flush() to actually consume the data (removing it from read buffer)
  • If not enough data available, call again on next read event
Parameters
dataSet to reference next line from read buffer [out]
Returns
Whether successful, false if no newline received yet so call again on next read event

◆ read_reset()

void read_reset ( size_t  max_size,
size_t  min_size = 0 
)
inline

Reset read buffer thresholds.

  • This sets the read buffer min/max thresholds (also known as watermarks) as conditions for calling the next read event
  • Call to prepare for next read
  • See read_fixed()
Parameters
max_sizeMaximum read buffer size, stop reading from socket when buffer hits this size, 0 for no max
min_sizeMinimum read buffer size, read event isn't called until this size is reached, 0 for any size

◆ read_size()

size_t read_size ( )
inline

Get read buffer data size in bytes.

Returns
Buffered data size

◆ reset()

void reset ( )
inline

Reset buffer pointers (used internally).

◆ write()

void write ( const char *  data,
size_t  size 
)
inline

◆ write_clear()

void write_clear ( )
inline

◆ write_reserve()

void write_reserve ( size_t  size)
inline

◆ write_size()

size_t write_size ( ) const
inline

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