#include <evo/ioasync_base.h>
Holds data for async I/O buffers (used internally with AsyncServer and protocol implementations).
◆ AsyncBuffers()
Constructor (used internally).
◆ attach()
void attach |
( |
struct bufferevent * |
bev | ) |
|
|
inline |
Attach to active buffers (used internally).
- Parameters
-
◆ attach_read()
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
-
◆ read_fixed()
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
-
data | Set to reference data from read buffer on success [out] |
size | Data size to read |
max_size | Max 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
-
T | Parent ProtocolHandler type with on_read() and on_read_fixed() event methods (inferred from parent) |
- Parameters
-
parent | Parent ProtocolHandler calling this from on_read() event method |
fixed_size | Reference to fixed_size param passed to on_read() |
size | Data size to read |
max_size | Max read buffer size, 0 for no limit, must be at least enough for size |
context | Context 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;
}
}
}
◆ read_flush()
Flush and consume next line from read buffer.
◆ read_line()
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
-
data | Set 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_size | Maximum read buffer size, stop reading from socket when buffer hits this size, 0 for no max |
min_size | Minimum read buffer size, read event isn't called until this size is reached, 0 for any size |
◆ read_size()
Get read buffer data size in bytes.
- Returns
- Buffered data size
◆ reset()
Reset buffer pointers (used internally).
◆ write()
void write |
( |
const char * |
data, |
|
|
size_t |
size |
|
) |
| |
|
inline |
◆ write_clear()
◆ 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: