2 * Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu>
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.
39 #define WIN32_LEAN_AND_MEAN
41 #undef WIN32_LEAN_AND_MEAN
42 typedef unsigned char u_char
;
43 typedef unsigned short u_short
;
46 #define EVLIST_TIMEOUT 0x01
47 #define EVLIST_INSERTED 0x02
48 #define EVLIST_SIGNAL 0x04
49 #define EVLIST_ACTIVE 0x08
50 #define EVLIST_INTERNAL 0x10
51 #define EVLIST_INIT 0x80
53 /* EVLIST_X_ Private space: 0x1000-0xf000 */
54 #define EVLIST_ALL (0xf000 | 0x9f)
56 #define EV_TIMEOUT 0x01
59 #define EV_SIGNAL 0x08
60 #define EV_PERSIST 0x10 /* Persistant event */
62 /* Fix so that ppl dont have to run with <sys/queue.h> */
64 #define _EVENT_DEFINED_TQENTRY
65 #define TAILQ_ENTRY(type) \
67 struct type *tqe_next; /* next element */ \
68 struct type **tqe_prev; /* address of previous next element */ \
70 #endif /* !TAILQ_ENTRY */
72 #define _EVENT_DEFINED_RBENTRY
73 #define RB_ENTRY(type) \
75 struct type *rbe_left; /* left element */ \
76 struct type *rbe_right; /* right element */ \
77 struct type *rbe_parent; /* parent element */ \
78 int rbe_color; /* node color */ \
80 #endif /* !RB_ENTRY */
84 TAILQ_ENTRY (event
) ev_next
;
85 TAILQ_ENTRY (event
) ev_active_next
;
86 TAILQ_ENTRY (event
) ev_signal_next
;
87 RB_ENTRY (event
) ev_timeout_node
;
89 struct event_base
*ev_base
;
93 short *ev_pncalls
; /* Allows deletes in callback */
95 struct timeval ev_timeout
;
97 int ev_pri
; /* smaller numbers are higher priority */
99 void (*ev_callback
)(int, short, void *arg
);
102 int ev_res
; /* result passed to event callback */
106 #define EVENT_SIGNAL(ev) (int)(ev)->ev_fd
107 #define EVENT_FD(ev) (int)(ev)->ev_fd
110 * Key-Value pairs. Can be used for HTTP headers but also for
111 * query argument parsing.
114 TAILQ_ENTRY(evkeyval
) next
;
120 #ifdef _EVENT_DEFINED_TQENTRY
124 #undef _EVENT_DEFINED_TQENTRY
126 TAILQ_HEAD (event_list
, event
);
127 TAILQ_HEAD (evkeyvalq
, evkeyval
);
128 #endif /* _EVENT_DEFINED_TQENTRY */
129 #ifdef _EVENT_DEFINED_RBENTRY
131 #undef _EVENT_DEFINED_RBENTRY
132 #endif /* _EVENT_DEFINED_RBENTRY */
136 void *(*init
)(struct event_base
*);
137 int (*add
)(void *, struct event
*);
138 int (*del
)(void *, struct event
*);
139 int (*recalc
)(struct event_base
*, void *, int);
140 int (*dispatch
)(struct event_base
*, void *, struct timeval
*);
141 void (*dealloc
)(struct event_base
*, void *);
144 void *event_init(void);
145 int event_dispatch(void);
146 int event_base_dispatch(struct event_base
*);
147 void event_base_free(struct event_base
*);
149 #define _EVENT_LOG_DEBUG 0
150 #define _EVENT_LOG_MSG 1
151 #define _EVENT_LOG_WARN 2
152 #define _EVENT_LOG_ERR 3
153 typedef void (*event_log_cb
)(int severity
, const char *msg
);
154 void event_set_log_callback(event_log_cb cb
);
156 /* Associate a different event base with an event */
157 int event_base_set(struct event_base
*, struct event
*);
159 #define EVLOOP_ONCE 0x01
160 #define EVLOOP_NONBLOCK 0x02
162 int event_base_loop(struct event_base
*, int);
163 int event_loopexit(struct timeval
*); /* Causes the loop to exit */
164 int event_base_loopexit(struct event_base
*, struct timeval
*);
166 #define evtimer_add(ev, tv) event_add(ev, tv)
167 #define evtimer_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg)
168 #define evtimer_del(ev) event_del(ev)
169 #define evtimer_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv)
170 #define evtimer_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
172 #define timeout_add(ev, tv) event_add(ev, tv)
173 #define timeout_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg)
174 #define timeout_del(ev) event_del(ev)
175 #define timeout_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv)
176 #define timeout_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
178 #define signal_add(ev, tv) event_add(ev, tv)
179 #define signal_set(ev, x, cb, arg) \
180 event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg)
181 #define signal_del(ev) event_del(ev)
182 #define signal_pending(ev, tv) event_pending(ev, EV_SIGNAL, tv)
183 #define signal_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
185 void event_set(struct event
*, int, short, void (*)(int, short, void *), void *);
186 int event_once(int, short, void (*)(int, short, void *), void *, struct timeval
*);
187 int event_base_once(struct event_base
*, int, short, void (*)(int, short, void *), void *, struct timeval
*);
189 int event_add(struct event
*, struct timeval
*);
190 int event_del(struct event
*);
191 void event_active(struct event
*, int, short);
193 int event_pending(struct event
*, short, struct timeval
*);
196 #define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT && (ev)->ev_fd != (int)INVALID_HANDLE_VALUE)
198 #define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
201 /* Some simple debugging functions */
202 const char *event_get_version(void);
203 const char *event_get_method(void);
205 /* These functions deal with event priorities */
207 int event_priority_init(int);
208 int event_base_priority_init(struct event_base
*, int);
209 int event_priority_set(struct event
*, int);
211 /* These functions deal with buffering input and output */
221 void (*cb
)(struct evbuffer
*, size_t, size_t, void *);
225 /* Just for error reporting - use other constants otherwise */
226 #define EVBUFFER_READ 0x01
227 #define EVBUFFER_WRITE 0x02
228 #define EVBUFFER_EOF 0x10
229 #define EVBUFFER_ERROR 0x20
230 #define EVBUFFER_TIMEOUT 0x40
233 typedef void (*evbuffercb
)(struct bufferevent
*, void *);
234 typedef void (*everrorcb
)(struct bufferevent
*, short what
, void *);
236 struct event_watermark
{
242 struct event ev_read
;
243 struct event ev_write
;
245 struct evbuffer
*input
;
246 struct evbuffer
*output
;
248 struct event_watermark wm_read
;
249 struct event_watermark wm_write
;
256 int timeout_read
; /* in seconds */
257 int timeout_write
; /* in seconds */
259 short enabled
; /* events that are currently enabled */
262 struct bufferevent
*bufferevent_new(int fd
,
263 evbuffercb readcb
, evbuffercb writecb
, everrorcb errorcb
, void *cbarg
);
264 int bufferevent_base_set(struct event_base
*base
, struct bufferevent
*bufev
);
265 int bufferevent_priority_set(struct bufferevent
*bufev
, int pri
);
266 void bufferevent_free(struct bufferevent
*bufev
);
267 int bufferevent_write(struct bufferevent
*bufev
, void *data
, size_t size
);
268 int bufferevent_write_buffer(struct bufferevent
*bufev
, struct evbuffer
*buf
);
269 size_t bufferevent_read(struct bufferevent
*bufev
, void *data
, size_t size
);
270 int bufferevent_enable(struct bufferevent
*bufev
, short event
);
271 int bufferevent_disable(struct bufferevent
*bufev
, short event
);
272 void bufferevent_settimeout(struct bufferevent
*bufev
,
273 int timeout_read
, int timeout_write
);
275 #define EVBUFFER_LENGTH(x) (x)->off
276 #define EVBUFFER_DATA(x) (x)->buffer
277 #define EVBUFFER_INPUT(x) (x)->input
278 #define EVBUFFER_OUTPUT(x) (x)->output
280 struct evbuffer
*evbuffer_new(void);
281 void evbuffer_free(struct evbuffer
*);
282 int evbuffer_expand(struct evbuffer
*, size_t);
283 int evbuffer_add(struct evbuffer
*, const void *, size_t);
284 int evbuffer_remove(struct evbuffer
*, void *, size_t);
285 char *evbuffer_readline(struct evbuffer
*);
286 int evbuffer_add_buffer(struct evbuffer
*, struct evbuffer
*);
287 int evbuffer_add_printf(struct evbuffer
*, const char *fmt
, ...);
288 int evbuffer_add_vprintf(struct evbuffer
*, const char *fmt
, va_list ap
);
289 void evbuffer_drain(struct evbuffer
*, size_t);
290 int evbuffer_write(struct evbuffer
*, int);
291 int evbuffer_read(struct evbuffer
*, int, int);
292 u_char
*evbuffer_find(struct evbuffer
*, const u_char
*, size_t);
293 void evbuffer_setcb(struct evbuffer
*, void (*)(struct evbuffer
*, size_t, size_t, void *), void *);
296 * Marshaling tagged data - We assume that all tags are inserted in their
297 * numeric order - so that unknown tags will always be higher than the
298 * known ones - and we can just ignore the end of an event buffer.
301 void evtag_init(void);
303 void evtag_marshal(struct evbuffer
*evbuf
, uint8_t tag
, const void *data
,
306 void encode_int(struct evbuffer
*evbuf
, uint32_t number
);
308 void evtag_marshal_int(struct evbuffer
*evbuf
, uint8_t tag
, uint32_t integer
);
310 void evtag_marshal_string(struct evbuffer
*buf
, uint8_t tag
,
313 void evtag_marshal_timeval(struct evbuffer
*evbuf
, uint8_t tag
,
316 void evtag_test(void);
318 int evtag_unmarshal(struct evbuffer
*src
, uint8_t *ptag
, struct evbuffer
*dst
);
319 int evtag_peek(struct evbuffer
*evbuf
, uint8_t *ptag
);
320 int evtag_peek_length(struct evbuffer
*evbuf
, uint32_t *plength
);
321 int evtag_payload_length(struct evbuffer
*evbuf
, uint32_t *plength
);
322 int evtag_consume(struct evbuffer
*evbuf
);
324 int evtag_unmarshal_int(struct evbuffer
*evbuf
, uint8_t need_tag
,
327 int evtag_unmarshal_fixed(struct evbuffer
*src
, uint8_t need_tag
, void *data
,
330 int evtag_unmarshal_string(struct evbuffer
*evbuf
, uint8_t need_tag
,
333 int evtag_unmarshal_timeval(struct evbuffer
*evbuf
, uint8_t need_tag
,
334 struct timeval
*ptv
);
340 #endif /* _EVENT_H_ */