libevent
|
00001 /* 00002 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu> 00003 * Copyright (c) 2007-2011 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 00077 #ifdef __cplusplus 00078 extern "C" { 00079 #endif 00080 00081 #include <event2/event-config.h> 00082 #ifdef _EVENT_HAVE_SYS_TYPES_H 00083 #include <sys/types.h> 00084 #endif 00085 #ifdef _EVENT_HAVE_SYS_TIME_H 00086 #include <sys/time.h> 00087 #endif 00088 00089 /* For int types. */ 00090 #include <event2/util.h> 00091 00098 #define BEV_EVENT_READING 0x01 00099 #define BEV_EVENT_WRITING 0x02 00100 #define BEV_EVENT_EOF 0x10 00101 #define BEV_EVENT_ERROR 0x20 00102 #define BEV_EVENT_TIMEOUT 0x40 00103 #define BEV_EVENT_CONNECTED 0x80 00111 struct bufferevent 00112 #ifdef _EVENT_IN_DOXYGEN 00113 {} 00114 #endif 00115 ; 00116 struct event_base; 00117 struct evbuffer; 00118 struct sockaddr; 00119 00133 typedef void (*bufferevent_data_cb)(struct bufferevent *bev, void *ctx); 00134 00149 typedef void (*bufferevent_event_cb)(struct bufferevent *bev, short what, void *ctx); 00150 00152 enum bufferevent_options { 00155 BEV_OPT_CLOSE_ON_FREE = (1<<0), 00156 00159 BEV_OPT_THREADSAFE = (1<<1), 00160 00162 BEV_OPT_DEFER_CALLBACKS = (1<<2), 00163 00168 BEV_OPT_UNLOCK_CALLBACKS = (1<<3) 00169 }; 00170 00184 struct bufferevent *bufferevent_socket_new(struct event_base *base, evutil_socket_t fd, int options); 00185 00205 int bufferevent_socket_connect(struct bufferevent *, struct sockaddr *, int); 00206 00207 struct evdns_base; 00234 int bufferevent_socket_connect_hostname(struct bufferevent *, 00235 struct evdns_base *, int, const char *, int); 00236 00245 int bufferevent_socket_get_dns_error(struct bufferevent *bev); 00246 00258 int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev); 00259 00263 struct event_base *bufferevent_get_base(struct bufferevent *bev); 00264 00274 int bufferevent_priority_set(struct bufferevent *bufev, int pri); 00275 00276 00282 void bufferevent_free(struct bufferevent *bufev); 00283 00284 00299 void bufferevent_setcb(struct bufferevent *bufev, 00300 bufferevent_data_cb readcb, bufferevent_data_cb writecb, 00301 bufferevent_event_cb eventcb, void *cbarg); 00302 00310 int bufferevent_setfd(struct bufferevent *bufev, evutil_socket_t fd); 00311 00316 evutil_socket_t bufferevent_getfd(struct bufferevent *bufev); 00317 00322 struct bufferevent *bufferevent_get_underlying(struct bufferevent *bufev); 00323 00337 int bufferevent_write(struct bufferevent *bufev, 00338 const void *data, size_t size); 00339 00340 00350 int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf); 00351 00352 00363 size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size); 00364 00373 int bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf); 00374 00384 struct evbuffer *bufferevent_get_input(struct bufferevent *bufev); 00385 00398 struct evbuffer *bufferevent_get_output(struct bufferevent *bufev); 00399 00408 int bufferevent_enable(struct bufferevent *bufev, short event); 00409 00418 int bufferevent_disable(struct bufferevent *bufev, short event); 00419 00426 short bufferevent_get_enabled(struct bufferevent *bufev); 00427 00454 int bufferevent_set_timeouts(struct bufferevent *bufev, 00455 const struct timeval *timeout_read, const struct timeval *timeout_write); 00456 00475 void bufferevent_setwatermark(struct bufferevent *bufev, short events, 00476 size_t lowmark, size_t highmark); 00477 00482 void bufferevent_lock(struct bufferevent *bufev); 00483 00488 void bufferevent_unlock(struct bufferevent *bufev); 00489 00494 enum bufferevent_flush_mode { 00496 BEV_NORMAL = 0, 00497 00499 BEV_FLUSH = 1, 00500 00502 BEV_FINISHED = 2 00503 }; 00504 00513 int bufferevent_flush(struct bufferevent *bufev, 00514 short iotype, 00515 enum bufferevent_flush_mode mode); 00516 00525 enum bufferevent_filter_result { 00527 BEV_OK = 0, 00528 00530 BEV_NEED_MORE = 1, 00531 00534 BEV_ERROR = 2 00535 }; 00536 00555 typedef enum bufferevent_filter_result (*bufferevent_filter_cb)( 00556 struct evbuffer *src, struct evbuffer *dst, ev_ssize_t dst_limit, 00557 enum bufferevent_flush_mode mode, void *ctx); 00558 00572 struct bufferevent * 00573 bufferevent_filter_new(struct bufferevent *underlying, 00574 bufferevent_filter_cb input_filter, 00575 bufferevent_filter_cb output_filter, 00576 int options, 00577 void (*free_context)(void *), 00578 void *ctx); 00591 int bufferevent_pair_new(struct event_base *base, int options, 00592 struct bufferevent *pair[2]); 00593 00598 struct bufferevent *bufferevent_pair_get_partner(struct bufferevent *bev); 00599 00604 struct ev_token_bucket_cfg; 00605 00610 struct bufferevent_rate_limit_group; 00611 00613 #define EV_RATE_LIMIT_MAX EV_SSIZE_MAX 00614 00631 struct ev_token_bucket_cfg *ev_token_bucket_cfg_new( 00632 size_t read_rate, size_t read_burst, 00633 size_t write_rate, size_t write_burst, 00634 const struct timeval *tick_len); 00635 00641 void ev_token_bucket_cfg_free(struct ev_token_bucket_cfg *cfg); 00642 00654 int bufferevent_set_rate_limit(struct bufferevent *bev, 00655 struct ev_token_bucket_cfg *cfg); 00656 00674 struct bufferevent_rate_limit_group *bufferevent_rate_limit_group_new( 00675 struct event_base *base, 00676 const struct ev_token_bucket_cfg *cfg); 00682 int bufferevent_rate_limit_group_set_cfg( 00683 struct bufferevent_rate_limit_group *, 00684 const struct ev_token_bucket_cfg *); 00685 00702 int bufferevent_rate_limit_group_set_min_share( 00703 struct bufferevent_rate_limit_group *, size_t); 00704 00709 void bufferevent_rate_limit_group_free(struct bufferevent_rate_limit_group *); 00710 00721 int bufferevent_add_to_rate_limit_group(struct bufferevent *bev, 00722 struct bufferevent_rate_limit_group *g); 00723 00725 int bufferevent_remove_from_rate_limit_group(struct bufferevent *bev); 00726 00738 ev_ssize_t bufferevent_get_read_limit(struct bufferevent *bev); 00739 ev_ssize_t bufferevent_get_write_limit(struct bufferevent *bev); 00742 ev_ssize_t bufferevent_get_max_to_read(struct bufferevent *bev); 00743 ev_ssize_t bufferevent_get_max_to_write(struct bufferevent *bev); 00744 00754 ev_ssize_t bufferevent_rate_limit_group_get_read_limit( 00755 struct bufferevent_rate_limit_group *); 00756 ev_ssize_t bufferevent_rate_limit_group_get_write_limit( 00757 struct bufferevent_rate_limit_group *); 00774 int bufferevent_decrement_read_limit(struct bufferevent *bev, ev_ssize_t decr); 00775 int bufferevent_decrement_write_limit(struct bufferevent *bev, ev_ssize_t decr); 00791 int bufferevent_rate_limit_group_decrement_read( 00792 struct bufferevent_rate_limit_group *, ev_ssize_t); 00793 int bufferevent_rate_limit_group_decrement_write( 00794 struct bufferevent_rate_limit_group *, ev_ssize_t); 00804 void bufferevent_rate_limit_group_get_totals( 00805 struct bufferevent_rate_limit_group *grp, 00806 ev_uint64_t *total_read_out, ev_uint64_t *total_written_out); 00807 00813 void 00814 bufferevent_rate_limit_group_reset_totals( 00815 struct bufferevent_rate_limit_group *grp); 00816 00817 #ifdef __cplusplus 00818 } 00819 #endif 00820 00821 #endif /* _EVENT2_BUFFEREVENT_H_ */