Manual browser: evbuffer(3)
evbuffer(3) | Library Functions Manual | evbuffer(3) |
NAME
evbuffer -Functions for buffering data for network sending or receiving.
SYNOPSIS
#include <event2/buffer.h>Data Structures
struct evbuffer
Macros
#define EVBUFFER_CB_ENABLED 1
Typedefs
typedef void(* evbuffer_cb_func )(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg)
Enumerations
enum evbuffer_eol_style { EVBUFFER_EOL_ANY, EVBUFFER_EOL_CRLF, EVBUFFER_EOL_CRLF_STRICT, EVBUFFER_EOL_LF }
Functions
int evbuffer_add (struct evbuffer *buf, const void *data, size_t datlen)
Detailed Description
Functions for buffering data for network sending or receiving.A new evbuffer can be allocated with evbuffer_new(), and can be freed with evbuffer_free(). Most users will be using evbuffers via the bufferevent interface. To access a bufferevent's evbuffers, use bufferevent_get_input() and bufferevent_get_output().
There are several guidelines for using evbuffers.
- •
- if you already know how much data you are going to add as a result of calling evbuffer_add() multiple times, it makes sense to use evbuffer_expand() first to make sure that enough memory is allocated before hand.
- •
- evbuffer_add_buffer() adds the contents of one buffer to the other without incurring any unnecessary memory copies.
- •
- evbuffer_add() and evbuffer_add_buffer() do not mix very well: if you use them, you will wind up with fragmented memory in your buffer.
- •
- For high-performance code, you may want to avoid copying data into and out of buffers. You can skip the copy step by using evbuffer_reserve_space()/evbuffer_commit_space() when writing into a buffer, and evbuffer_peek() when reading.
In Libevent 2.0 and later, evbuffers are represented using a linked list of memory chunks, with pointers to the first and last chunk in the chain.
As the contents of an evbuffer can be stored in multiple different memory blocks, it cannot be accessed directly. Instead, evbuffer_pullup() can be used to force a specified number of bytes to be contiguous. This will cause memory reallocation and memory copies if the data is split across multiple blocks. It is more efficient, however, to use evbuffer_peek() if you don't require that the memory to be contiguous.
Macro Definition Documentation
#define EVBUFFER_CB_ENABLED 1
If this flag is not set, then a callback is temporarily disabled, and should not be invoked. See Also:
#define EVBUFFER_FLAG_DRAINS_TO_FD 1
If this flag is set, then we will not use evbuffer_peek(), evbuffer_remove(), evbuffer_remove_buffer(), and so on to read bytes from this buffer: we'll only take bytes out of this buffer by writing them to the network (as with evbuffer_write_atmost), by removing them without observing them (as with evbuffer_drain), or by copying them all out at once (as with evbuffer_add_buffer). Using this option allows the implementation to use sendfile-based operations for evbuffer_add_file(); see that function for more information.This flag is on by default for bufferevents that can take advantage of it; you should never actually need to set it on a bufferevent's output buffer.
Typedef Documentation
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 executed 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!
Parameters:
typedef void(* evbuffer_ref_cleanup_cb)(const void *data, size_t datalen, void *extra)
A cleanup function for a piece of memory added to an evbuffer by reference. See Also:
Enumeration Type Documentation
enum evbuffer_eol_style
Used to tell evbuffer_readln what kind of line-ending to look for.
Enumerator
- EVBUFFER_EOL_ANY
- Any sequence of CR and LF characters is acceptable as an EOL. Note that this style can produce ambiguous results: the sequence 'CRLF' will be treated as a single EOL if it is all in the buffer at once, but if you first read a CR from the network and later read an LF from the network, it will be treated as two EOLs.
- EVBUFFER_EOL_CRLF
- An EOL is an LF, optionally preceded by a CR. This style is most useful for implementing text-based internet protocols.
- EVBUFFER_EOL_CRLF_STRICT
- An EOL is a CR followed by an LF.
- EVBUFFER_EOL_LF
- An EOL is a LF.
enum evbuffer_ptr_how
Defines how to adjust an evbuffer_ptr by evbuffer_ptr_set() See Also:
Enumerator
- EVBUFFER_PTR_SET
- Sets the pointer to the position; can be called on with an uninitialized evbuffer_ptr.
- EVBUFFER_PTR_ADD
- Advances the pointer by adding to the current position.
Function Documentation
int evbuffer_add (struct evbuffer *buf, const void *data, size_tdatlen)
Append data to the end of an evbuffer. Parameters:Returns:
int evbuffer_add_buffer (struct evbuffer *outbuf, struct evbuffer *inbuf)
Move all 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.Parameters:
Returns:
See Also:
struct evbuffer_cb_entry* evbuffer_add_cb (struct evbuffer *buffer, evbuffer_cb_funccb, 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.Parameters:
Returns:
int evbuffer_add_file (struct evbuffer *outbuf, intfd, ev_off_toffset, ev_off_tlength)
Copy data from a file into the evbuffer for writing to a socket. This function avoids unnecessary data copies between userland and kernel. If sendfile is available and the EVBUFFER_FLAG_DRAINS_TO_FD flag is set, it uses those functions. Otherwise, it tries to use mmap (or CreateFileMapping on Windows).The function owns the resulting file descriptor and will close it when finished transferring data.
The results of using evbuffer_remove() or evbuffer_pullup() on evbuffers whose data was added using this function are undefined.
Parameters:
Returns:
int evbuffer_add_printf (struct evbuffer *buf, const char *fmt, ...)
Append a formatted string to the end of an evbuffer. The string is formated as printf.Parameters:
Returns:
See Also:
int evbuffer_add_reference (struct evbuffer *outbuf, const void *data, size_tdatlen, evbuffer_ref_cleanup_cbcleanupfn, void *cleanupfn_arg)
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.Parameters:
Returns:
int evbuffer_add_vprintf (struct evbuffer *buf, const char *fmt, va_listap)
Append a va_list formatted string to the end of an evbuffer. Parameters:Returns:
int evbuffer_cb_clear_flags (struct evbuffer *buffer, struct evbuffer_cb_entry *cb, ev_uint32_tflags)
Change the flags that are set for a callback on a buffer by removing some. Parameters:Returns:
int evbuffer_cb_set_flags (struct evbuffer *buffer, struct evbuffer_cb_entry *cb, ev_uint32_tflags)
Change the flags that are set for a callback on a buffer by adding more. Parameters:Returns:
int evbuffer_clear_flags (struct evbuffer *buf, ev_uint64_tflags)
Change the flags that are set for an evbuffer by removing some. Parameters:Returns:
int evbuffer_commit_space (struct evbuffer *buf, struct evbuffer_iovec *vec, intn_vecs)
Commits previously reserved space. Commits some of the space previously reserved with evbuffer_reserve_space(). It then becomes available for reading.This function may return an error if the pointer in the extents do not match those returned from evbuffer_reserve_space, or if data has been added to the buffer since the space was reserved.
If you want to commit less data than you got reserved space for, modify the iov_len pointer of the appropriate extent to a smaller value. Note that you may have received more space than you requested if it was available!
Parameters:
Returns:
See Also:
ev_ssize_t evbuffer_copyout (struct evbuffer *buf, void *data_out, size_tdatlen)
Read data from an evbuffer, and leave the buffer unchanged. If more bytes are requested than are available in the evbuffer, we only extract as many bytes as were available.Parameters:
Returns:
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_tlen)
Remove a specified number of bytes data from the beginning of an evbuffer. Parameters:Returns:
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!Parameters:
Returns:
int evbuffer_expand (struct evbuffer *buf, size_tdatlen)
Expands the available space in an evbuffer. Expands the available space in the evbuffer to at least datlen, so that appending datlen additional bytes will not require any new allocations.Parameters:
Returns:
void evbuffer_free (struct evbuffer *buf)
Deallocate storage for an evbuffer. Parameters:
int evbuffer_freeze (struct evbuffer *buf, intat_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.
Parameters:
Returns:
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 processing 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.Parameters:
Returns:
size_t evbuffer_get_length (const struct evbuffer *buf)
Returns the total number of bytes stored in the evbuffer. Parameters:Returns:
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. Returns:
int evbuffer_peek (struct evbuffer *buffer, ev_ssize_tlen, struct evbuffer_ptr *start_at, struct evbuffer_iovec *vec_out, intn_vec)
Function to peek at data inside an evbuffer without removing it or copying it out. Pointers to the data are returned by filling the 'vec_out' array with pointers to one or more extents of data inside the buffer.The total data in the extents that you get back may be more than you requested (if there is more data last extent than you asked for), or less (if you do not provide enough evbuffer_iovecs, or if the buffer does not have as much data as you asked to see).
Parameters:
buffer.'
Returns:
int evbuffer_prepend (struct evbuffer *buf, const void *data, size_tsize)
Prepends data to the beginning of the evbuffer. Parameters:Returns:
int evbuffer_prepend_buffer (struct evbuffer *dst, struct evbuffer *src)
Prepends all data from the src evbuffer to the beginning of the dst evbuffer. Parameters:Returns:
int evbuffer_ptr_set (struct evbuffer *buffer, struct evbuffer_ptr *ptr, size_tposition, enum evbuffer_ptr_howhow)
Sets the search pointer in the buffer to position. If evbuffer_ptr is not initialized. This function can only be called with EVBUFFER_PTR_SET.Parameters:
Returns:
unsigned char* evbuffer_pullup (struct evbuffer *buf, ev_ssize_tsize)
Makes the data at the begging of an evbuffer contiguous. Parameters:Returns:
int evbuffer_read (struct evbuffer *buffer, evutil_socket_tfd, inthowmuch)
Read from a file descriptor and store the result in an evbuffer. Parameters:Returns:
See Also:
char* evbuffer_readln (struct evbuffer *buffer, size_t *n_read_out, enum evbuffer_eol_styleeol_style)
Read a single line from an evbuffer. 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.Parameters:
Returns:
int evbuffer_remove (struct evbuffer *buf, void *data, size_tdatlen)
Read data from an evbuffer and drain the bytes read. If more bytes are requested than are available in the evbuffer, we only extract as many bytes as were available.Parameters:
Returns:
int evbuffer_remove_buffer (struct evbuffer *src, struct evbuffer *dst, size_tdatlen)
Read data from an evbuffer into another evbuffer, draining the bytes from the source buffer. This function avoids copy operations to the extent possible.If more bytes are requested than are available in src, the src buffer is drained completely.
Parameters:
Returns:
int evbuffer_remove_cb (struct evbuffer *buffer, evbuffer_cb_funccb, void *cbarg)
Remove a callback from an evbuffer, given the function and argument used to add it. Returns:
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.Returns:
int evbuffer_reserve_space (struct evbuffer *buf, ev_ssize_tsize, struct evbuffer_iovec *vec, intn_vec)
Reserves space in the last chain or chains of an evbuffer. Makes space available in the last chain or chains of an evbuffer 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().The space is made available as one or more extents, represented by an initial pointer and a length. You can force the memory to be available as only one extent. Allowing more extents, however, makes the function more efficient.
Multiple subsequent calls to this function will make the same space available until evbuffer_commit_space() has been called.
It is an error to do anything that moves around the buffer's internal memory structures before committing the space.
NOTE: The code currently does not ever use more than two extents. This may change in future versions.
Parameters:
Returns:
See Also:
struct evbuffer_ptr evbuffer_search (struct evbuffer *buffer, const char *what, size_tlen, const struct evbuffer_ptr *start) [read]
Search for a string within an evbuffer. Parameters:Returns:
struct evbuffer_ptr evbuffer_search_eol (struct evbuffer *buffer, struct evbuffer_ptr *start, size_t *eol_len_out, enum evbuffer_eol_styleeol_style) [read]
Search for an end-of-line string within an evbuffer. Parameters:Returns:
struct evbuffer_ptr evbuffer_search_range (struct evbuffer *buffer, const char *what, size_tlen, const struct evbuffer_ptr *start, const struct evbuffer_ptr *end) [read]
Search for a string within part of an evbuffer. Parameters:Returns:
int evbuffer_set_flags (struct evbuffer *buf, ev_uint64_tflags)
Change the flags that are set for an evbuffer by adding more. Parameters:Returns:
int evbuffer_unfreeze (struct evbuffer *buf, intat_front)
Re-enable calls that modify an evbuffer. Parameters:Returns:
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_tfd)
Write the contents of an evbuffer to a file descriptor. The evbuffer will be drained after the bytes have been successfully written.Parameters:
Returns:
See Also:
int evbuffer_write_atmost (struct evbuffer *buffer, evutil_socket_tfd, ev_ssize_thowmuch)
Write some of the contents of an evbuffer to a file descriptor. The evbuffer will be drained after the bytes have been successfully written.Parameters:
Returns:
See Also:
Author
Generated automatically by Doxygen for libevent from the source code.Wed Apr 10 2013 | libevent |