libevent
|
00001 /* 00002 * Copyright (c) 2007-2011 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); 00149 void evbuffer_free(struct evbuffer *buf); 00150 00163 int evbuffer_enable_locking(struct evbuffer *buf, void *lock); 00164 00169 void evbuffer_lock(struct evbuffer *buf); 00170 00175 void evbuffer_unlock(struct evbuffer *buf); 00176 00177 00193 #define EVBUFFER_FLAG_DRAINS_TO_FD 1 00194 00202 int evbuffer_set_flags(struct evbuffer *buf, ev_uint64_t flags); 00210 int evbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags); 00211 00218 size_t evbuffer_get_length(const struct evbuffer *buf); 00219 00232 size_t evbuffer_get_contiguous_space(const struct evbuffer *buf); 00233 00244 int evbuffer_expand(struct evbuffer *buf, size_t datlen); 00245 00279 int 00280 evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size, 00281 struct evbuffer_iovec *vec, int n_vec); 00282 00304 int evbuffer_commit_space(struct evbuffer *buf, 00305 struct evbuffer_iovec *vec, int n_vecs); 00306 00315 int evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen); 00316 00317 00329 int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen); 00330 00342 ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen); 00343 00357 int evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst, 00358 size_t datlen); 00359 00362 enum evbuffer_eol_style { 00372 EVBUFFER_EOL_ANY, 00375 EVBUFFER_EOL_CRLF, 00377 EVBUFFER_EOL_CRLF_STRICT, 00379 EVBUFFER_EOL_LF 00380 }; 00381 00396 char *evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out, 00397 enum evbuffer_eol_style eol_style); 00398 00411 int evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf); 00412 00419 typedef void (*evbuffer_ref_cleanup_cb)(const void *data, 00420 size_t datalen, void *extra); 00421 00437 int evbuffer_add_reference(struct evbuffer *outbuf, 00438 const void *data, size_t datlen, 00439 evbuffer_ref_cleanup_cb cleanupfn, void *cleanupfn_arg); 00440 00462 int evbuffer_add_file(struct evbuffer *outbuf, int fd, ev_off_t offset, 00463 ev_off_t length); 00464 00477 int evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...) 00478 #ifdef __GNUC__ 00479 __attribute__((format(printf, 2, 3))) 00480 #endif 00481 ; 00482 00491 int evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap); 00492 00493 00501 int evbuffer_drain(struct evbuffer *buf, size_t len); 00502 00503 00514 int evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd); 00515 00528 int evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd, 00529 ev_ssize_t howmuch); 00530 00540 int evbuffer_read(struct evbuffer *buffer, evutil_socket_t fd, int howmuch); 00541 00553 struct evbuffer_ptr evbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start); 00554 00569 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); 00570 00575 enum evbuffer_ptr_how { 00578 EVBUFFER_PTR_SET, 00580 EVBUFFER_PTR_ADD 00581 }; 00582 00595 int 00596 evbuffer_ptr_set(struct evbuffer *buffer, struct evbuffer_ptr *ptr, 00597 size_t position, enum evbuffer_ptr_how how); 00598 00613 struct evbuffer_ptr evbuffer_search_eol(struct evbuffer *buffer, 00614 struct evbuffer_ptr *start, size_t *eol_len_out, 00615 enum evbuffer_eol_style eol_style); 00616 00643 int evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len, 00644 struct evbuffer_ptr *start_at, 00645 struct evbuffer_iovec *vec_out, int n_vec); 00646 00647 00652 struct evbuffer_cb_info { 00655 size_t orig_size; 00657 size_t n_added; 00659 size_t n_deleted; 00660 }; 00661 00681 typedef void (*evbuffer_cb_func)(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg); 00682 00683 struct evbuffer_cb_entry; 00695 struct evbuffer_cb_entry *evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg); 00696 00705 int evbuffer_remove_cb_entry(struct evbuffer *buffer, 00706 struct evbuffer_cb_entry *ent); 00707 00714 int evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg); 00715 00721 #define EVBUFFER_CB_ENABLED 1 00722 00730 int evbuffer_cb_set_flags(struct evbuffer *buffer, 00731 struct evbuffer_cb_entry *cb, ev_uint32_t flags); 00732 00740 int evbuffer_cb_clear_flags(struct evbuffer *buffer, 00741 struct evbuffer_cb_entry *cb, ev_uint32_t flags); 00742 00743 #if 0 00744 00753 void evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb); 00762 void evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb); 00763 #endif 00764 00774 unsigned char *evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size); 00775 00785 int evbuffer_prepend(struct evbuffer *buf, const void *data, size_t size); 00786 00795 int evbuffer_prepend_buffer(struct evbuffer *dst, struct evbuffer* src); 00796 00811 int evbuffer_freeze(struct evbuffer *buf, int at_front); 00820 int evbuffer_unfreeze(struct evbuffer *buf, int at_front); 00821 00822 struct event_base; 00830 int evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base); 00831 00832 #ifdef __cplusplus 00833 } 00834 #endif 00835 00836 #endif /* _EVENT2_BUFFER_H_ */