00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #ifndef _EVENT_H_
00028 #define _EVENT_H_
00029
00158 #ifdef __cplusplus
00159 extern "C" {
00160 #endif
00161
00162 #include <event-config.h>
00163 #ifdef _EVENT_HAVE_SYS_TYPES_H
00164 #include <sys/types.h>
00165 #endif
00166 #ifdef _EVENT_HAVE_SYS_TIME_H
00167 #include <sys/time.h>
00168 #endif
00169 #ifdef _EVENT_HAVE_STDINT_H
00170 #include <stdint.h>
00171 #endif
00172 #include <stdarg.h>
00173
00174
00175 #include <evutil.h>
00176
00177 #ifdef WIN32
00178 #define WIN32_LEAN_AND_MEAN
00179 #include <windows.h>
00180 #undef WIN32_LEAN_AND_MEAN
00181 typedef unsigned char u_char;
00182 typedef unsigned short u_short;
00183 #endif
00184
00185 #define EVLIST_TIMEOUT 0x01
00186 #define EVLIST_INSERTED 0x02
00187 #define EVLIST_SIGNAL 0x04
00188 #define EVLIST_ACTIVE 0x08
00189 #define EVLIST_INTERNAL 0x10
00190 #define EVLIST_INIT 0x80
00191
00192
00193 #define EVLIST_ALL (0xf000 | 0x9f)
00194
00195 #define EV_TIMEOUT 0x01
00196 #define EV_READ 0x02
00197 #define EV_WRITE 0x04
00198 #define EV_SIGNAL 0x08
00199 #define EV_PERSIST 0x10
00200
00201
00202 #ifndef TAILQ_ENTRY
00203 #define _EVENT_DEFINED_TQENTRY
00204 #define TAILQ_ENTRY(type) \
00205 struct { \
00206 struct type *tqe_next; \
00207 struct type **tqe_prev; \
00208 }
00209 #endif
00210
00211 struct event_base;
00212 struct event {
00213 TAILQ_ENTRY (event) ev_next;
00214 TAILQ_ENTRY (event) ev_active_next;
00215 TAILQ_ENTRY (event) ev_signal_next;
00216 unsigned int min_heap_idx;
00217
00218 struct event_base *ev_base;
00219
00220 int ev_fd;
00221 short ev_events;
00222 short ev_ncalls;
00223 short *ev_pncalls;
00224
00225 struct timeval ev_timeout;
00226
00227 int ev_pri;
00228
00229 void (*ev_callback)(int, short, void *arg);
00230 void *ev_arg;
00231
00232 int ev_res;
00233 int ev_flags;
00234 };
00235
00236 #define EVENT_SIGNAL(ev) (int)(ev)->ev_fd
00237 #define EVENT_FD(ev) (int)(ev)->ev_fd
00238
00239
00240
00241
00242
00243 struct evkeyval {
00244 TAILQ_ENTRY(evkeyval) next;
00245
00246 char *key;
00247 char *value;
00248 };
00249
00250 #ifdef _EVENT_DEFINED_TQENTRY
00251 #undef TAILQ_ENTRY
00252 struct event_list;
00253 struct evkeyvalq;
00254 #undef _EVENT_DEFINED_TQENTRY
00255 #else
00256 TAILQ_HEAD (event_list, event);
00257 TAILQ_HEAD (evkeyvalq, evkeyval);
00258 #endif
00259
00269 struct event_base *event_base_new(void);
00270
00280 struct event_base *event_init(void);
00281
00292 int event_reinit(struct event_base *base);
00293
00303 int event_dispatch(void);
00304
00305
00312 int event_base_dispatch(struct event_base *);
00313
00314
00321 const char *event_base_get_method(struct event_base *);
00322
00323
00332 void event_base_free(struct event_base *);
00333
00334
00335 #define _EVENT_LOG_DEBUG 0
00336 #define _EVENT_LOG_MSG 1
00337 #define _EVENT_LOG_WARN 2
00338 #define _EVENT_LOG_ERR 3
00339 typedef void (*event_log_cb)(int severity, const char *msg);
00347 void event_set_log_callback(event_log_cb cb);
00348
00355 int event_base_set(struct event_base *, struct event *);
00356
00361 #define EVLOOP_ONCE 0x01
00362 #define EVLOOP_NONBLOCK 0x02
00364
00365
00375 int event_loop(int);
00376
00388 int event_base_loop(struct event_base *, int);
00389
00403 int event_loopexit(const struct timeval *);
00404
00405
00420 int event_base_loopexit(struct event_base *, const struct timeval *);
00421
00434 int event_loopbreak(void);
00435
00449 int event_base_loopbreak(struct event_base *);
00450
00451
00458 #define evtimer_add(ev, tv) event_add(ev, tv)
00459
00460
00468 #define evtimer_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg)
00469
00470
00476 #define evtimer_del(ev) event_del(ev)
00477 #define evtimer_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv)
00478 #define evtimer_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
00479
00486 #define timeout_add(ev, tv) event_add(ev, tv)
00487
00488
00496 #define timeout_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg)
00497
00498
00504 #define timeout_del(ev) event_del(ev)
00505
00506 #define timeout_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv)
00507 #define timeout_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
00508
00509 #define signal_add(ev, tv) event_add(ev, tv)
00510 #define signal_set(ev, x, cb, arg) \
00511 event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg)
00512 #define signal_del(ev) event_del(ev)
00513 #define signal_pending(ev, tv) event_pending(ev, EV_SIGNAL, tv)
00514 #define signal_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
00515
00542 void event_set(struct event *, int, short, void (*)(int, short, void *), void *);
00543
00562 int event_once(int, short, void (*)(int, short, void *), void *,
00563 const struct timeval *);
00564
00565
00584 int event_base_once(struct event_base *base, int fd, short events,
00585 void (*callback)(int, short, void *), void *arg,
00586 const struct timeval *timeout);
00587
00588
00607 int event_add(struct event *ev, const struct timeval *timeout);
00608
00609
00621 int event_del(struct event *);
00622
00623 void event_active(struct event *, int, short);
00624
00625
00637 int event_pending(struct event *ev, short event, struct timeval *tv);
00638
00639
00650 #ifdef WIN32
00651 #define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT && (ev)->ev_fd != (int)INVALID_HANDLE_VALUE)
00652 #else
00653 #define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT)
00654 #endif
00655
00656
00662 const char *event_get_version(void);
00663
00664
00670 const char *event_get_method(void);
00671
00672
00693 int event_priority_init(int);
00694
00695
00706 int event_base_priority_init(struct event_base *, int);
00707
00708
00717 int event_priority_set(struct event *, int);
00718
00719
00720
00721
00722 struct evbuffer {
00723 u_char *buffer;
00724 u_char *orig_buffer;
00725
00726 size_t misalign;
00727 size_t totallen;
00728 size_t off;
00729
00730 void (*cb)(struct evbuffer *, size_t, size_t, void *);
00731 void *cbarg;
00732 };
00733
00734
00735 #define EVBUFFER_READ 0x01
00736 #define EVBUFFER_WRITE 0x02
00737 #define EVBUFFER_EOF 0x10
00738 #define EVBUFFER_ERROR 0x20
00739 #define EVBUFFER_TIMEOUT 0x40
00740
00741 struct bufferevent;
00742 typedef void (*evbuffercb)(struct bufferevent *, void *);
00743 typedef void (*everrorcb)(struct bufferevent *, short what, void *);
00744
00745 struct event_watermark {
00746 size_t low;
00747 size_t high;
00748 };
00749
00750 struct bufferevent {
00751 struct event_base *ev_base;
00752
00753 struct event ev_read;
00754 struct event ev_write;
00755
00756 struct evbuffer *input;
00757 struct evbuffer *output;
00758
00759 struct event_watermark wm_read;
00760 struct event_watermark wm_write;
00761
00762 evbuffercb readcb;
00763 evbuffercb writecb;
00764 everrorcb errorcb;
00765 void *cbarg;
00766
00767 int timeout_read;
00768 int timeout_write;
00769
00770 short enabled;
00771 };
00772
00773
00808 struct bufferevent *bufferevent_new(int fd,
00809 evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
00810
00811
00820 int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
00821
00822
00830 int bufferevent_priority_set(struct bufferevent *bufev, int pri);
00831
00832
00838 void bufferevent_free(struct bufferevent *bufev);
00839
00840
00855 void bufferevent_setcb(struct bufferevent *bufev,
00856 evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
00857
00864 void bufferevent_setfd(struct bufferevent *bufev, int fd);
00865
00879 int bufferevent_write(struct bufferevent *bufev,
00880 const void *data, size_t size);
00881
00882
00892 int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
00893
00894
00905 size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
00906
00915 int bufferevent_enable(struct bufferevent *bufev, short event);
00916
00917
00926 int bufferevent_disable(struct bufferevent *bufev, short event);
00927
00928
00936 void bufferevent_settimeout(struct bufferevent *bufev,
00937 int timeout_read, int timeout_write);
00938
00939
00956 void bufferevent_setwatermark(struct bufferevent *bufev, short events,
00957 size_t lowmark, size_t highmark);
00958
00959 #define EVBUFFER_LENGTH(x) (x)->off
00960 #define EVBUFFER_DATA(x) (x)->buffer
00961 #define EVBUFFER_INPUT(x) (x)->input
00962 #define EVBUFFER_OUTPUT(x) (x)->output
00963
00964
00971 struct evbuffer *evbuffer_new(void);
00972
00973
00979 void evbuffer_free(struct evbuffer *);
00980
00981
00991 int evbuffer_expand(struct evbuffer *, size_t);
00992
00993
01001 int evbuffer_add(struct evbuffer *, const void *, size_t);
01002
01003
01004
01013 int evbuffer_remove(struct evbuffer *, void *, size_t);
01014
01015
01025 char *evbuffer_readline(struct evbuffer *);
01026
01027
01038 int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *);
01039
01040
01049 int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...)
01050 #ifdef __GNUC__
01051 __attribute__((format(printf, 2, 3)))
01052 #endif
01053 ;
01054
01055
01064 int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap);
01065
01066
01074 void evbuffer_drain(struct evbuffer *, size_t);
01075
01076
01087 int evbuffer_write(struct evbuffer *, int);
01088
01089
01099 int evbuffer_read(struct evbuffer *, int, int);
01100
01101
01110 u_char *evbuffer_find(struct evbuffer *, const u_char *, size_t);
01111
01119 void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *);
01120
01121
01122
01123
01124
01125
01126
01127 void evtag_init(void);
01128
01129 void evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag, const void *data,
01130 ev_uint32_t len);
01131
01142 void encode_int(struct evbuffer *evbuf, ev_uint32_t number);
01143
01144 void evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag,
01145 ev_uint32_t integer);
01146
01147 void evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag,
01148 const char *string);
01149
01150 void evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag,
01151 struct timeval *tv);
01152
01153 int evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag,
01154 struct evbuffer *dst);
01155 int evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag);
01156 int evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength);
01157 int evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength);
01158 int evtag_consume(struct evbuffer *evbuf);
01159
01160 int evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag,
01161 ev_uint32_t *pinteger);
01162
01163 int evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag,
01164 void *data, size_t len);
01165
01166 int evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag,
01167 char **pstring);
01168
01169 int evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag,
01170 struct timeval *ptv);
01171
01172 #ifdef __cplusplus
01173 }
01174 #endif
01175
01176 #endif