From: Niels Provos Date: Fri, 3 Feb 2006 19:26:06 +0000 (+0000) Subject: allow setting an event base for bufferevents; from phil oleson X-Git-Tag: release-2.0.1-alpha~748 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=f296e6336ac80c8c46e563e84372501434d8884b;p=libevent allow setting an event base for bufferevents; from phil oleson svn:r199 --- diff --git a/evbuffer.c b/evbuffer.c index f9109c79..0e7ef00c 100644 --- a/evbuffer.c +++ b/evbuffer.c @@ -226,7 +226,12 @@ bufferevent_new(int fd, evbuffercb readcb, evbuffercb writecb, bufev->cbarg = cbarg; - bufev->enabled = EV_READ | EV_WRITE; + /* + * Set to EV_WRITE so that using bufferevent_write is going to + * trigger a callback. Reading needs to be explicitly enabled + * because otherwise no data will be available. + */ + bufev->enabled = EV_WRITE; return (bufev); } @@ -372,3 +377,16 @@ bufferevent_setwatermark(struct bufferevent *bufev, short events, bufferevent_read_pressure_cb(bufev->input, 0, EVBUFFER_LENGTH(bufev->input), bufev); } + +int +bufferevent_base_set(struct event_base *base, struct bufferevent *bufev) +{ + int res; + + res = event_base_set(base, &bufev->ev_read); + if (res == -1) + return (res); + + res = event_base_set(base, &bufev->ev_write); + return (res); +} diff --git a/event.3 b/event.3 index 21b1c68a..536775ff 100644 --- a/event.3 +++ b/event.3 @@ -64,6 +64,7 @@ .Nm bufferevent_enable , .Nm bufferevent_disable , .Nm bufferevent_settimeout , +.Nm bufferevent_base_set , .Nm evbuffer_new , .Nm evbuffer_free , .Nm evbuffer_add , @@ -147,6 +148,8 @@ .Fn "bufferevent_disable" "struct bufferevent *bufev" "short event" .Ft void .Fn "bufferevent_settimeout" "struct bufferevent *bufev" "int timeout_read" "int timeout_write" +.Ft int +.Fn "bufferevent_base_set" "struct event_base *base" "struct bufferevent *bufev" .Ft "struct evbuffer *" .Fn "evbuffer_new" "void" .Ft void @@ -443,14 +446,18 @@ This event base can be used in conjunction with calls to .Fn event_base_set , .Fn event_base_dispatch , .Fn event_base_loop , +.Fn event_base_loopexit , and -.Fn event_base_loopexit . +.Fn bufferevent_base_set . .Fn event_base_set should be called after preparing an event with .Fn event_set , as .Fn event_set assigns the provided event to the most recently created event base. +.Fn bufferevent_base_set +should be called after preparing a bufferevent with +.Fn bufferevent_new . .Sh BUFFERED EVENTS .Nm libevent provides an abstraction on top of the regular event callbacks. @@ -472,13 +479,25 @@ The next three parameters are callbacks. The read and write callback have the following form: .Ft void .Fn "(*cb)" "struct bufferevent *bufev" "void *arg" . +The error callback has the following form: +.Ft void +.Fn "(*cb)" "struct bufferevent *bufev" "short what" "void *arg" . The argument is specified by the fourth parameter .Fa "cbarg" . +A +.Fa bufferevent struct +pointer is returned on success, NULL on error. .Pp -By default the buffered event is read enabled and will try to read -from the file descriptor. -The write callback is executed whenever the output buffer is drained -below the write low watermark, which is +Once initialized, the bufferevent structure can be used repeatedly with +bufferevent_enable() and bufferevent_disable(). The flags parameter can +be a combination of +.Va EV_READ +and +.Va EV_WRITE . +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 .Va 0 by default. .Pp @@ -491,6 +510,9 @@ The .Fn bufferevent_read function is used to read data from the input buffer. Both functions return the amount of data written or read. +.Pp +If multiple bases are in use, bufferevent_base_set() must be called before +enabling the bufferevent for the first time. .Sh RETURN VALUES Upon successful completion .Fn event_add