diff options
author | Nick Mathewson <nickm@torproject.org> | 2010-02-20 18:44:35 -0500 |
---|---|---|
committer | Nick Mathewson <nickm@torproject.org> | 2010-02-23 15:20:33 -0500 |
commit | d3288293fdc6aef510cc217b171187aac19c444b (patch) | |
tree | b45eac524f5738d98f35b8e4bedc4a11a1bb20ec /bufferevent.c | |
parent | 38ec0a773b73375410cbb5a4eaeece3d0ab1caa0 (diff) | |
download | libevent-d3288293fdc6aef510cc217b171187aac19c444b.tar.gz |
Provide consistent, tested semantics for bufferevent timeouts
The different bufferevent implementations had different behavior for
their timeouts. Some of them kept re-triggering the timeouts
indefinitely; some disabled the event immediately the first time a
timeout triggered. Some of them made the timeouts only count when
the bufferevent was actively trying to read or write; some did not.
The new behavior is modeled after old socket bufferevents, since
they were here first and their behavior is relatively sane.
Basically, each timeout disables the bufferevent's corresponding
read or write operation when it fires. Timeouts are stopped
whenever we suspend writing or reading, and reset whenever we
unsuspend writing or reading. Calling bufferevent_enable resets a
timeout, as does changing the timeout value.
Diffstat (limited to 'bufferevent.c')
-rw-r--r-- | bufferevent.c | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/bufferevent.c b/bufferevent.c index 3864cece..1a83ed28 100644 --- a/bufferevent.c +++ b/bufferevent.c @@ -676,6 +676,7 @@ bufferevent_generic_read_timeout_cb(evutil_socket_t fd, short event, void *ctx) { struct bufferevent *bev = ctx; _bufferevent_incref_and_lock(bev); + bufferevent_disable(bev, EV_READ); _bufferevent_run_eventcb(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_READING); _bufferevent_decref_and_unlock(bev); } @@ -684,6 +685,7 @@ bufferevent_generic_write_timeout_cb(evutil_socket_t fd, short event, void *ctx) { struct bufferevent *bev = ctx; _bufferevent_incref_and_lock(bev); + bufferevent_disable(bev, EV_WRITE); _bufferevent_run_eventcb(bev, BEV_EVENT_TIMEOUT|BEV_EVENT_WRITING); _bufferevent_decref_and_unlock(bev); } @@ -712,13 +714,18 @@ int _bufferevent_generic_adj_timeouts(struct bufferevent *bev) { const short enabled = bev->enabled; + struct bufferevent_private *bev_p = + EVUTIL_UPCAST(bev, struct bufferevent_private, bev); int r1=0, r2=0; - if ((enabled & EV_READ) && evutil_timerisset(&bev->timeout_read)) + if ((enabled & EV_READ) && !bev_p->read_suspended && + evutil_timerisset(&bev->timeout_read)) r1 = event_add(&bev->ev_read, &bev->timeout_read); else r1 = event_del(&bev->ev_read); - if ((enabled & EV_WRITE) && evutil_timerisset(&bev->timeout_write)) + if ((enabled & EV_WRITE) && !bev_p->write_suspended && + evutil_timerisset(&bev->timeout_write) && + evbuffer_get_length(bev->output)) r2 = event_add(&bev->ev_write, &bev->timeout_write); else r2 = event_del(&bev->ev_write); |