bufferevent.h 34 KB

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