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