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 @mainpage 30 31 @section intro Introduction 32 33 Libevent is an event notification library for developing scalable network 34 servers. The Libevent API provides a mechanism to execute a callback 35 function when a specific event occurs on a file descriptor or after a 36 timeout has been reached. Furthermore, Libevent also support callbacks due 37 to signals or regular timeouts. 38 39 Libevent is meant to replace the event loop found in event driven network 40 servers. An application just needs to call event_dispatch() and then add or 41 remove events dynamically without having to change the event loop. 42 43 44 Currently, Libevent supports /dev/poll, kqueue(2), select(2), poll(2), 45 epoll(4), and evports. The internal event mechanism is completely 46 independent of the exposed event API, and a simple update of Libevent can 47 provide new functionality without having to redesign the applications. As a 48 result, Libevent allows for portable application development and provides 49 the most scalable event notification mechanism available on an operating 50 system. Libevent can also be used for multithreaded programs. Libevent 51 should compile on Linux, *BSD, Mac OS X, Solaris and, Windows. 52 53 @section usage Standard usage 54 55 Every program that uses Libevent must inclurde the <event2/event.h> 56 header, and pass the -levent flag to the linker. (You can instead link 57 -levent_core if you only want the main event and buffered IO-based code, 58 and don't want to link any protocol code.) 59 60 @section setup Library setup 61 62 Before you call any other Libevent functions, you need to set up the 63 library. If you're going to use Libevent from multiple threads in a 64 multithreaded application, you need to initialize thread support -- 65 typically by using evthread_use_pthreads() or 66 evthread_use_windows_threads(). See <event2/thread.h> for more 67 information. 68 69 This is also the point where you can replace Libevent's memory 70 management functions with event_set_mem_functions, and enable debug mode 71 with event_enable_debug_mode(). 72 73 @section base Creating an event base 74 75 Next, you need to create an event_base structure, using event_base_new() 76 or event_base_new_with_config(). The event_base is responsible for 77 keeping track of which events are "pending" (that is to say, being 78 watched to see if they become active) and which events are "active". 79 Every event is associated with a single event_base. 80 81 @section event Event notification 82 83 For each file descriptor that you wish to monitor, you must create an 84 event structure with event_new(). (You may also declare an event 85 structure and call event_assign() to initialize the members of the 86 structure.) To enable notification, you add the structure to the list 87 of monitored events by calling event_add(). The event structure must 88 remain allocated as long as it is active, so it should generally be 89 allocated on the heap. 90 91 @section loop Dispaching evets. 92 93 Finally, you call event_base_dispatch() to loop and dispatch events. 94 You can also use event_base_loop() for more fine-grained control. 95 96 Currently, only one thread can be dispatching a given event_base at a 97 time. If you want to run events in multiple threads at once, you can 98 either have a single event_base whose events add work to a work queue, 99 or you can create multiple event_base objects. 100 101 @section bufferevent I/O Buffers 102 103 Libevent provides a buffered I/O abstraction on top of the regular event 104 callbacks. This abstraction is called a bufferevent. A bufferevent 105 provides input and output buffers that get filled and drained 106 automatically. The user of a buffered event no longer deals directly 107 with the I/O, but instead is reading from input and writing to output 108 buffers. 109 110 Once initialized via bufferevent_socket_new(), the bufferevent structure 111 can be used repeatedly with bufferevent_enable() and 112 bufferevent_disable(). Instead of reading and writing directly to a 113 socket, you would call bufferevent_read() and bufferevent_write(). 114 115 When read enabled the bufferevent will try to read from the file descriptor 116 and call the read callback. The write callback is executed whenever the 117 output buffer is drained below the write low watermark, which is 0 by 118 default. 119 120 See <event2/bufferevent*.h> for more information. 121 122 @section timers Timers 123 124 Libevent can also be used to create timers that invoke a callback after a 125 certain amount of time has expired. The evtimer_new() function returns 126 an event to use as a timer. To activate the timer, call 127 evtimer_add(). Timers can be deactivated by calling evtimer_del(). 128 129 @section evdns Asynchronous DNS resolution 130 131 Libevent provides an asynchronous DNS resolver that should be used instead 132 of the standard DNS resolver functions. See the <event2/dns.h> 133 functions for more detail. 134 135 @section evhttp Event-driven HTTP servers 136 137 Libevent provides a very simple event-driven HTTP server that can be 138 embedded in your program and used to service HTTP requests. 139 140 To use this capability, you need to include the <event2/http.h> header in your 141 program. See that header for more information. 142 143 @section evrpc A framework for RPC servers and clients 144 145 Libevent provides a framework for creating RPC servers and clients. It 146 takes care of marshaling and unmarshaling all data structures. 147 148 @section api API Reference 149 150 To browse the complete documentation of the libevent API, click on any of 151 the following links. 152 153 event2/event.h 154 The primary libevent header 155 156 event2/thread.h 157 Functions for use by multithreaded programs 158 159 event2/buffer.h and event2/bufferevent.h 160 Buffer management for network reading and writing 161 162 event2/util.h 163 Utility functions for portable nonblocking network code 164 165 event2/dns.h 166 Asynchronous DNS resolution 167 168 event2/http.h 169 An embedded libevent-based HTTP server 170 171 event2/rpc.h 172 A framework for creating RPC servers and clients 173 174 */ 175 176 /** @file event2/event.h 177 178 Core functions for waiting for and receiving events, and using event bases. 179 */ 180 module deimos.event2.event; 181 182 extern (C): 183 nothrow: 184 185 import core.stdc.stdio; 186 187 /* For int types. */ 188 public import deimos.event2.util; 189 import deimos.event2._d_util; 190 191 /** 192 * Structure to hold information and state for a Libevent dispatch loop. 193 * 194 * The event_base lies at the center of Libevent; every application will 195 * have one. It keeps track of all pending and active events, and 196 * notifies your application of the active ones. 197 * 198 * This is an opaque structure; you can allocate one using 199 * event_base_new() or event_base_new_with_config(). 200 * 201 * @see event_base_new(), event_base_free(), event_base_loop(), 202 * event_base_new_with_config() 203 */ 204 public import deimos.event2.event_struct; 205 206 /** 207 * @struct event 208 * 209 * Structure to represent a single event. 210 * 211 * An event can have some underlying condition it represents: a socket 212 * becoming readable or writeable (or both), or a signal becoming raised. 213 * (An event that represents no underlying condition is still useful: you 214 * can use one to implement a timer, or to communicate between threads.) 215 * 216 * Generally, you can create events with event_new(), then make them 217 * pending with event_add(). As your event_base runs, it will run the 218 * callbacks of an events whose conditions are triggered. When you 219 * longer want the event, free it with event_free(). 220 * 221 * In more depth: 222 * 223 * An event may be "pending" (one whose condition we are watching), 224 * "active" (one whose condition has triggered and whose callback is about 225 * to run), neither, or both. Events come into existence via 226 * event_assign() or event_new(), and are then neither active nor pending. 227 * 228 * To make an event pending, pass it to event_add(). When doing so, you 229 * can also set a timeout for the event. 230 * 231 * Events become active during an event_base_loop() call when either their 232 * condition has triggered, or when their timeout has elapsed. You can 233 * also activate an event manually using event_active(). The even_base 234 * loop will run the callbacks of active events; after it has done so, it 235 * marks them as no longer active. 236 * 237 * You can make an event non-pending by passing it to event_del(). This 238 * also makes the event non-active. 239 * 240 * Events can be "persistent" or "non-persistent". A non-persistent event 241 * becomes non-pending as soon as it is triggered: thus, it only runs at 242 * most once per call to event_add(). A persistent event remains pending 243 * even when it becomes active: you'll need to event_del() it manually in 244 * order to make it non-pending. When a persistent event with a timeout 245 * becomes active, its timeout is reset: this means you can use persistent 246 * events to implement periodic timeouts. 247 * 248 * This should be treated as an opaque structure; you should never read or 249 * write any of its fields directly. For backward compatibility with old 250 * code, it is defined in the event2/event_struct.h header; including this 251 * header may make your code incompatible with other versions of Libevent. 252 * 253 * @see event_new(), event_free(), event_assign(), event_get_assignment(), 254 * event_add(), event_del(), event_active(), event_pending(), 255 * event_get_fd(), event_get_base(), event_get_events(), 256 * event_get_callback(), event_get_callback_arg(), 257 * event_priority_set() 258 */ 259 public import deimos.event2.event_struct; 260 261 /** 262 * Configuration for an event_base. 263 * 264 * There are many options that can be used to alter the behavior and 265 * implementation of an event_base. To avoid having to pass them all in a 266 * complex many-argument constructor, we provide an abstract data type 267 * wrhere you set up configation information before passing it to 268 * event_base_new_with_config(). 269 * 270 * @see event_config_new(), event_config_free(), event_base_new_with_config(), 271 * event_config_avoid_method(), event_config_require_features(), 272 * event_config_set_flag(), event_config_set_num_cpus_hint() 273 */ 274 struct event_config; 275 276 /** 277 * Enable some relatively expensive debugging checks in Libevent that 278 * would normally be turned off. Generally, these checks cause code that 279 * would otherwise crash mysteriously to fail earlier with an assertion 280 * failure. Note that this method MUST be called before any events or 281 * event_bases have been created. 282 * 283 * Debug mode can currently catch the following errors: 284 * An event is re-assigned while it is added 285 * Any function is called on a non-assigned event 286 * 287 * Note that debugging mode uses memory to track every event that has been 288 * initialized (via event_assign, event_set, or event_new) but not yet 289 * released (via event_free or event_debug_unassign). If you want to use 290 * debug mode, and you find yourself running out of memory, you will need 291 * to use event_debug_unassign to explicitly stop tracking events that 292 * are no longer considered set-up. 293 * 294 * @see event_debug_unassign() 295 */ 296 void event_enable_debug_mode(); 297 298 /** 299 * When debugging mode is enabled, informs Libevent that an event should no 300 * longer be considered as assigned. When debugging mode is not enabled, does 301 * nothing. 302 * 303 * This function must only be called on a non-added event. 304 * 305 * @see event_enable_debug_mode() 306 */ 307 void event_debug_unassign(event*); 308 309 /** 310 * Create and return a new event_base to use with the rest of Libevent. 311 * 312 * @return a new event_base on success, or NULL on failure. 313 * 314 * @see event_base_free(), event_base_new_with_config() 315 */ 316 event_base* event_base_new(); 317 318 /** 319 Reinitialize the event base after a fork 320 321 Some event mechanisms do not survive across fork. The event base needs 322 to be reinitialized with the event_reinit() function. 323 324 @param base the event base that needs to be re-initialized 325 @return 0 if successful, or -1 if some events could not be re-added. 326 @see event_base_new() 327 */ 328 int event_reinit(event_base* base); 329 330 /** 331 Event dispatching loop 332 333 This loop will run the event base until either there are no more added 334 events, or until something calls event_base_loopbreak() or 335 event_base_loopexit(). 336 337 @param base the event_base structure returned by event_base_new() or 338 event_base_new_with_config() 339 @return 0 if successful, -1 if an error occurred, or 1 if no events were 340 registered. 341 @see event_base_loop() 342 */ 343 int event_base_dispatch(event_base*); 344 345 /** 346 Get the kernel event notification mechanism used by Libevent. 347 348 @param eb the event_base structure returned by event_base_new() 349 @return a string identifying the kernel event mechanism (kqueue, epoll, etc.) 350 */ 351 const(char)* event_base_get_method(const(event_base)*); 352 353 /** 354 Gets all event notification mechanisms supported by Libevent. 355 356 This functions returns the event mechanism in order preferred by 357 Libevent. Note that this list will include all backends that 358 Libevent has compiled-in support for, and will not necessarily check 359 your OS to see whether it has the required resources. 360 361 @return an array with pointers to the names of support methods. 362 The end of the array is indicated by a NULL pointer. If an 363 error is encountered NULL is returned. 364 */ 365 const(char)* *event_get_supported_methods(); 366 367 /** 368 Allocates a new event configuration object. 369 370 The event configuration object can be used to change the behavior of 371 an event base. 372 373 @return an event_config object that can be used to store configuration, or 374 NULL if an error is encountered. 375 @see event_base_new_with_config(), event_config_free(), event_config 376 */ 377 event_config* event_config_new(); 378 379 /** 380 Deallocates all memory associated with an event configuration object 381 382 @param cfg the event configuration object to be freed. 383 */ 384 void event_config_free(event_config* cfg); 385 386 /** 387 Enters an event method that should be avoided into the configuration. 388 389 This can be used to avoid event mechanisms that do not support certain 390 file descriptor types, or for debugging to avoid certain event 391 mechanisms. An application can make use of multiple event bases to 392 accommodate incompatible file descriptor types. 393 394 @param cfg the event configuration object 395 @param method the name of the event method to avoid 396 @return 0 on success, -1 on failure. 397 */ 398 int event_config_avoid_method(event_config* cfg, const(char)* method); 399 400 /** 401 A flag used to describe which features an event_base (must) provide. 402 403 Because of OS limitations, not every Libevent backend supports every 404 possible feature. You can use this type with 405 event_config_require_features() to tell Libevent to only proceed if your 406 event_base implements a given feature, and you can receive this type from 407 event_base_get_features() to see which features are available. 408 */ 409 enum event_method_feature { 410 /** Require an event method that allows edge-triggered events with EV_ET. */ 411 EV_FEATURE_ET = 0x01, 412 /** Require an event method where having one event triggered among 413 * many is [approximately] an O(1) operation. This excludes (for 414 * example) select and poll, which are approximately O(N) for N 415 * equal to the total number of possible events. */ 416 EV_FEATURE_O1 = 0x02, 417 /** Require an event method that allows file descriptors as well as 418 * sockets. */ 419 EV_FEATURE_FDS = 0x04 420 }; 421 422 /** 423 A flag passed to event_config_set_flag(). 424 425 These flags change the behavior of an allocated event_base. 426 427 @see event_config_set_flag(), event_base_new_with_config(), 428 event_method_feature 429 */ 430 enum event_base_config_flag { 431 /** Do not allocate a lock for the event base, even if we have 432 locking set up. */ 433 EVENT_BASE_FLAG_NOLOCK = 0x01, 434 /** Do not check the EVENT_* environment variables when configuring 435 an event_base */ 436 EVENT_BASE_FLAG_IGNORE_ENV = 0x02, 437 /** Windows only: enable the IOCP dispatcher at startup 438 439 If this flag is set then bufferevent_socket_new() and 440 evconn_listener_new() will use IOCP-backed implementations 441 instead of the usual select-based one on Windows. 442 */ 443 EVENT_BASE_FLAG_STARTUP_IOCP = 0x04, 444 /** Instead of checking the current time every time the event loop is 445 ready to run timeout callbacks, check after each timeout callback. 446 */ 447 EVENT_BASE_FLAG_NO_CACHE_TIME = 0x08, 448 449 /** If we are using the epoll backend, this flag says that it is 450 safe to use Libevent's internal change-list code to batch up 451 adds and deletes in order to try to do as few syscalls as 452 possible. Setting this flag can make your code run faster, but 453 it may trigger a Linux bug: it is not safe to use this flag 454 if you have any fds cloned by dup() or its variants. Doing so 455 will produce strange and hard-to-diagnose bugs. 456 457 This flag can also be activated by settnig the 458 EVENT_EPOLL_USE_CHANGELIST environment variable. 459 460 This flag has no effect if you wind up using a backend other than 461 epoll. 462 */ 463 EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST = 0x10 464 }; 465 466 /** 467 Return a bitmask of the features implemented by an event base. This 468 will be a bitwise OR of one or more of the values of 469 event_method_feature 470 471 @see event_method_feature 472 */ 473 int event_base_get_features(const(event_base)* base); 474 475 /** 476 Enters a required event method feature that the application demands. 477 478 Note that not every feature or combination of features is supported 479 on every platform. Code that requests features should be prepared 480 to handle the case where event_base_new_with_config() returns NULL, as in: 481 <pre> 482 event_config_require_features(cfg, EV_FEATURE_ET); 483 base = event_base_new_with_config(cfg); 484 if (base == NULL) { 485 // We can't get edge-triggered behavior here. 486 event_config_require_features(cfg, 0); 487 base = event_base_new_with_config(cfg); 488 } 489 </pre> 490 491 @param cfg the event configuration object 492 @param feature a bitfield of one or more event_method_feature values. 493 Replaces values from previous calls to this function. 494 @return 0 on success, -1 on failure. 495 @see event_method_feature, event_base_new_with_config() 496 */ 497 int event_config_require_features(event_config* cfg, int feature); 498 499 /** 500 * Sets one or more flags to configure what parts of the eventual event_base 501 * will be initialized, and how they'll work. 502 * 503 * @see event_base_config_flags, event_base_new_with_config() 504 **/ 505 int event_config_set_flag(event_config* cfg, int flag); 506 507 /** 508 * Records a hint for the number of CPUs in the system. This is used for 509 * tuning thread pools, etc, for optimal performance. In Libevent 2.0, 510 * it is only on Windows, and only when IOCP is in use. 511 * 512 * @param cfg the event configuration object 513 * @param cpus the number of cpus 514 * @return 0 on success, -1 on failure. 515 */ 516 int event_config_set_num_cpus_hint(event_config* cfg, int cpus); 517 518 /** 519 Initialize the event API. 520 521 Use event_base_new_with_config() to initialize a new event base, taking 522 the specified configuration under consideration. The configuration object 523 can currently be used to avoid certain event notification mechanisms. 524 525 @param cfg the event configuration object 526 @return an initialized event_base that can be used to registering events, 527 or NULL if no event base can be created with the requested event_config. 528 @see event_base_new(), event_base_free(), event_init(), event_assign() 529 */ 530 event_base* event_base_new_with_config(const(event_config)*); 531 532 /** 533 Deallocate all memory associated with an event_base, and free the base. 534 535 Note that this function will not close any fds or free any memory passed 536 to event_new as the argument to callback. 537 538 @param eb an event_base to be freed 539 */ 540 void event_base_free(event_base*); 541 542 /** @name Log severities 543 */ 544 /**@{*/ 545 enum _EVENT_LOG_DEBUG = 0; 546 enum _EVENT_LOG_MSG = 1; 547 enum _EVENT_LOG_WARN = 2; 548 enum _EVENT_LOG_ERR = 3; 549 /**@}*/ 550 551 /** 552 A callback function used to intercept Libevent's log messages. 553 554 @see event_set_log_callback 555 */ 556 alias ExternC!(void function(int severity, const(char)* msg)) event_log_cb; 557 /** 558 Redirect Libevent's log messages. 559 560 @param cb a function taking two arguments: an integer severity between 561 _EVENT_LOG_DEBUG and _EVENT_LOG_ERR, and a string. If cb is NULL, 562 then the default log is used. 563 564 NOTE: The function you provide* must not* call any other libevent 565 functionality. Doing so can produce undefined behavior. 566 */ 567 void event_set_log_callback(event_log_cb cb); 568 569 /** 570 A function to be called if Libevent encounters a fatal internal error. 571 572 @see event_set_fatal_callback 573 */ 574 alias ExternC!(void function(int err)) event_fatal_cb; 575 576 /** 577 Override Libevent's behavior in the event of a fatal internal error. 578 579 By default, Libevent will call exit(1) if a programming error makes it 580 impossible to continue correct operation. This function allows you to supply 581 another callback instead. Note that if the function is ever invoked, 582 something is wrong with your program, or with Libevent: any subsequent calls 583 to Libevent may result in undefined behavior. 584 585 Libevent will (almost) always log an _EVENT_LOG_ERR message before calling 586 this function; look at the last log message to see why Libevent has died. 587 */ 588 void event_set_fatal_callback(event_fatal_cb cb); 589 590 /** 591 Associate a different event base with an event. 592 593 The event to be associated must not be currently active or pending. 594 595 @param eb the event base 596 @param ev the event 597 @return 0 on success, -1 on failure. 598 */ 599 int event_base_set(event_base*, event*); 600 601 /** @name Loop flags 602 603 These flags control the behavior of event_base_loop(). 604 */ 605 /**@{*/ 606 /** Block until we have an active event, then exit once all active events 607 * have had their callbacks run. */ 608 enum EVLOOP_ONCE = 0x01; 609 /** Do not block: see which events are ready now, run the callbacks 610 * of the highest-priority ones, then exit. */ 611 enum EVLOOP_NONBLOCK = 0x02; 612 /**@}*/ 613 614 /** 615 Wait for events to become active, and run their callbacks. 616 617 This is a more flexible version of event_base_dispatch(). 618 619 By default, this loop will run the event base until either there are no more 620 added events, or until something calls event_base_loopbreak() or 621 evenet_base_loopexit(). You can override this behavior with the 'flags' 622 argument. 623 624 @param eb the event_base structure returned by event_base_new() or 625 event_base_new_with_config() 626 @param flags any combination of EVLOOP_ONCE | EVLOOP_NONBLOCK 627 @return 0 if successful, -1 if an error occurred, or 1 if no events were 628 registered. 629 @see event_base_loopexit(), event_base_dispatch(), EVLOOP_ONCE, 630 EVLOOP_NONBLOCK 631 */ 632 int event_base_loop(event_base*, int); 633 634 /** 635 Exit the event loop after the specified time 636 637 The next event_base_loop() iteration after the given timer expires will 638 complete normally (handling all queued events) then exit without 639 blocking for events again. 640 641 Subsequent invocations of event_base_loop() will proceed normally. 642 643 @param eb the event_base structure returned by event_init() 644 @param tv the amount of time after which the loop should terminate, 645 or NULL to exit after running all currently active events. 646 @return 0 if successful, or -1 if an error occurred 647 @see event_base_loopbreak() 648 */ 649 int event_base_loopexit(event_base*, const(timeval)*); 650 651 /** 652 Abort the active event_base_loop() immediately. 653 654 event_base_loop() will abort the loop after the next event is completed; 655 event_base_loopbreak() is typically invoked from this event's callback. 656 This behavior is analogous to the "break;" statement. 657 658 Subsequent invocations of event_loop() will proceed normally. 659 660 @param eb the event_base structure returned by event_init() 661 @return 0 if successful, or -1 if an error occurred 662 @see event_base_loopexit() 663 */ 664 int event_base_loopbreak(event_base*); 665 666 /** 667 Checks if the event loop was told to exit by event_loopexit(). 668 669 This function will return true for an event_base at every point after 670 event_loopexit() is called, until the event loop is next entered. 671 672 @param eb the event_base structure returned by event_init() 673 @return true if event_base_loopexit() was called on this event base, 674 or 0 otherwise 675 @see event_base_loopexit() 676 @see event_base_got_break() 677 */ 678 int event_base_got_exit(event_base*); 679 680 /** 681 Checks if the event loop was told to abort immediately by event_loopbreak(). 682 683 This function will return true for an event_base at every point after 684 event_loopbreak() is called, until the event loop is next entered. 685 686 @param eb the event_base structure returned by event_init() 687 @return true if event_base_loopbreak() was called on this event base, 688 or 0 otherwise 689 @see event_base_loopbreak() 690 @see event_base_got_exit() 691 */ 692 int event_base_got_break(event_base*); 693 694 /** 695 * @name event flags 696 * 697 * Flags to pass to event_new(), event_assign(), event_pending(), and 698 * anything else with an argument of the form "short events" 699 */ 700 /**@{*/ 701 /** Indicates that a timeout has occurred. It's not necessary to pass 702 * this flag to event_for new()/event_assign() to get a timeout. */ 703 enum EV_TIMEOUT = 0x01; 704 /** Wait for a socket or FD to become readable */ 705 enum EV_READ = 0x02; 706 /** Wait for a socket or FD to become writeable */ 707 enum EV_WRITE = 0x04; 708 /** Wait for a POSIX signal to be raised*/ 709 enum EV_SIGNAL = 0x08; 710 /** 711 * Persistent event: won't get removed automatically when activated. 712 * 713 * When a persistent event with a timeout becomes activated, its timeout 714 * is reset to 0. 715 */ 716 enum EV_PERSIST = 0x10; 717 /** Select edge-triggered behavior, if supported by the backend. */ 718 enum EV_ET = 0x20; 719 /**@}*/ 720 721 /** 722 @name evtimer_* macros 723 724 Aliases for working with one-shot timer events */ 725 /**@{*/ 726 int evtimer_assign()(event* ev, event_base* b, event_callback_fn cb, void* arg) { 727 event_assign(ev, b, -1, 0, cb, arg); 728 } 729 int evsignal_pending()(const(event)* ev, timeval* tv) { 730 return event_pending((ev), EV_TIMEOUT, (tv)); 731 } 732 alias event_add evtimer_add; 733 alias event_del evtimer_del; 734 alias event_initialized evtimer_initialized; 735 /**@}*/ 736 737 /** 738 @name evsignal_* macros 739 740 Aliases for working with signal events 741 */ 742 /**@{*/ 743 alias event_add evsignal_add; 744 int evsignal_assign()(event* ev, event_base* b, evutil_socket_t x, event_callback_fn cb, void* arg) { 745 return event_assign(ev, b, x, EV_SIGNAL|EV_PERSIST, cb, arg); 746 } 747 int evsignal_new()(event_base* b, evutil_socket_t x, event_callback_fn cb, void* arg) { 748 return event_assign(b, x, EV_SIGNAL|EV_PERSIST, cb, arg); 749 } 750 alias event_del evsignal_del; 751 int evsignal_pending()(const(event)* ev, timeval* tv) { 752 return event_pending((ev), EV_SIGNAL, (tv)); 753 } 754 alias event_initialized evsignal_initialized; 755 /**@}*/ 756 757 /** 758 A callback function for an event. 759 760 It receives three arguments: 761 762 @param fd An fd or signal 763 @param events One or more EV_* flags 764 @param arg A user-supplied argument. 765 766 @see event_new() 767 */ 768 alias ExternC!(void function(evutil_socket_t, short, void*)) event_callback_fn; 769 770 /** 771 Allocate and asssign a new event structure, ready to be added. 772 773 The function event_new() returns a new event that can be used in 774 future calls to event_add() and event_del(). The fd and events 775 arguments determine which conditions will trigger the event; the 776 callback and callback_arg arguments tell Libevent what to do when the 777 event becomes active. 778 779 If events contains one of EV_READ, EV_WRITE, or EV_READ|EV_WRITE, then 780 fd is a file descriptor or socket that should get monitored for 781 readiness to read, readiness to write, or readiness for either operation 782 (respectively). If events contains EV_SIGNAL, then fd is a signal 783 number to wait for. If events contains none of those flags, then the 784 event can be triggered only by a timeout or by manual activation with 785 event_active(): In this case, fd must be -1. 786 787 The EV_PERSIST flag can also be passed in the events argument: it makes 788 event_add() persistent until event_del() is called. 789 790 The EV_ET flag is compatible with EV_READ and EV_WRITE, and supported 791 only by certain backends. It tells Libevent to use edge-triggered 792 events. 793 794 The EV_TIMEOUT flag has no effect here. 795 796 It is okay to have multiple events all listening on the same fds; but 797 they must either all be edge-triggered, or all not be edge triggerd. 798 799 When the event becomes active, the event loop will run the provided 800 callbuck function, with three arguments. The first will be the provided 801 fd value. The second will be a bitfield of the events that triggered: 802 EV_READ, EV_WRITE, or EV_SIGNAL. Here the EV_TIMEOUT flag indicates 803 that a timeout occurred, and EV_ET indicates that an edge-triggered 804 event occurred. The third event will be the callback_arg pointer that 805 you provide. 806 807 @param base the event base to which the event should be attached. 808 @param fd the file descriptor or signal to be monitored, or -1. 809 @param events desired events to monitor: bitfield of EV_READ, EV_WRITE, 810 EV_SIGNAL, EV_PERSIST, EV_ET. 811 @param callback callback function to be invoked when the event occurs 812 @param callback_arg an argument to be passed to the callback function 813 814 @return a newly allocated event that must later be freed with 815 event_free(). 816 @see event_free(), event_add(), event_del(), event_assign() 817 */ 818 event* event_new(event_base*, evutil_socket_t, short, event_callback_fn, void*); 819 820 821 /** 822 Prepare a new, already-allocated event structure to be added. 823 824 The function event_assign() prepares the event structure ev to be used 825 in future calls to event_add() and event_del(). Unlike event_new(), it 826 doesn't allocate memory itself: it requires that you have already 827 allocated a struct event, probably on the heap. Doing this will 828 typically make your code depend on the size of the event structure, and 829 thereby create incompatibility with future versions of Libevent. 830 831 The easiest way to avoid this problem is just to use event_new() and 832 event_free() instead. 833 834 A slightly harder way to future-proof your code is to use 835 event_get_struct_event_size() to determine the required size of an event 836 at runtime. 837 838 Note that it is NOT safe to call this function on an event that is 839 active or pending. Doing so WILL corrupt internal data structures in 840 Libevent, and lead to strange, hard-to-diagnose bugs. You _can_ use 841 event_assign to change an existing event, but only if it is not active 842 or pending! 843 844 The arguments for this function, and the behavior of the events that it 845 makes, are as for event_new(). 846 847 @param ev an event to be modified 848 @param base the event base to which ev should be attached. 849 @param fd the file descriptor to be monitored 850 @param events desired events to monitor; can be EV_READ and/or EV_WRITE 851 @param callback callback function to be invoked when the event occurs 852 @param callback_arg an argument to be passed to the callback function 853 854 @return 0 if success, or -1 on invalid arguments. 855 856 @see event_new(), event_add(), event_del(), event_base_once(), 857 event_get_struct_event_size() 858 */ 859 int event_assign(event*, event_base*, evutil_socket_t, short, event_callback_fn, void*); 860 861 /** 862 Deallocate a event* returned by event_new(). 863 864 If the event is pending or active, first make it non-pending and 865 non-active. 866 */ 867 void event_free(event*); 868 869 /** 870 Schedule a one-time event 871 872 The function event_base_once() is similar to event_set(). However, it 873 schedules a callback to be called exactly once, and does not require the 874 caller to prepare an event structure. 875 876 Note that in Libevent 2.0 and earlier, if the event is never triggered, 877 the internal memory used to hold it will never be freed. This may be 878 fixed in a later version of Libevent. 879 880 @param base an event_base 881 @param fd a file descriptor to monitor, or -1 for no fd. 882 @param events event(s) to monitor; can be any of EV_READ | 883 EV_WRITE, or EV_TIMEOUT 884 @param callback callback function to be invoked when the event occurs 885 @param arg an argument to be passed to the callback function 886 @param timeout the maximum amount of time to wait for the event. NULL 887 makes an EV_READ/EV_WRITE event make forever; NULL makes an 888 EV_TIMEOUT event succees immediately. 889 @return 0 if successful, or -1 if an error occurred 890 */ 891 int event_base_once(event_base*, evutil_socket_t, short, event_callback_fn, void*, const(timeval)*); 892 893 /** 894 Add an event to the set of pending events. 895 896 The function event_add() schedules the execution of the ev event when the 897 event specified in event_assign()/event_new() occurs, or when the time 898 specified in timeout has elapesed. If atimeout is NULL, no timeout 899 occurs and the function will only be 900 called if a matching event occurs. The event in the 901 ev argument must be already initialized by event_assign() or event_new() 902 and may not be used 903 in calls to event_assign() until it is no longer pending. 904 905 If the event in the ev argument already has a scheduled timeout, calling 906 event_add() replaces the old timeout with the new one, or clears the old 907 timeout if the timeout argument is NULL. 908 909 @param ev an event initialized via event_set() 910 @param timeout the maximum amount of time to wait for the event, or NULL 911 to wait forever 912 @return 0 if successful, or -1 if an error occurred 913 @see event_del(), event_assign(), event_new() 914 */ 915 int event_add(event* ev, const(timeval)* timeout); 916 917 /** 918 Remove an event from the set of monitored events. 919 920 The function event_del() will cancel the event in the argument ev. If the 921 event has already executed or has never been added the call will have no 922 effect. 923 924 @param ev an event to be removed from the working set 925 @return 0 if successful, or -1 if an error occurred 926 @see event_add() 927 */ 928 int event_del(event*); 929 930 931 /** 932 Make an event active. 933 934 You can use this function on a pending or a non-pending event to make it 935 active, so that its callback will be run by event_base_dispatch() or 936 event_base_loop(). 937 938 One common use in multithreaded programs is to wake the thread running 939 event_base_loop() from another thread. 940 941 @param ev an event to make active. 942 @param res a set of flags to pass to the event's callback. 943 @param ncalls an obsolete argument: this is ignored. 944 **/ 945 void event_active(event* ev, int res, short ncalls); 946 947 /** 948 Checks if a specific event is pending or scheduled. 949 950 @param ev an event previously passed to event_add() 951 @param events the requested event type; any of EV_TIMEOUT|EV_READ| 952 EV_WRITE|EV_SIGNAL 953 @param tv if this field is not NULL, and the event has a timeout, 954 this field is set to hold the time at which the timeout will 955 expire. 956 957 @return true if the event is pending on any of the events in 'what', (that 958 is to say, it has been added), or 0 if the event is not added. 959 */ 960 int event_pending(const(event)* ev, short events, timeval* tv); 961 962 963 /** 964 Test if an event structure might be initialized. 965 966 The event_initialized() function can be used to check if an event has been 967 initialized. 968 969 Warning: This function is only useful for distinguishing a a zeroed-out 970 piece of memory from an initialized event, it can easily be confused by 971 uninitialized memory. Thus, it should ONLY be used to distinguish an 972 initialized event from zero. 973 974 @param ev an event structure to be tested 975 @return 1 if the structure might be initialized, or 0 if it has not been 976 initialized 977 */ 978 int event_initialized(const(event)* ev); 979 980 /** 981 Get the signal number assigned to a signal event 982 */ 983 int event_get_signal()(const(event)* ev) { return cast(int)event_get_fd(ev); } 984 985 /** 986 Get the socket or signal assigned to an event, or -1 if the event has 987 no socket. 988 */ 989 evutil_socket_t event_get_fd(const(event)* ev); 990 991 /** 992 Get the event_base associated with an event. 993 */ 994 event_base* event_get_base(const(event)* ev); 995 996 /** 997 Return the events (EV_READ, EV_WRITE, etc) assigned to an event. 998 */ 999 short event_get_events(const(event)* ev); 1000 1001 /** 1002 Return the callback assigned to an event. 1003 */ 1004 event_callback_fn event_get_callback(const(event)* ev); 1005 1006 /** 1007 Return the callback argument assigned to an event. 1008 */ 1009 void* event_get_callback_arg(const(event)* ev); 1010 1011 /** 1012 Extract _all_ of arguments given to cona given event. The 1013 event_base is copied into* base_out, the fd is copied into* fd_out, and so 1014 on. 1015 1016 If any of the "_out" arguments is NULL, it will be ignored. 1017 */ 1018 void event_get_assignment(const(event)* event, 1019 event_base* *base_out, evutil_socket_t* fd_out, short* events_out, 1020 event_callback_fn* callback_out, void* *arg_out); 1021 1022 /** 1023 Return the size of event that the Libevent library was compiled 1024 with. 1025 1026 This will be NO GREATER than sizeof(struct event) if you're running with 1027 the same version of Libevent that your application was built with, but 1028 otherwise might not. 1029 1030 Note that it might be SMALLER than sizeof(struct event) if some future 1031 version of Libevent adds extra padding to the end of struct event. 1032 We might do this to help ensure ABI-compatibility between different 1033 versions of Libevent. 1034 */ 1035 size_t event_get_struct_event_size(); 1036 1037 /** 1038 Get the Libevent version. 1039 1040 Note that this will give you the version of the library that you're 1041 currently linked against, not the version of the headers that you've 1042 compiled against. 1043 1044 @return a string containing the version number of Libevent 1045 */ 1046 const(char)* event_get_version(); 1047 1048 /** 1049 Return a numeric representation of Libevent's version. 1050 1051 Note that this will give you the version of the library that you're 1052 currently linked against, not the version of the headers you've used to 1053 compile. 1054 1055 The format uses one byte each for the major, minor, and patchlevel parts of 1056 the version number. The low-order byte is unused. For example, version 1057 2.0.1-alpha has a numeric representation of 0x02000100 1058 */ 1059 ev_uint32_t event_get_version_number(); 1060 1061 /** As event_get_version, but gives the version of Libevent's headers. */ 1062 enum LIBEVENT_VERSION = "2.0.16-stable"; 1063 /** As event_get_version_number, but gives the version number of Libevent's 1064 * headers. */ 1065 enum LIBEVENT_VERSION_NUMBER = 0x02001000; 1066 1067 /** Largest number of priorities that Libevent can support. */ 1068 enum EVENT_MAX_PRIORITIES = 256; 1069 /** 1070 Set the number of different event priorities 1071 1072 By default Libevent schedules all active events with the same priority. 1073 However, some time it is desirable to process some events with a higher 1074 priority than others. For that reason, Libevent supports strict priority 1075 queues. Active events with a lower priority are always processed before 1076 events with a higher priority. 1077 1078 The number of different priorities can be set initially with the 1079 event_base_priority_init() function. This function should be called 1080 before the first call to event_base_dispatch(). The 1081 event_priority_set() function can be used to assign a priority to an 1082 event. By default, Libevent assigns the middle priority to all events 1083 unless their priority is explicitly set. 1084 1085 Note that urgent-priority events can starve less-urgent events: after 1086 running all urgent-priority callbacks, Libevent checks for more urgent 1087 events again_, before running less-urgent events. Less-urgent events 1088 will not have their callbacks run until there are no events more urgent 1089 than them that want to be active. 1090 1091 @param eb the event_base structure returned by event_base_new() 1092 @param npriorities the maximum number of priorities 1093 @return 0 if successful, or -1 if an error occurred 1094 @see event_priority_set() 1095 */ 1096 int event_base_priority_init(event_base*, int); 1097 1098 /** 1099 Assign a priority to an event. 1100 1101 @param ev an event struct 1102 @param priority the new priority to be assigned 1103 @return 0 if successful, or -1 if an error occurred 1104 @see event_priority_init() 1105 */ 1106 int event_priority_set(event*, int); 1107 1108 /** 1109 Prepare an event_base to use a large number of timeouts with the same 1110 duration. 1111 1112 Libevent's default scheduling algorithm is optimized for having a large 1113 number of timeouts with their durations more or less randomly 1114 distributed. But if you have a large number of timeouts that all have 1115 the same duration (for example, if you have a large number of 1116 connections that all have a 10-second timeout), then you can improve 1117 Libevent's performance by telling Libevent about it. 1118 1119 To do this, call this function with the common duration. It will return a 1120 pointer to a different, opaque timeout value. (Don't depend on its actual 1121 contents!) When you use this timeout value in event_add(), Libevent will 1122 schedule the event more efficiently. 1123 1124 (This optimization probably will not be worthwhile until you have thousands 1125 or tens of thousands of events with the same timeout.) 1126 */ 1127 const(timeval)* event_base_init_common_timeout(event_base* base, 1128 const(timeval)* duration); 1129 1130 /** 1131 Override the functions that Libevent uses for memory management. 1132 1133 Usually, Libevent uses the standard libc functions malloc, realloc, and 1134 free to allocate memory. Passing replacements for those functions to 1135 event_set_mem_functions() overrides this behavior. 1136 1137 Note that all memory returned from Libevent will be allocated by the 1138 replacement functions rather than by malloc() and realloc(). Thus, if you 1139 have replaced those functions, it will not be appropriate to free() memory 1140 that you get from Libevent. Instead, you must use the free_fn replacement 1141 that you provided. 1142 1143 Note also that if you are going to call this function, you should do so 1144 before any call to any Libevent function that does allocation. 1145 Otherwise, those funtions will allocate their memory using malloc(), but 1146 then later free it using your provided free_fn. 1147 1148 @param malloc_fn A replacement for malloc. 1149 @param realloc_fn A replacement for realloc 1150 @param free_fn A replacement for free. 1151 **/ 1152 void event_set_mem_functions( 1153 ExternC!(void* function(size_t sz)) malloc_fn, 1154 ExternC!(void* function(void* ptr, size_t sz)) realloc_fn, 1155 ExternC!(void function(void* ptr)) free_fn); 1156 /** This definition is present if Libevent was built with support for 1157 event_set_mem_functions() */ 1158 enum EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED = true; 1159 1160 void event_base_dump_events(event_base*, FILE *); 1161 1162 /** Sets 'tv' to the current time (as returned by gettimeofday()), 1163 looking at the cached value in 'base' if possible, and calling 1164 gettimeofday() or clock_gettime() as appropriate if there is no 1165 cached time. 1166 1167 Generally, this value will only be cached while actually 1168 processing event callbacks, and may be very inaccuate if your 1169 callbacks take a long time to execute. 1170 1171 Returns 0 on success, negative on failure. 1172 */ 1173 int event_base_gettimeofday_cached(event_base* base, 1174 timeval* tv);