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