From: Niels Provos Date: Sun, 27 Apr 2008 20:40:56 +0000 (+0000) Subject: introduce bufferevent_setcb and bufferevent_setfd to allow better manipulation of... X-Git-Tag: release-2.0.1-alpha~358 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9485ff9a6629e2b5f73a775a7fa799f56d589e30;p=libevent introduce bufferevent_setcb and bufferevent_setfd to allow better manipulation of bufferevents svn:r737 --- diff --git a/ChangeLog b/ChangeLog index f5f2dfe8..0b56306a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -75,6 +75,7 @@ Changes in current version: o fix a bug in buffrevent read water marks and add a test for them o fix a bug in which bufferevent_write_buffer would not schedule a write event o provide bufferevent_input and bufferevent_output without requiring knowledge of the structure + o introduce bufferevent_setcb and bufferevent_setfd to allow better manipulation of bufferevents Changes in 1.4.0: o allow \r or \n individually to separate HTTP headers instead of the standard "\r\n"; from Charles Kerr. diff --git a/evbuffer.c b/evbuffer.c index ca86ae39..9442da67 100644 --- a/evbuffer.c +++ b/evbuffer.c @@ -257,11 +257,7 @@ bufferevent_new(evutil_socket_t fd, evbuffercb readcb, evbuffercb writecb, event_set(&bufev->ev_read, fd, EV_READ, bufferevent_readcb, bufev); event_set(&bufev->ev_write, fd, EV_WRITE, bufferevent_writecb, bufev); - bufev->readcb = readcb; - bufev->writecb = writecb; - bufev->errorcb = errorcb; - - bufev->cbarg = cbarg; + bufferevent_setcb(bufev, readcb, writecb, errorcb, cbarg); /* * Set to EV_WRITE so that using bufferevent_write is going to @@ -273,6 +269,33 @@ bufferevent_new(evutil_socket_t fd, evbuffercb readcb, evbuffercb writecb, return (bufev); } +void +bufferevent_setcb(struct bufferevent *bufev, + evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg) +{ + bufev->readcb = readcb; + bufev->writecb = writecb; + bufev->errorcb = errorcb; + + bufev->cbarg = cbarg; +} + +void +bufferevent_setfd(struct bufferevent *bufev, evutil_socket_t fd) +{ + event_del(&bufev->ev_read); + event_del(&bufev->ev_write); + + event_set(&bufev->ev_read, fd, EV_READ, bufferevent_readcb, bufev); + event_set(&bufev->ev_write, fd, EV_WRITE, bufferevent_writecb, bufev); + if (bufev->ev_base != NULL) { + event_base_set(bufev->ev_base, &bufev->ev_read); + event_base_set(bufev->ev_base, &bufev->ev_write); + } + + /* might have to manually trigger event registration */ +} + struct evbuffer * bufferevent_input(struct bufferevent *bufev) { @@ -430,6 +453,8 @@ bufferevent_base_set(struct event_base *base, struct bufferevent *bufev) { int res; + bufev->ev_base = base; + res = event_base_set(base, &bufev->ev_read); if (res == -1) return (res); diff --git a/include/event2/bufferevent.h b/include/event2/bufferevent.h index f607d484..5ef4e8bc 100644 --- a/include/event2/bufferevent.h +++ b/include/event2/bufferevent.h @@ -140,6 +140,31 @@ int bufferevent_priority_set(struct bufferevent *bufev, int pri); void bufferevent_free(struct bufferevent *bufev); +/** + Changes the callbacks for a bufferevent. + + @param bufev the bufferevent object for which to change callbacks + @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) + @see bufferevent_new() + */ +void bufferevent_setcb(struct bufferevent *bufev, + evbuffercb readcb, evbuffercb writecb, everrorcb errorcb, void *cbarg); + +/** + Changes the file descriptor on which the bufferevent operates. + + @param bufev the bufferevent object for which to change the file descriptor + @param fd the file descriptor to operate on +*/ +void bufferevent_setfd(struct bufferevent *bufev, evutil_socket_t fd); + /** Write data to a bufferevent buffer. diff --git a/include/event2/bufferevent_struct.h b/include/event2/bufferevent_struct.h index c01640ff..f35ba2a2 100644 --- a/include/event2/bufferevent_struct.h +++ b/include/event2/bufferevent_struct.h @@ -65,6 +65,8 @@ struct event_watermark { }; struct bufferevent { + struct event_base *ev_base; + struct event ev_read; struct event ev_write;