Blame libevent/include/event2/event.h

Packit e9ba0d
/*
Packit e9ba0d
 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
Packit e9ba0d
 * Copyright (c) 2007-2012 Niels Provos and Nick Mathewson
Packit e9ba0d
 *
Packit e9ba0d
 * Redistribution and use in source and binary forms, with or without
Packit e9ba0d
 * modification, are permitted provided that the following conditions
Packit e9ba0d
 * are met:
Packit e9ba0d
 * 1. Redistributions of source code must retain the above copyright
Packit e9ba0d
 *    notice, this list of conditions and the following disclaimer.
Packit e9ba0d
 * 2. Redistributions in binary form must reproduce the above copyright
Packit e9ba0d
 *    notice, this list of conditions and the following disclaimer in the
Packit e9ba0d
 *    documentation and/or other materials provided with the distribution.
Packit e9ba0d
 * 3. The name of the author may not be used to endorse or promote products
Packit e9ba0d
 *    derived from this software without specific prior written permission.
Packit e9ba0d
 *
Packit e9ba0d
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
Packit e9ba0d
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
Packit e9ba0d
 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
Packit e9ba0d
 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
Packit e9ba0d
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
Packit e9ba0d
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
Packit e9ba0d
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
Packit e9ba0d
 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
Packit e9ba0d
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
Packit e9ba0d
 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Packit e9ba0d
 */
Packit e9ba0d
#ifndef _EVENT2_EVENT_H_
Packit e9ba0d
#define _EVENT2_EVENT_H_
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   @mainpage
Packit e9ba0d
Packit e9ba0d
  @section intro Introduction
Packit e9ba0d
Packit e9ba0d
  Libevent is an event notification library for developing scalable network
Packit e9ba0d
  servers.  The Libevent API provides a mechanism to execute a callback
Packit e9ba0d
  function when a specific event occurs on a file descriptor or after a
Packit e9ba0d
  timeout has been reached. Furthermore, Libevent also support callbacks due
Packit e9ba0d
  to signals or regular timeouts.
Packit e9ba0d
Packit e9ba0d
  Libevent is meant to replace the event loop found in event driven network
Packit e9ba0d
  servers. An application just needs to call event_dispatch() and then add or
Packit e9ba0d
  remove events dynamically without having to change the event loop.
Packit e9ba0d
Packit e9ba0d
Packit e9ba0d
  Currently, Libevent supports /dev/poll, kqueue(2), select(2), poll(2),
Packit e9ba0d
  epoll(4), and evports. The internal event mechanism is completely
Packit e9ba0d
  independent of the exposed event API, and a simple update of Libevent can
Packit e9ba0d
  provide new functionality without having to redesign the applications. As a
Packit e9ba0d
  result, Libevent allows for portable application development and provides
Packit e9ba0d
  the most scalable event notification mechanism available on an operating
Packit e9ba0d
  system.  Libevent can also be used for multithreaded programs.  Libevent
Packit e9ba0d
  should compile on Linux, *BSD, Mac OS X, Solaris and, Windows.
Packit e9ba0d
Packit e9ba0d
  @section usage Standard usage
Packit e9ba0d
Packit e9ba0d
  Every program that uses Libevent must inclurde the <event2/event.h>
Packit e9ba0d
  header, and pass the -levent flag to the linker.  (You can instead link
Packit e9ba0d
  -levent_core if you only want the main event and buffered IO-based code,
Packit e9ba0d
  and don't want to link any protocol code.)
Packit e9ba0d
Packit e9ba0d
  @section setup Library setup
Packit e9ba0d
Packit e9ba0d
  Before you call any other Libevent functions, you need to set up the
Packit e9ba0d
  library.  If you're going to use Libevent from multiple threads in a
Packit e9ba0d
  multithreaded application, you need to initialize thread support --
Packit e9ba0d
  typically by using evthread_use_pthreads() or
Packit e9ba0d
  evthread_use_windows_threads().  See <event2/thread.h> for more
Packit e9ba0d
  information.
Packit e9ba0d
Packit e9ba0d
  This is also the point where you can replace Libevent's memory
Packit e9ba0d
  management functions with event_set_mem_functions, and enable debug mode
Packit e9ba0d
  with event_enable_debug_mode().
Packit e9ba0d
Packit e9ba0d
  @section base Creating an event base
Packit e9ba0d
Packit e9ba0d
  Next, you need to create an event_base structure, using event_base_new()
Packit e9ba0d
  or event_base_new_with_config().  The event_base is responsible for
Packit e9ba0d
  keeping track of which events are "pending" (that is to say, being
Packit e9ba0d
  watched to see if they become active) and which events are "active".
Packit e9ba0d
  Every event is associated with a single event_base.
Packit e9ba0d
Packit e9ba0d
  @section event Event notification
Packit e9ba0d
Packit e9ba0d
  For each file descriptor that you wish to monitor, you must create an
Packit e9ba0d
  event structure with event_new().  (You may also declare an event
Packit e9ba0d
  structure and call event_assign() to initialize the members of the
Packit e9ba0d
  structure.)  To enable notification, you add the structure to the list
Packit e9ba0d
  of monitored events by calling event_add().  The event structure must
Packit e9ba0d
  remain allocated as long as it is active, so it should generally be
Packit e9ba0d
  allocated on the heap.
Packit e9ba0d
Packit e9ba0d
  @section loop Dispaching evets.
Packit e9ba0d
Packit e9ba0d
  Finally, you call event_base_dispatch() to loop and dispatch events.
Packit e9ba0d
  You can also use event_base_loop() for more fine-grained control.
Packit e9ba0d
Packit e9ba0d
  Currently, only one thread can be dispatching a given event_base at a
Packit e9ba0d
  time.  If you want to run events in multiple threads at once, you can
Packit e9ba0d
  either have a single event_base whose events add work to a work queue,
Packit e9ba0d
  or you can create multiple event_base objects.
Packit e9ba0d
Packit e9ba0d
  @section bufferevent I/O Buffers
Packit e9ba0d
Packit e9ba0d
  Libevent provides a buffered I/O abstraction on top of the regular event
Packit e9ba0d
  callbacks. This abstraction is called a bufferevent. A bufferevent
Packit e9ba0d
  provides input and output buffers that get filled and drained
Packit e9ba0d
  automatically. The user of a buffered event no longer deals directly
Packit e9ba0d
  with the I/O, but instead is reading from input and writing to output
Packit e9ba0d
  buffers.
Packit e9ba0d
Packit e9ba0d
  Once initialized via bufferevent_socket_new(), the bufferevent structure
Packit e9ba0d
  can be used repeatedly with bufferevent_enable() and
Packit e9ba0d
  bufferevent_disable().  Instead of reading and writing directly to a
Packit e9ba0d
  socket, you would call bufferevent_read() and bufferevent_write().
Packit e9ba0d
Packit e9ba0d
  When read enabled the bufferevent will try to read from the file descriptor
Packit e9ba0d
  and call the read callback. The write callback is executed whenever the
Packit e9ba0d
  output buffer is drained below the write low watermark, which is 0 by
Packit e9ba0d
  default.
Packit e9ba0d
Packit e9ba0d
  See <event2/bufferevent*.h> for more information.
Packit e9ba0d
Packit e9ba0d
  @section timers Timers
Packit e9ba0d
Packit e9ba0d
  Libevent can also be used to create timers that invoke a callback after a
Packit e9ba0d
  certain amount of time has expired. The evtimer_new() function returns
Packit e9ba0d
  an event struct to use as a timer. To activate the timer, call
Packit e9ba0d
  evtimer_add(). Timers can be deactivated by calling evtimer_del().
Packit e9ba0d
Packit e9ba0d
  @section evdns Asynchronous DNS resolution
Packit e9ba0d
Packit e9ba0d
  Libevent provides an asynchronous DNS resolver that should be used instead
Packit e9ba0d
  of the standard DNS resolver functions.  See the <event2/dns.h>
Packit e9ba0d
  functions for more detail.
Packit e9ba0d
Packit e9ba0d
  @section evhttp Event-driven HTTP servers
Packit e9ba0d
Packit e9ba0d
  Libevent provides a very simple event-driven HTTP server that can be
Packit e9ba0d
  embedded in your program and used to service HTTP requests.
Packit e9ba0d
Packit e9ba0d
  To use this capability, you need to include the <event2/http.h> header in your
Packit e9ba0d
  program.  See that header for more information.
Packit e9ba0d
Packit e9ba0d
  @section evrpc A framework for RPC servers and clients
Packit e9ba0d
Packit e9ba0d
  Libevent provides a framework for creating RPC servers and clients.  It
Packit e9ba0d
  takes care of marshaling and unmarshaling all data structures.
Packit e9ba0d
Packit e9ba0d
  @section api API Reference
Packit e9ba0d
Packit e9ba0d
  To browse the complete documentation of the libevent API, click on any of
Packit e9ba0d
  the following links.
Packit e9ba0d
Packit e9ba0d
  event2/event.h
Packit e9ba0d
  The primary libevent header
Packit e9ba0d
Packit e9ba0d
  event2/thread.h
Packit e9ba0d
  Functions for use by multithreaded programs
Packit e9ba0d
Packit e9ba0d
  event2/buffer.h and event2/bufferevent.h
Packit e9ba0d
  Buffer management for network reading and writing
Packit e9ba0d
Packit e9ba0d
  event2/util.h
Packit e9ba0d
  Utility functions for portable nonblocking network code
Packit e9ba0d
Packit e9ba0d
  event2/dns.h
Packit e9ba0d
  Asynchronous DNS resolution
Packit e9ba0d
Packit e9ba0d
  event2/http.h
Packit e9ba0d
  An embedded libevent-based HTTP server
Packit e9ba0d
Packit e9ba0d
  event2/rpc.h
Packit e9ba0d
  A framework for creating RPC servers and clients
Packit e9ba0d
Packit e9ba0d
 */
Packit e9ba0d
Packit e9ba0d
/** @file event2/event.h
Packit e9ba0d
Packit e9ba0d
  Core functions for waiting for and receiving events, and using event bases.
Packit e9ba0d
*/
Packit e9ba0d
Packit e9ba0d
#ifdef __cplusplus
Packit e9ba0d
extern "C" {
Packit e9ba0d
#endif
Packit e9ba0d
Packit e9ba0d
#include <event2/event-config.h>
Packit e9ba0d
#ifdef _EVENT_HAVE_SYS_TYPES_H
Packit e9ba0d
#include <sys/types.h>
Packit e9ba0d
#endif
Packit e9ba0d
#ifdef _EVENT_HAVE_SYS_TIME_H
Packit e9ba0d
#include <sys/time.h>
Packit e9ba0d
#endif
Packit e9ba0d
Packit e9ba0d
#include <stdio.h>
Packit e9ba0d
Packit e9ba0d
/* For int types. */
Packit e9ba0d
#include <event2/util.h>
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
 * Structure to hold information and state for a Libevent dispatch loop.
Packit e9ba0d
 *
Packit e9ba0d
 * The event_base lies at the center of Libevent; every application will
Packit e9ba0d
 * have one.  It keeps track of all pending and active events, and
Packit e9ba0d
 * notifies your application of the active ones.
Packit e9ba0d
 *
Packit e9ba0d
 * This is an opaque structure; you can allocate one using
Packit e9ba0d
 * event_base_new() or event_base_new_with_config().
Packit e9ba0d
 *
Packit e9ba0d
 * @see event_base_new(), event_base_free(), event_base_loop(),
Packit e9ba0d
 *    event_base_new_with_config()
Packit e9ba0d
 */
Packit e9ba0d
struct event_base
Packit e9ba0d
#ifdef _EVENT_IN_DOXYGEN
Packit e9ba0d
{/*Empty body so that doxygen will generate documentation here.*/}
Packit e9ba0d
#endif
Packit e9ba0d
;
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
 * @struct event
Packit e9ba0d
 *
Packit e9ba0d
 * Structure to represent a single event.
Packit e9ba0d
 *
Packit e9ba0d
 * An event can have some underlying condition it represents: a socket
Packit e9ba0d
 * becoming readable or writeable (or both), or a signal becoming raised.
Packit e9ba0d
 * (An event that represents no underlying condition is still useful: you
Packit e9ba0d
 * can use one to implement a timer, or to communicate between threads.)
Packit e9ba0d
 *
Packit e9ba0d
 * Generally, you can create events with event_new(), then make them
Packit e9ba0d
 * pending with event_add().  As your event_base runs, it will run the
Packit e9ba0d
 * callbacks of an events whose conditions are triggered.  When you
Packit e9ba0d
 * longer want the event, free it with event_free().
Packit e9ba0d
 *
Packit e9ba0d
 * In more depth:
Packit e9ba0d
 *
Packit e9ba0d
 * An event may be "pending" (one whose condition we are watching),
Packit e9ba0d
 * "active" (one whose condition has triggered and whose callback is about
Packit e9ba0d
 * to run), neither, or both.  Events come into existence via
Packit e9ba0d
 * event_assign() or event_new(), and are then neither active nor pending.
Packit e9ba0d
 *
Packit e9ba0d
 * To make an event pending, pass it to event_add().  When doing so, you
Packit e9ba0d
 * can also set a timeout for the event.
Packit e9ba0d
 *
Packit e9ba0d
 * Events become active during an event_base_loop() call when either their
Packit e9ba0d
 * condition has triggered, or when their timeout has elapsed.  You can
Packit e9ba0d
 * also activate an event manually using event_active().  The even_base
Packit e9ba0d
 * loop will run the callbacks of active events; after it has done so, it
Packit e9ba0d
 * marks them as no longer active.
Packit e9ba0d
 *
Packit e9ba0d
 * You can make an event non-pending by passing it to event_del().  This
Packit e9ba0d
 * also makes the event non-active.
Packit e9ba0d
 *
Packit e9ba0d
 * Events can be "persistent" or "non-persistent".  A non-persistent event
Packit e9ba0d
 * becomes non-pending as soon as it is triggered: thus, it only runs at
Packit e9ba0d
 * most once per call to event_add().  A persistent event remains pending
Packit e9ba0d
 * even when it becomes active: you'll need to event_del() it manually in
Packit e9ba0d
 * order to make it non-pending.  When a persistent event with a timeout
Packit e9ba0d
 * becomes active, its timeout is reset: this means you can use persistent
Packit e9ba0d
 * events to implement periodic timeouts.
Packit e9ba0d
 *
Packit e9ba0d
 * This should be treated as an opaque structure; you should never read or
Packit e9ba0d
 * write any of its fields directly.  For backward compatibility with old
Packit e9ba0d
 * code, it is defined in the event2/event_struct.h header; including this
Packit e9ba0d
 * header may make your code incompatible with other versions of Libevent.
Packit e9ba0d
 *
Packit e9ba0d
 * @see event_new(), event_free(), event_assign(), event_get_assignment(),
Packit e9ba0d
 *    event_add(), event_del(), event_active(), event_pending(),
Packit e9ba0d
 *    event_get_fd(), event_get_base(), event_get_events(),
Packit e9ba0d
 *    event_get_callback(), event_get_callback_arg(),
Packit e9ba0d
 *    event_priority_set()
Packit e9ba0d
 */
Packit e9ba0d
struct event
Packit e9ba0d
#ifdef _EVENT_IN_DOXYGEN
Packit e9ba0d
{/*Empty body so that doxygen will generate documentation here.*/}
Packit e9ba0d
#endif
Packit e9ba0d
;
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
 * Configuration for an event_base.
Packit e9ba0d
 *
Packit e9ba0d
 * There are many options that can be used to alter the behavior and
Packit e9ba0d
 * implementation of an event_base.  To avoid having to pass them all in a
Packit e9ba0d
 * complex many-argument constructor, we provide an abstract data type
Packit e9ba0d
 * wrhere you set up configation information before passing it to
Packit e9ba0d
 * event_base_new_with_config().
Packit e9ba0d
 *
Packit e9ba0d
 * @see event_config_new(), event_config_free(), event_base_new_with_config(),
Packit e9ba0d
 *   event_config_avoid_method(), event_config_require_features(),
Packit e9ba0d
 *   event_config_set_flag(), event_config_set_num_cpus_hint()
Packit e9ba0d
 */
Packit e9ba0d
struct event_config
Packit e9ba0d
#ifdef _EVENT_IN_DOXYGEN
Packit e9ba0d
{/*Empty body so that doxygen will generate documentation here.*/}
Packit e9ba0d
#endif
Packit e9ba0d
;
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
 * Enable some relatively expensive debugging checks in Libevent that
Packit e9ba0d
 * would normally be turned off.  Generally, these checks cause code that
Packit e9ba0d
 * would otherwise crash mysteriously to fail earlier with an assertion
Packit e9ba0d
 * failure.  Note that this method MUST be called before any events or
Packit e9ba0d
 * event_bases have been created.
Packit e9ba0d
 *
Packit e9ba0d
 * Debug mode can currently catch the following errors:
Packit e9ba0d
 *    An event is re-assigned while it is added
Packit e9ba0d
 *    Any function is called on a non-assigned event
Packit e9ba0d
 *
Packit e9ba0d
 * Note that debugging mode uses memory to track every event that has been
Packit e9ba0d
 * initialized (via event_assign, event_set, or event_new) but not yet
Packit e9ba0d
 * released (via event_free or event_debug_unassign).  If you want to use
Packit e9ba0d
 * debug mode, and you find yourself running out of memory, you will need
Packit e9ba0d
 * to use event_debug_unassign to explicitly stop tracking events that
Packit e9ba0d
 * are no longer considered set-up.
Packit e9ba0d
 *
Packit e9ba0d
 * @see event_debug_unassign()
Packit e9ba0d
 */
Packit e9ba0d
void event_enable_debug_mode(void);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
 * When debugging mode is enabled, informs Libevent that an event should no
Packit e9ba0d
 * longer be considered as assigned. When debugging mode is not enabled, does
Packit e9ba0d
 * nothing.
Packit e9ba0d
 *
Packit e9ba0d
 * This function must only be called on a non-added event.
Packit e9ba0d
 *
Packit e9ba0d
 * @see event_enable_debug_mode()
Packit e9ba0d
 */
Packit e9ba0d
void event_debug_unassign(struct event *);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
 * Create and return a new event_base to use with the rest of Libevent.
Packit e9ba0d
 *
Packit e9ba0d
 * @return a new event_base on success, or NULL on failure.
Packit e9ba0d
 *
Packit e9ba0d
 * @see event_base_free(), event_base_new_with_config()
Packit e9ba0d
 */
Packit e9ba0d
struct event_base *event_base_new(void);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Reinitialize the event base after a fork
Packit e9ba0d
Packit e9ba0d
  Some event mechanisms do not survive across fork.   The event base needs
Packit e9ba0d
  to be reinitialized with the event_reinit() function.
Packit e9ba0d
Packit e9ba0d
  @param base the event base that needs to be re-initialized
Packit e9ba0d
  @return 0 if successful, or -1 if some events could not be re-added.
Packit e9ba0d
  @see event_base_new()
Packit e9ba0d
*/
Packit e9ba0d
int event_reinit(struct event_base *base);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Event dispatching loop
Packit e9ba0d
Packit e9ba0d
  This loop will run the event base until either there are no more pending or
Packit e9ba0d
  active, or until something calls event_base_loopbreak() or
Packit e9ba0d
  event_base_loopexit().
Packit e9ba0d
Packit e9ba0d
  @param base the event_base structure returned by event_base_new() or
Packit e9ba0d
     event_base_new_with_config()
Packit e9ba0d
  @return 0 if successful, -1 if an error occurred, or 1 if we exited because
Packit e9ba0d
     no events were pending or active.
Packit e9ba0d
  @see event_base_loop()
Packit e9ba0d
 */
Packit e9ba0d
int event_base_dispatch(struct event_base *);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
 Get the kernel event notification mechanism used by Libevent.
Packit e9ba0d
Packit e9ba0d
 @param eb the event_base structure returned by event_base_new()
Packit e9ba0d
 @return a string identifying the kernel event mechanism (kqueue, epoll, etc.)
Packit e9ba0d
 */
Packit e9ba0d
const char *event_base_get_method(const struct event_base *);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Gets all event notification mechanisms supported by Libevent.
Packit e9ba0d
Packit e9ba0d
   This functions returns the event mechanism in order preferred by
Packit e9ba0d
   Libevent.  Note that this list will include all backends that
Packit e9ba0d
   Libevent has compiled-in support for, and will not necessarily check
Packit e9ba0d
   your OS to see whether it has the required resources.
Packit e9ba0d
Packit e9ba0d
   @return an array with pointers to the names of support methods.
Packit e9ba0d
     The end of the array is indicated by a NULL pointer.  If an
Packit e9ba0d
     error is encountered NULL is returned.
Packit e9ba0d
*/
Packit e9ba0d
const char **event_get_supported_methods(void);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Allocates a new event configuration object.
Packit e9ba0d
Packit e9ba0d
   The event configuration object can be used to change the behavior of
Packit e9ba0d
   an event base.
Packit e9ba0d
Packit e9ba0d
   @return an event_config object that can be used to store configuration, or
Packit e9ba0d
     NULL if an error is encountered.
Packit e9ba0d
   @see event_base_new_with_config(), event_config_free(), event_config
Packit e9ba0d
*/
Packit e9ba0d
struct event_config *event_config_new(void);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Deallocates all memory associated with an event configuration object
Packit e9ba0d
Packit e9ba0d
   @param cfg the event configuration object to be freed.
Packit e9ba0d
*/
Packit e9ba0d
void event_config_free(struct event_config *cfg);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Enters an event method that should be avoided into the configuration.
Packit e9ba0d
Packit e9ba0d
   This can be used to avoid event mechanisms that do not support certain
Packit e9ba0d
   file descriptor types, or for debugging to avoid certain event
Packit e9ba0d
   mechanisms.  An application can make use of multiple event bases to
Packit e9ba0d
   accommodate incompatible file descriptor types.
Packit e9ba0d
Packit e9ba0d
   @param cfg the event configuration object
Packit e9ba0d
   @param method the name of the event method to avoid
Packit e9ba0d
   @return 0 on success, -1 on failure.
Packit e9ba0d
*/
Packit e9ba0d
int event_config_avoid_method(struct event_config *cfg, const char *method);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   A flag used to describe which features an event_base (must) provide.
Packit e9ba0d
Packit e9ba0d
   Because of OS limitations, not every Libevent backend supports every
Packit e9ba0d
   possible feature.  You can use this type with
Packit e9ba0d
   event_config_require_features() to tell Libevent to only proceed if your
Packit e9ba0d
   event_base implements a given feature, and you can receive this type from
Packit e9ba0d
   event_base_get_features() to see which features are available.
Packit e9ba0d
*/
Packit e9ba0d
enum event_method_feature {
Packit e9ba0d
    /** Require an event method that allows edge-triggered events with EV_ET. */
Packit e9ba0d
    EV_FEATURE_ET = 0x01,
Packit e9ba0d
    /** Require an event method where having one event triggered among
Packit e9ba0d
     * many is [approximately] an O(1) operation. This excludes (for
Packit e9ba0d
     * example) select and poll, which are approximately O(N) for N
Packit e9ba0d
     * equal to the total number of possible events. */
Packit e9ba0d
    EV_FEATURE_O1 = 0x02,
Packit e9ba0d
    /** Require an event method that allows file descriptors as well as
Packit e9ba0d
     * sockets. */
Packit e9ba0d
    EV_FEATURE_FDS = 0x04
Packit e9ba0d
};
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   A flag passed to event_config_set_flag().
Packit e9ba0d
Packit e9ba0d
    These flags change the behavior of an allocated event_base.
Packit e9ba0d
Packit e9ba0d
    @see event_config_set_flag(), event_base_new_with_config(),
Packit e9ba0d
       event_method_feature
Packit e9ba0d
 */
Packit e9ba0d
enum event_base_config_flag {
Packit e9ba0d
	/** Do not allocate a lock for the event base, even if we have
Packit e9ba0d
	    locking set up. */
Packit e9ba0d
	EVENT_BASE_FLAG_NOLOCK = 0x01,
Packit e9ba0d
	/** Do not check the EVENT_* environment variables when configuring
Packit e9ba0d
	    an event_base  */
Packit e9ba0d
	EVENT_BASE_FLAG_IGNORE_ENV = 0x02,
Packit e9ba0d
	/** Windows only: enable the IOCP dispatcher at startup
Packit e9ba0d
Packit e9ba0d
	    If this flag is set then bufferevent_socket_new() and
Packit e9ba0d
	    evconn_listener_new() will use IOCP-backed implementations
Packit e9ba0d
	    instead of the usual select-based one on Windows.
Packit e9ba0d
	 */
Packit e9ba0d
	EVENT_BASE_FLAG_STARTUP_IOCP = 0x04,
Packit e9ba0d
	/** Instead of checking the current time every time the event loop is
Packit e9ba0d
	    ready to run timeout callbacks, check after each timeout callback.
Packit e9ba0d
	 */
Packit e9ba0d
	EVENT_BASE_FLAG_NO_CACHE_TIME = 0x08,
Packit e9ba0d
Packit e9ba0d
	/** If we are using the epoll backend, this flag says that it is
Packit e9ba0d
	    safe to use Libevent's internal change-list code to batch up
Packit e9ba0d
	    adds and deletes in order to try to do as few syscalls as
Packit e9ba0d
	    possible.  Setting this flag can make your code run faster, but
Packit e9ba0d
	    it may trigger a Linux bug: it is not safe to use this flag
Packit e9ba0d
	    if you have any fds cloned by dup() or its variants.  Doing so
Packit e9ba0d
	    will produce strange and hard-to-diagnose bugs.
Packit e9ba0d
Packit e9ba0d
	    This flag can also be activated by settnig the
Packit e9ba0d
	    EVENT_EPOLL_USE_CHANGELIST environment variable.
Packit e9ba0d
Packit e9ba0d
	    This flag has no effect if you wind up using a backend other than
Packit e9ba0d
	    epoll.
Packit e9ba0d
	 */
Packit e9ba0d
	EVENT_BASE_FLAG_EPOLL_USE_CHANGELIST = 0x10
Packit e9ba0d
};
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Return a bitmask of the features implemented by an event base.  This
Packit e9ba0d
   will be a bitwise OR of one or more of the values of
Packit e9ba0d
   event_method_feature
Packit e9ba0d
Packit e9ba0d
   @see event_method_feature
Packit e9ba0d
 */
Packit e9ba0d
int event_base_get_features(const struct event_base *base);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Enters a required event method feature that the application demands.
Packit e9ba0d
Packit e9ba0d
   Note that not every feature or combination of features is supported
Packit e9ba0d
   on every platform.  Code that requests features should be prepared
Packit e9ba0d
   to handle the case where event_base_new_with_config() returns NULL, as in:
Packit e9ba0d
   
Packit e9ba0d
     event_config_require_features(cfg, EV_FEATURE_ET);
Packit e9ba0d
     base = event_base_new_with_config(cfg);
Packit e9ba0d
     if (base == NULL) {
Packit e9ba0d
       // We can't get edge-triggered behavior here.
Packit e9ba0d
       event_config_require_features(cfg, 0);
Packit e9ba0d
       base = event_base_new_with_config(cfg);
Packit e9ba0d
     }
Packit e9ba0d
   
Packit e9ba0d
Packit e9ba0d
   @param cfg the event configuration object
Packit e9ba0d
   @param feature a bitfield of one or more event_method_feature values.
Packit e9ba0d
          Replaces values from previous calls to this function.
Packit e9ba0d
   @return 0 on success, -1 on failure.
Packit e9ba0d
   @see event_method_feature, event_base_new_with_config()
Packit e9ba0d
*/
Packit e9ba0d
int event_config_require_features(struct event_config *cfg, int feature);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
 * Sets one or more flags to configure what parts of the eventual event_base
Packit e9ba0d
 * will be initialized, and how they'll work.
Packit e9ba0d
 *
Packit e9ba0d
 * @see event_base_config_flags, event_base_new_with_config()
Packit e9ba0d
 **/
Packit e9ba0d
int event_config_set_flag(struct event_config *cfg, int flag);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
 * Records a hint for the number of CPUs in the system. This is used for
Packit e9ba0d
 * tuning thread pools, etc, for optimal performance.  In Libevent 2.0,
Packit e9ba0d
 * it is only on Windows, and only when IOCP is in use.
Packit e9ba0d
 *
Packit e9ba0d
 * @param cfg the event configuration object
Packit e9ba0d
 * @param cpus the number of cpus
Packit e9ba0d
 * @return 0 on success, -1 on failure.
Packit e9ba0d
 */
Packit e9ba0d
int event_config_set_num_cpus_hint(struct event_config *cfg, int cpus);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Initialize the event API.
Packit e9ba0d
Packit e9ba0d
  Use event_base_new_with_config() to initialize a new event base, taking
Packit e9ba0d
  the specified configuration under consideration.  The configuration object
Packit e9ba0d
  can currently be used to avoid certain event notification mechanisms.
Packit e9ba0d
Packit e9ba0d
  @param cfg the event configuration object
Packit e9ba0d
  @return an initialized event_base that can be used to registering events,
Packit e9ba0d
     or NULL if no event base can be created with the requested event_config.
Packit e9ba0d
  @see event_base_new(), event_base_free(), event_init(), event_assign()
Packit e9ba0d
*/
Packit e9ba0d
struct event_base *event_base_new_with_config(const struct event_config *);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Deallocate all memory associated with an event_base, and free the base.
Packit e9ba0d
Packit e9ba0d
  Note that this function will not close any fds or free any memory passed
Packit e9ba0d
  to event_new as the argument to callback.
Packit e9ba0d
Packit e9ba0d
  @param eb an event_base to be freed
Packit e9ba0d
 */
Packit e9ba0d
void event_base_free(struct event_base *);
Packit e9ba0d
Packit e9ba0d
/** @name Log severities
Packit e9ba0d
 */
Packit e9ba0d
/**@{*/
Packit e9ba0d
#define EVENT_LOG_DEBUG 0
Packit e9ba0d
#define EVENT_LOG_MSG   1
Packit e9ba0d
#define EVENT_LOG_WARN  2
Packit e9ba0d
#define EVENT_LOG_ERR   3
Packit e9ba0d
/**@}*/
Packit e9ba0d
Packit e9ba0d
/* Obsolete names: these are deprecated, but older programs might use them.
Packit e9ba0d
 * They violate the reserved-identifier namespace. */
Packit e9ba0d
#define _EVENT_LOG_DEBUG EVENT_LOG_DEBUG
Packit e9ba0d
#define _EVENT_LOG_MSG EVENT_LOG_MSG
Packit e9ba0d
#define _EVENT_LOG_WARN EVENT_LOG_WARN
Packit e9ba0d
#define _EVENT_LOG_ERR EVENT_LOG_ERR
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  A callback function used to intercept Libevent's log messages.
Packit e9ba0d
Packit e9ba0d
  @see event_set_log_callback
Packit e9ba0d
 */
Packit e9ba0d
typedef void (*event_log_cb)(int severity, const char *msg);
Packit e9ba0d
/**
Packit e9ba0d
  Redirect Libevent's log messages.
Packit e9ba0d
Packit e9ba0d
  @param cb a function taking two arguments: an integer severity between
Packit e9ba0d
     _EVENT_LOG_DEBUG and _EVENT_LOG_ERR, and a string.  If cb is NULL,
Packit e9ba0d
	 then the default log is used.
Packit e9ba0d
Packit e9ba0d
  NOTE: The function you provide *must not* call any other libevent
Packit e9ba0d
  functionality.  Doing so can produce undefined behavior.
Packit e9ba0d
  */
Packit e9ba0d
void event_set_log_callback(event_log_cb cb);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   A function to be called if Libevent encounters a fatal internal error.
Packit e9ba0d
Packit e9ba0d
   @see event_set_fatal_callback
Packit e9ba0d
 */
Packit e9ba0d
typedef void (*event_fatal_cb)(int err);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
 Override Libevent's behavior in the event of a fatal internal error.
Packit e9ba0d
Packit e9ba0d
 By default, Libevent will call exit(1) if a programming error makes it
Packit e9ba0d
 impossible to continue correct operation.  This function allows you to supply
Packit e9ba0d
 another callback instead.  Note that if the function is ever invoked,
Packit e9ba0d
 something is wrong with your program, or with Libevent: any subsequent calls
Packit e9ba0d
 to Libevent may result in undefined behavior.
Packit e9ba0d
Packit e9ba0d
 Libevent will (almost) always log an _EVENT_LOG_ERR message before calling
Packit e9ba0d
 this function; look at the last log message to see why Libevent has died.
Packit e9ba0d
 */
Packit e9ba0d
void event_set_fatal_callback(event_fatal_cb cb);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Associate a different event base with an event.
Packit e9ba0d
Packit e9ba0d
  The event to be associated must not be currently active or pending.
Packit e9ba0d
Packit e9ba0d
  @param eb the event base
Packit e9ba0d
  @param ev the event
Packit e9ba0d
  @return 0 on success, -1 on failure.
Packit e9ba0d
 */
Packit e9ba0d
int event_base_set(struct event_base *, struct event *);
Packit e9ba0d
Packit e9ba0d
/** @name Loop flags
Packit e9ba0d
Packit e9ba0d
    These flags control the behavior of event_base_loop().
Packit e9ba0d
 */
Packit e9ba0d
/**@{*/
Packit e9ba0d
/** Block until we have an active event, then exit once all active events
Packit e9ba0d
 * have had their callbacks run. */
Packit e9ba0d
#define EVLOOP_ONCE	0x01
Packit e9ba0d
/** Do not block: see which events are ready now, run the callbacks
Packit e9ba0d
 * of the highest-priority ones, then exit. */
Packit e9ba0d
#define EVLOOP_NONBLOCK	0x02
Packit e9ba0d
/**@}*/
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Wait for events to become active, and run their callbacks.
Packit e9ba0d
Packit e9ba0d
  This is a more flexible version of event_base_dispatch().
Packit e9ba0d
Packit e9ba0d
  By default, this loop will run the event base until either there are no more
Packit e9ba0d
  pending or active events, or until something calls event_base_loopbreak() or
Packit e9ba0d
  event_base_loopexit().  You can override this behavior with the 'flags'
Packit e9ba0d
  argument.
Packit e9ba0d
Packit e9ba0d
  @param eb the event_base structure returned by event_base_new() or
Packit e9ba0d
     event_base_new_with_config()
Packit e9ba0d
  @param flags any combination of EVLOOP_ONCE | EVLOOP_NONBLOCK
Packit e9ba0d
  @return 0 if successful, -1 if an error occurred, or 1 if we exited because
Packit e9ba0d
     no events were pending or active.
Packit e9ba0d
  @see event_base_loopexit(), event_base_dispatch(), EVLOOP_ONCE,
Packit e9ba0d
     EVLOOP_NONBLOCK
Packit e9ba0d
  */
Packit e9ba0d
int event_base_loop(struct event_base *, int);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Exit the event loop after the specified time
Packit e9ba0d
Packit e9ba0d
  The next event_base_loop() iteration after the given timer expires will
Packit e9ba0d
  complete normally (handling all queued events) then exit without
Packit e9ba0d
  blocking for events again.
Packit e9ba0d
Packit e9ba0d
  Subsequent invocations of event_base_loop() will proceed normally.
Packit e9ba0d
Packit e9ba0d
  @param eb the event_base structure returned by event_init()
Packit e9ba0d
  @param tv the amount of time after which the loop should terminate,
Packit e9ba0d
    or NULL to exit after running all currently active events.
Packit e9ba0d
  @return 0 if successful, or -1 if an error occurred
Packit e9ba0d
  @see event_base_loopbreak()
Packit e9ba0d
 */
Packit e9ba0d
int event_base_loopexit(struct event_base *, const struct timeval *);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Abort the active event_base_loop() immediately.
Packit e9ba0d
Packit e9ba0d
  event_base_loop() will abort the loop after the next event is completed;
Packit e9ba0d
  event_base_loopbreak() is typically invoked from this event's callback.
Packit e9ba0d
  This behavior is analogous to the "break;" statement.
Packit e9ba0d
Packit e9ba0d
  Subsequent invocations of event_loop() will proceed normally.
Packit e9ba0d
Packit e9ba0d
  @param eb the event_base structure returned by event_init()
Packit e9ba0d
  @return 0 if successful, or -1 if an error occurred
Packit e9ba0d
  @see event_base_loopexit()
Packit e9ba0d
 */
Packit e9ba0d
int event_base_loopbreak(struct event_base *);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Checks if the event loop was told to exit by event_loopexit().
Packit e9ba0d
Packit e9ba0d
  This function will return true for an event_base at every point after
Packit e9ba0d
  event_loopexit() is called, until the event loop is next entered.
Packit e9ba0d
Packit e9ba0d
  @param eb the event_base structure returned by event_init()
Packit e9ba0d
  @return true if event_base_loopexit() was called on this event base,
Packit e9ba0d
    or 0 otherwise
Packit e9ba0d
  @see event_base_loopexit()
Packit e9ba0d
  @see event_base_got_break()
Packit e9ba0d
 */
Packit e9ba0d
int event_base_got_exit(struct event_base *);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Checks if the event loop was told to abort immediately by event_loopbreak().
Packit e9ba0d
Packit e9ba0d
  This function will return true for an event_base at every point after
Packit e9ba0d
  event_loopbreak() is called, until the event loop is next entered.
Packit e9ba0d
Packit e9ba0d
  @param eb the event_base structure returned by event_init()
Packit e9ba0d
  @return true if event_base_loopbreak() was called on this event base,
Packit e9ba0d
    or 0 otherwise
Packit e9ba0d
  @see event_base_loopbreak()
Packit e9ba0d
  @see event_base_got_exit()
Packit e9ba0d
 */
Packit e9ba0d
int event_base_got_break(struct event_base *);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
 * @name event flags
Packit e9ba0d
 *
Packit e9ba0d
 * Flags to pass to event_new(), event_assign(), event_pending(), and
Packit e9ba0d
 * anything else with an argument of the form "short events"
Packit e9ba0d
 */
Packit e9ba0d
/**@{*/
Packit e9ba0d
/** Indicates that a timeout has occurred.  It's not necessary to pass
Packit e9ba0d
 * this flag to event_for new()/event_assign() to get a timeout. */
Packit e9ba0d
#define EV_TIMEOUT	0x01
Packit e9ba0d
/** Wait for a socket or FD to become readable */
Packit e9ba0d
#define EV_READ		0x02
Packit e9ba0d
/** Wait for a socket or FD to become writeable */
Packit e9ba0d
#define EV_WRITE	0x04
Packit e9ba0d
/** Wait for a POSIX signal to be raised*/
Packit e9ba0d
#define EV_SIGNAL	0x08
Packit e9ba0d
/**
Packit e9ba0d
 * Persistent event: won't get removed automatically when activated.
Packit e9ba0d
 *
Packit e9ba0d
 * When a persistent event with a timeout becomes activated, its timeout
Packit e9ba0d
 * is reset to 0.
Packit e9ba0d
 */
Packit e9ba0d
#define EV_PERSIST	0x10
Packit e9ba0d
/** Select edge-triggered behavior, if supported by the backend. */
Packit e9ba0d
#define EV_ET       0x20
Packit e9ba0d
/**@}*/
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   @name evtimer_* macros
Packit e9ba0d
Packit e9ba0d
    Aliases for working with one-shot timer events */
Packit e9ba0d
/**@{*/
Packit e9ba0d
#define evtimer_assign(ev, b, cb, arg) \
Packit e9ba0d
	event_assign((ev), (b), -1, 0, (cb), (arg))
Packit e9ba0d
#define evtimer_new(b, cb, arg)	       event_new((b), -1, 0, (cb), (arg))
Packit e9ba0d
#define evtimer_add(ev, tv)		event_add((ev), (tv))
Packit e9ba0d
#define evtimer_del(ev)			event_del(ev)
Packit e9ba0d
#define evtimer_pending(ev, tv)		event_pending((ev), EV_TIMEOUT, (tv))
Packit e9ba0d
#define evtimer_initialized(ev)		event_initialized(ev)
Packit e9ba0d
/**@}*/
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   @name evsignal_* macros
Packit e9ba0d
Packit e9ba0d
   Aliases for working with signal events
Packit e9ba0d
 */
Packit e9ba0d
/**@{*/
Packit e9ba0d
#define evsignal_add(ev, tv)		event_add((ev), (tv))
Packit e9ba0d
#define evsignal_assign(ev, b, x, cb, arg)			\
Packit e9ba0d
	event_assign((ev), (b), (x), EV_SIGNAL|EV_PERSIST, cb, (arg))
Packit e9ba0d
#define evsignal_new(b, x, cb, arg)				\
Packit e9ba0d
	event_new((b), (x), EV_SIGNAL|EV_PERSIST, (cb), (arg))
Packit e9ba0d
#define evsignal_del(ev)		event_del(ev)
Packit e9ba0d
#define evsignal_pending(ev, tv)	event_pending((ev), EV_SIGNAL, (tv))
Packit e9ba0d
#define evsignal_initialized(ev)	event_initialized(ev)
Packit e9ba0d
/**@}*/
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   A callback function for an event.
Packit e9ba0d
Packit e9ba0d
   It receives three arguments:
Packit e9ba0d
Packit e9ba0d
   @param fd An fd or signal
Packit e9ba0d
   @param events One or more EV_* flags
Packit e9ba0d
   @param arg A user-supplied argument.
Packit e9ba0d
Packit e9ba0d
   @see event_new()
Packit e9ba0d
 */
Packit e9ba0d
typedef void (*event_callback_fn)(evutil_socket_t, short, void *);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Allocate and asssign a new event structure, ready to be added.
Packit e9ba0d
Packit e9ba0d
  The function event_new() returns a new event that can be used in
Packit e9ba0d
  future calls to event_add() and event_del().  The fd and events
Packit e9ba0d
  arguments determine which conditions will trigger the event; the
Packit e9ba0d
  callback and callback_arg arguments tell Libevent what to do when the
Packit e9ba0d
  event becomes active.
Packit e9ba0d
Packit e9ba0d
  If events contains one of EV_READ, EV_WRITE, or EV_READ|EV_WRITE, then
Packit e9ba0d
  fd is a file descriptor or socket that should get monitored for
Packit e9ba0d
  readiness to read, readiness to write, or readiness for either operation
Packit e9ba0d
  (respectively).  If events contains EV_SIGNAL, then fd is a signal
Packit e9ba0d
  number to wait for.  If events contains none of those flags, then the
Packit e9ba0d
  event can be triggered only by a timeout or by manual activation with
Packit e9ba0d
  event_active(): In this case, fd must be -1.
Packit e9ba0d
Packit e9ba0d
  The EV_PERSIST flag can also be passed in the events argument: it makes
Packit e9ba0d
  event_add() persistent until event_del() is called.
Packit e9ba0d
Packit e9ba0d
  The EV_ET flag is compatible with EV_READ and EV_WRITE, and supported
Packit e9ba0d
  only by certain backends.  It tells Libevent to use edge-triggered
Packit e9ba0d
  events.
Packit e9ba0d
Packit e9ba0d
  The EV_TIMEOUT flag has no effect here.
Packit e9ba0d
Packit e9ba0d
  It is okay to have multiple events all listening on the same fds; but
Packit e9ba0d
  they must either all be edge-triggered, or all not be edge triggerd.
Packit e9ba0d
Packit e9ba0d
  When the event becomes active, the event loop will run the provided
Packit e9ba0d
  callbuck function, with three arguments.  The first will be the provided
Packit e9ba0d
  fd value.  The second will be a bitfield of the events that triggered:
Packit e9ba0d
  EV_READ, EV_WRITE, or EV_SIGNAL.  Here the EV_TIMEOUT flag indicates
Packit e9ba0d
  that a timeout occurred, and EV_ET indicates that an edge-triggered
Packit e9ba0d
  event occurred.  The third event will be the callback_arg pointer that
Packit e9ba0d
  you provide.
Packit e9ba0d
Packit e9ba0d
  @param base the event base to which the event should be attached.
Packit e9ba0d
  @param fd the file descriptor or signal to be monitored, or -1.
Packit e9ba0d
  @param events desired events to monitor: bitfield of EV_READ, EV_WRITE,
Packit e9ba0d
      EV_SIGNAL, EV_PERSIST, EV_ET.
Packit e9ba0d
  @param callback callback function to be invoked when the event occurs
Packit e9ba0d
  @param callback_arg an argument to be passed to the callback function
Packit e9ba0d
Packit e9ba0d
  @return a newly allocated struct event that must later be freed with
Packit e9ba0d
    event_free().
Packit e9ba0d
  @see event_free(), event_add(), event_del(), event_assign()
Packit e9ba0d
 */
Packit e9ba0d
struct event *event_new(struct event_base *, evutil_socket_t, short, event_callback_fn, void *);
Packit e9ba0d
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Prepare a new, already-allocated event structure to be added.
Packit e9ba0d
Packit e9ba0d
  The function event_assign() prepares the event structure ev to be used
Packit e9ba0d
  in future calls to event_add() and event_del().  Unlike event_new(), it
Packit e9ba0d
  doesn't allocate memory itself: it requires that you have already
Packit e9ba0d
  allocated a struct event, probably on the heap.  Doing this will
Packit e9ba0d
  typically make your code depend on the size of the event structure, and
Packit e9ba0d
  thereby create incompatibility with future versions of Libevent.
Packit e9ba0d
Packit e9ba0d
  The easiest way to avoid this problem is just to use event_new() and
Packit e9ba0d
  event_free() instead.
Packit e9ba0d
Packit e9ba0d
  A slightly harder way to future-proof your code is to use
Packit e9ba0d
  event_get_struct_event_size() to determine the required size of an event
Packit e9ba0d
  at runtime.
Packit e9ba0d
Packit e9ba0d
  Note that it is NOT safe to call this function on an event that is
Packit e9ba0d
  active or pending.  Doing so WILL corrupt internal data structures in
Packit e9ba0d
  Libevent, and lead to strange, hard-to-diagnose bugs.  You _can_ use
Packit e9ba0d
  event_assign to change an existing event, but only if it is not active
Packit e9ba0d
  or pending!
Packit e9ba0d
Packit e9ba0d
  The arguments for this function, and the behavior of the events that it
Packit e9ba0d
  makes, are as for event_new().
Packit e9ba0d
Packit e9ba0d
  @param ev an event struct to be modified
Packit e9ba0d
  @param base the event base to which ev should be attached.
Packit e9ba0d
  @param fd the file descriptor to be monitored
Packit e9ba0d
  @param events desired events to monitor; can be EV_READ and/or EV_WRITE
Packit e9ba0d
  @param callback callback function to be invoked when the event occurs
Packit e9ba0d
  @param callback_arg an argument to be passed to the callback function
Packit e9ba0d
Packit e9ba0d
  @return 0 if success, or -1 on invalid arguments.
Packit e9ba0d
Packit e9ba0d
  @see event_new(), event_add(), event_del(), event_base_once(),
Packit e9ba0d
    event_get_struct_event_size()
Packit e9ba0d
  */
Packit e9ba0d
int event_assign(struct event *, struct event_base *, evutil_socket_t, short, event_callback_fn, void *);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Deallocate a struct event * returned by event_new().
Packit e9ba0d
Packit e9ba0d
   If the event is pending or active, first make it non-pending and
Packit e9ba0d
   non-active.
Packit e9ba0d
 */
Packit e9ba0d
void event_free(struct event *);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Schedule a one-time event
Packit e9ba0d
Packit e9ba0d
  The function event_base_once() is similar to event_set().  However, it
Packit e9ba0d
  schedules a callback to be called exactly once, and does not require the
Packit e9ba0d
  caller to prepare an event structure.
Packit e9ba0d
Packit e9ba0d
  Note that in Libevent 2.0 and earlier, if the event is never triggered,
Packit e9ba0d
  the internal memory used to hold it will never be freed.  This may be
Packit e9ba0d
  fixed in a later version of Libevent.
Packit e9ba0d
Packit e9ba0d
  @param base an event_base
Packit e9ba0d
  @param fd a file descriptor to monitor, or -1 for no fd.
Packit e9ba0d
  @param events event(s) to monitor; can be any of EV_READ |
Packit e9ba0d
         EV_WRITE, or EV_TIMEOUT
Packit e9ba0d
  @param callback callback function to be invoked when the event occurs
Packit e9ba0d
  @param arg an argument to be passed to the callback function
Packit e9ba0d
  @param timeout the maximum amount of time to wait for the event. NULL
Packit e9ba0d
         makes an EV_READ/EV_WRITE event make forever; NULL makes an
Packit e9ba0d
        EV_TIMEOUT event succees immediately.
Packit e9ba0d
  @return 0 if successful, or -1 if an error occurred
Packit e9ba0d
 */
Packit e9ba0d
int event_base_once(struct event_base *, evutil_socket_t, short, event_callback_fn, void *, const struct timeval *);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Add an event to the set of pending events.
Packit e9ba0d
Packit e9ba0d
  The function event_add() schedules the execution of the ev event when the
Packit e9ba0d
  event specified in event_assign()/event_new() occurs, or when the time
Packit e9ba0d
  specified in timeout has elapesed.  If atimeout is NULL, no timeout
Packit e9ba0d
  occurs and the function will only be
Packit e9ba0d
  called if a matching event occurs.  The event in the
Packit e9ba0d
  ev argument must be already initialized by event_assign() or event_new()
Packit e9ba0d
  and may not be used
Packit e9ba0d
  in calls to event_assign() until it is no longer pending.
Packit e9ba0d
Packit e9ba0d
  If the event in the ev argument already has a scheduled timeout, calling
Packit e9ba0d
  event_add() replaces the old timeout with the new one, or clears the old
Packit e9ba0d
  timeout if the timeout argument is NULL.
Packit e9ba0d
Packit e9ba0d
  @param ev an event struct initialized via event_set()
Packit e9ba0d
  @param timeout the maximum amount of time to wait for the event, or NULL
Packit e9ba0d
         to wait forever
Packit e9ba0d
  @return 0 if successful, or -1 if an error occurred
Packit e9ba0d
  @see event_del(), event_assign(), event_new()
Packit e9ba0d
  */
Packit e9ba0d
int event_add(struct event *ev, const struct timeval *timeout);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Remove an event from the set of monitored events.
Packit e9ba0d
Packit e9ba0d
  The function event_del() will cancel the event in the argument ev.  If the
Packit e9ba0d
  event has already executed or has never been added the call will have no
Packit e9ba0d
  effect.
Packit e9ba0d
Packit e9ba0d
  @param ev an event struct to be removed from the working set
Packit e9ba0d
  @return 0 if successful, or -1 if an error occurred
Packit e9ba0d
  @see event_add()
Packit e9ba0d
 */
Packit e9ba0d
int event_del(struct event *);
Packit e9ba0d
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Make an event active.
Packit e9ba0d
Packit e9ba0d
  You can use this function on a pending or a non-pending event to make it
Packit e9ba0d
  active, so that its callback will be run by event_base_dispatch() or
Packit e9ba0d
  event_base_loop().
Packit e9ba0d
Packit e9ba0d
  One common use in multithreaded programs is to wake the thread running
Packit e9ba0d
  event_base_loop() from another thread.
Packit e9ba0d
Packit e9ba0d
  @param ev an event to make active.
Packit e9ba0d
  @param res a set of flags to pass to the event's callback.
Packit e9ba0d
  @param ncalls an obsolete argument: this is ignored.
Packit e9ba0d
 **/
Packit e9ba0d
void event_active(struct event *ev, int res, short ncalls);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Checks if a specific event is pending or scheduled.
Packit e9ba0d
Packit e9ba0d
  @param ev an event struct previously passed to event_add()
Packit e9ba0d
  @param events the requested event type; any of EV_TIMEOUT|EV_READ|
Packit e9ba0d
         EV_WRITE|EV_SIGNAL
Packit e9ba0d
  @param tv if this field is not NULL, and the event has a timeout,
Packit e9ba0d
         this field is set to hold the time at which the timeout will
Packit e9ba0d
	 expire.
Packit e9ba0d
Packit e9ba0d
  @return true if the event is pending on any of the events in 'what', (that
Packit e9ba0d
  is to say, it has been added), or 0 if the event is not added.
Packit e9ba0d
 */
Packit e9ba0d
int event_pending(const struct event *ev, short events, struct timeval *tv);
Packit e9ba0d
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Test if an event structure might be initialized.
Packit e9ba0d
Packit e9ba0d
  The event_initialized() function can be used to check if an event has been
Packit e9ba0d
  initialized.
Packit e9ba0d
Packit e9ba0d
  Warning: This function is only useful for distinguishing a a zeroed-out
Packit e9ba0d
    piece of memory from an initialized event, it can easily be confused by
Packit e9ba0d
    uninitialized memory.  Thus, it should ONLY be used to distinguish an
Packit e9ba0d
    initialized event from zero.
Packit e9ba0d
Packit e9ba0d
  @param ev an event structure to be tested
Packit e9ba0d
  @return 1 if the structure might be initialized, or 0 if it has not been
Packit e9ba0d
          initialized
Packit e9ba0d
 */
Packit e9ba0d
int event_initialized(const struct event *ev);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Get the signal number assigned to a signal event
Packit e9ba0d
*/
Packit e9ba0d
#define event_get_signal(ev) ((int)event_get_fd(ev))
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Get the socket or signal assigned to an event, or -1 if the event has
Packit e9ba0d
   no socket.
Packit e9ba0d
*/
Packit e9ba0d
evutil_socket_t event_get_fd(const struct event *ev);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Get the event_base associated with an event.
Packit e9ba0d
*/
Packit e9ba0d
struct event_base *event_get_base(const struct event *ev);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Return the events (EV_READ, EV_WRITE, etc) assigned to an event.
Packit e9ba0d
*/
Packit e9ba0d
short event_get_events(const struct event *ev);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Return the callback assigned to an event.
Packit e9ba0d
*/
Packit e9ba0d
event_callback_fn event_get_callback(const struct event *ev);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Return the callback argument assigned to an event.
Packit e9ba0d
*/
Packit e9ba0d
void *event_get_callback_arg(const struct event *ev);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Extract _all_ of arguments given to construct a given event.  The
Packit e9ba0d
   event_base is copied into *base_out, the fd is copied into *fd_out, and so
Packit e9ba0d
   on.
Packit e9ba0d
Packit e9ba0d
   If any of the "_out" arguments is NULL, it will be ignored.
Packit e9ba0d
 */
Packit e9ba0d
void event_get_assignment(const struct event *event,
Packit e9ba0d
    struct event_base **base_out, evutil_socket_t *fd_out, short *events_out,
Packit e9ba0d
    event_callback_fn *callback_out, void **arg_out);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Return the size of struct event that the Libevent library was compiled
Packit e9ba0d
   with.
Packit e9ba0d
Packit e9ba0d
   This will be NO GREATER than sizeof(struct event) if you're running with
Packit e9ba0d
   the same version of Libevent that your application was built with, but
Packit e9ba0d
   otherwise might not.
Packit e9ba0d
Packit e9ba0d
   Note that it might be SMALLER than sizeof(struct event) if some future
Packit e9ba0d
   version of Libevent adds extra padding to the end of struct event.
Packit e9ba0d
   We might do this to help ensure ABI-compatibility between different
Packit e9ba0d
   versions of Libevent.
Packit e9ba0d
 */
Packit e9ba0d
size_t event_get_struct_event_size(void);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Get the Libevent version.
Packit e9ba0d
Packit e9ba0d
   Note that this will give you the version of the library that you're
Packit e9ba0d
   currently linked against, not the version of the headers that you've
Packit e9ba0d
   compiled against.
Packit e9ba0d
Packit e9ba0d
   @return a string containing the version number of Libevent
Packit e9ba0d
*/
Packit e9ba0d
const char *event_get_version(void);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Return a numeric representation of Libevent's version.
Packit e9ba0d
Packit e9ba0d
   Note that this will give you the version of the library that you're
Packit e9ba0d
   currently linked against, not the version of the headers you've used to
Packit e9ba0d
   compile.
Packit e9ba0d
Packit e9ba0d
   The format uses one byte each for the major, minor, and patchlevel parts of
Packit e9ba0d
   the version number.  The low-order byte is unused.  For example, version
Packit e9ba0d
   2.0.1-alpha has a numeric representation of 0x02000100
Packit e9ba0d
*/
Packit e9ba0d
ev_uint32_t event_get_version_number(void);
Packit e9ba0d
Packit e9ba0d
/** As event_get_version, but gives the version of Libevent's headers. */
Packit e9ba0d
#define LIBEVENT_VERSION _EVENT_VERSION
Packit e9ba0d
/** As event_get_version_number, but gives the version number of Libevent's
Packit e9ba0d
 * headers. */
Packit e9ba0d
#define LIBEVENT_VERSION_NUMBER _EVENT_NUMERIC_VERSION
Packit e9ba0d
Packit e9ba0d
/** Largest number of priorities that Libevent can support. */
Packit e9ba0d
#define EVENT_MAX_PRIORITIES 256
Packit e9ba0d
/**
Packit e9ba0d
  Set the number of different event priorities
Packit e9ba0d
Packit e9ba0d
  By default Libevent schedules all active events with the same priority.
Packit e9ba0d
  However, some time it is desirable to process some events with a higher
Packit e9ba0d
  priority than others.  For that reason, Libevent supports strict priority
Packit e9ba0d
  queues.  Active events with a lower priority are always processed before
Packit e9ba0d
  events with a higher priority.
Packit e9ba0d
Packit e9ba0d
  The number of different priorities can be set initially with the
Packit e9ba0d
  event_base_priority_init() function.  This function should be called
Packit e9ba0d
  before the first call to event_base_dispatch().  The
Packit e9ba0d
  event_priority_set() function can be used to assign a priority to an
Packit e9ba0d
  event.  By default, Libevent assigns the middle priority to all events
Packit e9ba0d
  unless their priority is explicitly set.
Packit e9ba0d
Packit e9ba0d
  Note that urgent-priority events can starve less-urgent events: after
Packit e9ba0d
  running all urgent-priority callbacks, Libevent checks for more urgent
Packit e9ba0d
  events again, before running less-urgent events.  Less-urgent events
Packit e9ba0d
  will not have their callbacks run until there are no events more urgent
Packit e9ba0d
  than them that want to be active.
Packit e9ba0d
Packit e9ba0d
  @param eb the event_base structure returned by event_base_new()
Packit e9ba0d
  @param npriorities the maximum number of priorities
Packit e9ba0d
  @return 0 if successful, or -1 if an error occurred
Packit e9ba0d
  @see event_priority_set()
Packit e9ba0d
 */
Packit e9ba0d
int	event_base_priority_init(struct event_base *, int);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
  Assign a priority to an event.
Packit e9ba0d
Packit e9ba0d
  @param ev an event struct
Packit e9ba0d
  @param priority the new priority to be assigned
Packit e9ba0d
  @return 0 if successful, or -1 if an error occurred
Packit e9ba0d
  @see event_priority_init()
Packit e9ba0d
  */
Packit e9ba0d
int	event_priority_set(struct event *, int);
Packit e9ba0d
Packit e9ba0d
/**
Packit e9ba0d
   Prepare an event_base to use a large number of timeouts with the same
Packit e9ba0d
   duration.
Packit e9ba0d
Packit e9ba0d
   Libevent's default scheduling algorithm is optimized for having a large
Packit e9ba0d
   number of timeouts with their durations more or less randomly
Packit e9ba0d
   distributed.  But if you have a large number of timeouts that all have
Packit e9ba0d
   the same duration (for example, if you have a large number of
Packit e9ba0d
   connections that all have a 10-second timeout), then you can improve
Packit e9ba0d
   Libevent's performance by telling Libevent about it.
Packit e9ba0d
Packit e9ba0d
   To do this, call this function with the common duration.  It will return a
Packit e9ba0d
   pointer to a different, opaque timeout value.  (Don't depend on its actual
Packit e9ba0d
   contents!)  When you use this timeout value in event_add(), Libevent will
Packit e9ba0d
   schedule the event more efficiently.
Packit e9ba0d
Packit e9ba0d
   (This optimization probably will not be worthwhile until you have thousands
Packit e9ba0d
   or tens of thousands of events with the same timeout.)
Packit e9ba0d
 */
Packit e9ba0d
const struct timeval *event_base_init_common_timeout(struct event_base *base,
Packit e9ba0d
    const struct timeval *duration);
Packit e9ba0d
Packit e9ba0d
#if !defined(_EVENT_DISABLE_MM_REPLACEMENT) || defined(_EVENT_IN_DOXYGEN)
Packit e9ba0d
/**
Packit e9ba0d
 Override the functions that Libevent uses for memory management.
Packit e9ba0d
Packit e9ba0d
 Usually, Libevent uses the standard libc functions malloc, realloc, and
Packit e9ba0d
 free to allocate memory.  Passing replacements for those functions to
Packit e9ba0d
 event_set_mem_functions() overrides this behavior.
Packit e9ba0d
Packit e9ba0d
 Note that all memory returned from Libevent will be allocated by the
Packit e9ba0d
 replacement functions rather than by malloc() and realloc().  Thus, if you
Packit e9ba0d
 have replaced those functions, it will not be appropriate to free() memory
Packit e9ba0d
 that you get from Libevent.  Instead, you must use the free_fn replacement
Packit e9ba0d
 that you provided.
Packit e9ba0d
Packit e9ba0d
 Note also that if you are going to call this function, you should do so
Packit e9ba0d
 before any call to any Libevent function that does allocation.
Packit e9ba0d
 Otherwise, those funtions will allocate their memory using malloc(), but
Packit e9ba0d
 then later free it using your provided free_fn.
Packit e9ba0d
Packit e9ba0d
 @param malloc_fn A replacement for malloc.
Packit e9ba0d
 @param realloc_fn A replacement for realloc
Packit e9ba0d
 @param free_fn A replacement for free.
Packit e9ba0d
 **/
Packit e9ba0d
void event_set_mem_functions(
Packit e9ba0d
	void *(*malloc_fn)(size_t sz),
Packit e9ba0d
	void *(*realloc_fn)(void *ptr, size_t sz),
Packit e9ba0d
	void (*free_fn)(void *ptr));
Packit e9ba0d
/** This definition is present if Libevent was built with support for
Packit e9ba0d
    event_set_mem_functions() */
Packit e9ba0d
#define EVENT_SET_MEM_FUNCTIONS_IMPLEMENTED
Packit e9ba0d
#endif
Packit e9ba0d
Packit e9ba0d
void event_base_dump_events(struct event_base *, FILE *);
Packit e9ba0d
Packit e9ba0d
/** Sets 'tv' to the current time (as returned by gettimeofday()),
Packit e9ba0d
    looking at the cached value in 'base' if possible, and calling
Packit e9ba0d
    gettimeofday() or clock_gettime() as appropriate if there is no
Packit e9ba0d
    cached time.
Packit e9ba0d
Packit e9ba0d
    Generally, this value will only be cached while actually
Packit e9ba0d
    processing event callbacks, and may be very inaccuate if your
Packit e9ba0d
    callbacks take a long time to execute.
Packit e9ba0d
Packit e9ba0d
    Returns 0 on success, negative on failure.
Packit e9ba0d
 */
Packit e9ba0d
int event_base_gettimeofday_cached(struct event_base *base,
Packit e9ba0d
    struct timeval *tv);
Packit e9ba0d
Packit e9ba0d
#ifdef __cplusplus
Packit e9ba0d
}
Packit e9ba0d
#endif
Packit e9ba0d
Packit e9ba0d
#endif /* _EVENT2_EVENT_H_ */