MySQL 5.6.14 Source Code Document
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
event.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
3  * All rights reserved.
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 
158 #ifdef __cplusplus
159 extern "C" {
160 #endif
161 
162 #include <config.h>
163 #ifdef HAVE_SYS_TYPES_H
164 #include <sys/types.h>
165 #endif
166 #ifdef HAVE_SYS_TIME_H
167 #include <sys/time.h>
168 #endif
169 #ifdef HAVE_STDINT_H
170 #include <stdint.h>
171 #endif
172 #include <stdarg.h>
173 
174 /* For int types. */
175 #include <evutil.h>
176 
177 #ifdef WIN32
178 #define WIN32_LEAN_AND_MEAN
179 #include <windows.h>
180 #undef WIN32_LEAN_AND_MEAN
181 typedef unsigned char u_char;
182 typedef unsigned short u_short;
183 #endif
184 
185 #define EVLIST_TIMEOUT 0x01
186 #define EVLIST_INSERTED 0x02
187 #define EVLIST_SIGNAL 0x04
188 #define EVLIST_ACTIVE 0x08
189 #define EVLIST_INTERNAL 0x10
190 #define EVLIST_INIT 0x80
191 
192 /* EVLIST_X_ Private space: 0x1000-0xf000 */
193 #define EVLIST_ALL (0xf000 | 0x9f)
194 
195 #define EV_TIMEOUT 0x01
196 #define EV_READ 0x02
197 #define EV_WRITE 0x04
198 #define EV_SIGNAL 0x08
199 #define EV_PERSIST 0x10 /* Persistant event */
200 
201 /* Fix so that ppl dont have to run with <sys/queue.h> */
202 #ifndef TAILQ_ENTRY
203 #define _EVENT_DEFINED_TQENTRY
204 #define TAILQ_ENTRY(type) \
205 struct { \
206  struct type *tqe_next; /* next element */ \
207  struct type **tqe_prev; /* address of previous next element */ \
208 }
209 #endif /* !TAILQ_ENTRY */
210 
211 struct event_base;
212 struct event {
213  TAILQ_ENTRY (event) ev_next;
214  TAILQ_ENTRY (event) ev_active_next;
215  TAILQ_ENTRY (event) ev_signal_next;
216  unsigned int min_heap_idx; /* for managing timeouts */
217 
218  struct event_base *ev_base;
219 
220  int ev_fd;
221  short ev_events;
222  short ev_ncalls;
223  short *ev_pncalls; /* Allows deletes in callback */
224 
225  struct timeval ev_timeout;
226 
227  int ev_pri; /* smaller numbers are higher priority */
228 
229  void (*ev_callback)(int, short, void *arg);
230  void *ev_arg;
231 
232  int ev_res; /* result passed to event callback */
233  int ev_flags;
234 };
235 
236 #define EVENT_SIGNAL(ev) (int)(ev)->ev_fd
237 #define EVENT_FD(ev) (int)(ev)->ev_fd
238 
239 /*
240  * Key-Value pairs. Can be used for HTTP headers but also for
241  * query argument parsing.
242  */
243 struct evkeyval {
244  TAILQ_ENTRY(evkeyval) next;
245 
246  char *key;
247  char *value;
248 };
249 
250 #ifdef _EVENT_DEFINED_TQENTRY
251 #undef TAILQ_ENTRY
252 struct event_list;
253 struct evkeyvalq;
254 #undef _EVENT_DEFINED_TQENTRY
255 #else
256 TAILQ_HEAD (event_list, event);
257 TAILQ_HEAD (evkeyvalq, evkeyval);
258 #endif /* _EVENT_DEFINED_TQENTRY */
259 
269 struct event_base *event_base_new(void);
270 
280 struct event_base *event_init(void);
281 
292 int event_reinit(struct event_base *base);
293 
303 int event_dispatch(void);
304 
305 
312 int event_base_dispatch(struct event_base *);
313 
314 
321 const char *event_base_get_method(struct event_base *);
322 
323 
332 void event_base_free(struct event_base *);
333 
334 
335 #define _EVENT_LOG_DEBUG 0
336 #define _EVENT_LOG_MSG 1
337 #define _EVENT_LOG_WARN 2
338 #define _EVENT_LOG_ERR 3
339 typedef void (*event_log_cb)(int severity, const char *msg);
347 void event_set_log_callback(event_log_cb cb);
348 
355 int event_base_set(struct event_base *, struct event *);
356 
361 #define EVLOOP_ONCE 0x01
362 #define EVLOOP_NONBLOCK 0x02
364 
365 
375 int event_loop(int);
376 
388 int event_base_loop(struct event_base *, int);
389 
403 int event_loopexit(const struct timeval *);
404 
405 
420 int event_base_loopexit(struct event_base *, const struct timeval *);
421 
434 int event_loopbreak(void);
435 
449 int event_base_loopbreak(struct event_base *);
450 
451 
458 #define evtimer_add(ev, tv) event_add(ev, tv)
459 
460 
468 #define evtimer_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg)
469 
470 
476 #define evtimer_del(ev) event_del(ev)
477 #define evtimer_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv)
478 #define evtimer_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
479 
486 #define timeout_add(ev, tv) event_add(ev, tv)
487 
488 
496 #define timeout_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg)
497 
498 
504 #define timeout_del(ev) event_del(ev)
505 
506 #define timeout_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv)
507 #define timeout_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
508 
509 #define signal_add(ev, tv) event_add(ev, tv)
510 #define signal_set(ev, x, cb, arg) \
511  event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg)
512 #define signal_del(ev) event_del(ev)
513 #define signal_pending(ev, tv) event_pending(ev, EV_SIGNAL, tv)
514 #define signal_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
515 
542 void event_set(struct event *, int, short, void (*)(int, short, void *), void *);
543 
562 int event_once(int, short, void (*)(int, short, void *), void *,
563  const struct timeval *);
564 
565 
584 int event_base_once(struct event_base *base, int fd, short events,
585  void (*callback)(int, short, void *), void *arg,
586  const struct timeval *timeout);
587 
588 
607 int event_add(struct event *ev, const struct timeval *timeout);
608 
609 
621 int event_del(struct event *);
622 
623 void event_active(struct event *, int, short);
624 
625 
637 int event_pending(struct event *ev, short event, struct timeval *tv);
638 
639 
650 #ifdef WIN32
651 #define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT && (ev)->ev_fd != (int)INVALID_HANDLE_VALUE)
652 #else
653 #define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
654 #endif
655 
656 
662 const char *event_get_version(void);
663 
664 
670 const char *event_get_method(void);
671 
672 
693 int event_priority_init(int);
694 
695 
706 int event_base_priority_init(struct event_base *, int);
707 
708 
717 int event_priority_set(struct event *, int);
718 
719 
720 /* These functions deal with buffering input and output */
721 
722 struct evbuffer {
723  u_char *buffer;
724  u_char *orig_buffer;
725 
726  size_t misalign;
727  size_t totallen;
728  size_t off;
729 
730  void (*cb)(struct evbuffer *, size_t, size_t, void *);
731  void *cbarg;
732 };
733 
734 /* Just for error reporting - use other constants otherwise */
735 #define EVBUFFER_READ 0x01
736 #define EVBUFFER_WRITE 0x02
737 #define EVBUFFER_EOF 0x10
738 #define EVBUFFER_ERROR 0x20
739 #define EVBUFFER_TIMEOUT 0x40
740 
741 struct bufferevent;
742 typedef void (*evbuffercb)(struct bufferevent *, void *);
743 typedef void (*everrorcb)(struct bufferevent *, short what, void *);
744 
746  size_t low;
747  size_t high;
748 };
749 
750 struct bufferevent {
751  struct event_base *ev_base;
752 
753  struct event ev_read;
754  struct event ev_write;
755 
756  struct evbuffer *input;
757  struct evbuffer *output;
758 
759  struct event_watermark wm_read;
760  struct event_watermark wm_write;
761 
762  evbuffercb readcb;
763  evbuffercb writecb;
764  everrorcb errorcb;
765  void *cbarg;
766 
767  int timeout_read; /* in seconds */
768  int timeout_write; /* in seconds */
769 
770  short enabled; /* events that are currently enabled */
771 };
772 
773 
808 struct bufferevent *bufferevent_new(int fd,
809  evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
810 
811 
820 int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
821 
822 
830 int bufferevent_priority_set(struct bufferevent *bufev, int pri);
831 
832 
838 void bufferevent_free(struct bufferevent *bufev);
839 
840 
855 void bufferevent_setcb(struct bufferevent *bufev,
856  evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
857 
864 void bufferevent_setfd(struct bufferevent *bufev, int fd);
865 
879 int bufferevent_write(struct bufferevent *bufev,
880  const void *data, size_t size);
881 
882 
892 int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
893 
894 
905 size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
906 
915 int bufferevent_enable(struct bufferevent *bufev, short event);
916 
917 
926 int bufferevent_disable(struct bufferevent *bufev, short event);
927 
928 
936 void bufferevent_settimeout(struct bufferevent *bufev,
937  int timeout_read, int timeout_write);
938 
939 
956 void bufferevent_setwatermark(struct bufferevent *bufev, short events,
957  size_t lowmark, size_t highmark);
958 
959 #define EVBUFFER_LENGTH(x) (x)->off
960 #define EVBUFFER_DATA(x) (x)->buffer
961 #define EVBUFFER_INPUT(x) (x)->input
962 #define EVBUFFER_OUTPUT(x) (x)->output
963 
964 
971 struct evbuffer *evbuffer_new(void);
972 
973 
979 void evbuffer_free(struct evbuffer *);
980 
981 
991 int evbuffer_expand(struct evbuffer *, size_t);
992 
993 
1001 int evbuffer_add(struct evbuffer *, const void *, size_t);
1002 
1003 
1004 
1013 int evbuffer_remove(struct evbuffer *, void *, size_t);
1014 
1015 
1025 char *evbuffer_readline(struct evbuffer *);
1026 
1027 
1038 int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *);
1039 
1040 
1049 int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...)
1050 #ifdef __GNUC__
1051  __attribute__((format(printf, 2, 3)))
1052 #endif
1053 ;
1054 
1055 
1064 int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap);
1065 
1066 
1073 void evbuffer_drain(struct evbuffer *, size_t);
1074 
1075 
1086 int evbuffer_write(struct evbuffer *, int);
1087 
1088 
1098 int evbuffer_read(struct evbuffer *, int, int);
1099 
1100 
1109 u_char *evbuffer_find(struct evbuffer *, const u_char *, size_t);
1110 
1118 void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *);
1119 
1120 /*
1121  * Marshaling tagged data - We assume that all tags are inserted in their
1122  * numeric order - so that unknown tags will always be higher than the
1123  * known ones - and we can just ignore the end of an event buffer.
1124  */
1125 
1126 void evtag_init(void);
1127 
1128 void evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag, const void *data,
1129  ev_uint32_t len);
1130 
1141 void encode_int(struct evbuffer *evbuf, ev_uint32_t number);
1142 
1143 void evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag,
1144  ev_uint32_t integer);
1145 
1146 void evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag,
1147  const char *string);
1148 
1149 void evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag,
1150  struct timeval *tv);
1151 
1152 int evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag,
1153  struct evbuffer *dst);
1154 int evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag);
1155 int evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength);
1156 int evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength);
1157 int evtag_consume(struct evbuffer *evbuf);
1158 
1159 int evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag,
1160  ev_uint32_t *pinteger);
1161 
1162 int evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag,
1163  void *data, size_t len);
1164 
1165 int evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag,
1166  char **pstring);
1167 
1168 int evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag,
1169  struct timeval *ptv);
1170 
1171 #ifdef __cplusplus
1172 }
1173 #endif
1174 
1175 #endif /* _EVENT_H_ */