libevent
include/event2/bufferevent.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
00003  * Copyright (c) 2007-2010 Niels Provos and Nick Mathewson
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00018  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00019  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00020  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00021  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00022  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00023  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00025  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  */
00027 #ifndef _EVENT2_BUFFEREVENT_H_
00028 #define _EVENT2_BUFFEREVENT_H_
00029 
00053 #ifdef __cplusplus
00054 extern "C" {
00055 #endif
00056 
00057 #include <event2/event-config.h>
00058 #ifdef _EVENT_HAVE_SYS_TYPES_H
00059 #include <sys/types.h>
00060 #endif
00061 #ifdef _EVENT_HAVE_SYS_TIME_H
00062 #include <sys/time.h>
00063 #endif
00064 
00065 /* For int types. */
00066 #include <event2/util.h>
00067 
00068 /* Just for error reporting - use other constants otherwise */
00069 #define BEV_EVENT_READING       0x01    
00070 #define BEV_EVENT_WRITING       0x02    
00071 #define BEV_EVENT_EOF           0x10    
00072 #define BEV_EVENT_ERROR         0x20    
00073 #define BEV_EVENT_TIMEOUT       0x40    
00074 #define BEV_EVENT_CONNECTED     0x80    
00075 struct bufferevent;
00076 struct event_base;
00077 struct evbuffer;
00078 struct sockaddr;
00079 
00093 typedef void (*bufferevent_data_cb)(struct bufferevent *bev, void *ctx);
00094 
00109 typedef void (*bufferevent_event_cb)(struct bufferevent *bev, short what, void *ctx);
00110 
00112 enum bufferevent_options {
00115         BEV_OPT_CLOSE_ON_FREE = (1<<0),
00116 
00119         BEV_OPT_THREADSAFE = (1<<1),
00120 
00122         BEV_OPT_DEFER_CALLBACKS = (1<<2),
00123 
00128         BEV_OPT_UNLOCK_CALLBACKS = (1<<3)
00129 };
00130 
00143 struct bufferevent *bufferevent_socket_new(struct event_base *base, evutil_socket_t fd, int options);
00144 
00162 int bufferevent_socket_connect(struct bufferevent *, struct sockaddr *, int);
00163 
00164 struct evdns_base;
00191 int bufferevent_socket_connect_hostname(struct bufferevent *b,
00192     struct evdns_base *, int, const char *, int);
00193 
00202 int bufferevent_socket_get_dns_error(struct bufferevent *bev);
00203 
00215 int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
00216 
00220 struct event_base *bufferevent_get_base(struct bufferevent *bev);
00221 
00231 int bufferevent_priority_set(struct bufferevent *bufev, int pri);
00232 
00233 
00239 void bufferevent_free(struct bufferevent *bufev);
00240 
00241 
00256 void bufferevent_setcb(struct bufferevent *bufev,
00257     bufferevent_data_cb readcb, bufferevent_data_cb writecb,
00258     bufferevent_event_cb eventcb, void *cbarg);
00259 
00267 int bufferevent_setfd(struct bufferevent *bufev, evutil_socket_t fd);
00268 
00273 evutil_socket_t bufferevent_getfd(struct bufferevent *bufev);
00274 
00279 struct bufferevent *bufferevent_get_underlying(struct bufferevent *bufev);
00280 
00294 int bufferevent_write(struct bufferevent *bufev,
00295     const void *data, size_t size);
00296 
00297 
00307 int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
00308 
00309 
00320 size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
00321 
00330 int bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf);
00331 
00341 struct evbuffer *bufferevent_get_input(struct bufferevent *bufev);
00342 
00355 struct evbuffer *bufferevent_get_output(struct bufferevent *bufev);
00356 
00365 int bufferevent_enable(struct bufferevent *bufev, short event);
00366 
00375 int bufferevent_disable(struct bufferevent *bufev, short event);
00376 
00383 short bufferevent_get_enabled(struct bufferevent *bufev);
00384 
00411 int bufferevent_set_timeouts(struct bufferevent *bufev,
00412     const struct timeval *timeout_read, const struct timeval *timeout_write);
00413 
00432 void bufferevent_setwatermark(struct bufferevent *bufev, short events,
00433     size_t lowmark, size_t highmark);
00434 
00439 void bufferevent_lock(struct bufferevent *bufev);
00440 
00445 void bufferevent_unlock(struct bufferevent *bufev);
00446 
00451 enum bufferevent_flush_mode {
00453         BEV_NORMAL = 0,
00454 
00456         BEV_FLUSH = 1,
00457 
00459         BEV_FINISHED = 2
00460 };
00461 
00471 int bufferevent_flush(struct bufferevent *bufev,
00472     short iotype,
00473     enum bufferevent_flush_mode mode);
00474 
00482 enum bufferevent_filter_result {
00484         BEV_OK = 0,
00485 
00487         BEV_NEED_MORE = 1,
00488 
00491         BEV_ERROR = 2
00492 };
00493 
00512 typedef enum bufferevent_filter_result (*bufferevent_filter_cb)(
00513     struct evbuffer *src, struct evbuffer *dst, ev_ssize_t dst_limit,
00514     enum bufferevent_flush_mode mode, void *ctx);
00515 
00529 struct bufferevent *
00530 bufferevent_filter_new(struct bufferevent *underlying,
00531                        bufferevent_filter_cb input_filter,
00532                        bufferevent_filter_cb output_filter,
00533                        int options,
00534                        void (*free_context)(void *),
00535                        void *ctx);
00536 
00547 int bufferevent_pair_new(struct event_base *base, int options,
00548     struct bufferevent *pair[2]);
00549 
00554 struct bufferevent *bufferevent_pair_get_partner(struct bufferevent *bev);
00555 
00560 struct ev_token_bucket_cfg;
00561 
00566 struct bufferevent_rate_limit_group;
00567 
00569 #define EV_RATE_LIMIT_MAX EV_SSIZE_MAX
00570 
00587 struct ev_token_bucket_cfg *ev_token_bucket_cfg_new(
00588         size_t read_rate, size_t read_burst,
00589         size_t write_rate, size_t write_burst,
00590         const struct timeval *tick_len);
00591 
00597 void ev_token_bucket_cfg_free(struct ev_token_bucket_cfg *cfg);
00598 
00610 int bufferevent_set_rate_limit(struct bufferevent *bev,
00611     struct ev_token_bucket_cfg *cfg);
00612 
00630 struct bufferevent_rate_limit_group *bufferevent_rate_limit_group_new(
00631         struct event_base *base,
00632         const struct ev_token_bucket_cfg *cfg);
00638 int bufferevent_rate_limit_group_set_cfg(
00639         struct bufferevent_rate_limit_group *,
00640         const struct ev_token_bucket_cfg *);
00641 
00658 int bufferevent_rate_limit_group_set_min_share(
00659         struct bufferevent_rate_limit_group *, size_t);
00660 
00665 void bufferevent_rate_limit_group_free(struct bufferevent_rate_limit_group *);
00666 
00677 int bufferevent_add_to_rate_limit_group(struct bufferevent *bev,
00678     struct bufferevent_rate_limit_group *g);
00679 
00681 int bufferevent_remove_from_rate_limit_group(struct bufferevent *bev);
00682 
00691 ev_ssize_t bufferevent_get_read_limit(struct bufferevent *bev);
00692 ev_ssize_t bufferevent_get_write_limit(struct bufferevent *bev);
00695 ev_ssize_t bufferevent_get_max_to_read(struct bufferevent *bev);
00696 ev_ssize_t bufferevent_get_max_to_write(struct bufferevent *bev);
00697 
00704 ev_ssize_t bufferevent_rate_limit_group_get_read_limit(
00705         struct bufferevent_rate_limit_group *);
00706 ev_ssize_t bufferevent_rate_limit_group_get_write_limit(
00707         struct bufferevent_rate_limit_group *);
00721 int bufferevent_decrement_read_limit(struct bufferevent *bev, ev_ssize_t decr);
00722 int bufferevent_decrement_write_limit(struct bufferevent *bev, ev_ssize_t decr);
00735 int bufferevent_rate_limit_group_decrement_read(
00736         struct bufferevent_rate_limit_group *, ev_ssize_t);
00737 int bufferevent_rate_limit_group_decrement_write(
00738         struct bufferevent_rate_limit_group *, ev_ssize_t);
00745 void bufferevent_rate_limit_group_get_totals(
00746     struct bufferevent_rate_limit_group *grp,
00747     ev_uint64_t *total_read_out, ev_uint64_t *total_written_out);
00748 
00751 void
00752 bufferevent_rate_limit_group_reset_totals(
00753         struct bufferevent_rate_limit_group *grp);
00754 
00755 #ifdef __cplusplus
00756 }
00757 #endif
00758 
00759 #endif /* _EVENT2_BUFFEREVENT_H_ */
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines