From 0ac73078ed42b5185a45610534ce3475ba6c27e7 Mon Sep 17 00:00:00 2001 From: Nick Mathewson Date: Wed, 16 Apr 2008 20:01:51 +0000 Subject: [PATCH] r15193@tombo: nickm | 2008-04-16 16:00:35 -0400 Split event.h into several new headers in include/event2. event.h is now just a wrapper that includes all the subheaders. svn:r711 --- buffer.c | 3 +- evbuffer.c | 7 +- event.c | 6 +- event.h | 812 +--------------------------- event_tagging.c | 4 +- evutil.c | 2 +- evutil.h | 160 +----- include/Makefile.am | 10 +- include/event2/bufferevent.h | 229 ++++++++ include/event2/bufferevent_struct.h | 97 ++++ include/event2/event.h | 443 +++++++++++++++ include/event2/event_compat.h | 187 +++++++ include/event2/event_struct.h | 143 +++++ include/event2/tag.h | 131 +++++ include/event2/util.h | 190 +++++++ kqueue.c | 3 +- poll.c | 3 +- select.c | 3 +- signal.c | 5 +- 19 files changed, 1459 insertions(+), 979 deletions(-) create mode 100644 include/event2/bufferevent.h create mode 100644 include/event2/bufferevent_struct.h create mode 100644 include/event2/event.h create mode 100644 include/event2/event_compat.h create mode 100644 include/event2/event_struct.h create mode 100644 include/event2/tag.h create mode 100644 include/event2/util.h diff --git a/buffer.c b/buffer.c index c669460b..720564cd 100644 --- a/buffer.c +++ b/buffer.c @@ -65,7 +65,8 @@ #include #endif -#include "event.h" +#include "event2/event.h" +#include "event2/buffer.h" #include "config.h" #include "log.h" #include "mm-internal.h" diff --git a/evbuffer.c b/evbuffer.c index d86c1947..f8aa289f 100644 --- a/evbuffer.c +++ b/evbuffer.c @@ -47,8 +47,11 @@ #include #endif -#include "evutil.h" -#include "event.h" +#include "event2/util.h" +#include "event2/bufferevent.h" +#include "event2/buffer.h" +#include "event2/bufferevent_struct.h" +#include "event2/event.h" #include "mm-internal.h" /* prototypes */ diff --git a/event.c b/event.c index da4929a2..f367d3f7 100644 --- a/event.c +++ b/event.c @@ -55,11 +55,13 @@ #include #include -#include "event.h" +#include "event2/event.h" +#include "event2/event_struct.h" +#include "event2/event_compat.h" #include "event-internal.h" #include "evthread-internal.h" #include "event2/thread.h" -#include "evutil.h" +#include "event2/util.h" #include "log.h" #ifdef HAVE_EVENT_PORTS diff --git a/event.h b/event.h index 4d3ec08c..8cbb93df 100644 --- a/event.h +++ b/event.h @@ -185,813 +185,13 @@ typedef unsigned char u_char; typedef unsigned short u_short; #endif -#define EVLIST_TIMEOUT 0x01 -#define EVLIST_INSERTED 0x02 -#define EVLIST_SIGNAL 0x04 -#define EVLIST_ACTIVE 0x08 -#define EVLIST_INTERNAL 0x10 -#define EVLIST_INIT 0x80 - -/* EVLIST_X_ Private space: 0x1000-0xf000 */ -#define EVLIST_ALL (0xf000 | 0x9f) - -#define EV_TIMEOUT 0x01 -#define EV_READ 0x02 -#define EV_WRITE 0x04 -#define EV_SIGNAL 0x08 -#define EV_PERSIST 0x10 /* Persistant event */ - -/* Fix so that ppl dont have to run with */ -#ifndef TAILQ_ENTRY -#define _EVENT_DEFINED_TQENTRY -#define TAILQ_ENTRY(type) \ -struct { \ - struct type *tqe_next; /* next element */ \ - struct type **tqe_prev; /* address of previous next element */ \ -} -#endif /* !TAILQ_ENTRY */ - -struct event_base; -struct event { - TAILQ_ENTRY (event) ev_next; - TAILQ_ENTRY (event) ev_active_next; - TAILQ_ENTRY (event) ev_signal_next; - unsigned int min_heap_idx; /* for managing timeouts */ - - struct event_base *ev_base; - - evutil_socket_t ev_fd; - short ev_events; - short ev_ncalls; - short *ev_pncalls; /* Allows deletes in callback */ - - struct timeval ev_timeout; - - int ev_pri; /* smaller numbers are higher priority */ - - void (*ev_callback)(evutil_socket_t, short, void *arg); - void *ev_arg; - - int ev_res; /* result passed to event callback */ - int ev_flags; -}; - -#define EVENT_SIGNAL(ev) (int)(ev)->ev_fd -#define EVENT_FD(ev) (int)(ev)->ev_fd - -/* - * Key-Value pairs. Can be used for HTTP headers but also for - * query argument parsing. - */ -struct evkeyval { - TAILQ_ENTRY(evkeyval) next; - - char *key; - char *value; -}; - -#ifdef _EVENT_DEFINED_TQENTRY -#undef TAILQ_ENTRY -struct event_list; -struct evkeyvalq; -#undef _EVENT_DEFINED_TQENTRY -#else -TAILQ_HEAD (event_list, event); -TAILQ_HEAD (evkeyvalq, evkeyval); -#endif /* _EVENT_DEFINED_TQENTRY */ - -/** - Initialize the event API. - - Use event_base_new() to initialize a new event base, but does not set - the current_base global. If using only event_base_new(), each event - added must have an event base set with event_base_set() - - @see event_base_set(), event_base_free(), event_init() - */ -struct event_base *event_base_new(void); - -/** - Initialize the event API. - - The event API needs to be initialized with event_init() before it can be - used. Sets the current_base global representing the default base for - events that have no base associated with them. - - @see event_base_set(), event_base_new() - */ -struct event_base *event_init(void); - -/** - Reinitialized the event base after a fork - - Some event mechanisms do not survive across fork. The event base needs - to be reinitialized with the event_reinit() function. - - @param base the event base that needs to be re-initialized - @return 0 if successful, or -1 if some events could not be re-added. - @see event_base_new(), event_init() -*/ -int event_reinit(struct event_base *base); - -/** - Loop to process events. - - In order to process events, an application needs to call - event_dispatch(). This function only returns on error, and should - replace the event core of the application program. - - @see event_base_dispatch() - */ -int event_dispatch(void); - - -/** - Threadsafe event dispatching loop. - - @param eb the event_base structure returned by event_init() - @see event_init(), event_dispatch() - */ -int event_base_dispatch(struct event_base *); - - -/** - Get the kernel event notification mechanism used by libevent. - - @param eb the event_base structure returned by event_base_new() - @return a string identifying the kernel event mechanism (kqueue, epoll, etc.) - */ -const char *event_base_get_method(struct event_base *); - - -/** - Deallocate all memory associated with an event_base, and free the base. - - Note that this function will not close any fds or free any memory passed - to event_set as the argument to callback. - - @param eb an event_base to be freed - */ -void event_base_free(struct event_base *); - - -#define _EVENT_LOG_DEBUG 0 -#define _EVENT_LOG_MSG 1 -#define _EVENT_LOG_WARN 2 -#define _EVENT_LOG_ERR 3 -typedef void (*event_log_cb)(int severity, const char *msg); -/** - Redirect libevent's log messages. - - @param cb a function taking two arguments: an integer severity between - _EVENT_LOG_DEBUG and _EVENT_LOG_ERR, and a string. If cb is NULL, - then the default log is used. - */ -void event_set_log_callback(event_log_cb cb); - -/** - Associate a different event base with an event. - - @param eb the event base - @param ev the event - */ -int event_base_set(struct event_base *, struct event *); - -/** - event_loop() flags - */ -/*@{*/ -#define EVLOOP_ONCE 0x01 /**< Block at most once. */ -#define EVLOOP_NONBLOCK 0x02 /**< Do not block. */ -/*@}*/ - -/** - Handle events. - - This is a more flexible version of event_dispatch(). - - @param flags any combination of EVLOOP_ONCE | EVLOOP_NONBLOCK - @return 0 if successful, -1 if an error occurred, or 1 if no events were - registered. - @see event_loopexit(), event_base_loop() -*/ -int event_loop(int); - -/** - Handle events (threadsafe version). - - This is a more flexible version of event_base_dispatch(). - - @param eb the event_base structure returned by event_init() - @param flags any combination of EVLOOP_ONCE | EVLOOP_NONBLOCK - @return 0 if successful, -1 if an error occurred, or 1 if no events were - registered. - @see event_loopexit(), event_base_loop() - */ -int event_base_loop(struct event_base *, int); - -/** - Exit the event loop after the specified time. - - The next event_loop() iteration after the given timer expires will - complete normally (handling all queued events) then exit without - blocking for events again. - - Subsequent invocations of event_loop() will proceed normally. - - @param tv the amount of time after which the loop should terminate. - @return 0 if successful, or -1 if an error occurred - @see event_loop(), event_base_loop(), event_base_loopexit() - */ -int event_loopexit(struct timeval *); - - -/** - Exit the event loop after the specified time (threadsafe variant). - - The next event_base_loop() iteration after the given timer expires will - complete normally (handling all queued events) then exit without - blocking for events again. - - Subsequent invocations of event_base_loop() will proceed normally. - - @param eb the event_base structure returned by event_init() - @param tv the amount of time after which the loop should terminate. - @return 0 if successful, or -1 if an error occurred - @see event_loopexit() - */ -int event_base_loopexit(struct event_base *, struct timeval *); - -/** - Abort the active event_loop() immediately. - - event_loop() will abort the loop after the next event is completed; - event_loopbreak() is typically invoked from this event's callback. - This behavior is analogous to the "break;" statement. - - Subsequent invocations of event_loop() will proceed normally. - - @return 0 if successful, or -1 if an error occurred - @see event_base_loopbreak(), event_loopexit() - */ -int event_loopbreak(void); - -/** - Abort the active event_base_loop() immediately. - - event_base_loop() will abort the loop after the next event is completed; - event_base_loopbreak() is typically invoked from this event's callback. - This behavior is analogous to the "break;" statement. - - Subsequent invocations of event_loop() will proceed normally. - - @param eb the event_base structure returned by event_init() - @return 0 if successful, or -1 if an error occurred - @see event_base_loopexit - */ -int event_base_loopbreak(struct event_base *); - - -/** - Add a timer event. - - @param ev the event struct - @param tv timeval struct - */ -#define evtimer_add(ev, tv) event_add(ev, tv) - - -/** - Define a timer event. - - @param ev event struct to be modified - @param cb callback function - @param arg argument that will be passed to the callback function - */ -#define evtimer_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg) - - -/** - * Delete a timer event. - * - * @param ev the event struct to be disabled - */ -#define evtimer_del(ev) event_del(ev) -#define evtimer_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv) -#define evtimer_initialized(ev) ((ev)->ev_flags & EVLIST_INIT) - -/** - * Add a timeout event. - * - * @param ev the event struct to be disabled - * @param tv the timeout value, in seconds - */ -#define timeout_add(ev, tv) event_add(ev, tv) - - -/** - * Define a timeout event. - * - * @param ev the event struct to be defined - * @param cb the callback to be invoked when the timeout expires - * @param arg the argument to be passed to the callback - */ -#define timeout_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg) - - -/** - * Disable a timeout event. - * - * @param ev the timeout event to be disabled - */ -#define timeout_del(ev) event_del(ev) - -#define timeout_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv) -#define timeout_initialized(ev) ((ev)->ev_flags & EVLIST_INIT) - -#define signal_add(ev, tv) event_add(ev, tv) -#define signal_set(ev, x, cb, arg) \ - event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg) -#define signal_del(ev) event_del(ev) -#define signal_pending(ev, tv) event_pending(ev, EV_SIGNAL, tv) -#define signal_initialized(ev) ((ev)->ev_flags & EVLIST_INIT) - -/** - Prepare an event structure to be added. - - The function event_set() prepares the event structure ev to be used in - future calls to event_add() and event_del(). The event will be prepared to - call the function specified by the fn argument with an int argument - indicating the file descriptor, a short argument indicating the type of - event, and a void * argument given in the arg argument. The fd indicates - the file descriptor that should be monitored for events. The events can be - either EV_READ, EV_WRITE, or both. Indicating that an application can read - or write from the file descriptor respectively without blocking. - - The function fn will be called with the file descriptor that triggered the - event and the type of event which will be either EV_TIMEOUT, EV_SIGNAL, - EV_READ, or EV_WRITE. The additional flag EV_PERSIST makes an event_add() - persistent until event_del() has been called. - - @param ev an event struct to be modified - @param fd the file descriptor to be monitored - @param event desired events to monitor; can be EV_READ and/or EV_WRITE - @param fn callback function to be invoked when the event occurs - @param arg an argument to be passed to the callback function - - @see event_add(), event_del(), event_once() - - */ -void event_set(struct event *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *); - -/** - Schedule a one-time event to occur. - - The function event_once() is similar to event_set(). However, it schedules - a callback to be called exactly once and does not require the caller to - prepare an event structure. - - @param fd a file descriptor to monitor - @param events event(s) to monitor; can be any of EV_TIMEOUT | EV_READ | - EV_WRITE - @param callback callback function to be invoked when the event occurs - @param arg an argument to be passed to the callback function - @param timeout the maximum amount of time to wait for the event, or NULL - to wait forever - @return 0 if successful, or -1 if an error occurred - @see event_set() - - */ -int event_once(evutil_socket_t , short, void (*)(evutil_socket_t, short, void *), void *, struct timeval *); - - -/** - Schedule a one-time event (threadsafe variant) - - The function event_base_once() is similar to event_set(). However, it - schedules a callback to be called exactly once and does not require the - caller to prepare an event structure. - - @param base an event_base returned by event_init() - @param fd a file descriptor to monitor - @param events event(s) to monitor; can be any of EV_TIMEOUT | EV_READ | - EV_WRITE - @param callback callback function to be invoked when the event occurs - @param arg an argument to be passed to the callback function - @param timeout the maximum amount of time to wait for the event, or NULL - to wait forever - @return 0 if successful, or -1 if an error occurred - @see event_once() - */ -int event_base_once(struct event_base *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *, struct timeval *); - - -/** - Add an event to the set of monitored events. - - The function event_add() schedules the execution of the ev event when the - event specified in event_set() occurs or in at least the time specified in - the tv. If tv is NULL, no timeout occurs and the function will only be - called if a matching event occurs on the file descriptor. The event in the - ev argument must be already initialized by event_set() and may not be used - in calls to event_set() until it has timed out or been removed with - event_del(). If the event in the ev argument already has a scheduled - timeout, the old timeout will be replaced by the new one. - - @param ev an event struct initialized via event_set() - @param timeout the maximum amount of time to wait for the event, or NULL - to wait forever - @return 0 if successful, or -1 if an error occurred - @see event_del(), event_set() - */ -int event_add(struct event *, struct timeval *); - - -/** - Remove an event from the set of monitored events. - - The function event_del() will cancel the event in the argument ev. If the - event has already executed or has never been added the call will have no - effect. - - @param ev an event struct to be removed from the working set - @return 0 if successful, or -1 if an error occurred - @see event_add() - */ -int event_del(struct event *); - -/** - Make an event active. - - @param ev an event to make active. - @param res a set of flags to pass to the event's callback. - @param ncalls - **/ -void event_active(struct event *, int, short); - - -/** - Checks if a specific event is pending or scheduled. - - @param ev an event struct previously passed to event_add() - @param event the requested event type; any of EV_TIMEOUT|EV_READ| - EV_WRITE|EV_SIGNAL - @param tv an alternate timeout (FIXME - is this true?) - - @return 1 if the event is pending, or 0 if the event has not occurred - - */ -int event_pending(struct event *, short, struct timeval *); - - -/** - Test if an event structure has been initialized. - - The event_initialized() macro can be used to check if an event has been - initialized. - - @param ev an event structure to be tested - @return 1 if the structure has been initialized, or 0 if it has not been - initialized - */ -#ifdef WIN32 -#define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT && (ev)->ev_fd != (int)INVALID_HANDLE_VALUE) -#else -#define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT) -#endif - - -/** - Get the libevent version number. - - @return a string containing the version number of libevent - */ -const char *event_get_version(void); - - -/** - Get the kernel event notification mechanism used by libevent. - - @return a string identifying the kernel event mechanism (kqueue, epoll, etc.) - */ -const char *event_get_method(void); - - -/** - Set the number of different event priorities. - - By default libevent schedules all active events with the same priority. - However, some time it is desirable to process some events with a higher - priority than others. For that reason, libevent supports strict priority - queues. Active events with a lower priority are always processed before - events with a higher priority. - - The number of different priorities can be set initially with the - event_priority_init() function. This function should be called before the - first call to event_dispatch(). The event_priority_set() function can be - used to assign a priority to an event. By default, libevent assigns the - middle priority to all events unless their priority is explicitly set. - - @param npriorities the maximum number of priorities - @return 0 if successful, or -1 if an error occurred - @see event_base_priority_init(), event_priority_set() - - */ -int event_priority_init(int); - - -/** - Set the number of different event priorities (threadsafe variant). - - See the description of event_priority_init() for more information. - - @param eb the event_base structure returned by event_init() - @param npriorities the maximum number of priorities - @return 0 if successful, or -1 if an error occurred - @see event_priority_init(), event_priority_set() - */ -int event_base_priority_init(struct event_base *, int); - - -/** - Assign a priority to an event. - - @param ev an event struct - @param priority the new priority to be assigned - @return 0 if successful, or -1 if an error occurred - @see event_priority_init() - */ -int event_priority_set(struct event *, int); - - +#include +#include +#include #include - -/* These functions deal with buffering input and output */ - -/* Just for error reporting - use other constants otherwise */ -#define EVBUFFER_READ 0x01 -#define EVBUFFER_WRITE 0x02 -#define EVBUFFER_EOF 0x10 -#define EVBUFFER_ERROR 0x20 -#define EVBUFFER_TIMEOUT 0x40 - -struct bufferevent; -typedef void (*evbuffercb)(struct bufferevent *, void *); -typedef void (*everrorcb)(struct bufferevent *, short what, void *); - -struct event_watermark { - size_t low; - size_t high; -}; - -struct bufferevent { - struct event ev_read; - struct event ev_write; - - struct evbuffer *input; - struct evbuffer *output; - - struct event_watermark wm_read; - struct event_watermark wm_write; - - evbuffercb readcb; - evbuffercb writecb; - everrorcb errorcb; - void *cbarg; - - int timeout_read; /* in seconds */ - int timeout_write; /* in seconds */ - - short enabled; /* events that are currently enabled */ -}; - - -/** - Create a new bufferevent. - - libevent provides an abstraction on top of the regular event callbacks. - This abstraction is called a buffered event. A buffered event provides - input and output buffers that get filled and drained automatically. The - user of a buffered event no longer deals directly with the I/O, but - instead is reading from input and writing to output buffers. - - Once initialized, the bufferevent structure can be used repeatedly with - bufferevent_enable() and bufferevent_disable(). - - When read enabled the bufferevent will try to read from the file descriptor - and call the read callback. The write callback is executed whenever the - output buffer is drained below the write low watermark, which is 0 by - default. - - If multiple bases are in use, bufferevent_base_set() must be called before - enabling the bufferevent for the first time. - - @param fd the file descriptor from which data is read and written to. - This file descriptor is not allowed to be a pipe(2). - @param readcb callback to invoke when there is data to be read, or NULL if - no callback is desired - @param writecb callback to invoke when the file descriptor is ready for - writing, or NULL if no callback is desired - @param errorcb callback to invoke when there is an error on the file - descriptor - @param cbarg an argument that will be supplied to each of the callbacks - (readcb, writecb, and errorcb) - @return a pointer to a newly allocated bufferevent struct, or NULL if an - error occurred - @see bufferevent_base_set(), bufferevent_free() - */ -struct bufferevent *bufferevent_new(evutil_socket_t fd, - evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg); - - -/** - Assign a bufferevent to a specific event_base. - - @param base an event_base returned by event_init() - @param bufev a bufferevent struct returned by bufferevent_new() - @return 0 if successful, or -1 if an error occurred - @see bufferevent_new() - */ -int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev); - - -/** - Assign a priority to a bufferevent. - - @param bufev a bufferevent struct - @param pri the priority to be assigned - @return 0 if successful, or -1 if an error occurred - */ -int bufferevent_priority_set(struct bufferevent *bufev, int pri); - - -/** - Deallocate the storage associated with a bufferevent structure. - - @param bufev the bufferevent structure to be freed. - */ -void bufferevent_free(struct bufferevent *bufev); - - -/** - Write data to a bufferevent buffer. - - The bufferevent_write() function can be used to write data to the file - descriptor. The data is appended to the output buffer and written to the - descriptor automatically as it becomes available for writing. - - @param bufev the bufferevent to be written to - @param data a pointer to the data to be written - @param size the length of the data, in bytes - @return 0 if successful, or -1 if an error occurred - @see bufferevent_write_buffer() - */ -int bufferevent_write(struct bufferevent *bufev, - const void *data, size_t size); - - -/** - Write data from an evbuffer to a bufferevent buffer. The evbuffer is - being drained as a result. - - @param bufev the bufferevent to be written to - @param buf the evbuffer to be written - @return 0 if successful, or -1 if an error occurred - @see bufferevent_write() - */ -int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf); - - -/** - Read data from a bufferevent buffer. - - The bufferevent_read() function is used to read data from the input buffer. - - @param bufev the bufferevent to be read from - @param data pointer to a buffer that will store the data - @param size the size of the data buffer, in bytes - @return the amount of data read, in bytes. - */ -size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size); - -/** - Enable a bufferevent. - - @param bufev the bufferevent to be enabled - @param event any combination of EV_READ | EV_WRITE. - @return 0 if successful, or -1 if an error occurred - @see bufferevent_disable() - */ -int bufferevent_enable(struct bufferevent *bufev, short event); - - -/** - Disable a bufferevent. - - @param bufev the bufferevent to be disabled - @param event any combination of EV_READ | EV_WRITE. - @return 0 if successful, or -1 if an error occurred - @see bufferevent_enable() - */ -int bufferevent_disable(struct bufferevent *bufev, short event); - - -/** - Set the read and write timeout for a buffered event. - - @param bufev the bufferevent to be modified - @param timeout_read the read timeout - @param timeout_write the write timeout - */ -void bufferevent_settimeout(struct bufferevent *bufev, - int timeout_read, int timeout_write); - - -#define EVBUFFER_INPUT(x) (x)->input -#define EVBUFFER_OUTPUT(x) (x)->output - -/* - * Marshaling tagged data - We assume that all tags are inserted in their - * numeric order - so that unknown tags will always be higher than the - * known ones - and we can just ignore the end of an event buffer. - */ - -void evtag_init(void); - -/** - Unmarshals the header and returns the length of the payload - - @param evbuf the buffer from which to unmarshal data - @param ptag a pointer in which the tag id is being stored - @returns -1 on failure or the number of bytes in the remaining payload. -*/ -int evtag_unmarshal_header(struct evbuffer *evbuf, ev_uint32_t *ptag); - -void evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag, const void *data, - ev_uint32_t len); -void evtag_marshal_buffer(struct evbuffer *evbuf, ev_uint32_t tag, - struct evbuffer *data); - -/** - Encode an integer and store it in an evbuffer. - - We encode integer's by nibbles; the first nibble contains the number - of significant nibbles - 1; this allows us to encode up to 64-bit - integers. This function is byte-order independent. - - @param evbuf evbuffer to store the encoded number - @param number a 32-bit integer - */ -void encode_int(struct evbuffer *evbuf, ev_uint32_t number); - -void evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag, - ev_uint32_t integer); - -void evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag, - const char *string); - -void evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag, - struct timeval *tv); - -int evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag, - struct evbuffer *dst); -int evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag); -int evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength); -int evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength); -int evtag_consume(struct evbuffer *evbuf); - -int evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag, - ev_uint32_t *pinteger); - -int evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag, - void *data, size_t len); - -int evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag, - char **pstring); - -int evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag, - struct timeval *ptv); - -/** - Override the functions that libevent uses for memory management. - - Usually, libevent uses the standard libc functions malloc, realloc, and - free to allocate memory. Passing replacements for those functions to - event_set_mem_functions() overrides this behavior. To restore the default - behavior, pass NULLs as the arguments to this function. - - Note that all memory returned from libevent will be allocated by the - replacement functions rather than by malloc() and realloc(). Thus, if you - have replaced those functions, it may not be appropriate to free() memory - that you get from libevent. - - @param malloc_fn A replacement for malloc. - @param realloc_fn A replacement for realloc - @param free_fn A replacement for free. - **/ -void event_set_mem_functions(void *(*malloc_fn)(size_t sz), - void *(*realloc_fn)(void *ptr, size_t sz), - void (*free_fn)(void *ptr)); +#include +#include +#include #ifdef __cplusplus } diff --git a/event_tagging.c b/event_tagging.c index 4f12ac78..955a63fe 100644 --- a/event_tagging.c +++ b/event_tagging.c @@ -61,7 +61,9 @@ #include #endif -#include "event.h" +#include "event2/event.h" +#include "event2/tag.h" +#include "event2/buffer.h" #include "log.h" #include "mm-internal.h" diff --git a/evutil.c b/evutil.c index c75ce82a..7407a707 100644 --- a/evutil.c +++ b/evutil.c @@ -51,7 +51,7 @@ #endif #include -#include "evutil.h" +#include "event2/util.h" #include "log.h" int diff --git a/evutil.h b/evutil.h index d597db0a..3f7fd07a 100644 --- a/evutil.h +++ b/evutil.h @@ -27,164 +27,6 @@ #ifndef _EVUTIL_H_ #define _EVUTIL_H_ -/** @file evutil.h - - Common convenience functions for cross-platform portability and - related socket manipulations. - - */ - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#ifdef _EVENT_HAVE_SYS_TIME_H -#include -#endif -#ifdef _EVENT_HAVE_STDINT_H -#include -#elif defined(_EVENT_HAVE_INTTYPES_H) -#include -#endif -#ifdef _EVENT_HAVE_SYS_TYPES_H -#include -#endif -#ifdef _EVENT_HAVE_STDDEF_H -#include -#endif - -#ifdef _EVENT_HAVE_UINT64_T -#define ev_uint64_t uint64_t -#define ev_int64_t int64_t -#elif defined(WIN32) -#define ev_uint64_t __uint64_t -#define ev_int64_t __int64_t -#elif _EVENT_SIZEOF_LONG_LONG == 8 -#define ev_uint64_t unsigned long long -#define ev_int64_t long long -#elif _EVENT_SIZEOF_LONG == 8 -#define ev_uint64_t unsigned long -#define ev_int64_t long -#else -#error "No way to define ev_uint64_t" -#endif - -#ifdef _EVENT_HAVE_UINT32_T -#define ev_uint32_t uint32_t -#elif defined(WIN32) -#define ev_uint32_t unsigned int -#elif _EVENT_SIZEOF_LONG == 4 -#define ev_uint32_t unsigned long -#elif _EVENT_SIZEOF_INT == 4 -#define ev_uint32_t unsigned int -#else -#error "No way to define ev_uint32_t" -#endif - -#ifdef _EVENT_HAVE_UINT16_T -#define ev_uint16_t uint16_t -#elif defined(WIN32) -#define ev_uint16_t unsigned short -#elif _EVENT_SIZEOF_INT == 2 -#define ev_uint16_t unsigned int -#elif _EVENT_SIZEOF_SHORT == 2 -#define ev_uint16_t unsigned short -#else -#error "No way to define ev_uint16_t" -#endif - -#ifdef _EVENT_HAVE_UINT8_T -#define ev_uint8_t uint8_t -#else -#define ev_uint8_t unsigned char -#endif - -#ifdef WIN32 -/** Type to hold the output of "socket()" or "accept()". On Windows, this is - * an intptr_t; elsewhere, it is an int. */ -#define evutil_socket_t intptr_t -#else -#define evutil_socket_t int -#endif - -int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]); -int evutil_make_socket_nonblocking(evutil_socket_t sock); -#ifdef WIN32 -#define EVUTIL_CLOSESOCKET(s) closesocket(s) -#else -#define EVUTIL_CLOSESOCKET(s) close(s) -#endif - -#ifdef WIN32 -#define EVUTIL_SOCKET_ERROR() WSAGetLastError() -#define EVUTIL_SET_SOCKET_ERROR(errcode) \ - do { WSASetLastError(errcode); } while (0) -#else -#define EVUTIL_SOCKET_ERROR() (errno) -#define EVUTIL_SET_SOCKET_ERROR(errcode) \ - do { errno = (errcode); } while (0) -#endif - -/* - * Manipulation functions for struct timeval - */ -#ifdef _EVENT_HAVE_TIMERADD -#define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp)) -#define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp)) -#else -#define evutil_timeradd(tvp, uvp, vvp) \ - do { \ - (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ - (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ - if ((vvp)->tv_usec >= 1000000) { \ - (vvp)->tv_sec++; \ - (vvp)->tv_usec -= 1000000; \ - } \ - } while (0) -#define evutil_timersub(tvp, uvp, vvp) \ - do { \ - (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ - (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ - if ((vvp)->tv_usec < 0) { \ - (vvp)->tv_sec--; \ - (vvp)->tv_usec += 1000000; \ - } \ - } while (0) -#endif /* !_EVENT_HAVE_HAVE_TIMERADD */ - -#ifdef _EVENT_HAVE_TIMERCLEAR -#define evutil_timerclear(tvp) timerclear(tvp) -#else -#define evutil_timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0 -#endif - -#ifdef _EVENT_HAVE_TIMERCMP -#define evutil_timercmp(tvp, uvp, cmp) timercmp((tvp), (uvp), cmp) -#else -#define evutil_timercmp(tvp, uvp, cmp) \ - (((tvp)->tv_sec == (uvp)->tv_sec) ? \ - ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ - ((tvp)->tv_sec cmp (uvp)->tv_sec)) -#endif - -#ifdef _EVENT_HAVE_TIMERISSET -#define evutil_timerisset(tvp) timerisset(tvp) -#else -#define evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) -#endif - -#ifdef offsetof -#define evutil_offsetof(type, field) offsetof(type, field) -#else -#define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field)) -#endif - -/* big-int related functions */ -ev_int64_t evutil_strtoll(const char *s, char **endptr, int base); - -#ifdef __cplusplus -} -#endif +#include #endif /* _EVUTIL_H_ */ diff --git a/include/Makefile.am b/include/Makefile.am index dadb974e..f4c8255c 100644 --- a/include/Makefile.am +++ b/include/Makefile.am @@ -1,4 +1,10 @@ AUTOMAKE_OPTIONS = foreign -EXTRA_SRC = event2/buffer.h event2/thread.h -nobase_include_HEADERS = event2/buffer.h event2/thread.h +EXTRA_SRC = event2/buffer.h event2/thread.h event2/bufferevent.h \ + event2/bufferevent_struct.h event2/event.h event2/event_compat.h \ + event2/event_struct.h event2/tag.h event2/util.h + +nobase_include_HEADERS = \ + event2/buffer.h event2/thread.h event2/bufferevent.h \ + event2/bufferevent_struct.h event2/event.h event2/event_compat.h \ + event2/event_struct.h event2/tag.h event2/util.h diff --git a/include/event2/bufferevent.h b/include/event2/bufferevent.h new file mode 100644 index 00000000..0ade7bf2 --- /dev/null +++ b/include/event2/bufferevent.h @@ -0,0 +1,229 @@ +/* + * Copyright (c) 2000-2007 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef _EVENT2_BUFFEREVENT_H_ +#define _EVENT2_BUFFEREVENT_H_ + +/** @file bufferevent.h + + Functions for buffering data for network sending or receiving. Bufferevents + are higher level than evbuffers: each has an underlying evbuffer for reading + and one for writing, and callbacks that are invoked under certain + circumstances. + + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#ifdef _EVENT_HAVE_SYS_TYPES_H +#include +#endif +#ifdef _EVENT_HAVE_SYS_TIME_H +#include +#endif +#ifdef _EVENT_HAVE_STDINT_H +#include +#endif +#include + +/* For int types. */ +#include + +#ifdef WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#undef WIN32_LEAN_AND_MEAN +typedef unsigned char u_char; +typedef unsigned short u_short; +#endif + + +/* Just for error reporting - use other constants otherwise */ +#define EVBUFFER_READ 0x01 +#define EVBUFFER_WRITE 0x02 +#define EVBUFFER_EOF 0x10 +#define EVBUFFER_ERROR 0x20 +#define EVBUFFER_TIMEOUT 0x40 +struct bufferevent; +struct event_base; +struct evbuffer; +typedef void (*evbuffercb)(struct bufferevent *, void *); +typedef void (*everrorcb)(struct bufferevent *, short what, void *); + + +/** + Create a new bufferevent. + + libevent provides an abstraction on top of the regular event callbacks. + This abstraction is called a buffered event. A buffered event provides + input and output buffers that get filled and drained automatically. The + user of a buffered event no longer deals directly with the I/O, but + instead is reading from input and writing to output buffers. + + Once initialized, the bufferevent structure can be used repeatedly with + bufferevent_enable() and bufferevent_disable(). + + When read enabled the bufferevent will try to read from the file descriptor + and call the read callback. The write callback is executed whenever the + output buffer is drained below the write low watermark, which is 0 by + default. + + If multiple bases are in use, bufferevent_base_set() must be called before + enabling the bufferevent for the first time. + + @param fd the file descriptor from which data is read and written to. + This file descriptor is not allowed to be a pipe(2). + @param readcb callback to invoke when there is data to be read, or NULL if + no callback is desired + @param writecb callback to invoke when the file descriptor is ready for + writing, or NULL if no callback is desired + @param errorcb callback to invoke when there is an error on the file + descriptor + @param cbarg an argument that will be supplied to each of the callbacks + (readcb, writecb, and errorcb) + @return a pointer to a newly allocated bufferevent struct, or NULL if an + error occurred + @see bufferevent_base_set(), bufferevent_free() + */ +struct bufferevent *bufferevent_new(evutil_socket_t fd, + evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg); + + +/** + Assign a bufferevent to a specific event_base. + + @param base an event_base returned by event_init() + @param bufev a bufferevent struct returned by bufferevent_new() + @return 0 if successful, or -1 if an error occurred + @see bufferevent_new() + */ +int bufferevent_base_set(struct event_base *base, struct bufferevent *bufev); + + +/** + Assign a priority to a bufferevent. + + @param bufev a bufferevent struct + @param pri the priority to be assigned + @return 0 if successful, or -1 if an error occurred + */ +int bufferevent_priority_set(struct bufferevent *bufev, int pri); + + +/** + Deallocate the storage associated with a bufferevent structure. + + @param bufev the bufferevent structure to be freed. + */ +void bufferevent_free(struct bufferevent *bufev); + + +/** + Write data to a bufferevent buffer. + + The bufferevent_write() function can be used to write data to the file + descriptor. The data is appended to the output buffer and written to the + descriptor automatically as it becomes available for writing. + + @param bufev the bufferevent to be written to + @param data a pointer to the data to be written + @param size the length of the data, in bytes + @return 0 if successful, or -1 if an error occurred + @see bufferevent_write_buffer() + */ +int bufferevent_write(struct bufferevent *bufev, + const void *data, size_t size); + + +/** + Write data from an evbuffer to a bufferevent buffer. The evbuffer is + being drained as a result. + + @param bufev the bufferevent to be written to + @param buf the evbuffer to be written + @return 0 if successful, or -1 if an error occurred + @see bufferevent_write() + */ +int bufferevent_write_buffer(struct bufferevent *bufev, struct evbuffer *buf); + + +/** + Read data from a bufferevent buffer. + + The bufferevent_read() function is used to read data from the input buffer. + + @param bufev the bufferevent to be read from + @param data pointer to a buffer that will store the data + @param size the size of the data buffer, in bytes + @return the amount of data read, in bytes. + */ +size_t bufferevent_read(struct bufferevent *bufev, void *data, size_t size); + +/** + Enable a bufferevent. + + @param bufev the bufferevent to be enabled + @param event any combination of EV_READ | EV_WRITE. + @return 0 if successful, or -1 if an error occurred + @see bufferevent_disable() + */ +int bufferevent_enable(struct bufferevent *bufev, short event); + + +/** + Disable a bufferevent. + + @param bufev the bufferevent to be disabled + @param event any combination of EV_READ | EV_WRITE. + @return 0 if successful, or -1 if an error occurred + @see bufferevent_enable() + */ +int bufferevent_disable(struct bufferevent *bufev, short event); + + +/** + Set the read and write timeout for a buffered event. + + @param bufev the bufferevent to be modified + @param timeout_read the read timeout + @param timeout_write the write timeout + */ +void bufferevent_settimeout(struct bufferevent *bufev, + int timeout_read, int timeout_write); + + +#define EVBUFFER_INPUT(x) (x)->input +#define EVBUFFER_OUTPUT(x) (x)->output + + +#ifdef __cplusplus +} +#endif + +#endif /* _EVENT2_BUFFEREVENT_H_ */ diff --git a/include/event2/bufferevent_struct.h b/include/event2/bufferevent_struct.h new file mode 100644 index 00000000..9e2c296a --- /dev/null +++ b/include/event2/bufferevent_struct.h @@ -0,0 +1,97 @@ +/* + * Copyright (c) 2000-2007 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef _EVENT2_BUFFEREVENT_STRUCT_H_ +#define _EVENT2_BUFFEREVENT_STRUCT_H_ + +/** @file bufferevent_struct.h + + Data structures for bufferevents. Using these structures may hurt forward + compatibility with later versions of libevent: be careful! + + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#ifdef _EVENT_HAVE_SYS_TYPES_H +#include +#endif +#ifdef _EVENT_HAVE_SYS_TIME_H +#include +#endif +#ifdef _EVENT_HAVE_STDINT_H +#include +#endif +#include + +/* For int types. */ +#include +/* For struct event */ +#include + +#ifdef WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#undef WIN32_LEAN_AND_MEAN +typedef unsigned char u_char; +typedef unsigned short u_short; +#endif + +struct event_watermark { + size_t low; + size_t high; +}; + +struct bufferevent { + struct event ev_read; + struct event ev_write; + + struct evbuffer *input; + struct evbuffer *output; + + struct event_watermark wm_read; + struct event_watermark wm_write; + + evbuffercb readcb; + evbuffercb writecb; + everrorcb errorcb; + void *cbarg; + + int timeout_read; /* in seconds */ + int timeout_write; /* in seconds */ + + short enabled; /* events that are currently enabled */ +}; + + +#ifdef __cplusplus +} +#endif + +#endif /* _EVENT2_BUFFEREVENT_STRUCT_H_ */ diff --git a/include/event2/event.h b/include/event2/event.h new file mode 100644 index 00000000..1812fb80 --- /dev/null +++ b/include/event2/event.h @@ -0,0 +1,443 @@ +/* + * Copyright (c) 2000-2007 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef _EVENT2_EVENT_H_ +#define _EVENT2_EVENT_H_ + +/** @file event.h + + Core functions for waiting for and receiving events, and using event bases. + + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#ifdef _EVENT_HAVE_SYS_TYPES_H +#include +#endif +#ifdef _EVENT_HAVE_SYS_TIME_H +#include +#endif +#ifdef _EVENT_HAVE_STDINT_H +#include +#endif +#include + +/* For int types. */ +#include + +#ifdef WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#undef WIN32_LEAN_AND_MEAN +typedef unsigned char u_char; +typedef unsigned short u_short; +#endif + +struct event_base; +struct event; + +/** + Initialize the event API. + + Use event_base_new() to initialize a new event base, but does not set + the current_base global. If using only event_base_new(), each event + added must have an event base set with event_base_set() + + @see event_base_set(), event_base_free(), event_init() + */ +struct event_base *event_base_new(void); + +/** + Reinitialized the event base after a fork + + Some event mechanisms do not survive across fork. The event base needs + to be reinitialized with the event_reinit() function. + + @param base the event base that needs to be re-initialized + @return 0 if successful, or -1 if some events could not be re-added. + @see event_base_new(), event_init() +*/ +int event_reinit(struct event_base *base); + +/** + Threadsafe event dispatching loop. + + @param eb the event_base structure returned by event_init() + @see event_init(), event_dispatch() + */ +int event_base_dispatch(struct event_base *); + + +/** + Get the kernel event notification mechanism used by libevent. + + @param eb the event_base structure returned by event_base_new() + @return a string identifying the kernel event mechanism (kqueue, epoll, etc.) + */ +const char *event_base_get_method(struct event_base *); + + +/** + Deallocate all memory associated with an event_base, and free the base. + + Note that this function will not close any fds or free any memory passed + to event_set as the argument to callback. + + @param eb an event_base to be freed + */ +void event_base_free(struct event_base *); + +#define _EVENT_LOG_DEBUG 0 +#define _EVENT_LOG_MSG 1 +#define _EVENT_LOG_WARN 2 +#define _EVENT_LOG_ERR 3 +typedef void (*event_log_cb)(int severity, const char *msg); +/** + Redirect libevent's log messages. + + @param cb a function taking two arguments: an integer severity between + _EVENT_LOG_DEBUG and _EVENT_LOG_ERR, and a string. If cb is NULL, + then the default log is used. + */ +void event_set_log_callback(event_log_cb cb); + +/** + Associate a different event base with an event. + + @param eb the event base + @param ev the event + */ +int event_base_set(struct event_base *, struct event *); + +/** + event_loop() flags + */ +/*@{*/ +#define EVLOOP_ONCE 0x01 /**< Block at most once. */ +#define EVLOOP_NONBLOCK 0x02 /**< Do not block. */ +/*@}*/ + +/** + Handle events (threadsafe version). + + This is a more flexible version of event_base_dispatch(). + + @param eb the event_base structure returned by event_init() + @param flags any combination of EVLOOP_ONCE | EVLOOP_NONBLOCK + @return 0 if successful, -1 if an error occurred, or 1 if no events were + registered. + @see event_loopexit(), event_base_loop() + */ +int event_base_loop(struct event_base *, int); + +/** + Exit the event loop after the specified time (threadsafe variant). + + The next event_base_loop() iteration after the given timer expires will + complete normally (handling all queued events) then exit without + blocking for events again. + + Subsequent invocations of event_base_loop() will proceed normally. + + @param eb the event_base structure returned by event_init() + @param tv the amount of time after which the loop should terminate. + @return 0 if successful, or -1 if an error occurred + @see event_loopexit() + */ +int event_base_loopexit(struct event_base *, struct timeval *); + +/** + Abort the active event_base_loop() immediately. + + event_base_loop() will abort the loop after the next event is completed; + event_base_loopbreak() is typically invoked from this event's callback. + This behavior is analogous to the "break;" statement. + + Subsequent invocations of event_loop() will proceed normally. + + @param eb the event_base structure returned by event_init() + @return 0 if successful, or -1 if an error occurred + @see event_base_loopexit + */ +int event_base_loopbreak(struct event_base *); + +/** + Define a timer event. + + @param ev event struct to be modified + @param cb callback function + @param arg argument that will be passed to the callback function + */ +#define evtimer_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg) + +/** + Add a timer event. + + @param ev the event struct + @param tv timeval struct + */ +#define evtimer_add(ev, tv) event_add(ev, tv) + +/** + Define a timer event. + + @param ev event struct to be modified + @param cb callback function + @param arg argument that will be passed to the callback function + */ +#define evtimer_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg) + + +/** + * Delete a timer event. + * + * @param ev the event struct to be disabled + */ +#define evtimer_del(ev) event_del(ev) +#define evtimer_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv) +#define evtimer_initialized(ev) ((ev)->ev_flags & EVLIST_INIT) + +/** + * Add a timeout event. + * + * @param ev the event struct to be disabled + * @param tv the timeout value, in seconds + */ +#define timeout_add(ev, tv) event_add(ev, tv) + + +/** + * Define a timeout event. + * + * @param ev the event struct to be defined + * @param cb the callback to be invoked when the timeout expires + * @param arg the argument to be passed to the callback + */ +#define timeout_set(ev, cb, arg) event_set(ev, -1, 0, cb, arg) + + +/** + * Disable a timeout event. + * + * @param ev the timeout event to be disabled + */ +#define timeout_del(ev) event_del(ev) + +#define timeout_pending(ev, tv) event_pending(ev, EV_TIMEOUT, tv) +#define timeout_initialized(ev) ((ev)->ev_flags & EVLIST_INIT) + +#define signal_add(ev, tv) event_add(ev, tv) +#define signal_set(ev, x, cb, arg) \ + event_set(ev, x, EV_SIGNAL|EV_PERSIST, cb, arg) +#define signal_del(ev) event_del(ev) +#define signal_pending(ev, tv) event_pending(ev, EV_SIGNAL, tv) +#define signal_initialized(ev) ((ev)->ev_flags & EVLIST_INIT) + + +/** + Prepare an event structure to be added. + + The function event_set() prepares the event structure ev to be used in + future calls to event_add() and event_del(). The event will be prepared to + call the function specified by the fn argument with an int argument + indicating the file descriptor, a short argument indicating the type of + event, and a void * argument given in the arg argument. The fd indicates + the file descriptor that should be monitored for events. The events can be + either EV_READ, EV_WRITE, or both. Indicating that an application can read + or write from the file descriptor respectively without blocking. + + The function fn will be called with the file descriptor that triggered the + event and the type of event which will be either EV_TIMEOUT, EV_SIGNAL, + EV_READ, or EV_WRITE. The additional flag EV_PERSIST makes an event_add() + persistent until event_del() has been called. + + @param ev an event struct to be modified + @param fd the file descriptor to be monitored + @param event desired events to monitor; can be EV_READ and/or EV_WRITE + @param fn callback function to be invoked when the event occurs + @param arg an argument to be passed to the callback function + + @see event_add(), event_del(), event_once() + + */ +void event_set(struct event *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *); + +/** + Schedule a one-time event (threadsafe variant) + + The function event_base_once() is similar to event_set(). However, it + schedules a callback to be called exactly once and does not require the + caller to prepare an event structure. + + @param base an event_base returned by event_init() + @param fd a file descriptor to monitor + @param events event(s) to monitor; can be any of EV_TIMEOUT | EV_READ | + EV_WRITE + @param callback callback function to be invoked when the event occurs + @param arg an argument to be passed to the callback function + @param timeout the maximum amount of time to wait for the event, or NULL + to wait forever + @return 0 if successful, or -1 if an error occurred + @see event_once() + */ +int event_base_once(struct event_base *, evutil_socket_t, short, void (*)(evutil_socket_t, short, void *), void *, struct timeval *); + +/** + Add an event to the set of monitored events. + + The function event_add() schedules the execution of the ev event when the + event specified in event_set() occurs or in at least the time specified in + the tv. If tv is NULL, no timeout occurs and the function will only be + called if a matching event occurs on the file descriptor. The event in the + ev argument must be already initialized by event_set() and may not be used + in calls to event_set() until it has timed out or been removed with + event_del(). If the event in the ev argument already has a scheduled + timeout, the old timeout will be replaced by the new one. + + @param ev an event struct initialized via event_set() + @param timeout the maximum amount of time to wait for the event, or NULL + to wait forever + @return 0 if successful, or -1 if an error occurred + @see event_del(), event_set() + */ +int event_add(struct event *, struct timeval *); + +/** + Remove an event from the set of monitored events. + + The function event_del() will cancel the event in the argument ev. If the + event has already executed or has never been added the call will have no + effect. + + @param ev an event struct to be removed from the working set + @return 0 if successful, or -1 if an error occurred + @see event_add() + */ +int event_del(struct event *); + + +/** + Make an event active. + + @param ev an event to make active. + @param res a set of flags to pass to the event's callback. + @param ncalls + **/ +void event_active(struct event *, int, short); + + +/** + Checks if a specific event is pending or scheduled. + + @param ev an event struct previously passed to event_add() + @param event the requested event type; any of EV_TIMEOUT|EV_READ| + EV_WRITE|EV_SIGNAL + @param tv an alternate timeout (FIXME - is this true?) + + @return 1 if the event is pending, or 0 if the event has not occurred + + */ +int event_pending(struct event *, short, struct timeval *); + + +/** + Test if an event structure has been initialized. + + The event_initialized() macro can be used to check if an event has been + initialized. + + @param ev an event structure to be tested + @return 1 if the structure has been initialized, or 0 if it has not been + initialized + */ +#ifdef WIN32 +#define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT && (ev)->ev_fd != (int)INVALID_HANDLE_VALUE) +#else +#define event_initialized(ev) ((ev)->ev_flags & EVLIST_INIT) +#endif + + +/** + Get the libevent version number. + + @return a string containing the version number of libevent + */ +const char *event_get_version(void); + + +/** + Set the number of different event priorities (threadsafe variant). + + See the description of event_priority_init() for more information. + + @param eb the event_base structure returned by event_init() + @param npriorities the maximum number of priorities + @return 0 if successful, or -1 if an error occurred + @see event_priority_init(), event_priority_set() + */ +int event_base_priority_init(struct event_base *, int); + + +/** + Assign a priority to an event. + + @param ev an event struct + @param priority the new priority to be assigned + @return 0 if successful, or -1 if an error occurred + @see event_priority_init() + */ +int event_priority_set(struct event *, int); + +/** + Override the functions that libevent uses for memory management. + + Usually, libevent uses the standard libc functions malloc, realloc, and + free to allocate memory. Passing replacements for those functions to + event_set_mem_functions() overrides this behavior. To restore the default + behavior, pass NULLs as the arguments to this function. + + Note that all memory returned from libevent will be allocated by the + replacement functions rather than by malloc() and realloc(). Thus, if you + have replaced those functions, it may not be appropriate to free() memory + that you get from libevent. + + @param malloc_fn A replacement for malloc. + @param realloc_fn A replacement for realloc + @param free_fn A replacement for free. + **/ +void event_set_mem_functions(void *(*malloc_fn)(size_t sz), + void *(*realloc_fn)(void *ptr, size_t sz), + void (*free_fn)(void *ptr)); + +#ifdef __cplusplus +} +#endif + +#endif /* _EVENT2_EVENT_H_ */ diff --git a/include/event2/event_compat.h b/include/event2/event_compat.h new file mode 100644 index 00000000..f55796b8 --- /dev/null +++ b/include/event2/event_compat.h @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2000-2007 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef _EVENT2_EVENT_COMPAT_H_ +#define _EVENT2_EVENT_COMPAT_H_ + +/** @file event_compat.h + + Potentially non-threadsafe versions of the functions in event.h: provided + only for backwards compatibility. + + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#ifdef _EVENT_HAVE_SYS_TYPES_H +#include +#endif +#ifdef _EVENT_HAVE_SYS_TIME_H +#include +#endif +#ifdef _EVENT_HAVE_STDINT_H +#include +#endif +#include + +/* For int types. */ +#include + +#ifdef WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#undef WIN32_LEAN_AND_MEAN +typedef unsigned char u_char; +typedef unsigned short u_short; +#endif + +/** + Initialize the event API. + + The event API needs to be initialized with event_init() before it can be + used. Sets the current_base global representing the default base for + events that have no base associated with them. + + @see event_base_set(), event_base_new() + */ +struct event_base *event_init(void); + +/** + Loop to process events. + + In order to process events, an application needs to call + event_dispatch(). This function only returns on error, and should + replace the event core of the application program. + + @see event_base_dispatch() + */ +int event_dispatch(void); + +/** + Handle events. + + This is a more flexible version of event_dispatch(). + + @param flags any combination of EVLOOP_ONCE | EVLOOP_NONBLOCK + @return 0 if successful, -1 if an error occurred, or 1 if no events were + registered. + @see event_loopexit(), event_base_loop() +*/ +int event_loop(int); + + +/** + Exit the event loop after the specified time. + + The next event_loop() iteration after the given timer expires will + complete normally (handling all queued events) then exit without + blocking for events again. + + Subsequent invocations of event_loop() will proceed normally. + + @param tv the amount of time after which the loop should terminate. + @return 0 if successful, or -1 if an error occurred + @see event_loop(), event_base_loop(), event_base_loopexit() + */ +int event_loopexit(struct timeval *); + + +/** + Abort the active event_loop() immediately. + + event_loop() will abort the loop after the next event is completed; + event_loopbreak() is typically invoked from this event's callback. + This behavior is analogous to the "break;" statement. + + Subsequent invocations of event_loop() will proceed normally. + + @return 0 if successful, or -1 if an error occurred + @see event_base_loopbreak(), event_loopexit() + */ +int event_loopbreak(void); + +/** + Schedule a one-time event to occur. + + The function event_once() is similar to event_set(). However, it schedules + a callback to be called exactly once and does not require the caller to + prepare an event structure. + + @param fd a file descriptor to monitor + @param events event(s) to monitor; can be any of EV_TIMEOUT | EV_READ | + EV_WRITE + @param callback callback function to be invoked when the event occurs + @param arg an argument to be passed to the callback function + @param timeout the maximum amount of time to wait for the event, or NULL + to wait forever + @return 0 if successful, or -1 if an error occurred + @see event_set() + + */ +int event_once(evutil_socket_t , short, void (*)(evutil_socket_t, short, void *), void *, struct timeval *); + + +/** + Get the kernel event notification mechanism used by libevent. + + @return a string identifying the kernel event mechanism (kqueue, epoll, etc.) + */ +const char *event_get_method(void); + + +/** + Set the number of different event priorities. + + By default libevent schedules all active events with the same priority. + However, some time it is desirable to process some events with a higher + priority than others. For that reason, libevent supports strict priority + queues. Active events with a lower priority are always processed before + events with a higher priority. + + The number of different priorities can be set initially with the + event_priority_init() function. This function should be called before the + first call to event_dispatch(). The event_priority_set() function can be + used to assign a priority to an event. By default, libevent assigns the + middle priority to all events unless their priority is explicitly set. + + @param npriorities the maximum number of priorities + @return 0 if successful, or -1 if an error occurred + @see event_base_priority_init(), event_priority_set() + + */ +int event_priority_init(int); + + + + +#ifdef __cplusplus +} +#endif + +#endif /* _EVENT2_EVENT_COMPAT_H_ */ diff --git a/include/event2/event_struct.h b/include/event2/event_struct.h new file mode 100644 index 00000000..1d145f86 --- /dev/null +++ b/include/event2/event_struct.h @@ -0,0 +1,143 @@ +/* + * Copyright (c) 2000-2007 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef _EVENT2_EVENT_STRUCT_H_ +#define _EVENT2_EVENT_STRUCT_H_ + +/** @file event_struct.h + + Structures used by event.h. Using these structures directly may harm + forward compatibility: be careful! + + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#ifdef _EVENT_HAVE_SYS_TYPES_H +#include +#endif +#ifdef _EVENT_HAVE_SYS_TIME_H +#include +#endif +#ifdef _EVENT_HAVE_STDINT_H +#include +#endif +#include + +/* For int types. */ +#include + +#ifdef WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#undef WIN32_LEAN_AND_MEAN +typedef unsigned char u_char; +typedef unsigned short u_short; +#endif + +#define EVLIST_TIMEOUT 0x01 +#define EVLIST_INSERTED 0x02 +#define EVLIST_SIGNAL 0x04 +#define EVLIST_ACTIVE 0x08 +#define EVLIST_INTERNAL 0x10 +#define EVLIST_INIT 0x80 + +/* EVLIST_X_ Private space: 0x1000-0xf000 */ +#define EVLIST_ALL (0xf000 | 0x9f) + +#define EV_TIMEOUT 0x01 +#define EV_READ 0x02 +#define EV_WRITE 0x04 +#define EV_SIGNAL 0x08 +#define EV_PERSIST 0x10 /* Persistant event */ + +/* Fix so that ppl dont have to run with */ +#ifndef TAILQ_ENTRY +#define _EVENT_DEFINED_TQENTRY +#define TAILQ_ENTRY(type) \ +struct { \ + struct type *tqe_next; /* next element */ \ + struct type **tqe_prev; /* address of previous next element */ \ +} +#endif /* !TAILQ_ENTRY */ + +struct event_base; +struct event { + TAILQ_ENTRY (event) ev_next; + TAILQ_ENTRY (event) ev_active_next; + TAILQ_ENTRY (event) ev_signal_next; + unsigned int min_heap_idx; /* for managing timeouts */ + + struct event_base *ev_base; + + evutil_socket_t ev_fd; + short ev_events; + short ev_ncalls; + short *ev_pncalls; /* Allows deletes in callback */ + + struct timeval ev_timeout; + + int ev_pri; /* smaller numbers are higher priority */ + + void (*ev_callback)(evutil_socket_t, short, void *arg); + void *ev_arg; + + int ev_res; /* result passed to event callback */ + int ev_flags; +}; + +#define EVENT_SIGNAL(ev) (int)(ev)->ev_fd +#define EVENT_FD(ev) (int)(ev)->ev_fd + +/* + * Key-Value pairs. Can be used for HTTP headers but also for + * query argument parsing. + */ +struct evkeyval { + TAILQ_ENTRY(evkeyval) next; + + char *key; + char *value; +}; + +#ifdef _EVENT_DEFINED_TQENTRY +#undef TAILQ_ENTRY +struct event_list; +struct evkeyvalq; +#undef _EVENT_DEFINED_TQENTRY +#else +TAILQ_HEAD (event_list, event); +TAILQ_HEAD (evkeyvalq, evkeyval); +#endif /* _EVENT_DEFINED_TQENTRY */ + +#ifdef __cplusplus +} +#endif + +#endif /* _EVENT2_EVENT_STRUCT_H_ */ diff --git a/include/event2/tag.h b/include/event2/tag.h new file mode 100644 index 00000000..4b6c79b4 --- /dev/null +++ b/include/event2/tag.h @@ -0,0 +1,131 @@ +/* + * Copyright (c) 2000-2007 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef _EVENT2_TAG_H_ +#define _EVENT2_TAG_H_ + +/** @file tag.h + + Helper functions for reading and writing tagged data onto buffers. + + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#ifdef _EVENT_HAVE_SYS_TYPES_H +#include +#endif +#ifdef _EVENT_HAVE_SYS_TIME_H +#include +#endif +#ifdef _EVENT_HAVE_STDINT_H +#include +#endif +#include + +/* For int types. */ +#include + +#ifdef WIN32 +#define WIN32_LEAN_AND_MEAN +#include +#undef WIN32_LEAN_AND_MEAN +typedef unsigned char u_char; +typedef unsigned short u_short; +#endif + +struct evbuffer; + +/* + * Marshaling tagged data - We assume that all tags are inserted in their + * numeric order - so that unknown tags will always be higher than the + * known ones - and we can just ignore the end of an event buffer. + */ + +void evtag_init(void); + +/** + Unmarshals the header and returns the length of the payload + + @param evbuf the buffer from which to unmarshal data + @param ptag a pointer in which the tag id is being stored + @returns -1 on failure or the number of bytes in the remaining payload. +*/ +int evtag_unmarshal_header(struct evbuffer *evbuf, ev_uint32_t *ptag); + +void evtag_marshal(struct evbuffer *evbuf, ev_uint32_t tag, const void *data, + ev_uint32_t len); +void evtag_marshal_buffer(struct evbuffer *evbuf, ev_uint32_t tag, + struct evbuffer *data); + +/** + Encode an integer and store it in an evbuffer. + + We encode integers by nybbles; the first nibble contains the number + of significant nibbles - 1; this allows us to encode up to 64-bit + integers. This function is byte-order independent. + + @param evbuf evbuffer to store the encoded number + @param number a 32-bit integer + */ +void encode_int(struct evbuffer *evbuf, ev_uint32_t number); + +void evtag_marshal_int(struct evbuffer *evbuf, ev_uint32_t tag, + ev_uint32_t integer); + +void evtag_marshal_string(struct evbuffer *buf, ev_uint32_t tag, + const char *string); + +void evtag_marshal_timeval(struct evbuffer *evbuf, ev_uint32_t tag, + struct timeval *tv); + +int evtag_unmarshal(struct evbuffer *src, ev_uint32_t *ptag, + struct evbuffer *dst); +int evtag_peek(struct evbuffer *evbuf, ev_uint32_t *ptag); +int evtag_peek_length(struct evbuffer *evbuf, ev_uint32_t *plength); +int evtag_payload_length(struct evbuffer *evbuf, ev_uint32_t *plength); +int evtag_consume(struct evbuffer *evbuf); + +int evtag_unmarshal_int(struct evbuffer *evbuf, ev_uint32_t need_tag, + ev_uint32_t *pinteger); + +int evtag_unmarshal_fixed(struct evbuffer *src, ev_uint32_t need_tag, + void *data, size_t len); + +int evtag_unmarshal_string(struct evbuffer *evbuf, ev_uint32_t need_tag, + char **pstring); + +int evtag_unmarshal_timeval(struct evbuffer *evbuf, ev_uint32_t need_tag, + struct timeval *ptv); + +#ifdef __cplusplus +} +#endif + +#endif /* _EVENT2_TAG_H_ */ diff --git a/include/event2/util.h b/include/event2/util.h new file mode 100644 index 00000000..88778767 --- /dev/null +++ b/include/event2/util.h @@ -0,0 +1,190 @@ +/* + * Copyright (c) 2007 Niels Provos + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * 3. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR + * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES + * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. + * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT + * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +#ifndef _EVENT2_UTIL_H_ +#define _EVENT2_UTIL_H_ + +/** @file event2/util.h + + Common convenience functions for cross-platform portability and + related socket manipulations. + + */ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#ifdef _EVENT_HAVE_SYS_TIME_H +#include +#endif +#ifdef _EVENT_HAVE_STDINT_H +#include +#elif defined(_EVENT_HAVE_INTTYPES_H) +#include +#endif +#ifdef _EVENT_HAVE_SYS_TYPES_H +#include +#endif +#ifdef _EVENT_HAVE_STDDEF_H +#include +#endif + +#ifdef _EVENT_HAVE_UINT64_T +#define ev_uint64_t uint64_t +#define ev_int64_t int64_t +#elif defined(WIN32) +#define ev_uint64_t __uint64_t +#define ev_int64_t __int64_t +#elif _EVENT_SIZEOF_LONG_LONG == 8 +#define ev_uint64_t unsigned long long +#define ev_int64_t long long +#elif _EVENT_SIZEOF_LONG == 8 +#define ev_uint64_t unsigned long +#define ev_int64_t long +#else +#error "No way to define ev_uint64_t" +#endif + +#ifdef _EVENT_HAVE_UINT32_T +#define ev_uint32_t uint32_t +#elif defined(WIN32) +#define ev_uint32_t unsigned int +#elif _EVENT_SIZEOF_LONG == 4 +#define ev_uint32_t unsigned long +#elif _EVENT_SIZEOF_INT == 4 +#define ev_uint32_t unsigned int +#else +#error "No way to define ev_uint32_t" +#endif + +#ifdef _EVENT_HAVE_UINT16_T +#define ev_uint16_t uint16_t +#elif defined(WIN32) +#define ev_uint16_t unsigned short +#elif _EVENT_SIZEOF_INT == 2 +#define ev_uint16_t unsigned int +#elif _EVENT_SIZEOF_SHORT == 2 +#define ev_uint16_t unsigned short +#else +#error "No way to define ev_uint16_t" +#endif + +#ifdef _EVENT_HAVE_UINT8_T +#define ev_uint8_t uint8_t +#else +#define ev_uint8_t unsigned char +#endif + +#ifdef WIN32 +/** Type to hold the output of "socket()" or "accept()". On Windows, this is + * an intptr_t; elsewhere, it is an int. */ +#define evutil_socket_t intptr_t +#else +#define evutil_socket_t int +#endif + +int evutil_socketpair(int d, int type, int protocol, evutil_socket_t sv[2]); +int evutil_make_socket_nonblocking(evutil_socket_t sock); +#ifdef WIN32 +#define EVUTIL_CLOSESOCKET(s) closesocket(s) +#else +#define EVUTIL_CLOSESOCKET(s) close(s) +#endif + +#ifdef WIN32 +#define EVUTIL_SOCKET_ERROR() WSAGetLastError() +#define EVUTIL_SET_SOCKET_ERROR(errcode) \ + do { WSASetLastError(errcode); } while (0) +#else +#define EVUTIL_SOCKET_ERROR() (errno) +#define EVUTIL_SET_SOCKET_ERROR(errcode) \ + do { errno = (errcode); } while (0) +#endif + +/* + * Manipulation functions for struct timeval + */ +#ifdef _EVENT_HAVE_TIMERADD +#define evutil_timeradd(tvp, uvp, vvp) timeradd((tvp), (uvp), (vvp)) +#define evutil_timersub(tvp, uvp, vvp) timersub((tvp), (uvp), (vvp)) +#else +#define evutil_timeradd(tvp, uvp, vvp) \ + do { \ + (vvp)->tv_sec = (tvp)->tv_sec + (uvp)->tv_sec; \ + (vvp)->tv_usec = (tvp)->tv_usec + (uvp)->tv_usec; \ + if ((vvp)->tv_usec >= 1000000) { \ + (vvp)->tv_sec++; \ + (vvp)->tv_usec -= 1000000; \ + } \ + } while (0) +#define evutil_timersub(tvp, uvp, vvp) \ + do { \ + (vvp)->tv_sec = (tvp)->tv_sec - (uvp)->tv_sec; \ + (vvp)->tv_usec = (tvp)->tv_usec - (uvp)->tv_usec; \ + if ((vvp)->tv_usec < 0) { \ + (vvp)->tv_sec--; \ + (vvp)->tv_usec += 1000000; \ + } \ + } while (0) +#endif /* !_EVENT_HAVE_HAVE_TIMERADD */ + +#ifdef _EVENT_HAVE_TIMERCLEAR +#define evutil_timerclear(tvp) timerclear(tvp) +#else +#define evutil_timerclear(tvp) (tvp)->tv_sec = (tvp)->tv_usec = 0 +#endif + +#ifdef _EVENT_HAVE_TIMERCMP +#define evutil_timercmp(tvp, uvp, cmp) timercmp((tvp), (uvp), cmp) +#else +#define evutil_timercmp(tvp, uvp, cmp) \ + (((tvp)->tv_sec == (uvp)->tv_sec) ? \ + ((tvp)->tv_usec cmp (uvp)->tv_usec) : \ + ((tvp)->tv_sec cmp (uvp)->tv_sec)) +#endif + +#ifdef _EVENT_HAVE_TIMERISSET +#define evutil_timerisset(tvp) timerisset(tvp) +#else +#define evutil_timerisset(tvp) ((tvp)->tv_sec || (tvp)->tv_usec) +#endif + +#ifdef offsetof +#define evutil_offsetof(type, field) offsetof(type, field) +#else +#define evutil_offsetof(type, field) ((off_t)(&((type *)0)->field)) +#endif + +/* big-int related functions */ +ev_int64_t evutil_strtoll(const char *s, char **endptr, int base); + +#ifdef __cplusplus +} +#endif + +#endif /* _EVUTIL_H_ */ diff --git a/kqueue.c b/kqueue.c index e51df61c..0056fffc 100644 --- a/kqueue.c +++ b/kqueue.c @@ -57,7 +57,8 @@ #define PTR_TO_UDATA(x) (x) #endif -#include "event.h" +#include "event2/event.h" +#include "event2/event_struct.h" #include "event-internal.h" #include "log.h" #include "event-internal.h" diff --git a/poll.c b/poll.c index ef14c4fe..c2859526 100644 --- a/poll.c +++ b/poll.c @@ -48,7 +48,8 @@ #include #endif -#include "event.h" +#include "event2/event.h" +#include "event2/event_struct.h" #include "event-internal.h" #include "evsignal.h" #include "log.h" diff --git a/select.c b/select.c index cf8e88aa..84ea8f2e 100644 --- a/select.c +++ b/select.c @@ -50,7 +50,8 @@ #include #endif -#include "event.h" +#include "event2/event.h" +#include "event2/event_struct.h" #include "event-internal.h" #include "evsignal.h" #include "log.h" diff --git a/signal.c b/signal.c index fc1f7e3a..93a5f6a5 100644 --- a/signal.c +++ b/signal.c @@ -57,9 +57,10 @@ #endif #include -#include "event.h" +#include "event2/event.h" +#include "event2/event_struct.h" #include "event-internal.h" -#include "evutil.h" +#include "event2/util.h" #include "evsignal.h" #include "log.h" -- 2.40.0