1 /*
2 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. The name of the author may not be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 #ifndef _EVENT2_BUFFEREVENT_H_
28 #define _EVENT2_BUFFEREVENT_H_
29
77 #ifdef __cplusplus
78 extern "C" {
79 #endif
80
81 #include <event2/event-config.h>
82 #ifdef _EVENT_HAVE_SYS_TYPES_H
83 #include <sys/types.h>
84 #endif
85 #ifdef _EVENT_HAVE_SYS_TIME_H
86 #include <sys/time.h>
87 #endif
88
89 /* For int types. */
91
98 #define BEV_EVENT_READING 0x01 99 #define BEV_EVENT_WRITING 0x02 100 #define BEV_EVENT_EOF 0x10 101 #define BEV_EVENT_ERROR 0x20 102 #define BEV_EVENT_TIMEOUT 0x40 103 #define BEV_EVENT_CONNECTED 0x80 112 #ifdef _EVENT_IN_DOXYGEN
113 {}
114 #endif
115 ;
118 struct sockaddr;
119
134
150
156
160
163
169 };
170
185
206
207 struct evdns_base;
235 struct evdns_base *, int, const char *, int);
236
246
259
264
275
276
283
284
302
311
317
323
338 const void *data, size_t size);
339
340
351
352
364
374
385
399
409
419
427
455 const struct timeval *timeout_read, const struct timeval *timeout_write);
456
476 size_t lowmark, size_t highmark);
477
483
489
497
500
503 };
504
514 short iotype,
516
528
531
535 };
536
558
576 int options,
577 void (*free_context)(void *),
578 void *ctx);
593
599
604 struct ev_token_bucket_cfg;
605
610 struct bufferevent_rate_limit_group;
611
613 #define EV_RATE_LIMIT_MAX EV_SSIZE_MAX 614
632 size_t read_rate, size_t read_burst,
633 size_t write_rate, size_t write_burst,
634 const struct timeval *tick_len);
635
642
655 struct ev_token_bucket_cfg *cfg);
656
676 const struct ev_token_bucket_cfg *cfg);
683 struct bufferevent_rate_limit_group *,
684 const struct ev_token_bucket_cfg *);
685
703 struct bufferevent_rate_limit_group *, size_t);
704
710
722 struct bufferevent_rate_limit_group *g);
723
726
738 ev_ssize_t bufferevent_get_read_limit(
struct bufferevent *bev);
739 ev_ssize_t bufferevent_get_write_limit(
struct bufferevent *bev);
742 ev_ssize_t bufferevent_get_max_to_read(
struct bufferevent *bev);
743 ev_ssize_t bufferevent_get_max_to_write(
struct bufferevent *bev);
744
754 ev_ssize_t bufferevent_rate_limit_group_get_read_limit(
755 struct bufferevent_rate_limit_group *);
756 ev_ssize_t bufferevent_rate_limit_group_get_write_limit(
757 struct bufferevent_rate_limit_group *);
774 int bufferevent_decrement_read_limit(
struct bufferevent *bev, ev_ssize_t decr);
775 int bufferevent_decrement_write_limit(
struct bufferevent *bev, ev_ssize_t decr);
791 int bufferevent_rate_limit_group_decrement_read(
792 struct bufferevent_rate_limit_group *, ev_ssize_t);
793 int bufferevent_rate_limit_group_decrement_write(
794 struct bufferevent_rate_limit_group *, ev_ssize_t);
805 struct bufferevent_rate_limit_group *grp,
806 ev_uint64_t *total_read_out, ev_uint64_t *total_written_out);
807
813 void
815 struct bufferevent_rate_limit_group *grp);
816
817 #ifdef __cplusplus
818 }
819 #endif
820
821 #endif /* _EVENT2_BUFFEREVENT_H_ */
void bufferevent_setcb(struct bufferevent *bufev, bufferevent_data_cb readcb, bufferevent_data_cb writecb, bufferevent_event_cb eventcb, void *cbarg)
Changes the callbacks for a bufferevent.
void bufferevent_setwatermark(struct bufferevent *bufev, short events, size_t lowmark, size_t highmark)
Sets the watermarks for read and write events.
evutil_socket_t bufferevent_getfd(struct bufferevent *bufev)
Returns the file descriptor associated with a bufferevent, or -1 if no file descriptor is associated ...
int bufferevent_rate_limit_group_set_min_share(struct bufferevent_rate_limit_group *, size_t)
Change the smallest quantum we're willing to allocate to any single bufferevent in a group for readin...
int bufferevent_socket_get_dns_error(struct bufferevent *bev)
Return the error code for the last failed DNS lookup attempt made by bufferevent_socket_connect_hostn...
encountered EOF on read or done sending data
Definition: bufferevent.h:502
the filter encountered a critical error, no further data can be processed.
Definition: bufferevent.h:534
int bufferevent_flush(struct bufferevent *bufev, short iotype, enum bufferevent_flush_mode mode)
Triggers the bufferevent to produce more data if possible.
int bufferevent_set_timeouts(struct bufferevent *bufev, const struct timeval *timeout_read, const struct timeval *timeout_write)
Set the read and write timeout for a bufferevent.
struct bufferevent * bufferevent_pair_get_partner(struct bufferevent *bev)
Given one bufferevent returned by bufferevent_pair_new(), returns the other one if it still exists...
Structure to represent a single event.
Definition: event.h:272
usually set when processing data
Definition: bufferevent.h:496
int bufferevent_write(struct bufferevent *bufev, const void *data, size_t size)
Write data to a bufferevent buffer.
void bufferevent_rate_limit_group_get_totals(struct bufferevent_rate_limit_group *grp, ev_uint64_t *total_read_out, ev_uint64_t *total_written_out)
Inspect the total bytes read/written on a group.
int bufferevent_setfd(struct bufferevent *bufev, evutil_socket_t fd)
Changes the file descriptor on which the bufferevent operates.
int bufferevent_rate_limit_group_set_cfg(struct bufferevent_rate_limit_group *, const struct ev_token_bucket_cfg *)
Change the rate-limiting settings for a given rate-limiting group.
If set, callbacks are executed without locks being held on the bufferevent.
Definition: bufferevent.h:168
everything is okay
Definition: bufferevent.h:527
#define evutil_socket_t
A type wide enough to hold the output of "socket()" or "accept()".
Definition: util.h:274
the filter needs to read more data before output
Definition: bufferevent.h:530
int bufferevent_set_rate_limit(struct bufferevent *bev, struct ev_token_bucket_cfg *cfg)
Set the rate-limit of a the bufferevent 'bev' to the one specified in 'cfg'.
void bufferevent_unlock(struct bufferevent *bufev)
Release the lock on a bufferevent.
void(* bufferevent_data_cb)(struct bufferevent *bev, void *ctx)
A read or write callback for a bufferevent.
Definition: bufferevent.h:133
int bufferevent_remove_from_rate_limit_group(struct bufferevent *bev)
Remove 'bev' from its current rate-limit group (if any).
An evbuffer is an opaque data type for efficiently buffering data to be sent or received on the netwo...
Definition: buffer.h:95
int bufferevent_add_to_rate_limit_group(struct bufferevent *bev, struct bufferevent_rate_limit_group *g)
Add 'bev' to the list of bufferevents whose aggregate reading and writing is restricted by 'g'...
void ev_token_bucket_cfg_free(struct ev_token_bucket_cfg *cfg)
Free all storage held in 'cfg'.
If set, callbacks are run deferred in the event loop.
Definition: bufferevent.h:162
int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf)
Write data from an evbuffer to a bufferevent buffer.
enum bufferevent_filter_result(* bufferevent_filter_cb)(struct evbuffer *src, struct evbuffer *dst, ev_ssize_t dst_limit, enum bufferevent_flush_mode mode, void *ctx)
A callback function to implement a filter for a bufferevent.
Definition: bufferevent.h:555
Common convenience functions for cross-platform portability and related socket manipulations.
struct event_base * bufferevent_get_base(struct bufferevent *bev)
Return the event_base used by a bufferevent.
int bufferevent_disable(struct bufferevent *bufev, short event)
Disable a bufferevent.
struct evbuffer * bufferevent_get_output(struct bufferevent *bufev)
Returns the output buffer.
If set, and threading is enabled, operations on this bufferevent are protected by a lock...
Definition: bufferevent.h:159
int bufferevent_socket_connect(struct bufferevent *, struct sockaddr *, int)
Launch a connect() attempt with a socket-based bufferevent.
int bufferevent_socket_connect_hostname(struct bufferevent *, struct evdns_base *, int, const char *, int)
Resolve the hostname 'hostname' and connect to it as with bufferevent_socket_connect().
struct bufferevent_rate_limit_group * bufferevent_rate_limit_group_new(struct event_base *base, const struct ev_token_bucket_cfg *cfg)
Create a new rate-limit group for bufferevents.
If set, we close the underlying file descriptor/bufferevent/whatever when this bufferevent is freed...
Definition: bufferevent.h:155
struct evbuffer * bufferevent_get_input(struct bufferevent *bufev)
Returns the input buffer.
int bufferevent_enable(struct bufferevent *bufev, short event)
Enable a bufferevent.
struct bufferevent * bufferevent_get_underlying(struct bufferevent *bufev)
Returns the underlying bufferevent associated with a bufferevent (if the bufferevent is a wrapper)...
bufferevent_flush_mode
Flags that can be passed into filters to let them know how to deal with the incoming data...
Definition: bufferevent.h:494
int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev)
Assign a bufferevent to a specific event_base.
size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size)
Read data from a bufferevent buffer.
An opaque type for handling buffered IO.
Definition: bufferevent.h:111
void(* bufferevent_event_cb)(struct bufferevent *bev, short what, void *ctx)
An event/error callback for a bufferevent.
Definition: bufferevent.h:149
want to checkpoint all data sent.
Definition: bufferevent.h:499
struct ev_token_bucket_cfg * ev_token_bucket_cfg_new(size_t read_rate, size_t read_burst, size_t write_rate, size_t write_burst, const struct timeval *tick_len)
Initialize and return a new object to configure the rate-limiting behavior of bufferevents.
void bufferevent_lock(struct bufferevent *bufev)
Acquire the lock on a bufferevent.
int bufferevent_pair_new(struct event_base *base, int options, struct bufferevent *pair[2])
Allocate a pair of linked bufferevents.
struct bufferevent * bufferevent_filter_new(struct bufferevent *underlying, bufferevent_filter_cb input_filter, bufferevent_filter_cb output_filter, int options, void(*free_context)(void *), void *ctx)
Allocate a new filtering bufferevent on top of an existing bufferevent.
void bufferevent_free(struct bufferevent *bufev)
Deallocate the storage associated with a bufferevent structure.
bufferevent_options
Options that can be specified when creating a bufferevent.
Definition: bufferevent.h:152
Structure to hold information and state for a Libevent dispatch loop.
Definition: event.h:213
bufferevent_filter_result
Values that filters can return.
Definition: bufferevent.h:525
struct bufferevent * bufferevent_socket_new(struct event_base *base, evutil_socket_t fd, int options)
Create a new socket bufferevent over an existing socket.
int bufferevent_priority_set(struct bufferevent *bufev, int pri)
Assign a priority to a bufferevent.
short bufferevent_get_enabled(struct bufferevent *bufev)
Return the events that are enabled on a given bufferevent.
void bufferevent_rate_limit_group_reset_totals(struct bufferevent_rate_limit_group *grp)
Reset the total bytes read/written on a group.
int bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf)
Read data from a bufferevent buffer into an evbuffer.
void bufferevent_rate_limit_group_free(struct bufferevent_rate_limit_group *)
Free a rate-limiting group.