event.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2000-2007 Niels Provos <provos@citi.umich.edu>
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. The name of the author may not be used to endorse or promote products
00014  *    derived from this software without specific prior written permission.
00015  *
00016  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00017  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
00018  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00019  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
00020  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
00021  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00022  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00023  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00024  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
00025  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 /* For int types. */
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 /* EVLIST_X_ Private space: 0x1000-0xf000 */
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    /* Persistant event */
00200 
00201 /* Fix so that ppl dont have to run with <sys/queue.h> */
00202 #ifndef TAILQ_ENTRY
00203 #define _EVENT_DEFINED_TQENTRY
00204 #define TAILQ_ENTRY(type)                                               \
00205 struct {                                                                \
00206         struct type *tqe_next;  /* next element */                      \
00207         struct type **tqe_prev; /* address of previous next element */  \
00208 }
00209 #endif /* !TAILQ_ENTRY */
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;      /* for managing timeouts */
00217 
00218         struct event_base *ev_base;
00219 
00220         int ev_fd;
00221         short ev_events;
00222         short ev_ncalls;
00223         short *ev_pncalls;      /* Allows deletes in callback */
00224 
00225         struct timeval ev_timeout;
00226 
00227         int ev_pri;             /* smaller numbers are higher priority */
00228 
00229         void (*ev_callback)(int, short, void *arg);
00230         void *ev_arg;
00231 
00232         int ev_res;             /* result passed to event callback */
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  * Key-Value pairs.  Can be used for HTTP headers but also for
00241  * query argument parsing.
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 /* _EVENT_DEFINED_TQENTRY */
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(struct timeval *);
00404 
00405 
00420 int event_base_loopexit(struct event_base *, 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 *, struct timeval *);
00563 
00564 
00583 int event_base_once(struct event_base *, int, short, void (*)(int, short, void *), void *, struct timeval *);
00584 
00585 
00604 int event_add(struct event *, struct timeval *);
00605 
00606 
00618 int event_del(struct event *);
00619 
00620 void event_active(struct event *, int, short);
00621 
00622 
00634 int event_pending(struct event *, short, struct timeval *);
00635 
00636 
00647 #ifdef WIN32
00648 #define event_initialized(ev)           ((ev)->ev_flags & EVLIST_INIT && (ev)->ev_fd != (int)INVALID_HANDLE_VALUE)
00649 #else
00650 #define event_initialized(ev)           ((ev)->ev_flags & EVLIST_INIT)
00651 #endif
00652 
00653 
00659 const char *event_get_version(void);
00660 
00661 
00667 const char *event_get_method(void);
00668 
00669 
00690 int     event_priority_init(int);
00691 
00692 
00703 int     event_base_priority_init(struct event_base *, int);
00704 
00705 
00714 int     event_priority_set(struct event *, int);
00715 
00716 
00717 /* These functions deal with buffering input and output */
00718 
00719 struct evbuffer {
00720         u_char *buffer;
00721         u_char *orig_buffer;
00722 
00723         size_t misalign;
00724         size_t totallen;
00725         size_t off;
00726 
00727         void (*cb)(struct evbuffer *, size_t, size_t, void *);
00728         void *cbarg;
00729 };
00730 
00731 /* Just for error reporting - use other constants otherwise */
00732 #define EVBUFFER_READ           0x01
00733 #define EVBUFFER_WRITE          0x02
00734 #define EVBUFFER_EOF            0x10
00735 #define EVBUFFER_ERROR          0x20
00736 #define EVBUFFER_TIMEOUT        0x40
00737 
00738 struct bufferevent;
00739 typedef void (*evbuffercb)(struct bufferevent *, void *);
00740 typedef void (*everrorcb)(struct bufferevent *, short what, void *);
00741 
00742 struct event_watermark {
00743         size_t low;
00744         size_t high;
00745 };
00746 
00747 struct bufferevent {
00748         struct event ev_read;
00749         struct event ev_write;
00750 
00751         struct evbuffer *input;
00752         struct evbuffer *output;
00753 
00754         struct event_watermark wm_read;
00755         struct event_watermark wm_write;
00756 
00757         evbuffercb readcb;
00758         evbuffercb writecb;
00759         everrorcb errorcb;
00760         void *cbarg;
00761 
00762         int timeout_read;       /* in seconds */
00763         int timeout_write;      /* in seconds */
00764 
00765         short enabled;  /* events that are currently enabled */
00766 };
00767 
00768 
00803 struct bufferevent *bufferevent_new(int fd,
00804     evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg);
00805 
00806 
00815 int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev);
00816 
00817 
00825 int bufferevent_priority_set(struct bufferevent *bufev, int pri);
00826 
00827 
00833 void bufferevent_free(struct bufferevent *bufev);
00834 
00835 
00849 int bufferevent_write(struct bufferevent *bufev,
00850     const void *data, size_t size);
00851 
00852 
00862 int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf);
00863 
00864 
00875 size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size);
00876 
00885 int bufferevent_enable(struct bufferevent *bufev, short event);
00886 
00887 
00896 int bufferevent_disable(struct bufferevent *bufev, short event);
00897 
00898 
00906 void bufferevent_settimeout(struct bufferevent *bufev,
00907     int timeout_read, int timeout_write);
00908 
00909 
00910 #define EVBUFFER_LENGTH(x)      (x)->off
00911 #define EVBUFFER_DATA(x)        (x)->buffer
00912 #define EVBUFFER_INPUT(x)       (x)->input
00913 #define EVBUFFER_OUTPUT(x)      (x)->output
00914 
00915 
00922 struct evbuffer *evbuffer_new(void);
00923 
00924 
00930 void evbuffer_free(struct evbuffer *);
00931 
00932 
00942 int evbuffer_expand(struct evbuffer *, size_t);
00943 
00944 
00952 int evbuffer_add(struct evbuffer *, const void *, size_t);
00953 
00954 
00955 
00964 int evbuffer_remove(struct evbuffer *, void *, size_t);
00965 
00966 
00976 char *evbuffer_readline(struct evbuffer *);
00977 
00978 
00989 int evbuffer_add_buffer(struct evbuffer *, struct evbuffer *);
00990 
00991 
01000 int evbuffer_add_printf(struct evbuffer *, const char *fmt, ...)
01001 #ifdef __GNUC__
01002   __attribute__((format(printf, 2, 3)))
01003 #endif
01004 ;
01005 
01006 
01015 int evbuffer_add_vprintf(struct evbuffer *, const char *fmt, va_list ap);
01016 
01017 
01025 void evbuffer_drain(struct evbuffer *, size_t);
01026 
01027 
01038 int evbuffer_write(struct evbuffer *, int);
01039 
01040 
01050 int evbuffer_read(struct evbuffer *, int, int);
01051 
01052 
01061 u_char *evbuffer_find(struct evbuffer *, const u_char *, size_t);
01062 
01070 void evbuffer_setcb(struct evbuffer *, void (*)(struct evbuffer *, size_t, size_t, void *), void *);
01071 
01072 /*
01073  * Marshaling tagged data - We assume that all tags are inserted in their
01074  * numeric order - so that unknown tags will always be higher than the
01075  * known ones - and we can just ignore the end of an event buffer.
01076  */
01077 
01078 void evtag_init(void);
01079 
01080 void evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag, const void *data,
01081     ev_uint32_t len);
01082 
01093 void encode_int(struct evbuffer *evbuf, ev_uint32_t number);
01094 
01095 void evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag,
01096     ev_uint32_t integer);
01097 
01098 void evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag,
01099     const char *string);
01100 
01101 void evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag,
01102     struct timeval *tv);
01103 
01104 int evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag,
01105     struct evbuffer *dst);
01106 int evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag);
01107 int evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength);
01108 int evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength);
01109 int evtag_consume(struct evbuffer *evbuf);
01110 
01111 int evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag,
01112     ev_uint32_t *pinteger);
01113 
01114 int evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag,
01115     void *data, size_t len);
01116 
01117 int evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag,
01118     char **pstring);
01119 
01120 int evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag,
01121     struct timeval *ptv);
01122 
01123 #ifdef __cplusplus
01124 }
01125 #endif
01126 
01127 #endif /* _EVENT_H_ */

Generated on Fri Apr 11 08:26:23 2008 for libevent by  doxygen 1.5.3