2 * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3 * Copyright (c) 2007-2010 Niels Provos and Nick Mathewson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
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.
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.
32 @section intro Introduction
34 libevent is an event notification library for developing scalable network
35 servers. The libevent API provides a mechanism to execute a callback
36 function when a specific event occurs on a file descriptor or after a
37 timeout has been reached. Furthermore, libevent also support callbacks due
38 to signals or regular timeouts.
40 libevent is meant to replace the event loop found in event driven network
41 servers. An application just needs to call event_dispatch() and then add or
42 remove events dynamically without having to change the event loop.
44 Currently, libevent supports /dev/poll, kqueue(2), select(2), poll(2) and
45 epoll(4). It also has experimental support for real-time signals. The
46 internal event mechanism is completely independent of the exposed event API,
47 and a simple update of libevent can provide new functionality without having
48 to redesign the applications. As a result, Libevent allows for portable
49 application development and provides the most scalable event notification
50 mechanism available on an operating system. Libevent can also be used for
51 multi-threaded applications; see Steven Grimm's explanation. Libevent should
52 compile on Linux, *BSD, Mac OS X, Solaris and Windows.
54 @section usage Standard usage
56 Every program that uses libevent must include the <event.h> header, and pass
57 the -levent flag to the linker. Before using any of the functions in the
58 library, you must call event_init() or event_base_new() to perform one-time
59 initialization of the libevent library.
61 @section event Event notification
63 For each file descriptor that you wish to monitor, you must declare an event
64 structure and call event_set() to initialize the members of the structure.
65 To enable notification, you add the structure to the list of monitored
66 events by calling event_add(). The event structure must remain allocated as
67 long as it is active, so it should be allocated on the heap. Finally, you
68 call event_dispatch() to loop and dispatch events.
70 @section bufferevent I/O Buffers
72 libevent provides an abstraction on top of the regular event callbacks. This
73 abstraction is called a buffered event. A buffered event provides input and
74 output buffers that get filled and drained automatically. The user of a
75 buffered event no longer deals directly with the I/O, but instead is reading
76 from input and writing to output buffers.
78 Once initialized via bufferevent_new(), the bufferevent structure can be
79 used repeatedly with bufferevent_enable() and bufferevent_disable().
80 Instead of reading and writing directly to a socket, you would call
81 bufferevent_read() and bufferevent_write().
83 When read enabled the bufferevent will try to read from the file descriptor
84 and call the read callback. The write callback is executed whenever the
85 output buffer is drained below the write low watermark, which is 0 by
88 @section timers Timers
90 libevent can also be used to create timers that invoke a callback after a
91 certain amount of time has expired. The evtimer_set() function prepares an
92 event struct to be used as a timer. To activate the timer, call
93 evtimer_add(). Timers can be deactivated by calling evtimer_del().
95 @section timeouts Timeouts
97 In addition to simple timers, libevent can assign timeout events to file
98 descriptors that are triggered whenever a certain amount of time has passed
99 with no activity on a file descriptor. The timeout_set() function
100 initializes an event struct for use as a timeout. Once initialized, the
101 event must be activated by using timeout_add(). To cancel the timeout, call
104 @section evdns Asynchronous DNS resolution
106 libevent provides an asynchronous DNS resolver that should be used instead
107 of the standard DNS resolver functions. These functions can be imported by
108 including the <evdns.h> header in your program. Before using any of the
109 resolver functions, you must call evdns_init() to initialize the library. To
110 convert a hostname to an IP address, you call the evdns_resolve_ipv4()
111 function. To perform a reverse lookup, you would call the
112 evdns_resolve_reverse() function. All of these functions use callbacks to
113 avoid blocking while the lookup is performed.
115 @section evhttp Event-driven HTTP servers
117 libevent provides a very simple event-driven HTTP server that can be
118 embedded in your program and used to service HTTP requests.
120 To use this capability, you need to include the <evhttp.h> header in your
121 program. You create the server by calling evhttp_new(). Add addresses and
122 ports to listen on with evhttp_bind_socket(). You then register one or more
123 callbacks to handle incoming requests. Each URI can be assigned a callback
124 via the evhttp_set_cb() function. A generic callback function can also be
125 registered via evhttp_set_gencb(); this callback will be invoked if no other
126 callbacks have been registered for a given URI.
128 @section evrpc A framework for RPC servers and clients
130 libevent provides a framework for creating RPC servers and clients. It
131 takes care of marshaling and unmarshaling all data structures.
133 @section api API Reference
135 To browse the complete documentation of the libevent API, click on any of
139 The primary libevent header
142 Buffer management for network reading and writing
145 Asynchronous DNS resolution
148 An embedded libevent-based HTTP server
151 A framework for creating RPC servers and clients
155 /** @file libevent/event.h
157 A library for writing event-driven network servers
165 #include <event2/event-config.h>
166 #ifdef _EVENT_HAVE_SYS_TYPES_H
167 #include <sys/types.h>
169 #ifdef _EVENT_HAVE_SYS_TIME_H
170 #include <sys/time.h>
172 #ifdef _EVENT_HAVE_STDINT_H
181 #ifndef WIN32_LEAN_AND_MEAN
182 #define WIN32_LEAN_AND_MEAN
184 #include <winsock2.h>
186 #undef WIN32_LEAN_AND_MEAN
187 typedef unsigned char u_char;
188 typedef unsigned short u_short;
191 #include <event2/event_struct.h>
192 #include <event2/event.h>
193 #include <event2/event_compat.h>
194 #include <event2/buffer.h>
195 #include <event2/buffer_compat.h>
196 #include <event2/bufferevent.h>
197 #include <event2/bufferevent_struct.h>
198 #include <event2/bufferevent_compat.h>
199 #include <event2/tag.h>
200 #include <event2/tag_compat.h>
206 #endif /* _EVENT_H_ */