libevent
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
buffer.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  * 3. The name of the author may not be used to endorse or promote products
13  * derived from this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 #ifndef _EVENT2_BUFFER_H_
27 #define _EVENT2_BUFFER_H_
28 
75 #ifdef __cplusplus
76 extern "C" {
77 #endif
78 
79 #include <event2/event-config.h>
80 #include <stdarg.h>
81 #ifdef _EVENT_HAVE_SYS_TYPES_H
82 #include <sys/types.h>
83 #endif
84 #ifdef _EVENT_HAVE_SYS_UIO_H
85 #include <sys/uio.h>
86 #endif
87 #include <event2/util.h>
88 
95 struct evbuffer
96 #ifdef _EVENT_IN_DOXYGEN
97 {}
98 #endif
99 ;
100 
109 struct evbuffer_ptr {
110  ev_ssize_t pos;
111 
112  /* Do not alter the values of fields. */
113  struct {
114  void *chain;
115  size_t pos_in_chain;
116  } _internal;
117 };
118 
124 #ifdef _EVENT_HAVE_SYS_UIO_H
125 #define evbuffer_iovec iovec
126 /* Internal use -- defined only if we are using the native struct iovec */
127 #define _EVBUFFER_IOVEC_IS_NATIVE
128 #else
131  void *iov_base;
133  size_t iov_len;
134 };
135 #endif
136 
143 struct evbuffer *evbuffer_new(void);
149 void evbuffer_free(struct evbuffer *buf);
150 
163 int evbuffer_enable_locking(struct evbuffer *buf, void *lock);
164 
169 void evbuffer_lock(struct evbuffer *buf);
170 
175 void evbuffer_unlock(struct evbuffer *buf);
176 
177 
193 #define EVBUFFER_FLAG_DRAINS_TO_FD 1
194 
202 int evbuffer_set_flags(struct evbuffer *buf, ev_uint64_t flags);
210 int evbuffer_clear_flags(struct evbuffer *buf, ev_uint64_t flags);
211 
218 size_t evbuffer_get_length(const struct evbuffer *buf);
219 
232 size_t evbuffer_get_contiguous_space(const struct evbuffer *buf);
233 
244 int evbuffer_expand(struct evbuffer *buf, size_t datlen);
245 
279 int
280 evbuffer_reserve_space(struct evbuffer *buf, ev_ssize_t size,
281  struct evbuffer_iovec *vec, int n_vec);
282 
304 int evbuffer_commit_space(struct evbuffer *buf,
305  struct evbuffer_iovec *vec, int n_vecs);
306 
315 int evbuffer_add(struct evbuffer *buf, const void *data, size_t datlen);
316 
317 
329 int evbuffer_remove(struct evbuffer *buf, void *data, size_t datlen);
330 
342 ev_ssize_t evbuffer_copyout(struct evbuffer *buf, void *data_out, size_t datlen);
343 
357 int evbuffer_remove_buffer(struct evbuffer *src, struct evbuffer *dst,
358  size_t datlen);
359 
380 };
381 
396 char *evbuffer_readln(struct evbuffer *buffer, size_t *n_read_out,
397  enum evbuffer_eol_style eol_style);
398 
411 int evbuffer_add_buffer(struct evbuffer *outbuf, struct evbuffer *inbuf);
412 
419 typedef void (*evbuffer_ref_cleanup_cb)(const void *data,
420  size_t datalen, void *extra);
421 
437 int evbuffer_add_reference(struct evbuffer *outbuf,
438  const void *data, size_t datlen,
439  evbuffer_ref_cleanup_cb cleanupfn, void *cleanupfn_arg);
440 
462 int evbuffer_add_file(struct evbuffer *outbuf, int fd, ev_off_t offset,
463  ev_off_t length);
464 
477 int evbuffer_add_printf(struct evbuffer *buf, const char *fmt, ...)
478 #ifdef __GNUC__
479  __attribute__((format(printf, 2, 3)))
480 #endif
481 ;
482 
491 int evbuffer_add_vprintf(struct evbuffer *buf, const char *fmt, va_list ap)
492 #ifdef __GNUC__
493  __attribute__((format(printf, 2, 0)))
494 #endif
495 ;
496 
497 
505 int evbuffer_drain(struct evbuffer *buf, size_t len);
506 
507 
518 int evbuffer_write(struct evbuffer *buffer, evutil_socket_t fd);
519 
532 int evbuffer_write_atmost(struct evbuffer *buffer, evutil_socket_t fd,
533  ev_ssize_t howmuch);
534 
544 int evbuffer_read(struct evbuffer *buffer, evutil_socket_t fd, int howmuch);
545 
557 struct evbuffer_ptr evbuffer_search(struct evbuffer *buffer, const char *what, size_t len, const struct evbuffer_ptr *start);
558 
573 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);
574 
585 };
586 
599 int
600 evbuffer_ptr_set(struct evbuffer *buffer, struct evbuffer_ptr *ptr,
601  size_t position, enum evbuffer_ptr_how how);
602 
617 struct evbuffer_ptr evbuffer_search_eol(struct evbuffer *buffer,
618  struct evbuffer_ptr *start, size_t *eol_len_out,
619  enum evbuffer_eol_style eol_style);
620 
649 int evbuffer_peek(struct evbuffer *buffer, ev_ssize_t len,
650  struct evbuffer_ptr *start_at,
651  struct evbuffer_iovec *vec_out, int n_vec);
652 
653 
661  size_t orig_size;
663  size_t n_added;
665  size_t n_deleted;
666 };
667 
687 typedef void (*evbuffer_cb_func)(struct evbuffer *buffer, const struct evbuffer_cb_info *info, void *arg);
688 
689 struct evbuffer_cb_entry;
701 struct evbuffer_cb_entry *evbuffer_add_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
702 
711 int evbuffer_remove_cb_entry(struct evbuffer *buffer,
712  struct evbuffer_cb_entry *ent);
713 
720 int evbuffer_remove_cb(struct evbuffer *buffer, evbuffer_cb_func cb, void *cbarg);
721 
727 #define EVBUFFER_CB_ENABLED 1
728 
736 int evbuffer_cb_set_flags(struct evbuffer *buffer,
737  struct evbuffer_cb_entry *cb, ev_uint32_t flags);
738 
746 int evbuffer_cb_clear_flags(struct evbuffer *buffer,
747  struct evbuffer_cb_entry *cb, ev_uint32_t flags);
748 
749 #if 0
750 
759 void evbuffer_cb_suspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
768 void evbuffer_cb_unsuspend(struct evbuffer *buffer, struct evbuffer_cb_entry *cb);
769 #endif
770 
780 unsigned char *evbuffer_pullup(struct evbuffer *buf, ev_ssize_t size);
781 
791 int evbuffer_prepend(struct evbuffer *buf, const void *data, size_t size);
792 
801 int evbuffer_prepend_buffer(struct evbuffer *dst, struct evbuffer* src);
802 
817 int evbuffer_freeze(struct evbuffer *buf, int at_front);
826 int evbuffer_unfreeze(struct evbuffer *buf, int at_front);
827 
828 struct event_base;
836 int evbuffer_defer_callbacks(struct evbuffer *buffer, struct event_base *base);
837 
838 #ifdef __cplusplus
839 }
840 #endif
841 
842 #endif /* _EVENT2_BUFFER_H_ */