root / lab4 / .minix-src / include / event2 / bufferevent.h @ 14
History | View | Annotate | Download (28.1 KB)
1 | 13 | up20180614 | /* $NetBSD: bufferevent.h,v 1.1.1.2 2015/01/29 06:38:27 spz Exp $ */
|
---|---|---|---|
2 | /* $NetBSD: bufferevent.h,v 1.1.1.2 2015/01/29 06:38:27 spz Exp $ */
|
||
3 | /*
|
||
4 | * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
|
||
5 | * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
|
||
6 | *
|
||
7 | * Redistribution and use in source and binary forms, with or without
|
||
8 | * modification, are permitted provided that the following conditions
|
||
9 | * are met:
|
||
10 | * 1. Redistributions of source code must retain the above copyright
|
||
11 | * notice, this list of conditions and the following disclaimer.
|
||
12 | * 2. Redistributions in binary form must reproduce the above copyright
|
||
13 | * notice, this list of conditions and the following disclaimer in the
|
||
14 | * documentation and/or other materials provided with the distribution.
|
||
15 | * 3. The name of the author may not be used to endorse or promote products
|
||
16 | * derived from this software without specific prior written permission.
|
||
17 | *
|
||
18 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
||
19 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||
20 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
||
21 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
||
22 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
||
23 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
||
24 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
||
25 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||
26 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
||
27 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||
28 | */
|
||
29 | #ifndef _EVENT2_BUFFEREVENT_H_
|
||
30 | #define _EVENT2_BUFFEREVENT_H_
|
||
31 | |||
32 | /**
|
||
33 | @file event2/bufferevent.h
|
||
34 | |||
35 | Functions for buffering data for network sending or receiving. Bufferevents
|
||
36 | are higher level than evbuffers: each has an underlying evbuffer for reading
|
||
37 | and one for writing, and callbacks that are invoked under certain
|
||
38 | circumstances.
|
||
39 | |||
40 | A bufferevent provides input and output buffers that get filled and
|
||
41 | drained automatically. The user of a bufferevent no longer deals
|
||
42 | directly with the I/O, but instead is reading from input and writing
|
||
43 | to output buffers.
|
||
44 | |||
45 | Once initialized, the bufferevent structure can be used repeatedly
|
||
46 | with bufferevent_enable() and bufferevent_disable().
|
||
47 | |||
48 | When reading is enabled, the bufferevent will try to read from the
|
||
49 | file descriptor onto its input buffer, and call the read callback.
|
||
50 | When writing is enabled, the bufferevent will try to write data onto its
|
||
51 | file descriptor when the output buffer has enough data, and call the write
|
||
52 | callback when the output buffer is sufficiently drained.
|
||
53 | |||
54 | Bufferevents come in several flavors, including:
|
||
55 | |||
56 | <dl>
|
||
57 | <dt>Socket-based bufferevents</dt>
|
||
58 | <dd>A bufferevent that reads and writes data onto a network
|
||
59 | socket. Created with bufferevent_socket_new().</dd>
|
||
60 | |||
61 | <dt>Paired bufferevents</dt>
|
||
62 | <dd>A pair of bufferevents that send and receive data to one
|
||
63 | another without touching the network. Created with
|
||
64 | bufferevent_pair_new().</dd>
|
||
65 | |||
66 | <dt>Filtering bufferevents</dt>
|
||
67 | <dd>A bufferevent that transforms data, and sends or receives it
|
||
68 | over another underlying bufferevent. Created with
|
||
69 | bufferevent_filter_new().</dd>
|
||
70 | |||
71 | <dt>SSL-backed bufferevents</dt>
|
||
72 | <dd>A bufferevent that uses the openssl library to send and
|
||
73 | receive data over an encrypted connection. Created with
|
||
74 | bufferevent_openssl_socket_new() or
|
||
75 | bufferevent_openssl_filter_new().</dd>
|
||
76 | </dl>
|
||
77 | */
|
||
78 | |||
79 | #ifdef __cplusplus
|
||
80 | extern "C" { |
||
81 | #endif
|
||
82 | |||
83 | #include <event2/event-config.h> |
||
84 | #ifdef _EVENT_HAVE_SYS_TYPES_H
|
||
85 | #include <sys/types.h> |
||
86 | #endif
|
||
87 | #ifdef _EVENT_HAVE_SYS_TIME_H
|
||
88 | #include <sys/time.h> |
||
89 | #endif
|
||
90 | |||
91 | /* For int types. */
|
||
92 | #include <event2/util.h> |
||
93 | |||
94 | /** @name Bufferevent event codes
|
||
95 | |||
96 | These flags are passed as arguments to a bufferevent's event callback.
|
||
97 | |||
98 | @{
|
||
99 | */
|
||
100 | #define BEV_EVENT_READING 0x01 /**< error encountered while reading */ |
||
101 | #define BEV_EVENT_WRITING 0x02 /**< error encountered while writing */ |
||
102 | #define BEV_EVENT_EOF 0x10 /**< eof file reached */ |
||
103 | #define BEV_EVENT_ERROR 0x20 /**< unrecoverable error encountered */ |
||
104 | #define BEV_EVENT_TIMEOUT 0x40 /**< user-specified timeout reached */ |
||
105 | #define BEV_EVENT_CONNECTED 0x80 /**< connect operation finished. */ |
||
106 | /**@}*/
|
||
107 | |||
108 | /**
|
||
109 | An opaque type for handling buffered IO
|
||
110 | |||
111 | @see event2/bufferevent.h
|
||
112 | */
|
||
113 | struct bufferevent
|
||
114 | #ifdef _EVENT_IN_DOXYGEN
|
||
115 | {} |
||
116 | #endif
|
||
117 | ; |
||
118 | struct event_base;
|
||
119 | struct evbuffer;
|
||
120 | struct sockaddr;
|
||
121 | |||
122 | /**
|
||
123 | A read or write callback for a bufferevent.
|
||
124 | |||
125 | The read callback is triggered when new data arrives in the input
|
||
126 | buffer and the amount of readable data exceed the low watermark
|
||
127 | which is 0 by default.
|
||
128 | |||
129 | The write callback is triggered if the write buffer has been
|
||
130 | exhausted or fell below its low watermark.
|
||
131 | |||
132 | @param bev the bufferevent that triggered the callback
|
||
133 | @param ctx the user-specified context for this bufferevent
|
||
134 | */
|
||
135 | typedef void (*bufferevent_data_cb)(struct bufferevent *bev, void *ctx); |
||
136 | |||
137 | /**
|
||
138 | An event/error callback for a bufferevent.
|
||
139 | |||
140 | The event callback is triggered if either an EOF condition or another
|
||
141 | unrecoverable error was encountered.
|
||
142 | |||
143 | @param bev the bufferevent for which the error condition was reached
|
||
144 | @param what a conjunction of flags: BEV_EVENT_READING or BEV_EVENT_WRITING
|
||
145 | to indicate if the error was encountered on the read or write path,
|
||
146 | and one of the following flags: BEV_EVENT_EOF, BEV_EVENT_ERROR,
|
||
147 | BEV_EVENT_TIMEOUT, BEV_EVENT_CONNECTED.
|
||
148 | |||
149 | @param ctx the user-specified context for this bufferevent
|
||
150 | */
|
||
151 | typedef void (*bufferevent_event_cb)(struct bufferevent *bev, short what, void *ctx); |
||
152 | |||
153 | /** Options that can be specified when creating a bufferevent */
|
||
154 | enum bufferevent_options {
|
||
155 | /** If set, we close the underlying file
|
||
156 | * descriptor/bufferevent/whatever when this bufferevent is freed. */
|
||
157 | BEV_OPT_CLOSE_ON_FREE = (1<<0), |
||
158 | |||
159 | /** If set, and threading is enabled, operations on this bufferevent
|
||
160 | * are protected by a lock */
|
||
161 | BEV_OPT_THREADSAFE = (1<<1), |
||
162 | |||
163 | /** If set, callbacks are run deferred in the event loop. */
|
||
164 | BEV_OPT_DEFER_CALLBACKS = (1<<2), |
||
165 | |||
166 | /** If set, callbacks are executed without locks being held on the
|
||
167 | * bufferevent. This option currently requires that
|
||
168 | * BEV_OPT_DEFER_CALLBACKS also be set; a future version of Libevent
|
||
169 | * might remove the requirement.*/
|
||
170 | BEV_OPT_UNLOCK_CALLBACKS = (1<<3) |
||
171 | }; |
||
172 | |||
173 | /**
|
||
174 | Create a new socket bufferevent over an existing socket.
|
||
175 | |||
176 | @param base the event base to associate with the new bufferevent.
|
||
177 | @param fd the file descriptor from which data is read and written to.
|
||
178 | This file descriptor is not allowed to be a pipe(2).
|
||
179 | It is safe to set the fd to -1, so long as you later
|
||
180 | set it with bufferevent_setfd or bufferevent_socket_connect().
|
||
181 | @param options Zero or more BEV_OPT_* flags
|
||
182 | @return a pointer to a newly allocated bufferevent struct, or NULL if an
|
||
183 | error occurred
|
||
184 | @see bufferevent_free()
|
||
185 | */
|
||
186 | struct bufferevent *bufferevent_socket_new(struct event_base *base, evutil_socket_t fd, int options); |
||
187 | |||
188 | /**
|
||
189 | Launch a connect() attempt with a socket-based bufferevent.
|
||
190 | |||
191 | When the connect succeeds, the eventcb will be invoked with
|
||
192 | BEV_EVENT_CONNECTED set.
|
||
193 | |||
194 | If the bufferevent does not already have a socket set, we allocate a new
|
||
195 | socket here and make it nonblocking before we begin.
|
||
196 | |||
197 | If no address is provided, we assume that the socket is already connecting,
|
||
198 | and configure the bufferevent so that a BEV_EVENT_CONNECTED event will be
|
||
199 | yielded when it is done connecting.
|
||
200 | |||
201 | @param bufev an existing bufferevent allocated with
|
||
202 | bufferevent_socket_new().
|
||
203 | @param addr the address we should connect to
|
||
204 | @param socklen The length of the address
|
||
205 | @return 0 on success, -1 on failure.
|
||
206 | */
|
||
207 | int bufferevent_socket_connect(struct bufferevent *, struct sockaddr *, int); |
||
208 | |||
209 | struct evdns_base;
|
||
210 | /**
|
||
211 | Resolve the hostname 'hostname' and connect to it as with
|
||
212 | bufferevent_socket_connect().
|
||
213 | |||
214 | @param bufev An existing bufferevent allocated with bufferevent_socket_new()
|
||
215 | @param evdns_base Optionally, an evdns_base to use for resolving hostnames
|
||
216 | asynchronously. May be set to NULL for a blocking resolve.
|
||
217 | @param family A preferred address family to resolve addresses to, or
|
||
218 | AF_UNSPEC for no preference. Only AF_INET, AF_INET6, and AF_UNSPEC are
|
||
219 | supported.
|
||
220 | @param hostname The hostname to resolve; see below for notes on recognized
|
||
221 | formats
|
||
222 | @param port The port to connect to on the resolved address.
|
||
223 | @return 0 if successful, -1 on failure.
|
||
224 | |||
225 | Recognized hostname formats are:
|
||
226 | |||
227 | www.example.com (hostname)
|
||
228 | 1.2.3.4 (ipv4address)
|
||
229 | ::1 (ipv6address)
|
||
230 | [::1] ([ipv6address])
|
||
231 | |||
232 | Performance note: If you do not provide an evdns_base, this function
|
||
233 | may block while it waits for a DNS response. This is probably not
|
||
234 | what you want.
|
||
235 | */
|
||
236 | int bufferevent_socket_connect_hostname(struct bufferevent *, |
||
237 | struct evdns_base *, int, const char *, int); |
||
238 | |||
239 | /**
|
||
240 | Return the error code for the last failed DNS lookup attempt made by
|
||
241 | bufferevent_socket_connect_hostname().
|
||
242 | |||
243 | @param bev The bufferevent object.
|
||
244 | @return DNS error code.
|
||
245 | @see evutil_gai_strerror()
|
||
246 | */
|
||
247 | int bufferevent_socket_get_dns_error(struct bufferevent *bev); |
||
248 | |||
249 | /**
|
||
250 | Assign a bufferevent to a specific event_base.
|
||
251 | |||
252 | NOTE that only socket bufferevents support this function.
|
||
253 | |||
254 | @param base an event_base returned by event_init()
|
||
255 | @param bufev a bufferevent struct returned by bufferevent_new()
|
||
256 | or bufferevent_socket_new()
|
||
257 | @return 0 if successful, or -1 if an error occurred
|
||
258 | @see bufferevent_new()
|
||
259 | */
|
||
260 | int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev); |
||
261 | |||
262 | /**
|
||
263 | Return the event_base used by a bufferevent
|
||
264 | */
|
||
265 | struct event_base *bufferevent_get_base(struct bufferevent *bev); |
||
266 | |||
267 | /**
|
||
268 | Assign a priority to a bufferevent.
|
||
269 | |||
270 | Only supported for socket bufferevents.
|
||
271 | |||
272 | @param bufev a bufferevent struct
|
||
273 | @param pri the priority to be assigned
|
||
274 | @return 0 if successful, or -1 if an error occurred
|
||
275 | */
|
||
276 | int bufferevent_priority_set(struct bufferevent *bufev, int pri); |
||
277 | |||
278 | |||
279 | /**
|
||
280 | Deallocate the storage associated with a bufferevent structure.
|
||
281 | |||
282 | @param bufev the bufferevent structure to be freed.
|
||
283 | */
|
||
284 | void bufferevent_free(struct bufferevent *bufev); |
||
285 | |||
286 | |||
287 | /**
|
||
288 | Changes the callbacks for a bufferevent.
|
||
289 | |||
290 | @param bufev the bufferevent object for which to change callbacks
|
||
291 | @param readcb callback to invoke when there is data to be read, or NULL if
|
||
292 | no callback is desired
|
||
293 | @param writecb callback to invoke when the file descriptor is ready for
|
||
294 | writing, or NULL if no callback is desired
|
||
295 | @param eventcb callback to invoke when there is an event on the file
|
||
296 | descriptor
|
||
297 | @param cbarg an argument that will be supplied to each of the callbacks
|
||
298 | (readcb, writecb, and errorcb)
|
||
299 | @see bufferevent_new()
|
||
300 | */
|
||
301 | void bufferevent_setcb(struct bufferevent *bufev, |
||
302 | bufferevent_data_cb readcb, bufferevent_data_cb writecb, |
||
303 | bufferevent_event_cb eventcb, void *cbarg);
|
||
304 | |||
305 | /**
|
||
306 | Changes the file descriptor on which the bufferevent operates.
|
||
307 | Not supported for all bufferevent types.
|
||
308 | |||
309 | @param bufev the bufferevent object for which to change the file descriptor
|
||
310 | @param fd the file descriptor to operate on
|
||
311 | */
|
||
312 | int bufferevent_setfd(struct bufferevent *bufev, evutil_socket_t fd); |
||
313 | |||
314 | /**
|
||
315 | Returns the file descriptor associated with a bufferevent, or -1 if
|
||
316 | no file descriptor is associated with the bufferevent.
|
||
317 | */
|
||
318 | evutil_socket_t bufferevent_getfd(struct bufferevent *bufev);
|
||
319 | |||
320 | /**
|
||
321 | Returns the underlying bufferevent associated with a bufferevent (if
|
||
322 | the bufferevent is a wrapper), or NULL if there is no underlying bufferevent.
|
||
323 | */
|
||
324 | struct bufferevent *bufferevent_get_underlying(struct bufferevent *bufev); |
||
325 | |||
326 | /**
|
||
327 | Write data to a bufferevent buffer.
|
||
328 | |||
329 | The bufferevent_write() function can be used to write data to the file
|
||
330 | descriptor. The data is appended to the output buffer and written to the
|
||
331 | descriptor automatically as it becomes available for writing.
|
||
332 | |||
333 | @param bufev the bufferevent to be written to
|
||
334 | @param data a pointer to the data to be written
|
||
335 | @param size the length of the data, in bytes
|
||
336 | @return 0 if successful, or -1 if an error occurred
|
||
337 | @see bufferevent_write_buffer()
|
||
338 | */
|
||
339 | int bufferevent_write(struct bufferevent *bufev, |
||
340 | const void *data, size_t size); |
||
341 | |||
342 | |||
343 | /**
|
||
344 | Write data from an evbuffer to a bufferevent buffer. The evbuffer is
|
||
345 | being drained as a result.
|
||
346 | |||
347 | @param bufev the bufferevent to be written to
|
||
348 | @param buf the evbuffer to be written
|
||
349 | @return 0 if successful, or -1 if an error occurred
|
||
350 | @see bufferevent_write()
|
||
351 | */
|
||
352 | int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf); |
||
353 | |||
354 | |||
355 | /**
|
||
356 | Read data from a bufferevent buffer.
|
||
357 | |||
358 | The bufferevent_read() function is used to read data from the input buffer.
|
||
359 | |||
360 | @param bufev the bufferevent to be read from
|
||
361 | @param data pointer to a buffer that will store the data
|
||
362 | @param size the size of the data buffer, in bytes
|
||
363 | @return the amount of data read, in bytes.
|
||
364 | */
|
||
365 | size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size); |
||
366 | |||
367 | /**
|
||
368 | Read data from a bufferevent buffer into an evbuffer. This avoids
|
||
369 | memory copies.
|
||
370 | |||
371 | @param bufev the bufferevent to be read from
|
||
372 | @param buf the evbuffer to which to add data
|
||
373 | @return 0 if successful, or -1 if an error occurred.
|
||
374 | */
|
||
375 | int bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf); |
||
376 | |||
377 | /**
|
||
378 | Returns the input buffer.
|
||
379 | |||
380 | The user MUST NOT set the callback on this buffer.
|
||
381 | |||
382 | @param bufev the bufferevent from which to get the evbuffer
|
||
383 | @return the evbuffer object for the input buffer
|
||
384 | */
|
||
385 | |||
386 | struct evbuffer *bufferevent_get_input(struct bufferevent *bufev); |
||
387 | |||
388 | /**
|
||
389 | Returns the output buffer.
|
||
390 | |||
391 | The user MUST NOT set the callback on this buffer.
|
||
392 | |||
393 | When filters are being used, the filters need to be manually
|
||
394 | triggered if the output buffer was manipulated.
|
||
395 | |||
396 | @param bufev the bufferevent from which to get the evbuffer
|
||
397 | @return the evbuffer object for the output buffer
|
||
398 | */
|
||
399 | |||
400 | struct evbuffer *bufferevent_get_output(struct bufferevent *bufev); |
||
401 | |||
402 | /**
|
||
403 | Enable a bufferevent.
|
||
404 | |||
405 | @param bufev the bufferevent to be enabled
|
||
406 | @param event any combination of EV_READ | EV_WRITE.
|
||
407 | @return 0 if successful, or -1 if an error occurred
|
||
408 | @see bufferevent_disable()
|
||
409 | */
|
||
410 | int bufferevent_enable(struct bufferevent *bufev, short event); |
||
411 | |||
412 | /**
|
||
413 | Disable a bufferevent.
|
||
414 | |||
415 | @param bufev the bufferevent to be disabled
|
||
416 | @param event any combination of EV_READ | EV_WRITE.
|
||
417 | @return 0 if successful, or -1 if an error occurred
|
||
418 | @see bufferevent_enable()
|
||
419 | */
|
||
420 | int bufferevent_disable(struct bufferevent *bufev, short event); |
||
421 | |||
422 | /**
|
||
423 | Return the events that are enabled on a given bufferevent.
|
||
424 | |||
425 | @param bufev the bufferevent to inspect
|
||
426 | @return A combination of EV_READ | EV_WRITE
|
||
427 | */
|
||
428 | short bufferevent_get_enabled(struct bufferevent *bufev); |
||
429 | |||
430 | /**
|
||
431 | Set the read and write timeout for a bufferevent.
|
||
432 | |||
433 | A bufferevent's timeout will fire the first time that the indicated
|
||
434 | amount of time has elapsed since a successful read or write operation,
|
||
435 | during which the bufferevent was trying to read or write.
|
||
436 | |||
437 | (In other words, if reading or writing is disabled, or if the
|
||
438 | bufferevent's read or write operation has been suspended because
|
||
439 | there's no data to write, or not enough banwidth, or so on, the
|
||
440 | timeout isn't active. The timeout only becomes active when we we're
|
||
441 | willing to actually read or write.)
|
||
442 | |||
443 | Calling bufferevent_enable or setting a timeout for a bufferevent
|
||
444 | whose timeout is already pending resets its timeout.
|
||
445 | |||
446 | If the timeout elapses, the corresponding operation (EV_READ or
|
||
447 | EV_WRITE) becomes disabled until you re-enable it again. The
|
||
448 | bufferevent's event callback is called with the
|
||
449 | BEV_EVENT_TIMEOUT|BEV_EVENT_READING or
|
||
450 | BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING.
|
||
451 | |||
452 | @param bufev the bufferevent to be modified
|
||
453 | @param timeout_read the read timeout, or NULL
|
||
454 | @param timeout_write the write timeout, or NULL
|
||
455 | */
|
||
456 | int bufferevent_set_timeouts(struct bufferevent *bufev, |
||
457 | const struct timeval *timeout_read, const struct timeval *timeout_write); |
||
458 | |||
459 | /**
|
||
460 | Sets the watermarks for read and write events.
|
||
461 | |||
462 | On input, a bufferevent does not invoke the user read callback unless
|
||
463 | there is at least low watermark data in the buffer. If the read buffer
|
||
464 | is beyond the high watermark, the bufferevent stops reading from the network.
|
||
465 | |||
466 | On output, the user write callback is invoked whenever the buffered data
|
||
467 | falls below the low watermark. Filters that write to this bufev will try
|
||
468 | not to write more bytes to this buffer than the high watermark would allow,
|
||
469 | except when flushing.
|
||
470 | |||
471 | @param bufev the bufferevent to be modified
|
||
472 | @param events EV_READ, EV_WRITE or both
|
||
473 | @param lowmark the lower watermark to set
|
||
474 | @param highmark the high watermark to set
|
||
475 | */
|
||
476 | |||
477 | void bufferevent_setwatermark(struct bufferevent *bufev, short events, |
||
478 | size_t lowmark, size_t highmark); |
||
479 | |||
480 | /**
|
||
481 | Acquire the lock on a bufferevent. Has no effect if locking was not
|
||
482 | enabled with BEV_OPT_THREADSAFE.
|
||
483 | */
|
||
484 | void bufferevent_lock(struct bufferevent *bufev); |
||
485 | |||
486 | /**
|
||
487 | Release the lock on a bufferevent. Has no effect if locking was not
|
||
488 | enabled with BEV_OPT_THREADSAFE.
|
||
489 | */
|
||
490 | void bufferevent_unlock(struct bufferevent *bufev); |
||
491 | |||
492 | /**
|
||
493 | Flags that can be passed into filters to let them know how to
|
||
494 | deal with the incoming data.
|
||
495 | */
|
||
496 | enum bufferevent_flush_mode {
|
||
497 | /** usually set when processing data */
|
||
498 | BEV_NORMAL = 0,
|
||
499 | |||
500 | /** want to checkpoint all data sent. */
|
||
501 | BEV_FLUSH = 1,
|
||
502 | |||
503 | /** encountered EOF on read or done sending data */
|
||
504 | BEV_FINISHED = 2
|
||
505 | }; |
||
506 | |||
507 | /**
|
||
508 | Triggers the bufferevent to produce more data if possible.
|
||
509 | |||
510 | @param bufev the bufferevent object
|
||
511 | @param iotype either EV_READ or EV_WRITE or both.
|
||
512 | @param mode either BEV_NORMAL or BEV_FLUSH or BEV_FINISHED
|
||
513 | @return -1 on failure, 0 if no data was produces, 1 if data was produced
|
||
514 | */
|
||
515 | int bufferevent_flush(struct bufferevent *bufev, |
||
516 | short iotype,
|
||
517 | enum bufferevent_flush_mode mode);
|
||
518 | |||
519 | /**
|
||
520 | @name Filtering support
|
||
521 | |||
522 | @{
|
||
523 | */
|
||
524 | /**
|
||
525 | Values that filters can return.
|
||
526 | */
|
||
527 | enum bufferevent_filter_result {
|
||
528 | /** everything is okay */
|
||
529 | BEV_OK = 0,
|
||
530 | |||
531 | /** the filter needs to read more data before output */
|
||
532 | BEV_NEED_MORE = 1,
|
||
533 | |||
534 | /** the filter encountered a critical error, no further data
|
||
535 | can be processed. */
|
||
536 | BEV_ERROR = 2
|
||
537 | }; |
||
538 | |||
539 | /** A callback function to implement a filter for a bufferevent.
|
||
540 | |||
541 | @param src An evbuffer to drain data from.
|
||
542 | @param dst An evbuffer to add data to.
|
||
543 | @param limit A suggested upper bound of bytes to write to dst.
|
||
544 | The filter may ignore this value, but doing so means that
|
||
545 | it will overflow the high-water mark associated with dst.
|
||
546 | -1 means "no limit".
|
||
547 | @param mode Whether we should write data as may be convenient
|
||
548 | (BEV_NORMAL), or flush as much data as we can (BEV_FLUSH),
|
||
549 | or flush as much as we can, possibly including an end-of-stream
|
||
550 | marker (BEV_FINISH).
|
||
551 | @param ctx A user-supplied pointer.
|
||
552 | |||
553 | @return BEV_OK if we wrote some data; BEV_NEED_MORE if we can't
|
||
554 | produce any more output until we get some input; and BEV_ERROR
|
||
555 | on an error.
|
||
556 | */
|
||
557 | typedef enum bufferevent_filter_result (*bufferevent_filter_cb)( |
||
558 | struct evbuffer *src, struct evbuffer *dst, ev_ssize_t dst_limit, |
||
559 | enum bufferevent_flush_mode mode, void *ctx); |
||
560 | |||
561 | /**
|
||
562 | Allocate a new filtering bufferevent on top of an existing bufferevent.
|
||
563 | |||
564 | @param underlying the underlying bufferevent.
|
||
565 | @param input_filter The filter to apply to data we read from the underlying
|
||
566 | bufferevent
|
||
567 | @param output_filter The filer to apply to data we write to the underlying
|
||
568 | bufferevent
|
||
569 | @param options A bitfield of bufferevent options.
|
||
570 | @param free_context A function to use to free the filter context when
|
||
571 | this bufferevent is freed.
|
||
572 | @param ctx A context pointer to pass to the filter functions.
|
||
573 | */
|
||
574 | struct bufferevent *
|
||
575 | bufferevent_filter_new(struct bufferevent *underlying,
|
||
576 | bufferevent_filter_cb input_filter, |
||
577 | bufferevent_filter_cb output_filter, |
||
578 | int options,
|
||
579 | void (*free_context)(void *), |
||
580 | void *ctx);
|
||
581 | /**@}*/
|
||
582 | |||
583 | /**
|
||
584 | Allocate a pair of linked bufferevents. The bufferevents behave as would
|
||
585 | two bufferevent_sock instances connected to opposite ends of a
|
||
586 | socketpair(), except that no internal socketpair is allocated.
|
||
587 | |||
588 | @param base The event base to associate with the socketpair.
|
||
589 | @param options A set of options for this bufferevent
|
||
590 | @param pair A pointer to an array to hold the two new bufferevent objects.
|
||
591 | @return 0 on success, -1 on failure.
|
||
592 | */
|
||
593 | int bufferevent_pair_new(struct event_base *base, int options, |
||
594 | struct bufferevent *pair[2]); |
||
595 | |||
596 | /**
|
||
597 | Given one bufferevent returned by bufferevent_pair_new(), returns the
|
||
598 | other one if it still exists. Otherwise returns NULL.
|
||
599 | */
|
||
600 | struct bufferevent *bufferevent_pair_get_partner(struct bufferevent *bev); |
||
601 | |||
602 | /**
|
||
603 | Abstract type used to configure rate-limiting on a bufferevent or a group
|
||
604 | of bufferevents.
|
||
605 | */
|
||
606 | struct ev_token_bucket_cfg;
|
||
607 | |||
608 | /**
|
||
609 | A group of bufferevents which are configured to respect the same rate
|
||
610 | limit.
|
||
611 | */
|
||
612 | struct bufferevent_rate_limit_group;
|
||
613 | |||
614 | /** Maximum configurable rate- or burst-limit. */
|
||
615 | #define EV_RATE_LIMIT_MAX EV_SSIZE_MAX
|
||
616 | |||
617 | /**
|
||
618 | Initialize and return a new object to configure the rate-limiting behavior
|
||
619 | of bufferevents.
|
||
620 | |||
621 | @param read_rate The maximum number of bytes to read per tick on
|
||
622 | average.
|
||
623 | @param read_burst The maximum number of bytes to read in any single tick.
|
||
624 | @param write_rate The maximum number of bytes to write per tick on
|
||
625 | average.
|
||
626 | @param write_burst The maximum number of bytes to write in any single tick.
|
||
627 | @param tick_len The length of a single tick. Defaults to one second.
|
||
628 | Any fractions of a millisecond are ignored.
|
||
629 | |||
630 | Note that all rate-limits hare are currently best-effort: future versions
|
||
631 | of Libevent may implement them more tightly.
|
||
632 | */
|
||
633 | struct ev_token_bucket_cfg *ev_token_bucket_cfg_new(
|
||
634 | size_t read_rate, size_t read_burst, |
||
635 | size_t write_rate, size_t write_burst, |
||
636 | const struct timeval *tick_len); |
||
637 | |||
638 | /** Free all storage held in 'cfg'.
|
||
639 | |||
640 | Note: 'cfg' is not currently reference-counted; it is not safe to free it
|
||
641 | until no bufferevent is using it.
|
||
642 | */
|
||
643 | void ev_token_bucket_cfg_free(struct ev_token_bucket_cfg *cfg); |
||
644 | |||
645 | /**
|
||
646 | Set the rate-limit of a the bufferevent 'bev' to the one specified in
|
||
647 | 'cfg'. If 'cfg' is NULL, disable any per-bufferevent rate-limiting on
|
||
648 | 'bev'.
|
||
649 | |||
650 | Note that only some bufferevent types currently respect rate-limiting.
|
||
651 | They are: socket-based bufferevents (normal and IOCP-based), and SSL-based
|
||
652 | bufferevents.
|
||
653 | |||
654 | Return 0 on sucess, -1 on failure.
|
||
655 | */
|
||
656 | int bufferevent_set_rate_limit(struct bufferevent *bev, |
||
657 | struct ev_token_bucket_cfg *cfg);
|
||
658 | |||
659 | /**
|
||
660 | Create a new rate-limit group for bufferevents. A rate-limit group
|
||
661 | constrains the maximum number of bytes sent and received, in toto,
|
||
662 | by all of its bufferevents.
|
||
663 | |||
664 | @param base An event_base to run any necessary timeouts for the group.
|
||
665 | Note that all bufferevents in the group do not necessarily need to share
|
||
666 | this event_base.
|
||
667 | @param cfg The rate-limit for this group.
|
||
668 | |||
669 | Note that all rate-limits hare are currently best-effort: future versions
|
||
670 | of Libevent may implement them more tightly.
|
||
671 | |||
672 | Note also that only some bufferevent types currently respect rate-limiting.
|
||
673 | They are: socket-based bufferevents (normal and IOCP-based), and SSL-based
|
||
674 | bufferevents.
|
||
675 | */
|
||
676 | struct bufferevent_rate_limit_group *bufferevent_rate_limit_group_new(
|
||
677 | struct event_base *base,
|
||
678 | const struct ev_token_bucket_cfg *cfg); |
||
679 | /**
|
||
680 | Change the rate-limiting settings for a given rate-limiting group.
|
||
681 | |||
682 | Return 0 on success, -1 on failure.
|
||
683 | */
|
||
684 | int bufferevent_rate_limit_group_set_cfg(
|
||
685 | struct bufferevent_rate_limit_group *,
|
||
686 | const struct ev_token_bucket_cfg *); |
||
687 | |||
688 | /**
|
||
689 | Change the smallest quantum we're willing to allocate to any single
|
||
690 | bufferevent in a group for reading or writing at a time.
|
||
691 | |||
692 | The rationale is that, because of TCP/IP protocol overheads and kernel
|
||
693 | behavior, if a rate-limiting group is so tight on bandwidth that you're
|
||
694 | only willing to send 1 byte per tick per bufferevent, you might instead
|
||
695 | want to batch up the reads and writes so that you send N bytes per
|
||
696 | 1/N of the bufferevents (chosen at random) each tick, so you still wind
|
||
697 | up send 1 byte per tick per bufferevent on average, but you don't send
|
||
698 | so many tiny packets.
|
||
699 | |||
700 | The default min-share is currently 64 bytes.
|
||
701 | |||
702 | Returns 0 on success, -1 on faulre.
|
||
703 | */
|
||
704 | int bufferevent_rate_limit_group_set_min_share(
|
||
705 | struct bufferevent_rate_limit_group *, size_t);
|
||
706 | |||
707 | /**
|
||
708 | Free a rate-limiting group. The group must have no members when
|
||
709 | this function is called.
|
||
710 | */
|
||
711 | void bufferevent_rate_limit_group_free(struct bufferevent_rate_limit_group *); |
||
712 | |||
713 | /**
|
||
714 | Add 'bev' to the list of bufferevents whose aggregate reading and writing
|
||
715 | is restricted by 'g'. If 'g' is NULL, remove 'bev' from its current group.
|
||
716 | |||
717 | A bufferevent may belong to no more than one rate-limit group at a time.
|
||
718 | If 'bev' is already a member of a group, it will be removed from its old
|
||
719 | group before being added to 'g'.
|
||
720 | |||
721 | Return 0 on success and -1 on failure.
|
||
722 | */
|
||
723 | int bufferevent_add_to_rate_limit_group(struct bufferevent *bev, |
||
724 | struct bufferevent_rate_limit_group *g);
|
||
725 | |||
726 | /** Remove 'bev' from its current rate-limit group (if any). */
|
||
727 | int bufferevent_remove_from_rate_limit_group(struct bufferevent *bev); |
||
728 | |||
729 | /**
|
||
730 | @name Rate limit inspection
|
||
731 | |||
732 | Return the current read or write bucket size for a bufferevent.
|
||
733 | If it is not configured with a per-bufferevent ratelimit, return
|
||
734 | EV_SSIZE_MAX. This function does not inspect the group limit, if any.
|
||
735 | Note that it can return a negative value if the bufferevent has been
|
||
736 | made to read or write more than its limit.
|
||
737 | |||
738 | @{
|
||
739 | */
|
||
740 | ev_ssize_t bufferevent_get_read_limit(struct bufferevent *bev);
|
||
741 | ev_ssize_t bufferevent_get_write_limit(struct bufferevent *bev);
|
||
742 | /*@}*/
|
||
743 | |||
744 | ev_ssize_t bufferevent_get_max_to_read(struct bufferevent *bev);
|
||
745 | ev_ssize_t bufferevent_get_max_to_write(struct bufferevent *bev);
|
||
746 | |||
747 | /**
|
||
748 | @name Group Rate limit inspection
|
||
749 | |||
750 | Return the read or write bucket size for a bufferevent rate limit
|
||
751 | group. Note that it can return a negative value if bufferevents in
|
||
752 | the group have been made to read or write more than their limits.
|
||
753 | |||
754 | @{
|
||
755 | */
|
||
756 | ev_ssize_t bufferevent_rate_limit_group_get_read_limit( |
||
757 | struct bufferevent_rate_limit_group *);
|
||
758 | ev_ssize_t bufferevent_rate_limit_group_get_write_limit( |
||
759 | struct bufferevent_rate_limit_group *);
|
||
760 | /*@}*/
|
||
761 | |||
762 | /**
|
||
763 | @name Rate limit manipulation
|
||
764 | |||
765 | Subtract a number of bytes from a bufferevent's read or write bucket.
|
||
766 | The decrement value can be negative, if you want to manually refill
|
||
767 | the bucket. If the change puts the bucket above or below zero, the
|
||
768 | bufferevent will resume or suspend reading writing as appropriate.
|
||
769 | These functions make no change in the buckets for the bufferevent's
|
||
770 | group, if any.
|
||
771 | |||
772 | Returns 0 on success, -1 on internal error.
|
||
773 | |||
774 | @{
|
||
775 | */
|
||
776 | int bufferevent_decrement_read_limit(struct bufferevent *bev, ev_ssize_t decr); |
||
777 | int bufferevent_decrement_write_limit(struct bufferevent *bev, ev_ssize_t decr); |
||
778 | /*@}*/
|
||
779 | |||
780 | /**
|
||
781 | @name Group rate limit manipulation
|
||
782 | |||
783 | Subtract a number of bytes from a bufferevent rate-limiting group's
|
||
784 | read or write bucket. The decrement value can be negative, if you
|
||
785 | want to manually refill the bucket. If the change puts the bucket
|
||
786 | above or below zero, the bufferevents in the group will resume or
|
||
787 | suspend reading writing as appropriate.
|
||
788 | |||
789 | Returns 0 on success, -1 on internal error.
|
||
790 | |||
791 | @{
|
||
792 | */
|
||
793 | int bufferevent_rate_limit_group_decrement_read(
|
||
794 | struct bufferevent_rate_limit_group *, ev_ssize_t);
|
||
795 | int bufferevent_rate_limit_group_decrement_write(
|
||
796 | struct bufferevent_rate_limit_group *, ev_ssize_t);
|
||
797 | /*@}*/
|
||
798 | |||
799 | |||
800 | /**
|
||
801 | * Inspect the total bytes read/written on a group.
|
||
802 | *
|
||
803 | * Set the variable pointed to by total_read_out to the total number of bytes
|
||
804 | * ever read on grp, and the variable pointed to by total_written_out to the
|
||
805 | * total number of bytes ever written on grp. */
|
||
806 | void bufferevent_rate_limit_group_get_totals(
|
||
807 | struct bufferevent_rate_limit_group *grp,
|
||
808 | ev_uint64_t *total_read_out, ev_uint64_t *total_written_out); |
||
809 | |||
810 | /**
|
||
811 | * Reset the total bytes read/written on a group.
|
||
812 | *
|
||
813 | * Reset the number of bytes read or written on grp as given by
|
||
814 | * bufferevent_rate_limit_group_reset_totals(). */
|
||
815 | void
|
||
816 | bufferevent_rate_limit_group_reset_totals( |
||
817 | struct bufferevent_rate_limit_group *grp);
|
||
818 | |||
819 | #ifdef __cplusplus
|
||
820 | } |
||
821 | #endif
|
||
822 | |||
823 | #endif /* _EVENT2_BUFFEREVENT_H_ */ |