Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:57:11

0001 /*
0002  * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
0003  * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
0004  *
0005  * Redistribution and use in source and binary forms, with or without
0006  * modification, are permitted provided that the following conditions
0007  * are met:
0008  * 1. Redistributions of source code must retain the above copyright
0009  *    notice, this list of conditions and the following disclaimer.
0010  * 2. Redistributions in binary form must reproduce the above copyright
0011  *    notice, this list of conditions and the following disclaimer in the
0012  *    documentation and/or other materials provided with the distribution.
0013  * 3. The name of the author may not be used to endorse or promote products
0014  *    derived from this software without specific prior written permission.
0015  *
0016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
0017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
0018  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
0019  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
0020  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
0021  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0022  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0023  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
0025  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0026  */
0027 #ifndef EVENT2_BUFFEREVENT_H_INCLUDED_
0028 #define EVENT2_BUFFEREVENT_H_INCLUDED_
0029 
0030 /**
0031    @file event2/bufferevent.h
0032 
0033   Functions for buffering data for network sending or receiving.  Bufferevents
0034   are higher level than evbuffers: each has an underlying evbuffer for reading
0035   and one for writing, and callbacks that are invoked under certain
0036   circumstances.
0037 
0038   A bufferevent provides input and output buffers that get filled and
0039   drained automatically.  The user of a bufferevent no longer deals
0040   directly with the I/O, but instead is reading from input and writing
0041   to output buffers.
0042 
0043   Once initialized, the bufferevent structure can be used repeatedly
0044   with bufferevent_enable() and bufferevent_disable().
0045 
0046   When reading is enabled, the bufferevent will try to read from the
0047   file descriptor onto its input buffer, and call the read callback.
0048   When writing is enabled, the bufferevent will try to write data onto its
0049   file descriptor when the output buffer has enough data, and call the write
0050   callback when the output buffer is sufficiently drained.
0051 
0052   Bufferevents come in several flavors, including:
0053 
0054   <dl>
0055     <dt>Socket-based bufferevents</dt>
0056       <dd>A bufferevent that reads and writes data onto a network
0057           socket. Created with bufferevent_socket_new().</dd>
0058 
0059     <dt>Paired bufferevents</dt>
0060       <dd>A pair of bufferevents that send and receive data to one
0061           another without touching the network.  Created with
0062           bufferevent_pair_new().</dd>
0063 
0064     <dt>Filtering bufferevents</dt>
0065        <dd>A bufferevent that transforms data, and sends or receives it
0066           over another underlying bufferevent.  Created with
0067           bufferevent_filter_new().</dd>
0068 
0069     <dt>SSL-backed bufferevents</dt>
0070       <dd>A bufferevent that uses the openssl library to send and
0071           receive data over an encrypted connection. Created with
0072       bufferevent_openssl_socket_new() or
0073       bufferevent_openssl_filter_new().</dd>
0074   </dl>
0075  */
0076 
0077 #include <event2/visibility.h>
0078 
0079 #ifdef __cplusplus
0080 extern "C" {
0081 #endif
0082 
0083 #include <event2/event-config.h>
0084 #ifdef EVENT__HAVE_SYS_TYPES_H
0085 #include <sys/types.h>
0086 #endif
0087 #ifdef EVENT__HAVE_SYS_TIME_H
0088 #include <sys/time.h>
0089 #endif
0090 
0091 /* For int types. */
0092 #include <event2/util.h>
0093 
0094 /** @name Bufferevent event codes
0095 
0096     These flags are passed as arguments to a bufferevent's event callback.
0097 
0098     @{
0099 */
0100 #define BEV_EVENT_READING   0x01    /**< error encountered while reading */
0101 #define BEV_EVENT_WRITING   0x02    /**< error encountered while writing */
0102 #define BEV_EVENT_EOF       0x10    /**< eof file reached */
0103 #define BEV_EVENT_ERROR     0x20    /**< unrecoverable error encountered */
0104 #define BEV_EVENT_TIMEOUT   0x40    /**< user-specified timeout reached */
0105 #define BEV_EVENT_CONNECTED 0x80    /**< connect operation finished. */
0106 /**@}*/
0107 
0108 /**
0109    An opaque type for handling buffered IO
0110 
0111    @see event2/bufferevent.h
0112  */
0113 struct bufferevent
0114 #ifdef EVENT_IN_DOXYGEN_
0115 {}
0116 #endif
0117 ;
0118 struct event_base;
0119 struct evbuffer;
0120 struct sockaddr;
0121 
0122 /**
0123    A read or write callback for a bufferevent.
0124 
0125    The read callback is triggered when new data arrives in the input
0126    buffer and the amount of readable data exceed the low watermark
0127    which is 0 by default.
0128 
0129    The write callback is triggered if the write buffer has been
0130    exhausted or fell below its low watermark.
0131 
0132    @param bev the bufferevent that triggered the callback
0133    @param ctx the user-specified context for this bufferevent
0134  */
0135 typedef void (*bufferevent_data_cb)(struct bufferevent *bev, void *ctx);
0136 
0137 /**
0138    An event/error callback for a bufferevent.
0139 
0140    The event callback is triggered if either an EOF condition or another
0141    unrecoverable error was encountered.
0142 
0143    For bufferevents with deferred callbacks, this is a bitwise OR of all errors
0144    that have happened on the bufferevent since the last callback invocation.
0145 
0146    @param bev the bufferevent for which the error condition was reached
0147    @param what a conjunction of flags: BEV_EVENT_READING or BEV_EVENT_WRITING
0148       to indicate if the error was encountered on the read or write path,
0149       and one of the following flags: BEV_EVENT_EOF, BEV_EVENT_ERROR,
0150       BEV_EVENT_TIMEOUT, BEV_EVENT_CONNECTED.
0151 
0152    @param ctx the user-specified context for this bufferevent
0153 */
0154 typedef void (*bufferevent_event_cb)(struct bufferevent *bev, short what, void *ctx);
0155 
0156 /** Options that can be specified when creating a bufferevent */
0157 enum bufferevent_options {
0158     /** If set, we close the underlying file
0159      * descriptor/bufferevent/whatever when this bufferevent is freed. */
0160     BEV_OPT_CLOSE_ON_FREE = (1<<0),
0161 
0162     /** If set, and threading is enabled, operations on this bufferevent
0163      * are protected by a lock */
0164     BEV_OPT_THREADSAFE = (1<<1),
0165 
0166     /** If set, callbacks are run deferred in the event loop. */
0167     BEV_OPT_DEFER_CALLBACKS = (1<<2),
0168 
0169     /** If set, callbacks are executed without locks being held on the
0170     * bufferevent.  This option currently requires that
0171     * BEV_OPT_DEFER_CALLBACKS also be set; a future version of Libevent
0172     * might remove the requirement.*/
0173     BEV_OPT_UNLOCK_CALLBACKS = (1<<3)
0174 };
0175 
0176 /**
0177   Create a new socket bufferevent over an existing socket.
0178 
0179   @param base the event base to associate with the new bufferevent.
0180   @param fd the file descriptor from which data is read and written to.
0181         This file descriptor is not allowed to be a pipe(2).
0182         It is safe to set the fd to -1, so long as you later
0183         set it with bufferevent_setfd or bufferevent_socket_connect().
0184   @param options Zero or more BEV_OPT_* flags
0185   @return a pointer to a newly allocated bufferevent struct, or NULL if an
0186       error occurred
0187   @see bufferevent_free()
0188   */
0189 EVENT2_EXPORT_SYMBOL
0190 struct bufferevent *bufferevent_socket_new(struct event_base *base, evutil_socket_t fd, int options);
0191 
0192 /**
0193    Launch a connect() attempt with a socket-based bufferevent.
0194 
0195    When the connect succeeds, the eventcb will be invoked with
0196    BEV_EVENT_CONNECTED set.
0197 
0198    If the bufferevent does not already have a socket set, we allocate a new
0199    socket here and make it nonblocking before we begin.
0200 
0201    If no address is provided, we assume that the socket is already connecting,
0202    and configure the bufferevent so that a BEV_EVENT_CONNECTED event will be
0203    yielded when it is done connecting.
0204 
0205    @param bufev an existing bufferevent allocated with
0206        bufferevent_socket_new().
0207    @param addr the address we should connect to
0208    @param socklen The length of the address
0209    @return 0 on success, -1 on failure.
0210  */
0211 EVENT2_EXPORT_SYMBOL
0212 int bufferevent_socket_connect(struct bufferevent *, const struct sockaddr *, int);
0213 
0214 struct evdns_base;
0215 /**
0216    Resolve the hostname 'hostname' and connect to it as with
0217    bufferevent_socket_connect().
0218 
0219    @param bufev An existing bufferevent allocated with bufferevent_socket_new()
0220    @param evdns_base Optionally, an evdns_base to use for resolving hostnames
0221       asynchronously. May be set to NULL for a blocking resolve.
0222    @param family A preferred address family to resolve addresses to, or
0223       AF_UNSPEC for no preference.  Only AF_INET, AF_INET6, and AF_UNSPEC are
0224       supported.
0225    @param hostname The hostname to resolve; see below for notes on recognized
0226       formats
0227    @param port The port to connect to on the resolved address.
0228    @return 0 if successful, -1 on failure.
0229 
0230    Recognized hostname formats are:
0231 
0232        www.example.com  (hostname)
0233        1.2.3.4      (ipv4address)
0234        ::1      (ipv6address)
0235        [::1]        ([ipv6address])
0236 
0237    Performance note: If you do not provide an evdns_base, this function
0238    may block while it waits for a DNS response.  This is probably not
0239    what you want.
0240  */
0241 EVENT2_EXPORT_SYMBOL
0242 int bufferevent_socket_connect_hostname(struct bufferevent *,
0243     struct evdns_base *, int, const char *, int);
0244 
0245 /**
0246    Return the error code for the last failed DNS lookup attempt made by
0247    bufferevent_socket_connect_hostname().
0248 
0249    @param bev The bufferevent object.
0250    @return DNS error code.
0251    @see evutil_gai_strerror()
0252 */
0253 EVENT2_EXPORT_SYMBOL
0254 int bufferevent_socket_get_dns_error(struct bufferevent *bev);
0255 
0256 /**
0257   Assign a bufferevent to a specific event_base.
0258 
0259   NOTE that only socket bufferevents support this function.
0260 
0261   @param base an event_base returned by event_init()
0262   @param bufev a bufferevent struct returned by bufferevent_new()
0263      or bufferevent_socket_new()
0264   @return 0 if successful, or -1 if an error occurred
0265   @see bufferevent_new()
0266  */
0267 EVENT2_EXPORT_SYMBOL
0268 int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
0269 
0270 /**
0271    Return the event_base used by a bufferevent
0272 */
0273 EVENT2_EXPORT_SYMBOL
0274 struct event_base *bufferevent_get_base(struct bufferevent *bev);
0275 
0276 /**
0277   Assign a priority to a bufferevent.
0278 
0279   Only supported for socket bufferevents.
0280 
0281   @param bufev a bufferevent struct
0282   @param pri the priority to be assigned
0283   @return 0 if successful, or -1 if an error occurred
0284   */
0285 EVENT2_EXPORT_SYMBOL
0286 int bufferevent_priority_set(struct bufferevent *bufev, int pri);
0287 
0288 /**
0289    Return the priority of a bufferevent.
0290 
0291    Only supported for socket bufferevents
0292  */
0293 EVENT2_EXPORT_SYMBOL
0294 int bufferevent_get_priority(const struct bufferevent *bufev);
0295 
0296 /**
0297   Deallocate the storage associated with a bufferevent structure.
0298 
0299   If there is pending data to write on the bufferevent, it probably won't be
0300   flushed before the bufferevent is freed.
0301 
0302   @param bufev the bufferevent structure to be freed.
0303   */
0304 EVENT2_EXPORT_SYMBOL
0305 void bufferevent_free(struct bufferevent *bufev);
0306 
0307 
0308 /**
0309   Changes the callbacks for a bufferevent.
0310 
0311   @param bufev the bufferevent object for which to change callbacks
0312   @param readcb callback to invoke when there is data to be read, or NULL if
0313      no callback is desired
0314   @param writecb callback to invoke when the file descriptor is ready for
0315      writing, or NULL if no callback is desired
0316   @param eventcb callback to invoke when there is an event on the file
0317      descriptor
0318   @param cbarg an argument that will be supplied to each of the callbacks
0319      (readcb, writecb, and errorcb)
0320   @see bufferevent_new()
0321   */
0322 EVENT2_EXPORT_SYMBOL
0323 void bufferevent_setcb(struct bufferevent *bufev,
0324     bufferevent_data_cb readcb, bufferevent_data_cb writecb,
0325     bufferevent_event_cb eventcb, void *cbarg);
0326 
0327 /**
0328  Retrieves the callbacks for a bufferevent.
0329 
0330  @param bufev the bufferevent to examine.
0331  @param readcb_ptr if readcb_ptr is nonnull, *readcb_ptr is set to the current
0332     read callback for the bufferevent.
0333  @param writecb_ptr if writecb_ptr is nonnull, *writecb_ptr is set to the
0334     current write callback for the bufferevent.
0335  @param eventcb_ptr if eventcb_ptr is nonnull, *eventcb_ptr is set to the
0336     current event callback for the bufferevent.
0337  @param cbarg_ptr if cbarg_ptr is nonnull, *cbarg_ptr is set to the current
0338     callback argument for the bufferevent.
0339  @see buffervent_setcb()
0340 */
0341 EVENT2_EXPORT_SYMBOL
0342 void bufferevent_getcb(struct bufferevent *bufev,
0343     bufferevent_data_cb *readcb_ptr,
0344     bufferevent_data_cb *writecb_ptr,
0345     bufferevent_event_cb *eventcb_ptr,
0346     void **cbarg_ptr);
0347 
0348 /**
0349   Changes the file descriptor on which the bufferevent operates.
0350   Not supported for all bufferevent types.
0351 
0352   @param bufev the bufferevent object for which to change the file descriptor
0353   @param fd the file descriptor to operate on
0354 */
0355 EVENT2_EXPORT_SYMBOL
0356 int bufferevent_setfd(struct bufferevent *bufev, evutil_socket_t fd);
0357 
0358 /**
0359    Returns the file descriptor associated with a bufferevent, or -1 if
0360    no file descriptor is associated with the bufferevent.
0361  */
0362 EVENT2_EXPORT_SYMBOL
0363 evutil_socket_t bufferevent_getfd(struct bufferevent *bufev);
0364 
0365 /**
0366    Returns the underlying bufferevent associated with a bufferevent (if
0367    the bufferevent is a wrapper), or NULL if there is no underlying bufferevent.
0368  */
0369 EVENT2_EXPORT_SYMBOL
0370 struct bufferevent *bufferevent_get_underlying(struct bufferevent *bufev);
0371 
0372 /**
0373   Write data to a bufferevent buffer.
0374 
0375   The bufferevent_write() function can be used to write data to the file
0376   descriptor.  The data is appended to the output buffer and written to the
0377   descriptor automatically as it becomes available for writing.
0378 
0379   @param bufev the bufferevent to be written to
0380   @param data a pointer to the data to be written
0381   @param size the length of the data, in bytes
0382   @return 0 if successful, or -1 if an error occurred
0383   @see bufferevent_write_buffer()
0384   */
0385 EVENT2_EXPORT_SYMBOL
0386 int bufferevent_write(struct bufferevent *bufev,
0387     const void *data, size_t size);
0388 
0389 
0390 /**
0391   Write data from an evbuffer to a bufferevent buffer.  The evbuffer is
0392   being drained as a result.
0393 
0394   @param bufev the bufferevent to be written to
0395   @param buf the evbuffer to be written
0396   @return 0 if successful, or -1 if an error occurred
0397   @see bufferevent_write()
0398  */
0399 EVENT2_EXPORT_SYMBOL
0400 int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
0401 
0402 
0403 /**
0404   Read data from a bufferevent buffer.
0405 
0406   The bufferevent_read() function is used to read data from the input buffer.
0407 
0408   @param bufev the bufferevent to be read from
0409   @param data pointer to a buffer that will store the data
0410   @param size the size of the data buffer, in bytes
0411   @return the amount of data read, in bytes.
0412  */
0413 EVENT2_EXPORT_SYMBOL
0414 size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
0415 
0416 /**
0417   Read data from a bufferevent buffer into an evbuffer.  This avoids
0418   memory copies.
0419 
0420   @param bufev the bufferevent to be read from
0421   @param buf the evbuffer to which to add data
0422   @return 0 if successful, or -1 if an error occurred.
0423  */
0424 EVENT2_EXPORT_SYMBOL
0425 int bufferevent_read_buffer(struct bufferevent *bufev, struct evbuffer *buf);
0426 
0427 /**
0428    Returns the input buffer.
0429 
0430    The user MUST NOT set the callback on this buffer.
0431 
0432    @param bufev the bufferevent from which to get the evbuffer
0433    @return the evbuffer object for the input buffer
0434  */
0435 
0436 EVENT2_EXPORT_SYMBOL
0437 struct evbuffer *bufferevent_get_input(struct bufferevent *bufev);
0438 
0439 /**
0440    Returns the output buffer.
0441 
0442    The user MUST NOT set the callback on this buffer.
0443 
0444    When filters are being used, the filters need to be manually
0445    triggered if the output buffer was manipulated.
0446 
0447    @param bufev the bufferevent from which to get the evbuffer
0448    @return the evbuffer object for the output buffer
0449  */
0450 
0451 EVENT2_EXPORT_SYMBOL
0452 struct evbuffer *bufferevent_get_output(struct bufferevent *bufev);
0453 
0454 /**
0455   Enable a bufferevent.
0456 
0457   @param bufev the bufferevent to be enabled
0458   @param event any combination of EV_READ | EV_WRITE.
0459   @return 0 if successful, or -1 if an error occurred
0460   @see bufferevent_disable()
0461  */
0462 EVENT2_EXPORT_SYMBOL
0463 int bufferevent_enable(struct bufferevent *bufev, short event);
0464 
0465 /**
0466   Disable a bufferevent.
0467 
0468   @param bufev the bufferevent to be disabled
0469   @param event any combination of EV_READ | EV_WRITE.
0470   @return 0 if successful, or -1 if an error occurred
0471   @see bufferevent_enable()
0472  */
0473 EVENT2_EXPORT_SYMBOL
0474 int bufferevent_disable(struct bufferevent *bufev, short event);
0475 
0476 /**
0477    Return the events that are enabled on a given bufferevent.
0478 
0479    @param bufev the bufferevent to inspect
0480    @return A combination of EV_READ | EV_WRITE
0481  */
0482 EVENT2_EXPORT_SYMBOL
0483 short bufferevent_get_enabled(struct bufferevent *bufev);
0484 
0485 /**
0486   Set the read and write timeout for a bufferevent.
0487 
0488   A bufferevent's timeout will fire the first time that the indicated
0489   amount of time has elapsed since a successful read or write operation,
0490   during which the bufferevent was trying to read or write.
0491 
0492   (In other words, if reading or writing is disabled, or if the
0493   bufferevent's read or write operation has been suspended because
0494   there's no data to write, or not enough bandwidth, or so on, the
0495   timeout isn't active.  The timeout only becomes active when we we're
0496   willing to actually read or write.)
0497 
0498   Calling bufferevent_enable or setting a timeout for a bufferevent
0499   whose timeout is already pending resets its timeout.
0500 
0501   If the timeout elapses, the corresponding operation (EV_READ or
0502   EV_WRITE) becomes disabled until you re-enable it again.  The
0503   bufferevent's event callback is called with the
0504   BEV_EVENT_TIMEOUT|BEV_EVENT_READING or
0505   BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING.
0506 
0507   @param bufev the bufferevent to be modified
0508   @param timeout_read the read timeout, or NULL
0509   @param timeout_write the write timeout, or NULL
0510  */
0511 EVENT2_EXPORT_SYMBOL
0512 int bufferevent_set_timeouts(struct bufferevent *bufev,
0513     const struct timeval *timeout_read, const struct timeval *timeout_write);
0514 
0515 /**
0516   Sets the watermarks for read and write events.
0517 
0518   On input, a bufferevent does not invoke the user read callback unless
0519   there is at least low watermark data in the buffer.   If the read buffer
0520   is beyond the high watermark, the bufferevent stops reading from the network.
0521   But be aware that bufferevent input/read buffer can overrun high watermark
0522   limit (typical example is openssl bufferevent), so you should not relay in
0523   this.
0524 
0525   On output, the user write callback is invoked whenever the buffered data
0526   falls below the low watermark.  Filters that write to this bufev will try
0527   not to write more bytes to this buffer than the high watermark would allow,
0528   except when flushing.
0529 
0530   @param bufev the bufferevent to be modified
0531   @param events EV_READ, EV_WRITE or both
0532   @param lowmark the lower watermark to set
0533   @param highmark the high watermark to set
0534 */
0535 
0536 EVENT2_EXPORT_SYMBOL
0537 void bufferevent_setwatermark(struct bufferevent *bufev, short events,
0538     size_t lowmark, size_t highmark);
0539 
0540 /**
0541   Retrieves the watermarks for read or write events.
0542   Returns non-zero if events contains not only EV_READ or EV_WRITE.
0543   Returns zero if events equal EV_READ or EV_WRITE
0544 
0545   @param bufev the bufferevent to be examined
0546   @param events EV_READ or EV_WRITE
0547   @param lowmark receives the lower watermark if not NULL
0548   @param highmark receives the high watermark if not NULL
0549 */
0550 EVENT2_EXPORT_SYMBOL
0551 int bufferevent_getwatermark(struct bufferevent *bufev, short events,
0552     size_t *lowmark, size_t *highmark);
0553 
0554 /**
0555    Acquire the lock on a bufferevent.  Has no effect if locking was not
0556    enabled with BEV_OPT_THREADSAFE.
0557  */
0558 EVENT2_EXPORT_SYMBOL
0559 void bufferevent_lock(struct bufferevent *bufev);
0560 
0561 /**
0562    Release the lock on a bufferevent.  Has no effect if locking was not
0563    enabled with BEV_OPT_THREADSAFE.
0564  */
0565 EVENT2_EXPORT_SYMBOL
0566 void bufferevent_unlock(struct bufferevent *bufev);
0567 
0568 
0569 /**
0570  * Public interface to manually increase the reference count of a bufferevent
0571  * this is useful in situations where a user may reference the bufferevent
0572  * somewhere else (unknown to libevent)
0573  *
0574  * @param bufev the bufferevent to increase the refcount on
0575  *
0576  */
0577 EVENT2_EXPORT_SYMBOL
0578 void bufferevent_incref(struct bufferevent *bufev);
0579 
0580 /**
0581  * Public interface to manually decrement the reference count of a bufferevent
0582  *
0583  * Warning: make sure you know what you're doing. This is mainly used in
0584  * conjunction with bufferevent_incref(). This will free up all data associated
0585  * with a bufferevent if the reference count hits 0.
0586  *
0587  * @param bufev the bufferevent to decrement the refcount on
0588  *
0589  * @return 1 if the bufferevent was freed, otherwise 0 (still referenced)
0590  */
0591 EVENT2_EXPORT_SYMBOL
0592 int bufferevent_decref(struct bufferevent *bufev);
0593 
0594 /**
0595    Flags that can be passed into filters to let them know how to
0596    deal with the incoming data.
0597 */
0598 enum bufferevent_flush_mode {
0599     /** usually set when processing data */
0600     BEV_NORMAL = 0,
0601 
0602     /** want to checkpoint all data sent. */
0603     BEV_FLUSH = 1,
0604 
0605     /** encountered EOF on read or done sending data */
0606     BEV_FINISHED = 2
0607 };
0608 
0609 /**
0610    Triggers the bufferevent to produce more data if possible.
0611 
0612    @param bufev the bufferevent object
0613    @param iotype either EV_READ or EV_WRITE or both.
0614    @param mode either BEV_NORMAL or BEV_FLUSH or BEV_FINISHED
0615    @return -1 on failure, 0 if no data was produces, 1 if data was produced
0616  */
0617 EVENT2_EXPORT_SYMBOL
0618 int bufferevent_flush(struct bufferevent *bufev,
0619     short iotype,
0620     enum bufferevent_flush_mode mode);
0621 
0622 /**
0623    Flags for bufferevent_trigger(_event) that modify when and how to trigger
0624    the callback.
0625 */
0626 enum bufferevent_trigger_options {
0627     /** trigger the callback regardless of the watermarks */
0628     BEV_TRIG_IGNORE_WATERMARKS = (1<<16),
0629 
0630     /** defer even if the callbacks are not */
0631     BEV_TRIG_DEFER_CALLBACKS = BEV_OPT_DEFER_CALLBACKS
0632 
0633     /* (Note: for internal reasons, these need to be disjoint from
0634      * bufferevent_options, except when they mean the same thing. */
0635 };
0636 
0637 /**
0638    Triggers bufferevent data callbacks.
0639 
0640    The function will honor watermarks unless options contain
0641    BEV_TRIG_IGNORE_WATERMARKS. If the options contain BEV_OPT_DEFER_CALLBACKS,
0642    the callbacks are deferred.
0643 
0644    @param bufev the bufferevent object
0645    @param iotype either EV_READ or EV_WRITE or both.
0646    @param options
0647  */
0648 EVENT2_EXPORT_SYMBOL
0649 void bufferevent_trigger(struct bufferevent *bufev, short iotype,
0650     int options);
0651 
0652 /**
0653    Triggers the bufferevent event callback.
0654 
0655    If the options contain BEV_OPT_DEFER_CALLBACKS, the callbacks are deferred.
0656 
0657    @param bufev the bufferevent object
0658    @param what the flags to pass onto the event callback
0659    @param options
0660  */
0661 EVENT2_EXPORT_SYMBOL
0662 void bufferevent_trigger_event(struct bufferevent *bufev, short what,
0663     int options);
0664 
0665 /**
0666    @name Filtering support
0667 
0668    @{
0669 */
0670 /**
0671    Values that filters can return.
0672  */
0673 enum bufferevent_filter_result {
0674     /** everything is okay */
0675     BEV_OK = 0,
0676 
0677     /** the filter needs to read more data before output */
0678     BEV_NEED_MORE = 1,
0679 
0680     /** the filter encountered a critical error, no further data
0681         can be processed. */
0682     BEV_ERROR = 2
0683 };
0684 
0685 /** A callback function to implement a filter for a bufferevent.
0686 
0687     @param src An evbuffer to drain data from.
0688     @param dst An evbuffer to add data to.
0689     @param limit A suggested upper bound of bytes to write to dst.
0690        The filter may ignore this value, but doing so means that
0691        it will overflow the high-water mark associated with dst.
0692        -1 means "no limit".
0693     @param mode Whether we should write data as may be convenient
0694        (BEV_NORMAL), or flush as much data as we can (BEV_FLUSH),
0695        or flush as much as we can, possibly including an end-of-stream
0696        marker (BEV_FINISH).
0697     @param ctx A user-supplied pointer.
0698 
0699     @return BEV_OK if we wrote some data; BEV_NEED_MORE if we can't
0700        produce any more output until we get some input; and BEV_ERROR
0701        on an error.
0702  */
0703 typedef enum bufferevent_filter_result (*bufferevent_filter_cb)(
0704     struct evbuffer *src, struct evbuffer *dst, ev_ssize_t dst_limit,
0705     enum bufferevent_flush_mode mode, void *ctx);
0706 
0707 /**
0708    Allocate a new filtering bufferevent on top of an existing bufferevent.
0709 
0710    @param underlying the underlying bufferevent.
0711    @param input_filter The filter to apply to data we read from the underlying
0712      bufferevent
0713    @param output_filter The filer to apply to data we write to the underlying
0714      bufferevent
0715    @param options A bitfield of bufferevent options.
0716    @param free_context A function to use to free the filter context when
0717      this bufferevent is freed.
0718    @param ctx A context pointer to pass to the filter functions.
0719  */
0720 EVENT2_EXPORT_SYMBOL
0721 struct bufferevent *
0722 bufferevent_filter_new(struct bufferevent *underlying,
0723                bufferevent_filter_cb input_filter,
0724                bufferevent_filter_cb output_filter,
0725                int options,
0726                void (*free_context)(void *),
0727                void *ctx);
0728 /**@}*/
0729 
0730 /**
0731    Allocate a pair of linked bufferevents.  The bufferevents behave as would
0732    two bufferevent_sock instances connected to opposite ends of a
0733    socketpair(), except that no internal socketpair is allocated.
0734 
0735    @param base The event base to associate with the socketpair.
0736    @param options A set of options for this bufferevent
0737    @param pair A pointer to an array to hold the two new bufferevent objects.
0738    @return 0 on success, -1 on failure.
0739  */
0740 EVENT2_EXPORT_SYMBOL
0741 int bufferevent_pair_new(struct event_base *base, int options,
0742     struct bufferevent *pair[2]);
0743 
0744 /**
0745    Given one bufferevent returned by bufferevent_pair_new(), returns the
0746    other one if it still exists.  Otherwise returns NULL.
0747  */
0748 EVENT2_EXPORT_SYMBOL
0749 struct bufferevent *bufferevent_pair_get_partner(struct bufferevent *bev);
0750 
0751 /**
0752    Abstract type used to configure rate-limiting on a bufferevent or a group
0753    of bufferevents.
0754  */
0755 struct ev_token_bucket_cfg;
0756 
0757 /**
0758    A group of bufferevents which are configured to respect the same rate
0759    limit.
0760 */
0761 struct bufferevent_rate_limit_group;
0762 
0763 /** Maximum configurable rate- or burst-limit. */
0764 #define EV_RATE_LIMIT_MAX EV_SSIZE_MAX
0765 
0766 /**
0767    Initialize and return a new object to configure the rate-limiting behavior
0768    of bufferevents.
0769 
0770    @param read_rate The maximum number of bytes to read per tick on
0771      average.
0772    @param read_burst The maximum number of bytes to read in any single tick.
0773    @param write_rate The maximum number of bytes to write per tick on
0774      average.
0775    @param write_burst The maximum number of bytes to write in any single tick.
0776    @param tick_len The length of a single tick.  Defaults to one second.
0777      Any fractions of a millisecond are ignored.
0778 
0779    Note that all rate-limits hare are currently best-effort: future versions
0780    of Libevent may implement them more tightly.
0781  */
0782 EVENT2_EXPORT_SYMBOL
0783 struct ev_token_bucket_cfg *ev_token_bucket_cfg_new(
0784     size_t read_rate, size_t read_burst,
0785     size_t write_rate, size_t write_burst,
0786     const struct timeval *tick_len);
0787 
0788 /** Free all storage held in 'cfg'.
0789 
0790     Note: 'cfg' is not currently reference-counted; it is not safe to free it
0791     until no bufferevent is using it.
0792  */
0793 EVENT2_EXPORT_SYMBOL
0794 void ev_token_bucket_cfg_free(struct ev_token_bucket_cfg *cfg);
0795 
0796 /**
0797    Set the rate-limit of a the bufferevent 'bev' to the one specified in
0798    'cfg'.  If 'cfg' is NULL, disable any per-bufferevent rate-limiting on
0799    'bev'.
0800 
0801    Note that only some bufferevent types currently respect rate-limiting.
0802    They are: socket-based bufferevents (normal and IOCP-based), and SSL-based
0803    bufferevents.
0804 
0805    Return 0 on success, -1 on failure.
0806  */
0807 EVENT2_EXPORT_SYMBOL
0808 int bufferevent_set_rate_limit(struct bufferevent *bev,
0809     struct ev_token_bucket_cfg *cfg);
0810 
0811 /**
0812    Create a new rate-limit group for bufferevents.  A rate-limit group
0813    constrains the maximum number of bytes sent and received, in toto,
0814    by all of its bufferevents.
0815 
0816    @param base An event_base to run any necessary timeouts for the group.
0817       Note that all bufferevents in the group do not necessarily need to share
0818       this event_base.
0819    @param cfg The rate-limit for this group.
0820 
0821    Note that all rate-limits hare are currently best-effort: future versions
0822    of Libevent may implement them more tightly.
0823 
0824    Note also that only some bufferevent types currently respect rate-limiting.
0825    They are: socket-based bufferevents (normal and IOCP-based), and SSL-based
0826    bufferevents.
0827  */
0828 EVENT2_EXPORT_SYMBOL
0829 struct bufferevent_rate_limit_group *bufferevent_rate_limit_group_new(
0830     struct event_base *base,
0831     const struct ev_token_bucket_cfg *cfg);
0832 /**
0833    Change the rate-limiting settings for a given rate-limiting group.
0834 
0835    Return 0 on success, -1 on failure.
0836 */
0837 EVENT2_EXPORT_SYMBOL
0838 int bufferevent_rate_limit_group_set_cfg(
0839     struct bufferevent_rate_limit_group *,
0840     const struct ev_token_bucket_cfg *);
0841 
0842 /**
0843    Change the smallest quantum we're willing to allocate to any single
0844    bufferevent in a group for reading or writing at a time.
0845 
0846    The rationale is that, because of TCP/IP protocol overheads and kernel
0847    behavior, if a rate-limiting group is so tight on bandwidth that you're
0848    only willing to send 1 byte per tick per bufferevent, you might instead
0849    want to batch up the reads and writes so that you send N bytes per
0850    1/N of the bufferevents (chosen at random) each tick, so you still wind
0851    up send 1 byte per tick per bufferevent on average, but you don't send
0852    so many tiny packets.
0853 
0854    The default min-share is currently 64 bytes.
0855 
0856    Returns 0 on success, -1 on failure.
0857  */
0858 EVENT2_EXPORT_SYMBOL
0859 int bufferevent_rate_limit_group_set_min_share(
0860     struct bufferevent_rate_limit_group *, size_t);
0861 
0862 /**
0863    Free a rate-limiting group.  The group must have no members when
0864    this function is called.
0865 */
0866 EVENT2_EXPORT_SYMBOL
0867 void bufferevent_rate_limit_group_free(struct bufferevent_rate_limit_group *);
0868 
0869 /**
0870    Add 'bev' to the list of bufferevents whose aggregate reading and writing
0871    is restricted by 'g'.  If 'g' is NULL, remove 'bev' from its current group.
0872 
0873    A bufferevent may belong to no more than one rate-limit group at a time.
0874    If 'bev' is already a member of a group, it will be removed from its old
0875    group before being added to 'g'.
0876 
0877    Return 0 on success and -1 on failure.
0878  */
0879 EVENT2_EXPORT_SYMBOL
0880 int bufferevent_add_to_rate_limit_group(struct bufferevent *bev,
0881     struct bufferevent_rate_limit_group *g);
0882 
0883 /** Remove 'bev' from its current rate-limit group (if any). */
0884 EVENT2_EXPORT_SYMBOL
0885 int bufferevent_remove_from_rate_limit_group(struct bufferevent *bev);
0886 
0887 /**
0888    Set the size limit for single read operation.
0889 
0890    Set to 0 for a reasonable default.
0891 
0892    Return 0 on success and -1 on failure.
0893  */
0894 EVENT2_EXPORT_SYMBOL
0895 int bufferevent_set_max_single_read(struct bufferevent *bev, size_t size);
0896 
0897 /**
0898    Set the size limit for single write operation.
0899 
0900    Set to 0 for a reasonable default.
0901 
0902    Return 0 on success and -1 on failure.
0903  */
0904 EVENT2_EXPORT_SYMBOL
0905 int bufferevent_set_max_single_write(struct bufferevent *bev, size_t size);
0906 
0907 /** Get the current size limit for single read operation. */
0908 EVENT2_EXPORT_SYMBOL
0909 ev_ssize_t bufferevent_get_max_single_read(struct bufferevent *bev);
0910 
0911 /** Get the current size limit for single write operation. */
0912 EVENT2_EXPORT_SYMBOL
0913 ev_ssize_t bufferevent_get_max_single_write(struct bufferevent *bev);
0914 
0915 /**
0916    @name Rate limit inspection
0917 
0918    Return the current read or write bucket size for a bufferevent.
0919    If it is not configured with a per-bufferevent ratelimit, return
0920    EV_SSIZE_MAX.  This function does not inspect the group limit, if any.
0921    Note that it can return a negative value if the bufferevent has been
0922    made to read or write more than its limit.
0923 
0924    @{
0925  */
0926 EVENT2_EXPORT_SYMBOL
0927 ev_ssize_t bufferevent_get_read_limit(struct bufferevent *bev);
0928 EVENT2_EXPORT_SYMBOL
0929 ev_ssize_t bufferevent_get_write_limit(struct bufferevent *bev);
0930 /*@}*/
0931 
0932 EVENT2_EXPORT_SYMBOL
0933 ev_ssize_t bufferevent_get_max_to_read(struct bufferevent *bev);
0934 EVENT2_EXPORT_SYMBOL
0935 ev_ssize_t bufferevent_get_max_to_write(struct bufferevent *bev);
0936 
0937 EVENT2_EXPORT_SYMBOL
0938 const struct ev_token_bucket_cfg *bufferevent_get_token_bucket_cfg(const struct bufferevent * bev);
0939 
0940 /**
0941    @name Group Rate limit inspection
0942 
0943    Return the read or write bucket size for a bufferevent rate limit
0944    group.  Note that it can return a negative value if bufferevents in
0945    the group have been made to read or write more than their limits.
0946 
0947    @{
0948  */
0949 EVENT2_EXPORT_SYMBOL
0950 ev_ssize_t bufferevent_rate_limit_group_get_read_limit(
0951     struct bufferevent_rate_limit_group *);
0952 EVENT2_EXPORT_SYMBOL
0953 ev_ssize_t bufferevent_rate_limit_group_get_write_limit(
0954     struct bufferevent_rate_limit_group *);
0955 /*@}*/
0956 
0957 /**
0958    @name Rate limit manipulation
0959 
0960    Subtract a number of bytes from a bufferevent's read or write bucket.
0961    The decrement value can be negative, if you want to manually refill
0962    the bucket.  If the change puts the bucket above or below zero, the
0963    bufferevent will resume or suspend reading writing as appropriate.
0964    These functions make no change in the buckets for the bufferevent's
0965    group, if any.
0966 
0967    Returns 0 on success, -1 on internal error.
0968 
0969    @{
0970  */
0971 EVENT2_EXPORT_SYMBOL
0972 int bufferevent_decrement_read_limit(struct bufferevent *bev, ev_ssize_t decr);
0973 EVENT2_EXPORT_SYMBOL
0974 int bufferevent_decrement_write_limit(struct bufferevent *bev, ev_ssize_t decr);
0975 /*@}*/
0976 
0977 /**
0978    @name Group rate limit manipulation
0979 
0980    Subtract a number of bytes from a bufferevent rate-limiting group's
0981    read or write bucket.  The decrement value can be negative, if you
0982    want to manually refill the bucket.  If the change puts the bucket
0983    above or below zero, the bufferevents in the group will resume or
0984    suspend reading writing as appropriate.
0985 
0986    Returns 0 on success, -1 on internal error.
0987 
0988    @{
0989  */
0990 EVENT2_EXPORT_SYMBOL
0991 int bufferevent_rate_limit_group_decrement_read(
0992     struct bufferevent_rate_limit_group *, ev_ssize_t);
0993 EVENT2_EXPORT_SYMBOL
0994 int bufferevent_rate_limit_group_decrement_write(
0995     struct bufferevent_rate_limit_group *, ev_ssize_t);
0996 /*@}*/
0997 
0998 
0999 /**
1000  * Inspect the total bytes read/written on a group.
1001  *
1002  * Set the variable pointed to by total_read_out to the total number of bytes
1003  * ever read on grp, and the variable pointed to by total_written_out to the
1004  * total number of bytes ever written on grp. */
1005 EVENT2_EXPORT_SYMBOL
1006 void bufferevent_rate_limit_group_get_totals(
1007     struct bufferevent_rate_limit_group *grp,
1008     ev_uint64_t *total_read_out, ev_uint64_t *total_written_out);
1009 
1010 /**
1011  * Reset the total bytes read/written on a group.
1012  *
1013  * Reset the number of bytes read or written on grp as given by
1014  * bufferevent_rate_limit_group_reset_totals(). */
1015 EVENT2_EXPORT_SYMBOL
1016 void
1017 bufferevent_rate_limit_group_reset_totals(
1018     struct bufferevent_rate_limit_group *grp);
1019 
1020 #ifdef __cplusplus
1021 }
1022 #endif
1023 
1024 #endif /* EVENT2_BUFFEREVENT_H_INCLUDED_ */