1 /* 2 * Copyright (c) 2007-2011 Niels Provos and Nick Mathewson 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 3. The name of the author may not be used to endorse or promote products 13 * derived from this software without specific prior written permission. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 /** @file event2/buffer.h 28 29 Functions for buffering data for network sending or receiving. 30 31 An evbuffer can be used for preparing data before sending it to 32 the network or conversely for reading data from the network. 33 Evbuffers try to avoid memory copies as much as possible. As a 34 result, evbuffers can be used to pass data around without actually 35 incurring the overhead of copying the data. 36 37 A new evbuffer can be allocated with evbuffer_new(), and can be 38 freed with evbuffer_free(). Most users will be using evbuffers via 39 the bufferevent interface. To access a bufferevent's evbuffers, use 40 bufferevent_get_input() and bufferevent_get_output(). 41 42 There are several guidelines for using evbuffers. 43 44 - if you already know how much data you are going to add as a result 45 of calling evbuffer_add() multiple times, it makes sense to use 46 evbuffer_expand() first to make sure that enough memory is allocated 47 before hand. 48 49 - evbuffer_add_buffer() adds the contents of one buffer to the other 50 without incurring any unnecessary memory copies. 51 52 - evbuffer_add() and evbuffer_add_buffer() do not mix very well: 53 if you use them, you will wind up with fragmented memory in your 54 buffer. 55 56 - For high-performance code, you may want to avoid copying data into and out 57 of buffers. You can skip the copy step by using 58 evbuffer_reserve_space()/evbuffer_commit_space() when writing into a 59 buffer, and evbuffer_peek() when reading. 60 61 In Libevent 2.0 and later, evbuffers are represented using a linked 62 list of memory chunks, with pointers to the first and last chunk in 63 the chain. 64 65 As the contents of an evbuffer can be stored in multiple different 66 memory blocks, it cannot be accessed directly. Instead, evbuffer_pullup() 67 can be used to force a specified number of bytes to be contiguous. This 68 will cause memory reallocation and memory copies if the data is split 69 across multiple blocks. It is more efficient, however, to use 70 evbuffer_peek() if you don't require that the memory to be contiguous. 71 */ 72 module deimos.event2.buffer; 73 74 import core.stdc.stdarg; 75 import core.sys.posix.sys.uio; 76 import deimos.event2._d_util; 77 public import deimos.event2.util; 78 public import deimos.event2.event_struct; 79 80 extern (C): 81 nothrow: 82 83 /** 84 An evbuffer is an opaque data type for efficiently buffering data to be 85 sent or received on the network. 86 87 @see event2/event.h for more information 88 */ 89 alias void evbuffer; 90 91 /** 92 Pointer to a position within an evbuffer. 93 94 Used when repeatedly searching through a buffer. Calling any function 95 that modifies or re-packs the buffer contents may invalidate all 96 evbuffer_ptrs for that buffer. Do not modify these values except with 97 evbuffer_ptr_set. 98 */ 99 struct evbuffer_ptr { 100 ev_ssize_t pos; 101 102 /* Do not alter the values of fields. */ 103 struct _internal_ { 104 void* chain; 105 size_t pos_in_chain; 106 } 107 _internal_ _internal; 108 }; 109 110 /** Describes a single extent of memory inside an evbuffer. Used for 111 direct-access functions. 112 113 @see evbuffer_reserve_space, evbuffer_commit_space, evbuffer_peek 114 */ 115 static if (is(iovec)) { 116 alias iovec evbuffer_iovec; 117 } else { 118 struct evbuffer_iovec { 119 /** The start of the extent of memory. */ 120 void* iov_base; 121 /** The length of the extent of memory. */ 122 size_t iov_len; 123 } 124 } 125 126 /** 127 Allocate storage for a new evbuffer. 128 129 @return a pointer to a newly allocated evbuffer struct, or NULL if an error 130 occurred 131 */ 132 evbuffer* evbuffer_new(); 133 /** 134 Deallocate storage for an evbuffer. 135 136 @param buf pointer to the evbuffer to be freed 137 */ 138 void evbuffer_free(evbuffer* buf); 139 140 /** 141 Enable locking on an evbuffer so that it can safely be used by multiple 142 threads at the same time. 143 144 NOTE: when locking is enabled, the lock will be held when callbacks are 145 invoked. This could result in deadlock if you aren't careful. Plan 146 accordingly! 147 148 @param buf An evbuffer to make lockable. 149 @param lock A lock object, or NULL if we should allocate our own. 150 @return 0 on success, -1 on failure. 151 */ 152 int evbuffer_enable_locking(evbuffer* buf, void* lock); 153 154 /** 155 Acquire the lock on an evbuffer. Has no effect if locking was not enabled 156 with evbuffer_enable_locking. 157 */ 158 void evbuffer_lock(evbuffer* buf); 159 160 /** 161 Release the lock on an evbuffer. Has no effect if locking was not enabled 162 with evbuffer_enable_locking. 163 */ 164 void evbuffer_unlock(evbuffer* buf); 165 166 167 /** If this flag is set, then we will not use evbuffer_peek(), 168 * evbuffer_remove(), evbuffer_remove_buffer(), and so on to read bytes 169 * from this buffer: we'll only take bytes out of this buffer by 170 * writing them to the network (as with evbuffer_write_atmost), by 171 * removing them without observing them (as with evbuffer_drain_), 172 * or by copying them all out at once (as with evbuffer_add_buffer). 173 * 174 * Using this option allows the implementation to use sendfile-based 175 * operations for evbuffer_add_file(); see that function for more 176 * information. 177 * 178 * This flag is on by default for bufferevents that can take advantage 179 * of it; you should never actually need to set it on a bufferevent's 180 * output buffer. 181 */ 182 enum EVBUFFER_FLAG_DRAINS_TO_FD = 1; 183 184 /** Change the flags that are set for an evbuffer by adding more. 185 * 186 * @param buffer the evbuffer that the callback is watching. 187 * @param cb the callback whose status we want to change. 188 * @param flags One or more EVBUFFER_FLAG_* options 189 * @return 0 on success, -1 on failure. 190 */ 191 int evbuffer_set_flags(evbuffer* buf, ev_uint64_t flags); 192 /** Change the flags that are set for an evbuffer by removing some. 193 * 194 * @param buffer the evbuffer that the callback is watching. 195 * @param cb the callback whose status we want to change. 196 * @param flags One or more EVBUFFER_FLAG_* options 197 * @return 0 on success, -1 on failure. 198 */ 199 int evbuffer_clear_flags(evbuffer* buf, ev_uint64_t flags); 200 201 /** 202 Returns the total number of bytes stored in the evbuffer 203 204 @param buf pointer to the evbuffer 205 @return the number of bytes stored in the evbuffer 206 */ 207 size_t evbuffer_get_length(const(evbuffer)* buf); 208 209 /** 210 Returns the number of contiguous available bytes in the first buffer chain. 211 212 This is useful when processing data that might be split into multiple 213 chains, or that might all be in the first chain. Calls to 214 evbuffer_pullup() that cause reallocation and copying of data can thus be 215 avoided. 216 217 @param buf pointer to the evbuffer 218 @return 0 if no data is available, otherwise the number of available bytes 219 in the first buffer chain. 220 */ 221 size_t evbuffer_get_contiguous_space(const(evbuffer)* buf); 222 223 /** 224 Expands the available space in an evbuffer. 225 226 Expands the available space in the evbuffer to at least datlen, so that 227 appending datlen additional bytes will not require any new allocations. 228 229 @param buf the evbuffer to be expanded 230 @param datlen the new minimum length requirement 231 @return 0 if successful, or -1 if an error occurred 232 */ 233 int evbuffer_expand(evbuffer* buf, size_t datlen); 234 235 /** 236 Reserves space in the last chain or chains of an evbuffer. 237 238 Makes space available in the last chain or chains of an evbuffer that can 239 be arbitrarily written to by a user. The space does not become 240 available for reading until it has been committed with 241 evbuffer_commit_space(). 242 243 The space is made available as one or more extents, represented by 244 an initial pointer and a length. You can force the memory to be 245 available as only one extent. Allowing more extents, however, makes the 246 function more efficient. 247 248 Multiple subsequent calls to this function will make the same space 249 available until evbuffer_commit_space() has been called. 250 251 It is an error to do anything that moves around the buffer's internal 252 memory structures before committing the space. 253 254 NOTE: The code currently does not ever use more than two extents. 255 This may change in future versions. 256 257 @param buf the evbuffer in which to reserve space. 258 @param size how much space to make available, at minimum. The 259 total length of the extents may be greater than the requested 260 length. 261 @param vec an array of one or more evbuffer_iovec structures to 262 hold pointers to the reserved extents of memory. 263 @param n_vec The length of the vec array. Must be at least 1; 264 2 is more efficient. 265 @return the number of provided extents, or -1 on error. 266 @see evbuffer_commit_space() 267 */ 268 int 269 evbuffer_reserve_space(evbuffer* buf, ev_ssize_t size, 270 evbuffer_iovec* vec, int n_vec); 271 272 /** 273 Commits previously reserved space. 274 275 Commits some of the space previously reserved with 276 evbuffer_reserve_space(). It then becomes available for reading. 277 278 This function may return an error if the pointer in the extents do 279 not match those returned from evbuffer_reserve_space, or if data 280 has been added to the buffer since the space was reserved. 281 282 If you want to commit less data than you got reserved space for, 283 modify the iov_len pointer of the appropriate extent to a smaller 284 value. Note that you may have received more space than you 285 requested if it was available! 286 287 @param buf the evbuffer in which to reserve space. 288 @param vec one or two extents returned by evbuffer_reserve_space. 289 @param n_vecs the number of extents. 290 @return 0 on success, -1 on error 291 @see evbuffer_reserve_space() 292 */ 293 int evbuffer_commit_space(evbuffer* buf, 294 evbuffer_iovec* vec, int n_vecs); 295 296 /** 297 Append data to the end of an evbuffer. 298 299 @param buf the evbuffer to be appended to 300 @param data pointer to the beginning of the data buffer 301 @param datlen the number of bytes to be copied from the data buffer 302 @return 0 on success, -1 on failure. 303 */ 304 int evbuffer_add(evbuffer* buf, const(void)* data, size_t datlen); 305 306 307 /** 308 Read data from an evbuffer and drain the bytes read. 309 310 If more bytes are requested than are available in the evbuffer, we 311 only extract as many bytes as were available. 312 313 @param buf the evbuffer to be read from 314 @param data the destination buffer to store the result 315 @param datlen the maximum size of the destination buffer 316 @return the number of bytes read, or -1 if we can't drain the buffer. 317 */ 318 int evbuffer_remove(evbuffer* buf, void* data, size_t datlen); 319 320 /** 321 Read data from an evbuffer, and leave the buffer unchanged. 322 323 If more bytes are requested than are available in the evbuffer, we 324 only extract as many bytes as were available. 325 326 @param buf the evbuffer to be read from 327 @param data_out the destination buffer to store the result 328 @param datlen the maximum size of the destination buffer 329 @return the number of bytes read, or -1 if we can't drain the buffer. 330 */ 331 ev_ssize_t evbuffer_copyout(evbuffer* buf, void* data_out, size_t datlen); 332 333 /** 334 Read data from an evbuffer into another evbuffer, draining 335 the bytes from the source buffer. This function avoids copy 336 operations to the extent possible. 337 338 If more bytes are requested than are available in src, the src 339 buffer is drained completely. 340 341 @param src the evbuffer to be read from 342 @param dst the destination evbuffer to store the result into 343 @param datlen the maximum numbers of bytes to transfer 344 @return the number of bytes read 345 */ 346 int evbuffer_remove_buffer(evbuffer* src, evbuffer* dst, 347 size_t datlen); 348 349 /** Used to tell evbuffer_readln what kind of line-ending to look for. 350 */ 351 enum evbuffer_eol_style { 352 /** Any sequence of CR and LF characters is acceptable as an 353 * EOL. 354 * 355 * Note that this style can produce ambiguous results: the 356 * sequence "CRLF" will be treated as a single EOL if it is 357 * all in the buffer at once, but if you first read a CR from 358 * the network and later read an LF from the network, it will 359 * be treated as two EOLs. 360 */ 361 EVBUFFER_EOL_ANY, 362 /** An EOL is an LF, optionally preceded by a CR. This style is 363 * most useful for implementing text-based internet protocols. */ 364 EVBUFFER_EOL_CRLF, 365 /** An EOL is a CR followed by an LF. */ 366 EVBUFFER_EOL_CRLF_STRICT, 367 /** An EOL is a LF. */ 368 EVBUFFER_EOL_LF 369 }; 370 371 /** 372 * Read a single line from an evbuffer. 373 * 374 * Reads a line terminated by an EOL as determined by the evbuffer_eol_style 375 * argument. Returns a newly allocated nul-terminated string; the caller must 376 * free the returned value. The EOL is not included in the returned string. 377 * 378 * @param buffer the evbuffer to read from 379 * @param n_read_out if non-NULL, points to a size_t that is set to the 380 * number of characters in the returned string. This is useful for 381 * strings that can contain NUL characters. 382 * @param eol_style the style of line-ending to use. 383 * @return pointer to a single line, or NULL if an error occurred 384 */ 385 char* evbuffer_readln(evbuffer* buffer, size_t* n_read_out, 386 evbuffer_eol_style eol_style); 387 388 /** 389 Move all data from one evbuffer into another evbuffer. 390 391 This is a destructive add. The data from one buffer moves into 392 the other buffer. However, no unnecessary memory copies occur. 393 394 @param outbuf the output buffer 395 @param inbuf the input buffer 396 @return 0 if successful, or -1 if an error occurred 397 398 @see evbuffer_remove_buffer() 399 */ 400 int evbuffer_add_buffer(evbuffer* outbuf, evbuffer* inbuf); 401 402 /** 403 A cleanup function for a piece of memory added to an evbuffer by 404 reference. 405 406 @see evbuffer_add_reference() 407 */ 408 alias ExternC!(void function(const(void)* data, 409 size_t datalen, void* extra)) evbuffer_ref_cleanup_cb; 410 411 /** 412 Reference memory into an evbuffer without copying. 413 414 The memory needs to remain valid until all the added data has been 415 read. This function keeps just a reference to the memory without 416 actually incurring the overhead of a copy. 417 418 @param outbuf the output buffer 419 @param data the memory to reference 420 @param datlen how memory to reference 421 @param cleanupfn callback to be invoked when the memory is no longer 422 referenced by this evbuffer. 423 @param cleanupfn_arg optional argument to the cleanup callback 424 @return 0 if successful, or -1 if an error occurred 425 */ 426 int evbuffer_add_reference(evbuffer* outbuf, 427 const(void)* data, size_t datlen, 428 evbuffer_ref_cleanup_cb cleanupfn, void* cleanupfn_arg); 429 430 /** 431 Copy data from a file into the evbuffer for writing to a socket. 432 433 This function avoids unnecessary data copies between userland and 434 kernel. If sendfile is available and the EVBUFFER_FLAG_DRAINS_TO_FD 435 flag is set, it uses those functions. Otherwise, it tries to use 436 mmap (or CreateFileMapping on Windows). 437 438 The function owns the resulting file descriptor and will close it 439 when finished transferring data. 440 441 The results of using evbuffer_remove() or evbuffer_pullup() on 442 evbuffers whose data was added using this function are undefined. 443 444 @param outbuf the output buffer 445 @param fd the file descriptor 446 @param offset the offset from which to read data 447 @param length how much data to read 448 @return 0 if successful, or -1 if an error occurred 449 */ 450 451 int evbuffer_add_file(evbuffer* outbuf, int fd, ev_off_t offset, 452 ev_off_t length); 453 454 /** 455 Append a formatted string to the end of an evbuffer. 456 457 The string is formated as printf. 458 459 @param buf the evbuffer that will be appended to 460 @param fmt a format string 461 @param ... arguments that will be passed to printf(3) 462 @return The number of bytes added if successful, or -1 if an error occurred. 463 464 @see evutil_printf(), evbuffer_add_vprintf() 465 */ 466 int evbuffer_add_printf(evbuffer* buf, const(char)* fmt, ...) 467 ; 468 469 /** 470 Append a va_list formatted string to the end of an evbuffer. 471 472 @param buf the evbuffer that will be appended to 473 @param fmt a format string 474 @param ap a varargs va_list argument array that will be passed to vprintf(3) 475 @return The number of bytes added if successful, or -1 if an error occurred. 476 */ 477 int evbuffer_add_vprintf(evbuffer* buf, const(char)* fmt, va_list ap); 478 479 480 /** 481 Remove a specified number of bytes data from the beginning of an evbuffer. 482 483 @param buf the evbuffer to be drained 484 @param len the number of bytes to drain from the beginning of the buffer 485 @return 0 on success, -1 on failure. 486 */ 487 int evbuffer_drain(evbuffer* buf, size_t len); 488 489 490 /** 491 Write the contents of an evbuffer to a file descriptor. 492 493 The evbuffer will be drained after the bytes have been successfully written. 494 495 @param buffer the evbuffer to be written and drained 496 @param fd the file descriptor to be written to 497 @return the number of bytes written, or -1 if an error occurred 498 @see evbuffer_read() 499 */ 500 int evbuffer_write(evbuffer* buffer, evutil_socket_t fd); 501 502 /** 503 Write some of the contents of an evbuffer to a file descriptor. 504 505 The evbuffer will be drained after the bytes have been successfully written. 506 507 @param buffer the evbuffer to be written and drained 508 @param fd the file descriptor to be written to 509 @param howmuch the largest allowable number of bytes to write, or -1 510 to write as many bytes as we can. 511 @return the number of bytes written, or -1 if an error occurred 512 @see evbuffer_read() 513 */ 514 int evbuffer_write_atmost(evbuffer* buffer, evutil_socket_t fd, 515 ev_ssize_t howmuch); 516 517 /** 518 Read from a file descriptor and store the result in an evbuffer. 519 520 @param buffer the evbuffer to store the result 521 @param fd the file descriptor to read from 522 @param howmuch the number of bytes to be read 523 @return the number of bytes read, or -1 if an error occurred 524 @see evbuffer_write() 525 */ 526 int evbuffer_read(evbuffer* buffer, evutil_socket_t fd, int howmuch); 527 528 /** 529 Search for a string within an evbuffer. 530 531 @param buffer the evbuffer to be searched 532 @param what the string to be searched for 533 @param len the length of the search string 534 @param start NULL or a pointer to a valid struct evbuffer_ptr. 535 @return a evbuffer_ptr whose 'pos' field has the offset of the 536 first occurrence of the string in the buffer after 'start'. The 'pos' 537 field of the result is -1 if the string was not found. 538 */ 539 evbuffer_ptr evbuffer_search(evbuffer* buffer, const(char)* what, size_t len, const(evbuffer_ptr)* start); 540 541 /** 542 Search for a string within part of an evbuffer. 543 544 @param buffer the evbuffer to be searched 545 @param what the string to be searched for 546 @param len the length of the search string 547 @param start NULL or a pointer to a valid evbuffer_ptr that 548 indicates where we should start searching. 549 @param end NULL or a pointer to a valid evbuffer_ptr that 550 indicates where we should stop searching. 551 @return a evbuffer_ptr whose 'pos' field has the offset of the 552 first occurrence of the string in the buffer after 'start'. The 'pos' 553 field of the result is -1 if the string was not found. 554 */ 555 evbuffer_ptr evbuffer_search_range(evbuffer* buffer, const(char)* what, size_t len, const(evbuffer_ptr)* start, const(evbuffer_ptr)* end); 556 557 /** 558 Defines how to adjust an evbuffer_ptr by evbuffer_ptr_set() 559 560 @see evbuffer_ptr_set() */ 561 enum evbuffer_ptr_how { 562 /** Sets the pointer to the position; can be called on with an 563 uninitialized evbuffer_ptr. */ 564 EVBUFFER_PTR_SET, 565 /** Advances the pointer by adding to the current position. */ 566 EVBUFFER_PTR_ADD 567 }; 568 569 /** 570 Sets the search pointer in the buffer to position. 571 572 If evbuffer_ptr is not initialized. This function can only be called 573 with EVBUFFER_PTR_SET. 574 575 @param buffer the evbuffer to be search 576 @param ptr a pointer to a struct evbuffer_ptr 577 @param position the position at which to start the next search 578 @param how determines how the pointer should be manipulated. 579 @returns 0 on success or -1 otherwise 580 */ 581 int 582 evbuffer_ptr_set(evbuffer* buffer, evbuffer_ptr* ptr, 583 size_t position, evbuffer_ptr_how how); 584 585 /** 586 Search for an end-of-line string within an evbuffer. 587 588 @param buffer the evbuffer to be searched 589 @param start NULL or a pointer to a valid evbuffer_ptr to start 590 searching at. 591 @param eol_len_out If non-NULL, the pointed-to value will be set to 592 the length of the end-of-line string. 593 @param eol_style The kind of EOL to look for; see evbuffer_readln() for 594 more information 595 @return a evbuffer_ptr whose 'pos' field has the offset of the 596 first occurrence EOL in the buffer after 'start'. The 'pos' 597 field of the result is -1 if the string was not found. 598 */ 599 evbuffer_ptr evbuffer_search_eol(evbuffer* buffer, 600 evbuffer_ptr* start, size_t* eol_len_out, 601 evbuffer_eol_style eol_style); 602 603 /** Function to peek at data inside an evbuffer without removing it or 604 copying it out. 605 606 Pointers to the data are returned by filling the 'vec_out' array 607 with pointers to one or more extents of data inside the buffer. 608 609 The total data in the extents that you get back may be more than 610 you requested (if there is more data last extent than you asked 611 for), or less (if you do not provide enough evbuffer_iovecs, or if 612 the buffer does not have as much data as you asked to see). 613 614 @param buffer the evbuffer to peek into, 615 @param len the number of bytes to try to peek. If negative, we 616 will try to fill as much of vec_out as we can. 617 @param start_at an evbuffer_ptr indicating the point at which we 618 should start looking for data. NULL means, "At the start of the 619 buffer." 620 @param vec_out an array of evbuffer_iovec 621 @param n_vec the length of vec_out. If 0, we only count how many 622 extents would be necessary to point to the requested amount of 623 data. 624 @return The number of extents needed. This may be less than n_vec 625 if we didn't need all the evbuffer_iovecs we were given, or more 626 than n_vec if we would need more to return all the data that was 627 requested. 628 */ 629 int evbuffer_peek(evbuffer* buffer, ev_ssize_t len, 630 evbuffer_ptr* start_at, 631 evbuffer_iovec* vec_out, int n_vec); 632 633 634 /** Structure passed to an evbuffer_cb_func evbuffer callback 635 636 @see evbuffer_cb_func, evbuffer_add_cb() 637 */ 638 struct evbuffer_cb_info { 639 /** The number of bytes in this evbuffer when callbacks were last 640 * invoked. */ 641 size_t orig_size; 642 /** The number of bytes added since callbacks were last invoked. */ 643 size_t n_added; 644 /** The number of bytes removed since callbacks were last invoked. */ 645 size_t n_deleted; 646 }; 647 648 /** Type definition for a callback that is invoked whenever data is added or 649 removed from an evbuffer. 650 651 An evbuffer may have one or more callbacks set at a time. The order 652 in which they are executed is undefined. 653 654 A callback function may add more callbacks, or remove itself from the 655 list of callbacks, or add or remove data from the buffer. It may not 656 remove another callback from the list. 657 658 If a callback adds or removes data from the buffer or from another 659 buffer, this can cause a recursive invocation of your callback or 660 other callbacks. If you ask for an infinite loop, you might just get 661 one: watch out! 662 663 @param buffer the buffer whose size has changed 664 @param info a structure describing how the buffer changed. 665 @param arg a pointer to user data 666 */ 667 alias ExternC!(void function(evbuffer* buffer, const(evbuffer_cb_info)* info, void* arg)) evbuffer_cb_func; 668 669 alias void evbuffer_cb_entry; 670 /** Add a new callback to an evbuffer. 671 672 Subsequent calls to evbuffer_add_cb() add new callbacks. To remove this 673 callback, call evbuffer_remove_cb or evbuffer_remove_cb_entry. 674 675 @param buffer the evbuffer to be monitored 676 @param cb the callback function to invoke when the evbuffer is modified, 677 or NULL to remove all callbacks. 678 @param cbarg an argument to be provided to the callback function 679 @return a handle to the callback on success, or NULL on failure. 680 */ 681 evbuffer_cb_entry* evbuffer_add_cb(evbuffer* buffer, evbuffer_cb_func cb, void* cbarg); 682 683 /** Remove a callback from an evbuffer, given a handle returned from 684 evbuffer_add_cb. 685 686 Calling this function invalidates the handle. 687 688 @return 0 if a callback was removed, or -1 if no matching callback was 689 found. 690 */ 691 int evbuffer_remove_cb_entry(evbuffer* buffer, 692 evbuffer_cb_entry* ent); 693 694 /** Remove a callback from an evbuffer, given the function and argument 695 used to add it. 696 697 @return 0 if a callback was removed, or -1 if no matching callback was 698 found. 699 */ 700 int evbuffer_remove_cb(evbuffer* buffer, evbuffer_cb_func cb, void* cbarg); 701 702 /** If this flag is not set, then a callback is temporarily disabled, and 703 * should not be invoked. 704 * 705 * @see evbuffer_cb_set_flags(), evbuffer_cb_clear_flags() 706 */ 707 enum EVBUFFER_CB_ENABLED = 1; 708 709 /** Change the flags that are set for a callback on a buffer by adding more. 710 711 @param buffer the evbuffer that the callback is watching. 712 @param cb the callback whose status we want to change. 713 @param flags EVBUFFER_CB_ENABLED to re-enable the callback. 714 @return 0 on success, -1 on failure. 715 */ 716 int evbuffer_cb_set_flags(evbuffer* buffer, 717 evbuffer_cb_entry* cb, ev_uint32_t flags); 718 719 /** Change the flags that are set for a callback on a buffer by removing some 720 721 @param buffer the evbuffer that the callback is watching. 722 @param cb the callback whose status we want to change. 723 @param flags EVBUFFER_CB_ENABLED to disable the callback. 724 @return 0 on success, -1 on failure. 725 */ 726 int evbuffer_cb_clear_flags(evbuffer* buffer, 727 evbuffer_cb_entry* cb, ev_uint32_t flags); 728 729 version (none) { 730 /** Postpone calling a given callback until unsuspend is called later. 731 732 This is different from disabling the callback, since the callback will get 733 invoked later if the buffer size changes between now and when we unsuspend 734 it. 735 736 @param the buffer that the callback is watching. 737 @param cb the callback we want to suspend. 738 */ 739 void evbuffer_cb_suspend(evbuffer* buffer, evbuffer_cb_entry* cb); 740 /** Stop postponing a callback that we postponed with evbuffer_cb_suspend. 741 742 If data was added to or removed from the buffer while the callback was 743 suspended, the callback will get called once now. 744 745 @param the buffer that the callback is watching. 746 @param cb the callback we want to stop suspending. 747 */ 748 void evbuffer_cb_unsuspend(evbuffer* buffer, evbuffer_cb_entry* cb); 749 } 750 751 /** 752 Makes the data at the begging of an evbuffer contiguous. 753 754 @param buf the evbuffer to make contiguous 755 @param size the number of bytes to make contiguous, or -1 to make the 756 entire buffer contiguous. 757 @return a pointer to the contiguous memory array 758 */ 759 760 ubyte* evbuffer_pullup(evbuffer* buf, ev_ssize_t size); 761 762 /** 763 Prepends data to the beginning of the evbuffer 764 765 @param buf the evbuffer to which to prepend data 766 @param data a pointer to the memory to prepend 767 @param size the number of bytes to prepend 768 @return 0 if successful, or -1 otherwise 769 */ 770 771 int evbuffer_prepend(evbuffer* buf, const(void)* data, size_t size); 772 773 /** 774 Prepends all data from the src evbuffer to the beginning of the dst 775 evbuffer. 776 777 @param dst the evbuffer to which to prepend data 778 @param src the evbuffer to prepend; it will be emptied as a result 779 @return 0 if successful, or -1 otherwise 780 */ 781 int evbuffer_prepend_buffer(evbuffer* dst, evbuffer* src); 782 783 /** 784 Prevent calls that modify an evbuffer from succeeding. A buffer may 785 frozen at the front, at the back, or at both the front and the back. 786 787 If the front of a buffer is frozen, operations that drain data from 788 the front of the buffer, or that prepend data to the buffer, will 789 fail until it is unfrozen. If the back a buffer is frozen, operations 790 that append data from the buffer will fail until it is unfrozen. 791 792 @param buf The buffer to freeze 793 @param at_front If true, we freeze the front of the buffer. If false, 794 we freeze the back. 795 @return 0 on success, -1 on failure. 796 */ 797 int evbuffer_freeze(evbuffer* buf, int at_front); 798 /** 799 Re-enable calls that modify an evbuffer. 800 801 @param buf The buffer to un-freeze 802 @param at_front If true, we unfreeze the front of the buffer. If false, 803 we unfreeze the back. 804 @return 0 on success, -1 on failure. 805 */ 806 int evbuffer_unfreeze(evbuffer* buf, int at_front); 807 808 /** 809 Force all the callbacks on an evbuffer to be run, not immediately after 810 the evbuffer is altered, but instead from inside the event loop. 811 812 This can be used to serialize all the callbacks to a single thread 813 of execution. 814 */ 815 int evbuffer_defer_callbacks(evbuffer* buffer, event_base* base);