xref: /libevent-2.1.12/event.3 (revision fd36647a)
15f865858SNiels Provos.\"	$OpenBSD: event.3,v 1.4 2002/07/12 18:50:48 provos Exp $
2aa6567feSNiels Provos.\"
3aa6567feSNiels Provos.\" Copyright (c) 2000 Artur Grabowski <[email protected]>
4aa6567feSNiels Provos.\" All rights reserved.
5aa6567feSNiels Provos.\"
6aa6567feSNiels Provos.\" Redistribution and use in source and binary forms, with or without
7aa6567feSNiels Provos.\" modification, are permitted provided that the following conditions
8aa6567feSNiels Provos.\" are met:
9aa6567feSNiels Provos.\"
10aa6567feSNiels Provos.\" 1. Redistributions of source code must retain the above copyright
11aa6567feSNiels Provos.\"    notice, this list of conditions and the following disclaimer.
12aa6567feSNiels Provos.\" 2. Redistributions in binary form must reproduce the above copyright
13aa6567feSNiels Provos.\"    notice, this list of conditions and the following disclaimer in the
14aa6567feSNiels Provos.\"    documentation and/or other materials provided with the distribution.
15aa6567feSNiels Provos.\" 3. The name of the author may not be used to endorse or promote products
16aa6567feSNiels Provos.\"    derived from this software without specific prior written permission.
17aa6567feSNiels Provos.\"
18aa6567feSNiels Provos.\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
19aa6567feSNiels Provos.\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
20aa6567feSNiels Provos.\" AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
21aa6567feSNiels Provos.\" THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
22aa6567feSNiels Provos.\" EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
23aa6567feSNiels Provos.\" PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
24aa6567feSNiels Provos.\" OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25aa6567feSNiels Provos.\" WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
26aa6567feSNiels Provos.\" OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
27aa6567feSNiels Provos.\" ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28aa6567feSNiels Provos.\"
29dcf6cbe4SNiels Provos.Dd August 8, 2000
30aa6567feSNiels Provos.Dt EVENT 3
31aa6567feSNiels Provos.Os
32aa6567feSNiels Provos.Sh NAME
33aa6567feSNiels Provos.Nm event_init ,
34aa6567feSNiels Provos.Nm event_dispatch ,
35aa6567feSNiels Provos.Nm event_loop ,
36cd699abfSNiels Provos.Nm event_loopexit ,
371c23e219SNick Mathewson.Nm event_loopbreak ,
383c74f06fSNiels Provos.Nm event_set ,
393c74f06fSNiels Provos.Nm event_base_dispatch ,
40720f7fccSNiels Provos.Nm event_base_loop ,
41720f7fccSNiels Provos.Nm event_base_loopexit ,
421c23e219SNick Mathewson.Nm event_base_loopbreak ,
433c74f06fSNiels Provos.Nm event_base_set ,
44f5aa65c9SNiels Provos.Nm event_base_free ,
45aa6567feSNiels Provos.Nm event_add ,
46aa6567feSNiels Provos.Nm event_del ,
47ec2c1db4SNiels Provos.Nm event_once ,
4841b7cbc3SNiels Provos.Nm event_base_once ,
49aa6567feSNiels Provos.Nm event_pending ,
505f865858SNiels Provos.Nm event_initialized ,
51fa6c304dSNiels Provos.Nm event_priority_init ,
52fa6c304dSNiels Provos.Nm event_priority_set ,
535f865858SNiels Provos.Nm evtimer_set ,
545f865858SNiels Provos.Nm evtimer_add ,
5511a40d47SNiels Provos.Nm evtimer_del ,
565f865858SNiels Provos.Nm evtimer_pending ,
575f865858SNiels Provos.Nm evtimer_initialized ,
5806630e31SNiels Provos.Nm signal_set ,
5906630e31SNiels Provos.Nm signal_add ,
6011a40d47SNiels Provos.Nm signal_del ,
6106630e31SNiels Provos.Nm signal_pending ,
623772ec8eSNiels Provos.Nm signal_initialized ,
633772ec8eSNiels Provos.Nm bufferevent_new ,
643772ec8eSNiels Provos.Nm bufferevent_free ,
653772ec8eSNiels Provos.Nm bufferevent_write ,
663772ec8eSNiels Provos.Nm bufferevent_write_buffer ,
673772ec8eSNiels Provos.Nm bufferevent_read ,
683772ec8eSNiels Provos.Nm bufferevent_enable ,
693772ec8eSNiels Provos.Nm bufferevent_disable ,
703772ec8eSNiels Provos.Nm bufferevent_settimeout ,
71f296e633SNiels Provos.Nm bufferevent_base_set ,
723772ec8eSNiels Provos.Nm evbuffer_new ,
733772ec8eSNiels Provos.Nm evbuffer_free ,
743772ec8eSNiels Provos.Nm evbuffer_add ,
753772ec8eSNiels Provos.Nm evbuffer_add_buffer ,
763772ec8eSNiels Provos.Nm evbuffer_add_printf ,
773c74f06fSNiels Provos.Nm evbuffer_add_vprintf ,
783772ec8eSNiels Provos.Nm evbuffer_drain ,
793772ec8eSNiels Provos.Nm evbuffer_write ,
803772ec8eSNiels Provos.Nm evbuffer_read ,
8115850135SNiels Provos.Nm evbuffer_find ,
8238b33048SNiels Provos.Nm evbuffer_readline ,
8367947ce3SNiels Provos.Nm evhttp_new ,
8467947ce3SNiels Provos.Nm evhttp_bind_socket ,
8538b33048SNiels Provos.Nm evhttp_free
86aa6567feSNiels Provos.Nd execute a function when a specific event occurs
87aa6567feSNiels Provos.Sh SYNOPSIS
885f865858SNiels Provos.Fd #include <sys/time.h>
89aa6567feSNiels Provos.Fd #include <event.h>
90cacd8398SNiels Provos.Ft "struct event_base *"
91f5aa65c9SNiels Provos.Fn "event_init" "void"
92aa6567feSNiels Provos.Ft int
93f5aa65c9SNiels Provos.Fn "event_dispatch" "void"
94aa6567feSNiels Provos.Ft int
95aa6567feSNiels Provos.Fn "event_loop" "int flags"
96cd699abfSNiels Provos.Ft int
97cd699abfSNiels Provos.Fn "event_loopexit" "struct timeval *tv"
981c23e219SNick Mathewson.Ft int
991c23e219SNick Mathewson.Fn "event_loopbreak" "void"
100aa6567feSNiels Provos.Ft void
101aa6567feSNiels Provos.Fn "event_set" "struct event *ev" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg"
102aa6567feSNiels Provos.Ft int
1033c74f06fSNiels Provos.Fn "event_base_dispatch" "struct event_base *base"
1043c74f06fSNiels Provos.Ft int
1053c74f06fSNiels Provos.Fn "event_base_loop" "struct event_base *base" "int flags"
1063c74f06fSNiels Provos.Ft int
1073c74f06fSNiels Provos.Fn "event_base_loopexit" "struct event_base *base" "struct timeval *tv"
1083c74f06fSNiels Provos.Ft int
1091c23e219SNick Mathewson.Fn "event_base_loopbreak" "struct event_base *base"
1101c23e219SNick Mathewson.Ft int
1113c74f06fSNiels Provos.Fn "event_base_set" "struct event_base *base" "struct event *"
112f5aa65c9SNiels Provos.Ft void
113f5aa65c9SNiels Provos.Fn "event_base_free" "struct event_base *base"
1143c74f06fSNiels Provos.Ft int
115aa6567feSNiels Provos.Fn "event_add" "struct event *ev" "struct timeval *tv"
116aa6567feSNiels Provos.Ft int
117aa6567feSNiels Provos.Fn "event_del" "struct event *ev"
118aa6567feSNiels Provos.Ft int
119ec2c1db4SNiels Provos.Fn "event_once" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" "struct timeval *tv"
120ec2c1db4SNiels Provos.Ft int
12141b7cbc3SNiels Provos.Fn "event_base_once" "struct event_base *base" "int fd" "short event" "void (*fn)(int, short, void *)" "void *arg" "struct timeval *tv"
12241b7cbc3SNiels Provos.Ft int
123aa6567feSNiels Provos.Fn "event_pending" "struct event *ev" "short event" "struct timeval *tv"
124aa6567feSNiels Provos.Ft int
125aa6567feSNiels Provos.Fn "event_initialized" "struct event *ev"
126fa6c304dSNiels Provos.Ft int
127fa6c304dSNiels Provos.Fn "event_priority_init" "int npriorities"
128fa6c304dSNiels Provos.Ft int
129fa6c304dSNiels Provos.Fn "event_priority_set" "struct event *ev" "int priority"
130aa6567feSNiels Provos.Ft void
1315f865858SNiels Provos.Fn "evtimer_set" "struct event *ev" "void (*fn)(int, short, void *)" "void *arg"
132aa6567feSNiels Provos.Ft void
1335f865858SNiels Provos.Fn "evtimer_add" "struct event *ev" "struct timeval *"
134aa6567feSNiels Provos.Ft void
1355f865858SNiels Provos.Fn "evtimer_del" "struct event *ev"
136aa6567feSNiels Provos.Ft int
1375f865858SNiels Provos.Fn "evtimer_pending" "struct event *ev" "struct timeval *tv"
138aa6567feSNiels Provos.Ft int
1395f865858SNiels Provos.Fn "evtimer_initialized" "struct event *ev"
14006630e31SNiels Provos.Ft void
14106630e31SNiels Provos.Fn "signal_set" "struct event *ev" "int signal" "void (*fn)(int, short, void *)" "void *arg"
14206630e31SNiels Provos.Ft void
14306630e31SNiels Provos.Fn "signal_add" "struct event *ev" "struct timeval *"
14406630e31SNiels Provos.Ft void
14506630e31SNiels Provos.Fn "signal_del" "struct event *ev"
14606630e31SNiels Provos.Ft int
14706630e31SNiels Provos.Fn "signal_pending" "struct event *ev" "struct timeval *tv"
14806630e31SNiels Provos.Ft int
14906630e31SNiels Provos.Fn "signal_initialized" "struct event *ev"
1503772ec8eSNiels Provos.Ft "struct bufferevent *"
1513772ec8eSNiels Provos.Fn "bufferevent_new" "int fd" "evbuffercb readcb" "evbuffercb writecb" "everrorcb" "void *cbarg"
1523772ec8eSNiels Provos.Ft void
1533772ec8eSNiels Provos.Fn "bufferevent_free" "struct bufferevent *bufev"
1543772ec8eSNiels Provos.Ft int
1553772ec8eSNiels Provos.Fn "bufferevent_write" "struct bufferevent *bufev" "void *data" "size_t size"
1563772ec8eSNiels Provos.Ft int
1573772ec8eSNiels Provos.Fn "bufferevent_write_buffer" "struct bufferevent *bufev" "struct evbuffer *buf"
1583772ec8eSNiels Provos.Ft size_t
1593772ec8eSNiels Provos.Fn "bufferevent_read" "struct bufferevent *bufev" "void *data" "size_t size"
1603772ec8eSNiels Provos.Ft int
1613772ec8eSNiels Provos.Fn "bufferevent_enable" "struct bufferevent *bufev" "short event"
1623772ec8eSNiels Provos.Ft int
1633772ec8eSNiels Provos.Fn "bufferevent_disable" "struct bufferevent *bufev" "short event"
1643772ec8eSNiels Provos.Ft void
1653772ec8eSNiels Provos.Fn "bufferevent_settimeout" "struct bufferevent *bufev" "int timeout_read" "int timeout_write"
166f296e633SNiels Provos.Ft int
167f296e633SNiels Provos.Fn "bufferevent_base_set" "struct event_base *base" "struct bufferevent *bufev"
1683772ec8eSNiels Provos.Ft "struct evbuffer *"
1693772ec8eSNiels Provos.Fn "evbuffer_new" "void"
1703772ec8eSNiels Provos.Ft void
1713772ec8eSNiels Provos.Fn "evbuffer_free" "struct evbuffer *buf"
1723772ec8eSNiels Provos.Ft int
173f5aa65c9SNiels Provos.Fn "evbuffer_add" "struct evbuffer *buf" "const void *data" "size_t size"
1743772ec8eSNiels Provos.Ft int
1753772ec8eSNiels Provos.Fn "evbuffer_add_buffer" "struct evbuffer *dst" "struct evbuffer *src"
1763772ec8eSNiels Provos.Ft int
177f5aa65c9SNiels Provos.Fn "evbuffer_add_printf" "struct evbuffer *buf" "const char *fmt" "..."
1783c74f06fSNiels Provos.Ft int
1793c74f06fSNiels Provos.Fn "evbuffer_add_vprintf" "struct evbuffer *buf" "const char *fmt" "va_list ap"
1803772ec8eSNiels Provos.Ft void
1813772ec8eSNiels Provos.Fn "evbuffer_drain" "struct evbuffer *buf" "size_t size"
1823772ec8eSNiels Provos.Ft int
1833772ec8eSNiels Provos.Fn "evbuffer_write" "struct evbuffer *buf" "int fd"
1843772ec8eSNiels Provos.Ft int
1853772ec8eSNiels Provos.Fn "evbuffer_read" "struct evbuffer *buf" "int fd" "int size"
186*fd36647aSEd Schouten.Ft "unsigned char *"
187*fd36647aSEd Schouten.Fn "evbuffer_find" "struct evbuffer *buf" "const unsigned char *data" "size_t size"
18815850135SNiels Provos.Ft "char *"
18915850135SNiels Provos.Fn "evbuffer_readline" "struct evbuffer *buf"
19038b33048SNiels Provos.Ft "struct evhttp *"
19188173102SNiels Provos.Fn "evhttp_new" "struct event_base *base"
19267947ce3SNiels Provos.Ft int
193*fd36647aSEd Schouten.Fn "evhttp_bind_socket" "struct evhttp *http" "const char *address" "unsigned short port"
19438b33048SNiels Provos.Ft "void"
19538b33048SNiels Provos.Fn "evhttp_free" "struct evhttp *http"
196aa6567feSNiels Provos.Ft int
197aa6567feSNiels Provos.Fa (*event_sigcb)(void) ;
198f5aa65c9SNiels Provos.Ft volatile sig_atomic_t
199aa6567feSNiels Provos.Fa event_gotsig ;
200aa6567feSNiels Provos.Sh DESCRIPTION
201aa6567feSNiels ProvosThe
202aa6567feSNiels Provos.Nm event
203aa6567feSNiels ProvosAPI provides a mechanism to execute a function when a specific event
204ac44dddaSNiels Provoson a file descriptor occurs or after a given time has passed.
205aa6567feSNiels Provos.Pp
206aa6567feSNiels ProvosThe
207aa6567feSNiels Provos.Nm event
2085f865858SNiels ProvosAPI needs to be initialized with
209aa6567feSNiels Provos.Fn event_init
210aa6567feSNiels Provosbefore it can be used.
211aa6567feSNiels Provos.Pp
212aa6567feSNiels ProvosIn order to process events, an application needs to call
213aa6567feSNiels Provos.Fn event_dispatch .
214ac44dddaSNiels ProvosThis function only returns on error, and should replace the event core
215aa6567feSNiels Provosof the application program.
216aa6567feSNiels Provos.Pp
217aa6567feSNiels ProvosThe function
218aa6567feSNiels Provos.Fn event_set
219aa6567feSNiels Provosprepares the event structure
220aa6567feSNiels Provos.Fa ev
221aa6567feSNiels Provosto be used in future calls to
222aa6567feSNiels Provos.Fn event_add
223aa6567feSNiels Provosand
224aa6567feSNiels Provos.Fn event_del .
225aa6567feSNiels ProvosThe event will be prepared to call the function specified by the
226aa6567feSNiels Provos.Fa fn
227aa6567feSNiels Provosargument with an
228aa6567feSNiels Provos.Fa int
2295f865858SNiels Provosargument indicating the file descriptor, a
230aa6567feSNiels Provos.Fa short
231aa6567feSNiels Provosargument indicating the type of event, and a
232aa6567feSNiels Provos.Fa void *
233aa6567feSNiels Provosargument given in the
234aa6567feSNiels Provos.Fa arg
235aa6567feSNiels Provosargument.
236aa6567feSNiels ProvosThe
237aa6567feSNiels Provos.Fa fd
238aa6567feSNiels Provosindicates the file descriptor that should be monitored for events.
239aa6567feSNiels ProvosThe events can be either
240aa6567feSNiels Provos.Va EV_READ ,
241aa6567feSNiels Provos.Va EV_WRITE ,
24211a40d47SNiels Provosor both,
24311a40d47SNiels Provosindicating that an application can read or write from the file descriptor
244aa6567feSNiels Provosrespectively without blocking.
245aa6567feSNiels Provos.Pp
246aa6567feSNiels ProvosThe function
247aa6567feSNiels Provos.Fa fn
248aa6567feSNiels Provoswill be called with the file descriptor that triggered the event and
249aa6567feSNiels Provosthe type of event which will be either
250aa6567feSNiels Provos.Va EV_TIMEOUT ,
25106630e31SNiels Provos.Va EV_SIGNAL ,
252aa6567feSNiels Provos.Va EV_READ ,
253aa6567feSNiels Provosor
254aa6567feSNiels Provos.Va EV_WRITE .
255ccdc5990SNick MathewsonAdditionally, an event which has registered interest in more than one of the
256ccdc5990SNick Mathewsonpreceeding events, via bitwise-OR to
257ccdc5990SNick Mathewson.Fn event_set ,
258ccdc5990SNick Mathewsoncan provide its callback function with a bitwise-OR of more than one triggered
259ccdc5990SNick Mathewsonevent.
26006630e31SNiels ProvosThe additional flag
26106630e31SNiels Provos.Va EV_PERSIST
26206630e31SNiels Provosmakes an
26306630e31SNiels Provos.Fn event_add
26406630e31SNiels Provospersistent until
26506630e31SNiels Provos.Fn event_del
26606630e31SNiels Provoshas been called.
267aa6567feSNiels Provos.Pp
268aa6567feSNiels ProvosOnce initialized, the
269aa6567feSNiels Provos.Fa ev
27006630e31SNiels Provosstructure can be used repeatedly with
271aa6567feSNiels Provos.Fn event_add
272aa6567feSNiels Provosand
273aa6567feSNiels Provos.Fn event_del
2745f865858SNiels Provosand does not need to be reinitialized unless the function called and/or
2755f865858SNiels Provosthe argument to it are to be changed.
2763ba224dbSNiels ProvosHowever, when an
2773ba224dbSNiels Provos.Fa ev
2783ba224dbSNiels Provosstructure has been added to libevent using
2793ba224dbSNiels Provos.Fn event_add
2803ba224dbSNiels Provosthe structure must persist until the event occurs (assuming
2813ba224dbSNiels Provos.Fa EV_PERSIST
2823ba224dbSNiels Provosis not set) or is removed
2833ba224dbSNiels Provosusing
2843ba224dbSNiels Provos.Fn event_del .
2853ba224dbSNiels ProvosYou may not reuse the same
2863ba224dbSNiels Provos.Fa ev
2873ba224dbSNiels Provosstructure for multiple monitored descriptors; each descriptor
2883ba224dbSNiels Provosneeds its own
2893ba224dbSNiels Provos.Fa ev .
290aa6567feSNiels Provos.Pp
291aa6567feSNiels ProvosThe function
292aa6567feSNiels Provos.Fn event_add
293aa6567feSNiels Provosschedules the execution of the
294aa6567feSNiels Provos.Fa ev
295aa6567feSNiels Provosevent when the event specified in
296aa6567feSNiels Provos.Fn event_set
297aa6567feSNiels Provosoccurs or in at least the time specified in the
298aa6567feSNiels Provos.Fa tv .
299aa6567feSNiels ProvosIf
300aa6567feSNiels Provos.Fa tv
30111a40d47SNiels Provosis
30211a40d47SNiels Provos.Dv NULL ,
30311a40d47SNiels Provosno timeout occurs and the function will only be called
304aa6567feSNiels Provosif a matching event occurs on the file descriptor.
305aa6567feSNiels ProvosThe event in the
306aa6567feSNiels Provos.Fa ev
307aa6567feSNiels Provosargument must be already initialized by
308aa6567feSNiels Provos.Fn event_set
309aa6567feSNiels Provosand may not be used in calls to
310aa6567feSNiels Provos.Fn event_set
311aa6567feSNiels Provosuntil it has timed out or been removed with
312aa6567feSNiels Provos.Fn event_del .
313aa6567feSNiels ProvosIf the event in the
314aa6567feSNiels Provos.Fa ev
315ac44dddaSNiels Provosargument already has a scheduled timeout, the old timeout will be
316aa6567feSNiels Provosreplaced by the new one.
317aa6567feSNiels Provos.Pp
318aa6567feSNiels ProvosThe function
319aa6567feSNiels Provos.Fn event_del
320aa6567feSNiels Provoswill cancel the event in the argument
321aa6567feSNiels Provos.Fa ev .
322aa6567feSNiels ProvosIf the event has already executed or has never been added
323aa6567feSNiels Provosthe call will have no effect.
324aa6567feSNiels Provos.Pp
325ccdc5990SNick MathewsonThe functions
326ccdc5990SNick Mathewson.Fn evtimer_set ,
327ccdc5990SNick Mathewson.Fn evtimer_add ,
328ccdc5990SNick Mathewson.Fn evtimer_del ,
329ccdc5990SNick Mathewson.Fn evtimer_initialized ,
330ccdc5990SNick Mathewsonand
331ccdc5990SNick Mathewson.Fn evtimer_pending
332ccdc5990SNick Mathewsonare abbreviations for common situations where only a timeout is required.
333ccdc5990SNick MathewsonThe file descriptor passed will be \-1, and the event type will be
334ccdc5990SNick Mathewson.Va EV_TIMEOUT .
335ccdc5990SNick Mathewson.Pp
336ccdc5990SNick MathewsonThe functions
337ccdc5990SNick Mathewson.Fn signal_set ,
338ccdc5990SNick Mathewson.Fn signal_add ,
339ccdc5990SNick Mathewson.Fn signal_del ,
340ccdc5990SNick Mathewson.Fn signal_initialized ,
341ccdc5990SNick Mathewsonand
342ccdc5990SNick Mathewson.Fn signal_pending
343ccdc5990SNick Mathewsonare abbreviations.
344ccdc5990SNick MathewsonThe event type will be a persistent
345ccdc5990SNick Mathewson.Va EV_SIGNAL .
346ccdc5990SNick MathewsonThat means
347ccdc5990SNick Mathewson.Fn signal_set
348ccdc5990SNick Mathewsonadds
349ccdc5990SNick Mathewson.Va EV_PERSIST .
350ccdc5990SNick Mathewson.Pp
351ccdc5990SNick MathewsonIn order to avoid races in signal handlers, the
352ccdc5990SNick Mathewson.Nm event
353ccdc5990SNick MathewsonAPI provides two variables:
354ccdc5990SNick Mathewson.Va event_sigcb
355ccdc5990SNick Mathewsonand
356ccdc5990SNick Mathewson.Va event_gotsig .
357ccdc5990SNick MathewsonA signal handler
358ccdc5990SNick Mathewsonsets
359ccdc5990SNick Mathewson.Va event_gotsig
360ccdc5990SNick Mathewsonto indicate that a signal has been received.
361ccdc5990SNick MathewsonThe application sets
362ccdc5990SNick Mathewson.Va event_sigcb
363ccdc5990SNick Mathewsonto a callback function.
364ccdc5990SNick MathewsonAfter the signal handler sets
365ccdc5990SNick Mathewson.Va event_gotsig ,
366ccdc5990SNick Mathewson.Nm event_dispatch
367ccdc5990SNick Mathewsonwill execute the callback function to process received signals.
368ccdc5990SNick MathewsonThe callback returns 1 when no events are registered any more.
369ccdc5990SNick MathewsonIt can return \-1 to indicate an error to the
370ccdc5990SNick Mathewson.Nm event
371ccdc5990SNick Mathewsonlibrary, causing
372ccdc5990SNick Mathewson.Fn event_dispatch
373ccdc5990SNick Mathewsonto terminate with
374ccdc5990SNick Mathewson.Va errno
375ccdc5990SNick Mathewsonset to
376ccdc5990SNick Mathewson.Er EINTR .
377ccdc5990SNick Mathewson.Pp
378ec2c1db4SNiels ProvosThe function
379ec2c1db4SNiels Provos.Fn event_once
380bca504e4SNiels Provosis similar to
381ec2c1db4SNiels Provos.Fn event_set .
382ec2c1db4SNiels ProvosHowever, it schedules a callback to be called exactly once and does not
383ec2c1db4SNiels Provosrequire the caller to prepare an
384ec2c1db4SNiels Provos.Fa event
385ec2c1db4SNiels Provosstructure.
386ec2c1db4SNiels ProvosThis function supports
387ec2c1db4SNiels Provos.Fa EV_TIMEOUT ,
38811a40d47SNiels Provos.Fa EV_READ ,
389ec2c1db4SNiels Provosand
390ec2c1db4SNiels Provos.Fa EV_WRITE .
391ec2c1db4SNiels Provos.Pp
392aa6567feSNiels ProvosThe
393aa6567feSNiels Provos.Fn event_pending
394aa6567feSNiels Provosfunction can be used to check if the event specified by
395aa6567feSNiels Provos.Fa event
396aa6567feSNiels Provosis pending to run.
397aa6567feSNiels ProvosIf
398aa6567feSNiels Provos.Va EV_TIMEOUT
399aa6567feSNiels Provoswas specified and
400aa6567feSNiels Provos.Fa tv
401aa6567feSNiels Provosis not
40211a40d47SNiels Provos.Dv NULL ,
403aa6567feSNiels Provosthe expiration time of the event will be returned in
404aa6567feSNiels Provos.Fa tv .
405aa6567feSNiels Provos.Pp
406aa6567feSNiels ProvosThe
407aa6567feSNiels Provos.Fn event_initialized
408aa6567feSNiels Provosmacro can be used to check if an event has been initialized.
409aa6567feSNiels Provos.Pp
410ccdc5990SNick MathewsonThe
411ccdc5990SNick Mathewson.Nm event_loop
412ccdc5990SNick Mathewsonfunction provides an interface for single pass execution of pending
413ccdc5990SNick Mathewsonevents.
414ccdc5990SNick MathewsonThe flags
415ccdc5990SNick Mathewson.Va EVLOOP_ONCE
416aa6567feSNiels Provosand
417ccdc5990SNick Mathewson.Va EVLOOP_NONBLOCK
418ccdc5990SNick Mathewsonare recognized.
419ccdc5990SNick MathewsonThe
420ccdc5990SNick Mathewson.Nm event_loopexit
42170248ca8SNick Mathewsonfunction exits from the event loop. The next
42270248ca8SNick Mathewson.Fn event_loop
42370248ca8SNick Mathewsoniteration after the
42470248ca8SNick Mathewsongiven timer expires will complete normally (handling all queued events) then
42570248ca8SNick Mathewsonexit without blocking for events again. Subsequent invocations of
42670248ca8SNick Mathewson.Fn event_loop
42770248ca8SNick Mathewsonwill proceed normally.
4281c23e219SNick MathewsonThe
4291c23e219SNick Mathewson.Nm event_loopbreak
4301c23e219SNick Mathewsonfunction exits from the event loop immediately.
4311c23e219SNick Mathewson.Fn event_loop
4321c23e219SNick Mathewsonwill abort after the next event is completed;
4331c23e219SNick Mathewson.Fn event_loopbreak
4341c23e219SNick Mathewsonis typically invoked from this event's callback. This behavior is analogous
4351c23e219SNick Mathewsonto the "break;" statement. Subsequent invocations of
4361c23e219SNick Mathewson.Fn event_loop
4371c23e219SNick Mathewsonwill proceed normally.
438aa6567feSNiels Provos.Pp
439ccdc5990SNick MathewsonIt is the responsibility of the caller to provide these functions with
440ccdc5990SNick Mathewsonpre-allocated event structures.
44106630e31SNiels Provos.Pp
442fa6c304dSNiels Provos.Sh EVENT PRIORITIES
443fa6c304dSNiels ProvosBy default
444fa6c304dSNiels Provos.Nm libevent
445fa6c304dSNiels Provosschedules all active events with the same priority.
44611a40d47SNiels ProvosHowever, sometimes it is desirable to process some events with a higher
447fa6c304dSNiels Provospriority than others.
448fa6c304dSNiels ProvosFor that reason,
449fa6c304dSNiels Provos.Nm libevent
450fa6c304dSNiels Provossupports strict priority queues.
451fa6c304dSNiels ProvosActive events with a lower priority are always processed before events
452fa6c304dSNiels Provoswith a higher priority.
453fa6c304dSNiels Provos.Pp
454fa6c304dSNiels ProvosThe number of different priorities can be set initially with the
455fa6c304dSNiels Provos.Fn event_priority_init
456fa6c304dSNiels Provosfunction.
457fa6c304dSNiels ProvosThis function should be called before the first call to
458fa6c304dSNiels Provos.Fn event_dispatch .
459fa6c304dSNiels ProvosThe
460fa6c304dSNiels Provos.Fn event_priority_set
461fa6c304dSNiels Provosfunction can be used to assign a priority to an event.
462fa6c304dSNiels ProvosBy default,
463fa6c304dSNiels Provos.Nm libevent
464fa6c304dSNiels Provosassigns the middle priority to all events unless their priority
465fa6c304dSNiels Provosis explicitly set.
466cacd8398SNiels Provos.Sh THREAD SAFE EVENTS
467cacd8398SNiels Provos.Nm Libevent
468cacd8398SNiels Provoshas experimental support for thread-safe events.
469cacd8398SNiels ProvosWhen initializing the library via
470cacd8398SNiels Provos.Fn event_init ,
471cacd8398SNiels Provosan event base is returned.
472cacd8398SNiels ProvosThis event base can be used in conjunction with calls to
47311a40d47SNiels Provos.Fn event_base_set ,
474720f7fccSNiels Provos.Fn event_base_dispatch ,
475720f7fccSNiels Provos.Fn event_base_loop ,
476f296e633SNiels Provos.Fn event_base_loopexit ,
477f5aa65c9SNiels Provos.Fn bufferevent_base_set
478cacd8398SNiels Provosand
479f5aa65c9SNiels Provos.Fn event_base_free .
4803ba224dbSNiels Provos.Fn event_base_set
4813ba224dbSNiels Provosshould be called after preparing an event with
4823ba224dbSNiels Provos.Fn event_set ,
4833ba224dbSNiels Provosas
4843ba224dbSNiels Provos.Fn event_set
4853ba224dbSNiels Provosassigns the provided event to the most recently created event base.
486f296e633SNiels Provos.Fn bufferevent_base_set
487f296e633SNiels Provosshould be called after preparing a bufferevent with
488f296e633SNiels Provos.Fn bufferevent_new .
489f5aa65c9SNiels Provos.Fn event_base_free
490f5aa65c9SNiels Provosshould be used to free memory associated with the event base
491f5aa65c9SNiels Provoswhen it is no longer needed.
4928f2e1f6dSNiels Provos.Sh BUFFERED EVENTS
4938f2e1f6dSNiels Provos.Nm libevent
4948f2e1f6dSNiels Provosprovides an abstraction on top of the regular event callbacks.
4958f2e1f6dSNiels ProvosThis abstraction is called a
4968f2e1f6dSNiels Provos.Va "buffered event" .
49711a40d47SNiels ProvosA buffered event provides input and output buffers that get filled
4988f2e1f6dSNiels Provosand drained automatically.
4998f2e1f6dSNiels ProvosThe user of a buffered event no longer deals directly with the IO,
50024ffe1cbSNiels Provosbut instead is reading from input and writing to output buffers.
5018f2e1f6dSNiels Provos.Pp
5028f2e1f6dSNiels ProvosA new bufferevent is created by
5038f2e1f6dSNiels Provos.Fn bufferevent_new .
5048f2e1f6dSNiels ProvosThe parameter
50511a40d47SNiels Provos.Fa fd
5068f2e1f6dSNiels Provosspecifies the file descriptor from which data is read and written to.
5078f2e1f6dSNiels ProvosThis file descriptor is not allowed to be a
5088f2e1f6dSNiels Provos.Xr pipe 2 .
5098f2e1f6dSNiels ProvosThe next three parameters are callbacks.
51011a40d47SNiels ProvosThe read and write callback have the following form:
5118f2e1f6dSNiels Provos.Ft void
51211a40d47SNiels Provos.Fn "(*cb)" "struct bufferevent *bufev" "void *arg" .
513f296e633SNiels ProvosThe error callback has the following form:
514f296e633SNiels Provos.Ft void
515f296e633SNiels Provos.Fn "(*cb)" "struct bufferevent *bufev" "short what" "void *arg" .
5168f2e1f6dSNiels ProvosThe argument is specified by the fourth parameter
5178f2e1f6dSNiels Provos.Fa "cbarg" .
518f296e633SNiels ProvosA
519f296e633SNiels Provos.Fa bufferevent struct
520f296e633SNiels Provospointer is returned on success, NULL on error.
521b0b5e2c2SNiels ProvosBoth the read and the write callback may be NULL.
522b0b5e2c2SNiels ProvosThe error callback has to be always provided.
5238f2e1f6dSNiels Provos.Pp
524f296e633SNiels ProvosOnce initialized, the bufferevent structure can be used repeatedly with
525f5aa65c9SNiels Provosbufferevent_enable() and bufferevent_disable().
526f5aa65c9SNiels ProvosThe flags parameter can be a combination of
527f296e633SNiels Provos.Va EV_READ
528f296e633SNiels Provosand
529f296e633SNiels Provos.Va EV_WRITE .
530f296e633SNiels ProvosWhen read enabled the bufferevent will try to read from the file
531f5aa65c9SNiels Provosdescriptor and call the read callback.
532f5aa65c9SNiels ProvosThe write callback is executed
533f296e633SNiels Provoswhenever the output buffer is drained below the write low watermark,
534f296e633SNiels Provoswhich is
5358f2e1f6dSNiels Provos.Va 0
5368f2e1f6dSNiels Provosby default.
5378f2e1f6dSNiels Provos.Pp
5388f2e1f6dSNiels ProvosThe
5398f2e1f6dSNiels Provos.Fn bufferevent_write
5408f2e1f6dSNiels Provosfunction can be used to write data to the file descriptor.
5418f2e1f6dSNiels ProvosThe data is appended to the output buffer and written to the descriptor
5428f2e1f6dSNiels Provosautomatically as it becomes available for writing.
543f5aa65c9SNiels Provos.Fn bufferevent_write
544f5aa65c9SNiels Provosreturns 0 on success or \-1 on failure.
5458f2e1f6dSNiels ProvosThe
5468f2e1f6dSNiels Provos.Fn bufferevent_read
547f5aa65c9SNiels Provosfunction is used to read data from the input buffer,
548f5aa65c9SNiels Provosreturning the amount of data read.
549f296e633SNiels Provos.Pp
550f296e633SNiels ProvosIf multiple bases are in use, bufferevent_base_set() must be called before
551f296e633SNiels Provosenabling the bufferevent for the first time.
55238b33048SNiels Provos.Sh NON-BLOCKING HTTP SUPPORT
55338b33048SNiels Provos.Nm libevent
55438b33048SNiels Provosprovides a very thin HTTP layer that can be used both to host an HTTP
55538b33048SNiels Provosserver and also to make HTTP requests.
55638b33048SNiels ProvosAn HTTP server can be created by calling
55767947ce3SNiels Provos.Fn evhttp_new .
55867947ce3SNiels ProvosIt can be bound to any port and address with the
55967947ce3SNiels Provos.Fn evhttp_bind_socket
56067947ce3SNiels Provosfunction.
56138b33048SNiels ProvosWhen the HTTP server is no longer used, it can be freed via
56238b33048SNiels Provos.Fn evhttp_free .
56338b33048SNiels Provos.Pp
56438b33048SNiels ProvosTo be notified of HTTP requests, a user needs to register callbacks with the
56538b33048SNiels ProvosHTTP server.
56638b33048SNiels ProvosThis can be done by calling
56738b33048SNiels Provos.Fn evhttp_set_cb .
56838b33048SNiels ProvosThe second argument is the URI for which a callback is being registered.
56938b33048SNiels ProvosThe corresponding callback will receive an
57038b33048SNiels Provos.Va struct evhttp_request
57138b33048SNiels Provosobject that contains all information about the request.
57238b33048SNiels Provos.Pp
573f5aa65c9SNiels ProvosThis section does not document all the possible function calls; please
57438b33048SNiels Provoscheck
57538b33048SNiels Provos.Va event.h
57638b33048SNiels Provosfor the public interfaces.
577ccdc5990SNick Mathewson.Sh ADDITIONAL NOTES
578ccdc5990SNick MathewsonIt is possible to disable support for
579ccdc5990SNick Mathewson.Va epoll , kqueue , devpoll , poll
580ccdc5990SNick Mathewsonor
581ccdc5990SNick Mathewson.Va select
582ccdc5990SNick Mathewsonby setting the environment variable
583ccdc5990SNick Mathewson.Va EVENT_NOEPOLL , EVENT_NOKQUEUE , EVENT_NODEVPOLL , EVENT_NOPOLL
584ccdc5990SNick Mathewsonor
585ccdc5990SNick Mathewson.Va EVENT_NOSELECT ,
586ccdc5990SNick Mathewsonrespectively.
587ccdc5990SNick MathewsonBy setting the environment variable
588ccdc5990SNick Mathewson.Va EVENT_SHOW_METHOD ,
589ccdc5990SNick Mathewson.Nm libevent
590ccdc5990SNick Mathewsondisplays the kernel notification method that it uses.
591aa6567feSNiels Provos.Sh RETURN VALUES
592aa6567feSNiels ProvosUpon successful completion
593aa6567feSNiels Provos.Fn event_add
594aa6567feSNiels Provosand
595aa6567feSNiels Provos.Fn event_del
596aa6567feSNiels Provosreturn 0.
59711a40d47SNiels ProvosOtherwise, \-1 is returned and the global variable errno is
598aa6567feSNiels Provosset to indicate the error.
599aa6567feSNiels Provos.Sh SEE ALSO
60011a40d47SNiels Provos.Xr kqueue 2 ,
60111a40d47SNiels Provos.Xr poll 2 ,
602aa6567feSNiels Provos.Xr select 2 ,
603f5aa65c9SNiels Provos.Xr evdns 3 ,
60411a40d47SNiels Provos.Xr timeout 9
605aa6567feSNiels Provos.Sh HISTORY
606aa6567feSNiels ProvosThe
607aa6567feSNiels Provos.Nm event
608aa6567feSNiels ProvosAPI manpage is based on the
609aa6567feSNiels Provos.Xr timeout 9
610aa6567feSNiels Provosmanpage by Artur Grabowski.
611e0216ed7SNiels ProvosThe port of
612e0216ed7SNiels Provos.Nm libevent
613e0216ed7SNiels Provosto Windows is due to Michael A. Davis.
614e0216ed7SNiels ProvosSupport for real-time signals is due to Taral.
615aa6567feSNiels Provos.Sh AUTHORS
616aa6567feSNiels ProvosThe
617aa6567feSNiels Provos.Nm event
618aa6567feSNiels Provoslibrary was written by Niels Provos.
61924ffe1cbSNiels Provos.Sh BUGS
620bca504e4SNiels ProvosThis documentation is neither complete nor authoritative.
62124ffe1cbSNiels ProvosIf you are in doubt about the usage of this API then
62224ffe1cbSNiels Provoscheck the source code to find out how it works, write
62324ffe1cbSNiels Provosup the missing piece of documentation and send it to
62424ffe1cbSNiels Provosme for inclusion in this man page.
625