Skip to content

Commit

Permalink
allow setting an event base for bufferevents; from phil oleson
Browse files Browse the repository at this point in the history
svn:r199
  • Loading branch information
provos committed Feb 3, 2006
1 parent 6717cf3 commit f296e63
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 6 deletions.
20 changes: 19 additions & 1 deletion evbuffer.c
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
}
32 changes: 27 additions & 5 deletions event.3
Expand Up @@ -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 ,
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand All @@ -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
Expand All @@ -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
Expand Down

0 comments on commit f296e63

Please sign in to comment.