libevent

event2/buffer.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2007-2010 Niels Provos and Nick Mathewson
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  * 1. Redistributions of source code must retain the above copyright
00008  *    notice, this list of conditions and the following disclaimer.
00009  * 2. Redistributions in binary form must reproduce the above copyright
00010  *    notice, this list of conditions and the following disclaimer in the
00011  *    documentation and/or other materials provided with the distribution.
00012  * 3. The name of the author may not be used to endorse or promote products
00013  *    derived from this software without specific prior written permission.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00016  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00017  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00018  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00019  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00020  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00021  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00022  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00023  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00024  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00025  */
00026 #ifndef _EVENT2_BUFFER_H_
00027 #define _EVENT2_BUFFER_H_
00028 
00075 #ifdef __cplusplus
00076 extern "C" {
00077 #endif
00078 
00079 #include <event2/event-config.h>
00080 #include <stdarg.h>
00081 #ifdef _EVENT_HAVE_SYS_TYPES_H
00082 #include <sys/types.h>
00083 #endif
00084 #ifdef _EVENT_HAVE_SYS_UIO_H
00085 #include <sys/uio.h>
00086 #endif
00087 #include <event2/util.h>
00088 
00095 struct evbuffer
00096 #ifdef _EVENT_IN_DOXYGEN
00097 {}
00098 #endif
00099 ;
00100 
00109 struct evbuffer_ptr {
00110         ev_ssize_t pos;
00111 
00112         /* Do not alter the values of fields. */
00113         struct {
00114                 void *chain;
00115                 size_t pos_in_chain;
00116         } _internal;
00117 };
00118 
00124 #ifdef _EVENT_HAVE_SYS_UIO_H
00125 #define evbuffer_iovec iovec
00126 /* Internal use -- defined only if we are using the native struct iovec */
00127 #define _EVBUFFER_IOVEC_IS_NATIVE
00128 #else
00129 struct evbuffer_iovec {
00131         void *iov_base;
00133         size_t iov_len;
00134 };
00135 #endif
00136 
00143 struct evbuffer *evbuffer_new(void);
00144 
00150 void evbuffer_free(struct evbuffer *buf);
00151 
00164 int evbuffer_enable_locking(struct evbuffer *buf, void *lock);
00165 
00170 void evbuffer_lock(struct evbuffer *buf);
00171 
00176 void evbuffer_unlock(struct evbuffer *buf);
00177 
00184 size_t evbuffer_get_length(const struct evbuffer *buf);
00185 
00198 size_t evbuffer_get_contiguous_space(const struct evbuffer *buf);
00199 
00210 int evbuffer_expand(struct evbuffer *buf, size_t datlen);
00211 
00245 int
00246 evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
00247     struct evbuffer_iovec *vec, int n_vec);
00248 
00270 int evbuffer_commit_space(struct evbuffer *buf,
00271     struct evbuffer_iovec *vec, int n_vecs);
00272 
00281 int evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen);
00282 
00283 
00295 int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);
00296 
00308 ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen);
00309 
00323 int evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst,
00324     size_t datlen);
00325 
00328 enum evbuffer_eol_style {
00338         EVBUFFER_EOL_ANY,
00341         EVBUFFER_EOL_CRLF,
00343         EVBUFFER_EOL_CRLF_STRICT,
00345         EVBUFFER_EOL_LF
00346 };
00347 
00362 char *evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
00363     enum evbuffer_eol_style eol_style);
00364 
00377 int evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf);
00378 
00385 typedef void (*evbuffer_ref_cleanup_cb)(const void *data,
00386     size_t datalen, void *extra);
00387 
00403 int evbuffer_add_reference(struct evbuffer *outbuf,
00404     const void *data, size_t datlen,
00405     evbuffer_ref_cleanup_cb cleanupfn, void *cleanupfn_arg);
00406 
00427 int evbuffer_add_file(struct evbuffer *outbuf, int fd, ev_off_t offset,
00428     ev_off_t length);
00429 
00442 int evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
00443 #ifdef __GNUC__
00444   __attribute__((format(printf, 2, 3)))
00445 #endif
00446 ;
00447 
00456 int evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap);
00457 
00458 
00466 int evbuffer_drain(struct evbuffer *buf, size_t len);
00467 
00468 
00479 int evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd);
00480 
00493 int evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd,
00494                                                   ev_ssize_t howmuch);
00495 
00505 int evbuffer_read(struct evbuffer *buffer, evutil_socket_t fd, int howmuch);
00506 
00518 struct evbuffer_ptr evbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start);
00519 
00534 struct evbuffer_ptr evbuffer_search_range(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start, const struct evbuffer_ptr *end);
00535 
00540 enum evbuffer_ptr_how {
00543         EVBUFFER_PTR_SET,
00545         EVBUFFER_PTR_ADD
00546 };
00547 
00560 int
00561 evbuffer_ptr_set(struct evbuffer *buffer, struct evbuffer_ptr *ptr,
00562     size_t position, enum evbuffer_ptr_how how);
00563 
00578 struct evbuffer_ptr evbuffer_search_eol(struct evbuffer *buffer,
00579     struct evbuffer_ptr *start, size_t *eol_len_out,
00580     enum evbuffer_eol_style eol_style);
00581 
00608 int evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
00609     struct evbuffer_ptr *start_at,
00610     struct evbuffer_iovec *vec_out, int n_vec);
00611 
00612 
00617 struct evbuffer_cb_info {
00620         size_t orig_size;
00622         size_t n_added;
00624         size_t n_deleted;
00625 };
00626 
00646 typedef void (*evbuffer_cb_func)(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg);
00647 
00648 struct evbuffer_cb_entry;
00660 struct evbuffer_cb_entry *evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
00661 
00670 int evbuffer_remove_cb_entry(struct evbuffer *buffer,
00671                              struct evbuffer_cb_entry *ent);
00672 
00679 int evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
00680 
00686 #define EVBUFFER_CB_ENABLED 1
00687 
00695 int evbuffer_cb_set_flags(struct evbuffer *buffer,
00696                           struct evbuffer_cb_entry *cb, ev_uint32_t flags);
00697 
00705 int evbuffer_cb_clear_flags(struct evbuffer *buffer,
00706                           struct evbuffer_cb_entry *cb, ev_uint32_t flags);
00707 
00708 #if 0
00709 
00718 void evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
00727 void evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
00728 #endif
00729 
00739 unsigned char *evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size);
00740 
00750 int evbuffer_prepend(struct evbuffer *buf, const void *data, size_t size);
00751 
00760 int evbuffer_prepend_buffer(struct evbuffer *dst, struct evbuffer* src);
00761 
00776 int evbuffer_freeze(struct evbuffer *buf, int at_front);
00785 int evbuffer_unfreeze(struct evbuffer *buf, int at_front);
00786 
00787 struct event_base;
00795 int evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base);
00796 
00797 #ifdef __cplusplus
00798 }
00799 #endif
00800 
00801 #endif /* _EVENT2_BUFFER_H_ */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines