1 |
/* |
2 |
* Copyright (c) 2000-2004 Niels Provos <provos@citi.umich.edu> |
3 |
* Copyright (c) 2008 Marc Alexander Lehmann <libev@schmorp.de> |
4 |
* All rights reserved. |
5 |
* |
6 |
* Redistribution and use in source and binary forms, with or without |
7 |
* modification, are permitted provided that the following conditions |
8 |
* are met: |
9 |
* 1. Redistributions of source code must retain the above copyright |
10 |
* notice, this list of conditions and the following disclaimer. |
11 |
* 2. Redistributions in binary form must reproduce the above copyright |
12 |
* notice, this list of conditions and the following disclaimer in the |
13 |
* documentation and/or other materials provided with the distribution. |
14 |
* 3. The name of the author may not be used to endorse or promote products |
15 |
* derived from this software without specific prior written permission. |
16 |
* |
17 |
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 |
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 |
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 |
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 |
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 |
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 |
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 |
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 |
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 |
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 |
*/ |
28 |
#ifdef __cplusplus |
29 |
extern "C" { |
30 |
#endif |
31 |
|
32 |
#ifdef _WIN32 |
33 |
# define WIN32_LEAN_AND_MEAN |
34 |
# include <windows.h> |
35 |
# undef WIN32_LEAN_AND_MEAN |
36 |
typedef unsigned char u_char; |
37 |
typedef unsigned short u_short; |
38 |
#else |
39 |
# include <sys/types.h> |
40 |
# include <sys/time.h> |
41 |
# include <inttypes.h> |
42 |
#endif |
43 |
|
44 |
#include <stdarg.h> |
45 |
|
46 |
/* Fix so that ppl dont have to run with <sys/queue.h> */ |
47 |
#ifndef TAILQ_ENTRY |
48 |
#define _EVENT_DEFINED_TQENTRY |
49 |
#define TAILQ_ENTRY(type) \ |
50 |
struct { \ |
51 |
struct type *tqe_next; /* next element */ \ |
52 |
struct type **tqe_prev; /* address of previous next element */ \ |
53 |
} |
54 |
#endif /* !TAILQ_ENTRY */ |
55 |
#ifndef RB_ENTRY |
56 |
#define _EVENT_DEFINED_RBENTRY |
57 |
#define RB_ENTRY(type) \ |
58 |
struct { \ |
59 |
struct type *rbe_left; /* left element */ \ |
60 |
struct type *rbe_right; /* right element */ \ |
61 |
struct type *rbe_parent; /* parent element */ \ |
62 |
int rbe_color; /* node color */ \ |
63 |
} |
64 |
#endif /* !RB_ENTRY */ |
65 |
|
66 |
/* |
67 |
* Key-Value pairs. Can be used for HTTP headers but also for |
68 |
* query argument parsing. |
69 |
*/ |
70 |
struct evkeyval { |
71 |
TAILQ_ENTRY(evkeyval) next; |
72 |
|
73 |
char *key; |
74 |
char *value; |
75 |
}; |
76 |
|
77 |
#ifdef _EVENT_DEFINED_TQENTRY |
78 |
#undef TAILQ_ENTRY |
79 |
struct event_list; |
80 |
struct evkeyvalq; |
81 |
#undef _EVENT_DEFINED_TQENTRY |
82 |
#else |
83 |
TAILQ_HEAD (event_list, event); |
84 |
TAILQ_HEAD (evkeyvalq, evkeyval); |
85 |
#endif /* _EVENT_DEFINED_TQENTRY */ |
86 |
#ifdef _EVENT_DEFINED_RBENTRY |
87 |
#undef RB_ENTRY |
88 |
#undef _EVENT_DEFINED_RBENTRY |
89 |
#endif /* _EVENT_DEFINED_RBENTRY */ |
90 |
|
91 |
struct eventop { |
92 |
char *name; |
93 |
void *(*init)(struct event_base *); |
94 |
int (*add)(void *, struct event *); |
95 |
int (*del)(void *, struct event *); |
96 |
int (*recalc)(struct event_base *, void *, int); |
97 |
int (*dispatch)(struct event_base *, void *, struct timeval *); |
98 |
void (*dealloc)(struct event_base *, void *); |
99 |
}; |
100 |
|
101 |
/* These functions deal with buffering input and output */ |
102 |
|
103 |
struct evbuffer { |
104 |
u_char *buffer; |
105 |
u_char *orig_buffer; |
106 |
|
107 |
size_t misalign; |
108 |
size_t totallen; |
109 |
size_t off; |
110 |
|
111 |
void (*cb)(struct evbuffer *, size_t, size_t, void *); |
112 |
void *cbarg; |
113 |
}; |
114 |
|
115 |
/* Just for error reporting - use other constants otherwise */ |
116 |
#define EVBUFFER_READ 0x01 |
117 |
#define EVBUFFER_WRITE 0x02 |
118 |
#define EVBUFFER_EOF 0x10 |
119 |
#define EVBUFFER_ERROR 0x20 |
120 |
#define EVBUFFER_TIMEOUT 0x40 |
121 |
|
122 |
struct bufferevent; |
123 |
typedef void (*evbuffercb)(struct bufferevent *, void *); |
124 |
typedef void (*everrorcb)(struct bufferevent *, short what, void *); |
125 |
|
126 |
struct event_watermark { |
127 |
size_t low; |
128 |
size_t high; |
129 |
}; |
130 |
|
131 |
struct bufferevent { |
132 |
struct event ev_read; |
133 |
struct event ev_write; |
134 |
|
135 |
struct evbuffer *input; |
136 |
struct evbuffer *output; |
137 |
|
138 |
struct event_watermark wm_read; |
139 |
struct event_watermark wm_write; |
140 |
|
141 |
evbuffercb readcb; |
142 |
evbuffercb writecb; |
143 |
everrorcb errorcb; |
144 |
void *cbarg; |
145 |
|
146 |
int timeout_read; /* in seconds */ |
147 |
int timeout_write; /* in seconds */ |
148 |
|
149 |
short enabled; /* events that are currently enabled */ |
150 |
}; |
151 |
|
152 |
struct bufferevent *bufferevent_new(int fd, |
153 |
evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg); |
154 |
int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev); |
155 |
int bufferevent_priority_set(struct bufferevent *bufev, int pri); |
156 |
void bufferevent_free(struct bufferevent *bufev); |
157 |
int bufferevent_write(struct bufferevent *bufev, const void *data, size_t size); |
158 |
int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf); |
159 |
size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size); |
160 |
int bufferevent_enable(struct bufferevent *bufev, short event); |
161 |
int bufferevent_disable(struct bufferevent *bufev, short event); |
162 |
void bufferevent_settimeout(struct bufferevent *bufev, |
163 |
int timeout_read, int timeout_write); |
164 |
|
165 |
#define EVBUFFER_LENGTH(x) (x)->off |
166 |
#define EVBUFFER_DATA(x) (x)->buffer |
167 |
#define EVBUFFER_INPUT(x) (x)->input |
168 |
#define EVBUFFER_OUTPUT(x) (x)->output |
169 |
|
170 |
struct evbuffer *evbuffer_new(void); |
171 |
void evbuffer_free(struct evbuffer *); |
172 |
int evbuffer_expand(struct evbuffer *, size_t); |
173 |
int evbuffer_add(struct evbuffer *, const void *, size_t); |
174 |
int evbuffer_remove(struct evbuffer *, void *, size_t); |
175 |
char *evbuffer_readline(struct evbuffer *); |
176 |
int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *); |
177 |
int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...); |
178 |
int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap); |
179 |
void evbuffer_drain(struct evbuffer *, size_t); |
180 |
int evbuffer_write(struct evbuffer *, int); |
181 |
int evbuffer_read(struct evbuffer *, int, int); |
182 |
u_char *evbuffer_find(struct evbuffer *, const u_char *, size_t); |
183 |
void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *); |
184 |
|
185 |
/* |
186 |
* Marshaling tagged data - We assume that all tags are inserted in their |
187 |
* numeric order - so that unknown tags will always be higher than the |
188 |
* known ones - and we can just ignore the end of an event buffer. |
189 |
*/ |
190 |
|
191 |
void evtag_init(void); |
192 |
|
193 |
void evtag_marshal(struct evbuffer *evbuf, uint32_t tag, const void *data, |
194 |
uint32_t len); |
195 |
|
196 |
void encode_int(struct evbuffer *evbuf, uint32_t number); |
197 |
|
198 |
void evtag_marshal_int(struct evbuffer *evbuf, uint32_t tag, uint32_t integer); |
199 |
|
200 |
void evtag_marshal_string(struct evbuffer *buf, uint32_t tag, |
201 |
const char *string); |
202 |
|
203 |
void evtag_marshal_timeval(struct evbuffer *evbuf, uint32_t tag, |
204 |
struct timeval *tv); |
205 |
|
206 |
int evtag_unmarshal(struct evbuffer *src, uint32_t *ptag, struct evbuffer *dst); |
207 |
int evtag_peek(struct evbuffer *evbuf, uint32_t *ptag); |
208 |
int evtag_peek_length(struct evbuffer *evbuf, uint32_t *plength); |
209 |
int evtag_payload_length(struct evbuffer *evbuf, uint32_t *plength); |
210 |
int evtag_consume(struct evbuffer *evbuf); |
211 |
|
212 |
int evtag_unmarshal_int(struct evbuffer *evbuf, uint32_t need_tag, |
213 |
uint32_t *pinteger); |
214 |
|
215 |
int evtag_unmarshal_fixed(struct evbuffer *src, uint32_t need_tag, void *data, |
216 |
size_t len); |
217 |
|
218 |
int evtag_unmarshal_string(struct evbuffer *evbuf, uint32_t need_tag, |
219 |
char **pstring); |
220 |
|
221 |
int evtag_unmarshal_timeval(struct evbuffer *evbuf, uint32_t need_tag, |
222 |
struct timeval *ptv); |
223 |
|
224 |
#ifdef __cplusplus |
225 |
} |
226 |
#endif |