]> arthur.barton.de Git - netatalk.git/blob - libevent/event.h
Merge master
[netatalk.git] / libevent / event.h
1 /*
2  * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3  * Copyright (c) 2007-2010 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 #ifndef _EVENT_H_
28 #define _EVENT_H_
29
30 /** @mainpage
31
32   @section intro Introduction
33
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.
39
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.
43
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.
53
54   @section usage Standard usage
55
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.
60
61   @section event Event notification
62
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.
69
70   @section bufferevent I/O Buffers
71
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.
77
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().
82
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
86   default.
87
88   @section timers Timers
89
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().
94
95   @section timeouts Timeouts
96
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
102   timeout_del().
103
104   @section evdns Asynchronous DNS resolution
105
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.
114
115   @section evhttp Event-driven HTTP servers
116
117   libevent provides a very simple event-driven HTTP server that can be
118   embedded in your program and used to service HTTP requests.
119
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.
127
128   @section evrpc A framework for RPC servers and clients
129
130   libevent provides a framework for creating RPC servers and clients.  It
131   takes care of marshaling and unmarshaling all data structures.
132
133   @section api API Reference
134
135   To browse the complete documentation of the libevent API, click on any of
136   the following links.
137
138   event2/event.h
139   The primary libevent header
140
141   event2/buffer.h
142   Buffer management for network reading and writing
143
144   event2/dns.h
145   Asynchronous DNS resolution
146
147   event2/http.h
148   An embedded libevent-based HTTP server
149
150   evrpc.h
151   A framework for creating RPC servers and clients
152
153  */
154
155 /** @file libevent/event.h
156
157   A library for writing event-driven network servers
158
159  */
160
161 #ifdef __cplusplus
162 extern "C" {
163 #endif
164
165 #include <event2/event-config.h>
166 #ifdef _EVENT_HAVE_SYS_TYPES_H
167 #include <sys/types.h>
168 #endif
169 #ifdef _EVENT_HAVE_SYS_TIME_H
170 #include <sys/time.h>
171 #endif
172 #ifdef _EVENT_HAVE_STDINT_H
173 #include <stdint.h>
174 #endif
175 #include <stdarg.h>
176
177 /* For int types. */
178 #include <evutil.h>
179
180 #ifdef WIN32
181 #ifndef WIN32_LEAN_AND_MEAN
182 #define WIN32_LEAN_AND_MEAN
183 #endif
184 #include <winsock2.h>
185 #include <windows.h>
186 #undef WIN32_LEAN_AND_MEAN
187 typedef unsigned char u_char;
188 typedef unsigned short u_short;
189 #endif
190
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>
201
202 #ifdef __cplusplus
203 }
204 #endif
205
206 #endif /* _EVENT_H_ */