#include <event-config.h>
#include <stdarg.h>
#include <sys/types.h>
#include <event2/util.h>
Go to the source code of this file.
Data Structures | |
struct | evbuffer_cb_info |
Structure passed to an evbuffer callback. More... | |
struct | evbuffer_ptr |
Points to a position within an evbuffer. More... | |
Defines | |
#define | EVBUFFER_CB_DISABLED 0 |
#define | EVBUFFER_CB_ENABLED 1 |
Typedefs | |
typedef void(* | evbuffer_cb_func )(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg) |
Type definition for a callback that is invoked whenever data is added or removed from an evbuffer. | |
Enumerations | |
enum | evbuffer_eol_style { EVBUFFER_EOL_ANY, EVBUFFER_EOL_CRLF, EVBUFFER_EOL_CRLF_STRICT, EVBUFFER_EOL_LF } |
Used to tell evbuffer_readln what kind of line-ending to look for. More... | |
enum | evbuffer_ptr_how { EVBUFFER_PTR_SET, EVBUFFER_PTR_ADD } |
Functions | |
int | evbuffer_add (struct evbuffer *buf, const void *data, size_t datlen) |
Append data to the end of an evbuffer. | |
int | evbuffer_add_buffer (struct evbuffer *outbuf, struct evbuffer *inbuf) |
Move data from one evbuffer into another evbuffer. | |
struct evbuffer_cb_entry * | evbuffer_add_cb (struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg) |
Add a new callback to an evbuffer. | |
int | evbuffer_add_file (struct evbuffer *output, int fd, off_t offset, size_t length) |
Move data from a file into the evbuffer for writing to a socket. | |
int | evbuffer_add_printf (struct evbuffer *buf, const char *fmt,...) |
Append a formatted string to the end of an evbuffer. | |
int | evbuffer_add_reference (struct evbuffer *outbuf, const void *data, size_t datlen, void(*cleanupfn)(void *extra), void *extra) |
Reference memory into an evbuffer without copying. | |
int | evbuffer_add_vprintf (struct evbuffer *buf, const char *fmt, va_list ap) |
Append a va_list formatted string to the end of an evbuffer. | |
int | evbuffer_cb_set_flags (struct evbuffer *buffer, struct evbuffer_cb_entry *cb, ev_uint32_t flags) |
Change whether a given callback is enabled on a buffer or not. | |
int | evbuffer_commit_space (struct evbuffer *buf, size_t size) |
Commits previously reserved space. | |
int | evbuffer_defer_callbacks (struct evbuffer *buffer, struct event_base *base) |
Force all the callbacks on an evbuffer to be run, not immediately after the evbuffer is altered, but instead from inside the event loop. | |
int | evbuffer_drain (struct evbuffer *buf, size_t len) |
Remove a specified number of bytes data from the beginning of an evbuffer. | |
int | evbuffer_enable_locking (struct evbuffer *buf, void *lock) |
Enable locking on an evbuffer so that it can safely be used by multiple threads at the same time. | |
int | evbuffer_expand (struct evbuffer *buf, size_t datlen) |
Expands the available space in an event buffer. | |
void | evbuffer_free (struct evbuffer *buf) |
Deallocate storage for an evbuffer. | |
int | evbuffer_freeze (struct evbuffer *buf, int at_front) |
Prevent calls that modify an evbuffer from succeeding. | |
size_t | evbuffer_get_contiguous_space (const struct evbuffer *buf) |
Returns the number of contiguous available bytes in the first buffer chain. | |
size_t | evbuffer_get_length (const struct evbuffer *buf) |
Returns the total number of bytes stored in the event buffer. | |
void | evbuffer_lock (struct evbuffer *buf) |
Acquire the lock on an evbuffer. | |
struct evbuffer * | evbuffer_new (void) |
Allocate storage for a new evbuffer. | |
int | evbuffer_prepend (struct evbuffer *buf, const void *data, size_t size) |
Prepends data to the beginning of the evbuffer. | |
int | evbuffer_prepend_buffer (struct evbuffer *dst, struct evbuffer *src) |
Prepends all data from the src evbuffer to the beginning of the dst evbuffer. | |
int | evbuffer_ptr_set (struct evbuffer *buffer, struct evbuffer_ptr *pos, size_t position, enum evbuffer_ptr_how how) |
Sets the search pointer in the buffer to positiion. | |
unsigned char * | evbuffer_pullup (struct evbuffer *buf, ssize_t size) |
Makes the data at the begging of an evbuffer contiguous. | |
int | evbuffer_read (struct evbuffer *buffer, evutil_socket_t fd, int howmuch) |
Read from a file descriptor and store the result in an evbuffer. | |
char * | evbuffer_readln (struct evbuffer *buffer, size_t *n_read_out, enum evbuffer_eol_style eol_style) |
Read a single line from an event buffer. | |
int | evbuffer_remove (struct evbuffer *buf, void *data, size_t datlen) |
Read data from an event buffer and drain the bytes read. | |
int | evbuffer_remove_buffer (struct evbuffer *src, struct evbuffer *dst, size_t datlen) |
Read data from an event buffer into another event buffer draining the bytes from the src buffer read. | |
int | evbuffer_remove_cb (struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg) |
Remove a callback from an evbuffer, given the function and argument used to add it. | |
int | evbuffer_remove_cb_entry (struct evbuffer *buffer, struct evbuffer_cb_entry *ent) |
Remove a callback from an evbuffer, given a handle returned from evbuffer_add_cb. | |
unsigned char * | evbuffer_reserve_space (struct evbuffer *buf, size_t size) |
Reserves space in the last chain of an event buffer. | |
struct evbuffer_ptr | evbuffer_search (struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start) |
Search for a string within an evbuffer. | |
int | evbuffer_unfreeze (struct evbuffer *buf, int at_front) |
Re-enable calls that modify an evbuffer. | |
void | evbuffer_unlock (struct evbuffer *buf) |
Release the lock on an evbuffer. | |
int | evbuffer_write (struct evbuffer *buffer, evutil_socket_t fd) |
Write the contents of an evbuffer to a file descriptor. | |
int | evbuffer_write_atmost (struct evbuffer *buffer, evutil_socket_t fd, ssize_t howmuch) |
Write some of the contents of an evbuffer to a file descriptor. |
An evbuffer can be used for preparing data before sending it to the network or conversely for reading data from the network. Evbuffers try to avoid memory copies as much as possible. As a result evbuffers can be used to pass data around witout actually incurring the overhead of copying the data.
A new evbuffer can be allocated with evbuffer_new(), and can be freed with evbuffer_free().
There are several guide lines for using evbuffers.
As the contents of an evbuffer can be stored into multiple different memory blocks, it cannot be accessed directly. Instead, evbuffer_pullup() can be used to force a specified number of bytes to be continuous. This will cause memory reallocation and memory copies if the data is split across multiple blocks.
typedef void(* evbuffer_cb_func)(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg) |
Type definition for a callback that is invoked whenever data is added or removed from an evbuffer.
An evbuffer may have one or more callbacks set at a time. The order in which they are exectuded is undefined.
A callback function may add more callbacks, or remove itself from the list of callbacks, or add or remove data from the buffer. It may not remove another callback from the list.
If a callback adds or removes data from the buffer or from another buffer, this can cause a recursive invocation of your callback or other callbacks. If you ask for an infinite loop, you might just get one: watch out!
buffer | the buffer whose size has changed | |
info | a structure describing how the buffer changed. | |
arg | a pointer to user data |
enum evbuffer_eol_style |
Used to tell evbuffer_readln what kind of line-ending to look for.
enum evbuffer_ptr_how |
EVBUFFER_PTR_SET |
Sets the pointer to the position; can be called on with an uninitalized evbuffer_ptr.
|
EVBUFFER_PTR_ADD |
Advances the pointer by adding to the current position.
|
int evbuffer_add | ( | struct evbuffer * | buf, | |
const void * | data, | |||
size_t | datlen | |||
) |
Append data to the end of an evbuffer.
buf | the event buffer to be appended to | |
data | pointer to the beginning of the data buffer | |
datlen | the number of bytes to be copied from the data buffer |
int evbuffer_add_buffer | ( | struct evbuffer * | outbuf, | |
struct evbuffer * | inbuf | |||
) |
Move data from one evbuffer into another evbuffer.
This is a destructive add. The data from one buffer moves into the other buffer. However, no unnecessary memory copies occur.
outbuf | the output buffer | |
inbuf | the input buffer |
struct evbuffer_cb_entry* evbuffer_add_cb | ( | struct evbuffer * | buffer, | |
evbuffer_cb_func | cb, | |||
void * | cbarg | |||
) | [read] |
Add a new callback to an evbuffer.
Subsequent calls to evbuffer_add_cb() add new callbacks. To remove this callback, call evbuffer_remove_cb or evbuffer_remove_cb_entry.
buffer | the evbuffer to be monitored | |
cb | the callback function to invoke when the evbuffer is modified, or NULL to remove all callbacks. | |
cbarg | an argument to be provided to the callback function |
int evbuffer_add_file | ( | struct evbuffer * | output, | |
int | fd, | |||
off_t | offset, | |||
size_t | length | |||
) |
Move data from a file into the evbuffer for writing to a socket.
This function avoids unnecessary data copies between userland and kernel. Where available, it uses sendfile or splice.
The function owns the resulting file descriptor and will close it when finished transferring data.
The results of using evbuffer_remove() or evbuffer_pullup() are undefined.
outbuf | the output buffer | |
fd | the file descriptor | |
off | the offset from which to read data | |
length | how much data to read |
int evbuffer_add_printf | ( | struct evbuffer * | buf, | |
const char * | fmt, | |||
... | ||||
) |
Append a formatted string to the end of an evbuffer.
buf | the evbuffer that will be appended to | |
fmt | a format string | |
... | arguments that will be passed to printf(3) |
int evbuffer_add_reference | ( | struct evbuffer * | outbuf, | |
const void * | data, | |||
size_t | datlen, | |||
void(*)(void *extra) | cleanupfn, | |||
void * | extra | |||
) |
Reference memory into an evbuffer without copying.
The memory needs to remain valid until all the added data has been read. This function keeps just a reference to the memory without actually incurring the overhead of a copy.
outbuf | the output buffer | |
data | the memory to reference | |
datlen | how memory to reference | |
cleanupfn | callback to be invokved when the memory is no longer referenced | |
extra | optional argument to the cleanup callback |
int evbuffer_add_vprintf | ( | struct evbuffer * | buf, | |
const char * | fmt, | |||
va_list | ap | |||
) |
Append a va_list formatted string to the end of an evbuffer.
buf | the evbuffer that will be appended to | |
fmt | a format string | |
ap | a varargs va_list argument array that will be passed to vprintf(3) |
int evbuffer_cb_set_flags | ( | struct evbuffer * | buffer, | |
struct evbuffer_cb_entry * | cb, | |||
ev_uint32_t | flags | |||
) |
Change whether a given callback is enabled on a buffer or not.
A disabled callback is not invoked even when the buffer size changes.
buffer | the evbuffer that the callback is watching. | |
cb | the callback whose status we want to change. | |
flags | EVBUFFER_CB_ENABLED to enable the callback, or EVBUFFER_CB_DISABLED to disable it. |
int evbuffer_commit_space | ( | struct evbuffer * | buf, | |
size_t | size | |||
) |
Commits previously reserved space.
Commits some of the space previously reserved with evbuffer_reserve_space(). It then becomes available for reading.
buf | the event buffer in which to reserve space. | |
size | how much space to commit. |
int evbuffer_defer_callbacks | ( | struct evbuffer * | buffer, | |
struct event_base * | base | |||
) |
Force all the callbacks on an evbuffer to be run, not immediately after the evbuffer is altered, but instead from inside the event loop.
This can be used to serialize all the callbacks to a single thread of execution.
int evbuffer_drain | ( | struct evbuffer * | buf, | |
size_t | len | |||
) |
Remove a specified number of bytes data from the beginning of an evbuffer.
buf | the evbuffer to be drained | |
len | the number of bytes to drain from the beginning of the buffer |
int evbuffer_enable_locking | ( | struct evbuffer * | buf, | |
void * | lock | |||
) |
Enable locking on an evbuffer so that it can safely be used by multiple threads at the same time.
NOTE: when locking is enabled, the lock will be held when callbacks are invoked. This could result in deadlock if you aren't careful. Plan accordingly!
buf | An evbuffer to make lockable. | |
lock | A lock object, or NULL if we should allocate our own. |
int evbuffer_expand | ( | struct evbuffer * | buf, | |
size_t | datlen | |||
) |
Expands the available space in an event buffer.
Expands the available space in the event buffer to at least datlen, so that appending datlen additional bytes will not require any new allocations.
buf | the event buffer to be expanded | |
datlen | the new minimum length requirement |
void evbuffer_free | ( | struct evbuffer * | buf | ) |
Deallocate storage for an evbuffer.
buf | pointer to the evbuffer to be freed |
int evbuffer_freeze | ( | struct evbuffer * | buf, | |
int | at_front | |||
) |
Prevent calls that modify an evbuffer from succeeding.
A buffer may frozen at the front, at the back, or at both the front and the back.
If the front of a buffer is frozen, operations that drain data from the front of the buffer, or that prepend data to the buffer, will fail until it is unfrozen. If the back a buffer is frozen, operations that append data from the buffer will fail until it is unfrozen.
buf | The buffer to freeze | |
at_front | If true, we freeze the front of the buffer. If false, we freeze the back. |
size_t evbuffer_get_contiguous_space | ( | const struct evbuffer * | buf | ) |
Returns the number of contiguous available bytes in the first buffer chain.
This is useful when procesing data that might be split into multiple chains, or that might all be in the first chain. Calls to evbuffer_pullup() that cause reallocation and copying of data can thus be avoided.
buf | pointer to the evbuffer |
size_t evbuffer_get_length | ( | const struct evbuffer * | buf | ) |
Returns the total number of bytes stored in the event buffer.
buf | pointer to the evbuffer |
void evbuffer_lock | ( | struct evbuffer * | buf | ) |
Acquire the lock on an evbuffer.
Has no effect if locking was not enabled with evbuffer_enable_locking.
struct evbuffer* evbuffer_new | ( | void | ) | [read] |
Allocate storage for a new evbuffer.
int evbuffer_prepend | ( | struct evbuffer * | buf, | |
const void * | data, | |||
size_t | size | |||
) |
Prepends data to the beginning of the evbuffer.
buf | the evbuffer to which to prepend data | |
data | a pointer to the memory to prepend | |
size | the number of bytes to prepend |
int evbuffer_prepend_buffer | ( | struct evbuffer * | dst, | |
struct evbuffer * | src | |||
) |
Prepends all data from the src evbuffer to the beginning of the dst evbuffer.
dst | the evbuffer to which to prepend data | |
src | the evbuffer to prepend; it will be emptied as a result |
int evbuffer_ptr_set | ( | struct evbuffer * | buffer, | |
struct evbuffer_ptr * | pos, | |||
size_t | position, | |||
enum evbuffer_ptr_how | how | |||
) |
Sets the search pointer in the buffer to positiion.
If evbuffer_ptr is not initalized. This function can only be called with EVBUFFER_PTR_SET.
buffer | the evbuffer to be search | |
ptr | a pointer to a struct evbuffer_ptr | |
position | the position at which to start the next search | |
how | determines how the pointer should be manipulated. |
unsigned char* evbuffer_pullup | ( | struct evbuffer * | buf, | |
ssize_t | size | |||
) |
Makes the data at the begging of an evbuffer contiguous.
buf | the evbuffer to make contiguous | |
size | the number of bytes to make contiguous, or -1 to make the entire buffer contiguous. |
int evbuffer_read | ( | struct evbuffer * | buffer, | |
evutil_socket_t | fd, | |||
int | howmuch | |||
) |
Read from a file descriptor and store the result in an evbuffer.
buf | the evbuffer to store the result | |
fd | the file descriptor to read from | |
howmuch | the number of bytes to be read |
char* evbuffer_readln | ( | struct evbuffer * | buffer, | |
size_t * | n_read_out, | |||
enum evbuffer_eol_style | eol_style | |||
) |
Read a single line from an event buffer.
Reads a line terminated by an EOL as determined by the evbuffer_eol_style argument. Returns a newly allocated nul-terminated string; the caller must free the returned value. The EOL is not included in the returned string.
buffer | the evbuffer to read from | |
n_read_out | if non-NULL, points to a size_t that is set to the number of characters in the returned string. This is useful for strings that can contain NUL characters. | |
eol_style | the style of line-ending to use. |
int evbuffer_remove | ( | struct evbuffer * | buf, | |
void * | data, | |||
size_t | datlen | |||
) |
Read data from an event buffer and drain the bytes read.
buf | the event buffer to be read from | |
data | the destination buffer to store the result | |
datlen | the maximum size of the destination buffer |
int evbuffer_remove_buffer | ( | struct evbuffer * | src, | |
struct evbuffer * | dst, | |||
size_t | datlen | |||
) |
Read data from an event buffer into another event buffer draining the bytes from the src buffer read.
This function avoids memcpy as possible.
src | the event buffer to be read from | |
dst | the destination event buffer to store the result into | |
datlen | the maximum numbers of bytes to transfer |
int evbuffer_remove_cb | ( | struct evbuffer * | buffer, | |
evbuffer_cb_func | cb, | |||
void * | cbarg | |||
) |
Remove a callback from an evbuffer, given the function and argument used to add it.
int evbuffer_remove_cb_entry | ( | struct evbuffer * | buffer, | |
struct evbuffer_cb_entry * | ent | |||
) |
Remove a callback from an evbuffer, given a handle returned from evbuffer_add_cb.
Calling this function invalidates the handle.
unsigned char* evbuffer_reserve_space | ( | struct evbuffer * | buf, | |
size_t | size | |||
) |
Reserves space in the last chain of an event buffer.
Makes space available in the last chain of an event buffer that can be arbitrarily written to by a user. The space does not become available for reading until it has been committed with evbuffer_commit_space().
Multiple subsequent calls to this function will make the same space available until evbuffer_commit_space() has been called.
buf | the event buffer in which to reserve space. | |
size | how much space to make available. |
struct evbuffer_ptr evbuffer_search | ( | struct evbuffer * | buffer, | |
const char * | what, | |||
size_t | len, | |||
const struct evbuffer_ptr * | start | |||
) | [read] |
Search for a string within an evbuffer.
buffer | the evbuffer to be searched | |
what | the string to be searched for | |
len | the length of the search string | |
start | NULL or a pointer to a valid struct evbuffer_ptr. |
int evbuffer_unfreeze | ( | struct evbuffer * | buf, | |
int | at_front | |||
) |
Re-enable calls that modify an evbuffer.
buf | The buffer to un-freeze | |
at_front | If true, we unfreeze the front of the buffer. If false, we unfreeze the back. |
void evbuffer_unlock | ( | struct evbuffer * | buf | ) |
Release the lock on an evbuffer.
Has no effect if locking was not enabled with evbuffer_enable_locking.
int evbuffer_write | ( | struct evbuffer * | buffer, | |
evutil_socket_t | fd | |||
) |
Write the contents of an evbuffer to a file descriptor.
The evbuffer will be drained after the bytes have been successfully written.
buffer | the evbuffer to be written and drained | |
fd | the file descriptor to be written to |
int evbuffer_write_atmost | ( | struct evbuffer * | buffer, | |
evutil_socket_t | fd, | |||
ssize_t | howmuch | |||
) |
Write some of the contents of an evbuffer to a file descriptor.
The evbuffer will be drained after the bytes have been successfully written.
buffer | the evbuffer to be written and drained | |
fd | the file descriptor to be written to | |
howmuch | the largest allowable number of bytes to write, or -1 to write as many bytes as we can. |