]> granicus.if.org Git - libevent/commitdiff
allow setting an event base for bufferevents; from phil oleson
authorNiels Provos <provos@gmail.com>
Fri, 3 Feb 2006 19:26:06 +0000 (19:26 +0000)
committerNiels Provos <provos@gmail.com>
Fri, 3 Feb 2006 19:26:06 +0000 (19:26 +0000)
svn:r199

evbuffer.c
event.3

index f9109c799b31a11fa76d97ba66f27dcb323a449e..0e7ef00cd70522c366d002e41fbb9e3f077ea1c7 100644 (file)
@@ -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 21b1c68a1e80f119c7868d1aeca353602b607bca..536775ff439884c616e3478550b460e571bcbc08 100644 (file)
--- 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 ,
 .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