X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/disorder/blobdiff_plain/cb9a695c5b6058d2c319789ca154f555d6c0815c..16fb2830d52c1420afdee555a566d72a065d9616:/lib/event.c diff --git a/lib/event.c b/lib/event.c index 3d6d1f6..ee25024 100644 --- a/lib/event.c +++ b/lib/event.c @@ -1,27 +1,25 @@ /* * This file is part of DisOrder. - * Copyright (C) 2004, 2005, 2007 Richard Kettlewell + * Copyright (C) 2004, 2005, 2007, 2008 Richard Kettlewell * - * This program is free software; you can redistribute it and/or modify + * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or + * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. - * + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 - * USA + * along with this program. If not, see . */ /** @file lib/event.c * @brief DisOrder event loop */ -#include +#include "common.h" #include #include @@ -31,15 +29,11 @@ #include #include #include -#include #include #include -#include -#include #include #include #include -#include #include "event.h" #include "mem.h" #include "log.h" @@ -47,6 +41,8 @@ #include "printf.h" #include "sink.h" #include "vector.h" +#include "timeval.h" +#include "heap.h" /** @brief A timeout */ struct timeout { @@ -54,9 +50,18 @@ struct timeout { struct timeval when; ev_timeout_callback *callback; void *u; - int resolve; + int active; }; +/** @brief Comparison function for timeouts */ +static int timeout_lt(const struct timeout *a, + const struct timeout *b) { + return tvlt(&a->when, &b->when); +} + +HEAP_TYPE(timeout_heap, struct timeout *, timeout_lt); +HEAP_DEFINE(timeout_heap, struct timeout *, timeout_lt); + /** @brief A file descriptor in one mode */ struct fd { int fd; @@ -106,11 +111,8 @@ struct ev_source { /** @brief File descriptors, per mode */ struct fdmode mode[ev_nmodes]; - /** @brief Sorted linked list of timeouts - * - * We could use @ref HEAP_TYPE now, but there aren't many timeouts. - */ - struct timeout *timeouts; + /** @brief Heap of timeouts */ + struct timeout_heap timeouts[1]; /** @brief Array of handled signals */ struct signal signals[NSIG]; @@ -146,27 +148,6 @@ static const char *modenames[] = { "read", "write", "except" }; /* utilities ******************************************************************/ -/** @brief Great-than comparison for timevals - * - * Ought to be in @file lib/timeval.h - */ -static inline int gt(const struct timeval *a, const struct timeval *b) { - if(a->tv_sec > b->tv_sec) - return 1; - if(a->tv_sec == b->tv_sec - && a->tv_usec > b->tv_usec) - return 1; - return 0; -} - -/** @brief Greater-than-or-equal comparison for timevals - * - * Ought to be in @file lib/timeval.h - */ -static inline int ge(const struct timeval *a, const struct timeval *b) { - return !gt(b, a); -} - /* creation *******************************************************************/ /** @brief Create a new event loop */ @@ -179,6 +160,7 @@ ev_source *ev_new(void) { FD_ZERO(&ev->mode[n].enabled); ev->sigpipe[0] = ev->sigpipe[1] = -1; sigemptyset(&ev->sigmask); + timeout_heap_init(ev->timeouts); return ev; } @@ -194,7 +176,7 @@ int ev_run(ev_source *ev) { int n, mode; int ret; int maxfd; - struct timeout *t, **tt; + struct timeout *timeouts, *t, **tt; struct stat sb; xgettimeofday(&now, 0); @@ -202,10 +184,24 @@ int ev_run(ev_source *ev) { * while we're handling them (otherwise we'd have to break out of infinite * loops, preferrably without starving better-behaved subsystems). Hence * the slightly complicated two-phase approach here. */ - for(t = ev->timeouts; - t && ge(&now, &t->when); - t = t->next) { - t->resolve = 1; + /* First we read those timeouts that have triggered out of the heap. We + * keep them in the same order they came out of the heap in. */ + tt = &timeouts; + while(timeout_heap_count(ev->timeouts) + && tvle(&timeout_heap_first(ev->timeouts)->when, &now)) { + /* This timeout has reached its trigger time; provided it has not been + * cancelled we add it to the timeouts list. */ + t = timeout_heap_remove(ev->timeouts); + if(t->active) { + *tt = t; + tt = &t->next; + } + } + *tt = 0; + /* Now we can run the callbacks for those timeouts. They might add further + * timeouts that are already in the past but they won't trigger until the + * next time round the event loop. */ + for(t = timeouts; t; t = t->next) { D(("calling timeout for %ld.%ld callback %p %p", (long)t->when.tv_sec, (long)t->when.tv_usec, (void *)t->callback, t->u)); @@ -213,13 +209,6 @@ int ev_run(ev_source *ev) { if(ret) return ret; } - tt = &ev->timeouts; - while((t = *tt)) { - if(t->resolve) - *tt = t->next; - else - tt = &t->next; - } maxfd = 0; for(mode = 0; mode < ev_nmodes; ++mode) { ev->mode[mode].tripped = ev->mode[mode].enabled; @@ -228,10 +217,11 @@ int ev_run(ev_source *ev) { } xsigprocmask(SIG_UNBLOCK, &ev->sigmask, 0); do { - if(ev->timeouts) { + if(timeout_heap_count(ev->timeouts)) { + t = timeout_heap_first(ev->timeouts); xgettimeofday(&now, 0); - delta.tv_sec = ev->timeouts->when.tv_sec - now.tv_sec; - delta.tv_usec = ev->timeouts->when.tv_usec - now.tv_usec; + delta.tv_sec = t->when.tv_sec - now.tv_sec; + delta.tv_usec = t->when.tv_usec - now.tv_usec; if(delta.tv_usec < 0) { delta.tv_usec += 1000000; --delta.tv_sec; @@ -321,6 +311,8 @@ int ev_fd(ev_source *ev, D(("registering %s fd %d callback %p %p", modenames[mode], fd, (void *)callback, u)); + if(fd >= FD_SETSIZE) + return -1; assert(mode < ev_nmodes); if(ev->mode[mode].nfds >= ev->mode[mode].fdslots) { ev->mode[mode].fdslots = (ev->mode[mode].fdslots @@ -388,6 +380,7 @@ int ev_fd_cancel(ev_source *ev, ev_fdmode mode, int fd) { * cancelled. */ int ev_fd_enable(ev_source *ev, ev_fdmode mode, int fd) { + assert(fd >= 0); D(("enabling mode %s fd %d", modenames[mode], fd)); FD_SET(fd, &ev->mode[mode].enabled); return 0; @@ -406,6 +399,8 @@ int ev_fd_disable(ev_source *ev, ev_fdmode mode, int fd) { D(("disabling mode %s fd %d", modenames[mode], fd)); FD_CLR(fd, &ev->mode[mode].enabled); FD_CLR(fd, &ev->mode[mode].tripped); + /* Suppress any pending callbacks */ + ev->escape = 1; return 0; } @@ -416,15 +411,17 @@ void ev_report(ev_source *ev) { struct dynstr d[1]; char b[4096]; + if(!debugging) + return; dynstr_init(d); for(mode = 0; mode < ev_nmodes; ++mode) { - info("mode %s maxfd %d", modenames[mode], ev->mode[mode].maxfd); + D(("mode %s maxfd %d", modenames[mode], ev->mode[mode].maxfd)); for(n = 0; n < ev->mode[mode].nfds; ++n) { fd = ev->mode[mode].fds[n].fd; - info("fd %s %d%s%s (%s)", modenames[mode], fd, - FD_ISSET(fd, &ev->mode[mode].enabled) ? " enabled" : "", - FD_ISSET(fd, &ev->mode[mode].tripped) ? " tripped" : "", - ev->mode[mode].fds[n].what); + D(("fd %s %d%s%s (%s)", modenames[mode], fd, + FD_ISSET(fd, &ev->mode[mode].enabled) ? " enabled" : "", + FD_ISSET(fd, &ev->mode[mode].tripped) ? " tripped" : "", + ev->mode[mode].fds[n].what)); } d->nvec = 0; for(fd = 0; fd <= ev->mode[mode].maxfd; ++fd) { @@ -442,7 +439,7 @@ void ev_report(ev_source *ev) { dynstr_append_string(d, b); } dynstr_terminate(d); - info("%s enabled:%s", modenames[mode], d->vec); + D(("%s enabled:%s", modenames[mode], d->vec)); } } @@ -450,7 +447,7 @@ void ev_report(ev_source *ev) { /** @brief Register a timeout * @param ev Event source - * @param handle Where to store timeout handle, or @c NULL + * @param handlep Where to store timeout handle, or @c NULL * @param when Earliest time to call @p callback, or @c NULL * @param callback Function to call at or after @p when * @param u Passed to @p callback @@ -468,7 +465,7 @@ int ev_timeout(ev_source *ev, const struct timeval *when, ev_timeout_callback *callback, void *u) { - struct timeout *t, *p, **pp; + struct timeout *t; D(("registering timeout at %ld.%ld callback %p %p", when ? (long)when->tv_sec : 0, when ? (long)when->tv_usec : 0, @@ -478,11 +475,8 @@ int ev_timeout(ev_source *ev, t->when = *when; t->callback = callback; t->u = u; - pp = &ev->timeouts; - while((p = *pp) && gt(&t->when, &p->when)) - pp = &p->next; - t->next = p; - *pp = t; + t->active = 1; + timeout_heap_insert(ev->timeouts, t); if(handlep) *handlep = t; return 0; @@ -495,19 +489,13 @@ int ev_timeout(ev_source *ev, * * If @p handle is 0 then this is a no-op. */ -int ev_timeout_cancel(ev_source *ev, +int ev_timeout_cancel(ev_source attribute((unused)) *ev, ev_timeout_handle handle) { - struct timeout *t = handle, *p, **pp; + struct timeout *t = handle; - if(!t) - return 0; - for(pp = &ev->timeouts; (p = *pp) && p != t; pp = &p->next) - ; - if(p) { - *pp = p->next; - return 0; - } else - return -1; + if(t) + t->active = 0; + return 0; } /* signals ********************************************************************/ @@ -531,7 +519,9 @@ static void sighandler(int s) { /* probably the reader has stopped listening for some reason */ if(write(sigfd[s], &sc, 1) < 0) { - write(2, errmsg, sizeof errmsg - 1); + /* do the best we can as we're about to abort; shut _up_, gcc */ + int _ignore = write(2, errmsg, sizeof errmsg - 1); + (void)_ignore; abort(); } } @@ -886,31 +876,82 @@ static void buffer_space(struct buffer *b, size_t bytes) { (void *)b->base, (void *)b->start, (void *)b->end, (void *)b->top)); } -/* buffered writer ************************************************************/ +/* readers and writers *******************************************************/ /** @brief State structure for a buffered writer */ struct ev_writer { + /** @brief Sink used for writing to the buffer */ struct sink s; + + /** @brief Output buffer */ struct buffer b; + + /** @brief File descriptor to write to */ int fd; + + /** @brief Set if there'll be no more output */ int eof; + + /** @brief Error/termination callback */ ev_error_callback *callback; + + /** @brief Passed to @p callback */ void *u; + + /** @brief Parent event source */ ev_source *ev; /** @brief Maximum amount of time between succesful writes, 0 = don't care */ int timebound; /** @brief Maximum amount of data to buffer, 0 = don't care */ int spacebound; - /** @brief Synthesized error code */ - int syntherror; + /** @brief Error code to pass to @p callback (see writer_shutdown()) */ + int error; /** @brief Timeout handle for @p timebound (or 0) */ ev_timeout_handle timeout; + /** @brief Description of this writer */ const char *what; + + /** @brief Tied reader or 0 */ + ev_reader *reader; + + /** @brief Set when abandoned */ + int abandoned; }; -/** @brief Synthesized error callback +/** @brief State structure for a buffered reader */ +struct ev_reader { + /** @brief Input buffer */ + struct buffer b; + /** @brief File descriptor read from */ + int fd; + /** @brief Called when new data is available */ + ev_reader_callback *callback; + /** @brief Called on error and shutdown */ + ev_error_callback *error_callback; + /** @brief Passed to @p callback and @p error_callback */ + void *u; + /** @brief Parent event loop */ + ev_source *ev; + /** @brief Set when EOF is detected */ + int eof; + /** @brief Error code to pass to error callback */ + int error; + /** @brief Tied writer or NULL */ + ev_writer *writer; +}; + +/* buffered writer ************************************************************/ + +/** @brief Shut down the writer + * + * This is called to shut down a writer. The error callback is not called + * through any other path. Also we do not cancel @p fd from anywhere else, + * though we might disable it. + * + * It has the signature of a timeout callback so that it can be called from a + * time=0 timeout. * * Calls @p callback with @p w->syntherr as the error code (which might be 0). */ @@ -919,20 +960,39 @@ static int writer_shutdown(ev_source *ev, void *u) { ev_writer *w = u; + if(w->fd == -1) + return 0; /* already shut down */ + D(("writer_shutdown fd=%d error=%d", w->fd, w->error)); ev_timeout_cancel(ev, w->timeout); + ev_fd_cancel(ev, ev_write, w->fd); w->timeout = 0; - return w->callback(ev, w->fd, w->syntherror, w->u); + if(w->reader) { + D(("found a tied reader")); + /* If there is a reader still around we just untie it */ + w->reader->writer = 0; + shutdown(w->fd, SHUT_WR); /* there'll be no more writes */ + } else { + D(("no tied reader")); + /* There's no reader so we are free to close the FD */ + xclose(w->fd); + } + w->fd = -1; + return w->callback(ev, w->error, w->u); } /** @brief Called when a writer's @p timebound expires */ static int writer_timebound_exceeded(ev_source *ev, - const struct timeval attribute((unused)) *now, + const struct timeval *now, void *u) { ev_writer *const w = u; - error(0, "abandoning writer %s because no writes within %ds", - w->what, w->timebound); - return w->callback(ev, w->fd, ETIMEDOUT, w->u); + if(!w->abandoned) { + w->abandoned = 1; + error(0, "abandoning writer '%s' because no writes within %ds", + w->what, w->timebound); + w->error = ETIMEDOUT; + } + return writer_shutdown(ev, now, u); } /** @brief Set the time bound callback (if not set already) */ @@ -956,16 +1016,24 @@ static int writer_callback(ev_source *ev, int fd, void *u) { D(("callback for writer fd %d, %ld bytes, n=%d, errno=%d", fd, (long)(w->b.end - w->b.start), n, errno)); if(n >= 0) { + /* Consume bytes from the buffer */ w->b.start += n; + /* Suppress any outstanding timeout */ ev_timeout_cancel(ev, w->timeout); w->timeout = 0; if(w->b.start == w->b.end) { + /* The buffer is empty */ if(w->eof) { - ev_fd_cancel(ev, ev_write, fd); - return w->callback(ev, fd, 0, w->u); + /* We're done, we can shut down this writer */ + w->error = 0; + return writer_shutdown(ev, 0, w); } else + /* There might be more to come but we don't need writer_callback() to + * be called for the time being */ ev_fd_disable(ev, ev_write, fd); } else + /* The buffer isn't empty, set a timeout so we give up if we don't manage + * to write some more within a reasonable time */ writer_set_timebound(w); } else { switch(errno) { @@ -973,8 +1041,8 @@ static int writer_callback(ev_source *ev, int fd, void *u) { case EAGAIN: break; default: - ev_fd_cancel(ev, ev_write, fd); - return w->callback(ev, fd, errno, w->u); + w->error = errno; + return writer_shutdown(ev, 0, w); } } return 0; @@ -992,21 +1060,30 @@ static int ev_writer_write(struct sink *sk, const void *s, int n) { if(!n) return 0; /* avoid silliness */ + if(w->fd == -1) + error(0, "ev_writer_write on %s after shutdown", w->what); + if(w->spacebound && w->b.end - w->b.start + n > w->spacebound) { + /* The new buffer contents will exceed the space bound. We assume that the + * remote client has gone away and TCP hasn't noticed yet, or that it's got + * hopelessly stuck. */ + if(!w->abandoned) { + w->abandoned = 1; + error(0, "abandoning writer '%s' because buffer has reached %td bytes", + w->what, w->b.end - w->b.start); + ev_fd_disable(w->ev, ev_write, w->fd); + w->error = EPIPE; + return ev_timeout(w->ev, 0, 0, writer_shutdown, w); + } else + return 0; + } + /* Make sure there is space */ buffer_space(&w->b, n); + /* If the buffer was formerly empty then we'll need to re-enable the FD */ if(w->b.start == w->b.end) ev_fd_enable(w->ev, ev_write, w->fd); memcpy(w->b.end, s, n); w->b.end += n; - if(w->spacebound && w->b.end - w->b.start > w->spacebound) { - /* Buffer contents have exceeded the space bound. We assume that the - * remote client has gone away and TCP hasn't noticed yet, or that it's got - * hopelessly stuck. */ - error(0, "abandoning writer %s because buffer has reached %td bytes", - w->what, w->b.end - w->b.start); - w->syntherror = EPIPE; - ev_fd_cancel(w->ev, ev_write, w->fd); - return ev_timeout(w->ev, 0, 0, writer_shutdown, w); - } + /* Arrange a timeout if there wasn't one set already */ writer_set_timebound(w); return 0; } @@ -1018,6 +1095,12 @@ static int ev_writer_write(struct sink *sk, const void *s, int n) { * @param u Passed to @p callback * @param what Text description * @return New writer or @c NULL + * + * Writers own their file descriptor and close it when they have finished with + * it. + * + * If you pass the same fd to a reader and writer, you must tie them together + * with ev_tie(). */ ev_writer *ev_writer_new(ev_source *ev, int fd, @@ -1037,6 +1120,7 @@ ev_writer *ev_writer_new(ev_source *ev, w->what = what; if(ev_fd(ev, ev_write, fd, writer_callback, w, what)) return 0; + /* Buffer is initially empty so we don't want a callback */ ev_fd_disable(ev, ev_write, fd); return w; } @@ -1111,33 +1195,17 @@ struct sink *ev_writer_sink(ev_writer *w) { */ int ev_writer_close(ev_writer *w) { D(("close writer fd %d", w->fd)); + if(w->eof) + return 0; /* already closed */ w->eof = 1; if(w->b.start == w->b.end) { - /* we're already finished */ - w->syntherror = 0; /* no error */ - ev_fd_cancel(w->ev, ev_write, w->fd); + /* We're already finished */ + w->error = 0; /* no error */ return ev_timeout(w->ev, 0, 0, writer_shutdown, w); } return 0; } -/** @brief Cancel a writer discarding any buffered data - * @param w Writer to close - * @return 0 on success, non-0 on error - * - * This cancels a writer immediately. Any unwritten buffered data is discarded - * and the error callback is never called. This is appropriate to call if (for - * instance) the read half of a TCP connection is known to have failed and the - * writer is therefore obsolete. - */ -int ev_writer_cancel(ev_writer *w) { - ev_source *const ev = w->ev; - D(("cancel writer fd %d", w->fd)); - ev_timeout_cancel(ev, w->timeout); - w->timeout = 0; - return ev_fd_cancel(w->ev, ev_write, w->fd); -} - /** @brief Attempt to flush a writer * @param w Writer to flush * @return 0 on success, non-0 on error @@ -1151,16 +1219,41 @@ int ev_writer_flush(ev_writer *w) { /* buffered reader ************************************************************/ -/** @brief State structure for a buffered reader */ -struct ev_reader { - struct buffer b; - int fd; - ev_reader_callback *callback; - ev_error_callback *error_callback; - void *u; - ev_source *ev; - int eof; -}; +/** @brief Shut down a reader + * + * This is the only path through which we cancel and close the file descriptor. + * As with the writer case it is given timeout signature to allow it be + * deferred to the next iteration of the event loop. + * + * We only call @p error_callback if @p error is nonzero (unlike the writer + * case). + */ +static int reader_shutdown(ev_source *ev, + const attribute((unused)) struct timeval *now, + void *u) { + ev_reader *const r = u; + + if(r->fd == -1) + return 0; /* already shut down */ + D(("reader_shutdown fd=%d", r->fd)); + ev_fd_cancel(ev, ev_read, r->fd); + r->eof = 1; + if(r->writer) { + D(("found a tied writer")); + /* If there is a writer still around we just untie it */ + r->writer->reader = 0; + shutdown(r->fd, SHUT_RD); /* there'll be no more reads */ + } else { + D(("no tied writer found")); + /* There's no writer so we are free to close the FD */ + xclose(r->fd); + } + r->fd = -1; + if(r->error) + return r->error_callback(ev, r->error, r->u); + else + return 0; +} /** @brief Called when a reader's @p fd is readable */ static int reader_callback(ev_source *ev, int fd, void *u) { @@ -1173,19 +1266,22 @@ static int reader_callback(ev_source *ev, int fd, void *u) { fd, (int)(r->b.top - r->b.end), n, errno)); if(n > 0) { r->b.end += n; - return r->callback(ev, r, fd, r->b.start, r->b.end - r->b.start, 0, r->u); + return r->callback(ev, r, r->b.start, r->b.end - r->b.start, 0, r->u); } else if(n == 0) { - r->eof = 1; - ev_fd_cancel(ev, ev_read, fd); - return r->callback(ev, r, fd, r->b.start, r->b.end - r->b.start, 1, r->u); + /* No more read callbacks needed */ + ev_fd_disable(r->ev, ev_read, r->fd); + ev_timeout(r->ev, 0, 0, reader_shutdown, r); + /* Pass the remaining data and an eof indicator to the user */ + return r->callback(ev, r, r->b.start, r->b.end - r->b.start, 1, r->u); } else { switch(errno) { case EINTR: case EAGAIN: break; default: - ev_fd_cancel(ev, ev_read, fd); - return r->error_callback(ev, fd, errno, r->u); + /* Fatal error, kill the reader now */ + r->error = errno; + return reader_shutdown(ev, 0, r); } } return 0; @@ -1199,6 +1295,11 @@ static int reader_callback(ev_source *ev, int fd, void *u) { * @param u Passed to callbacks * @param what Text description * @return New reader or @c NULL + * + * Readers own their fd and close it when they are finished with it. + * + * If you pass the same fd to a reader and writer, you must tie them together + * with ev_tie(). */ ev_reader *ev_reader_new(ev_source *ev, int fd, @@ -1238,10 +1339,16 @@ void ev_reader_consume(ev_reader *r, size_t n) { /** @brief Cancel a reader * @param r Reader * @return 0 on success, non-0 on error + * + * No further callbacks will be made, and the FD will be closed (in a later + * iteration of the event loop). */ int ev_reader_cancel(ev_reader *r) { D(("cancel reader fd %d", r->fd)); - return ev_fd_cancel(r->ev, ev_read, r->fd); + if(r->fd == -1) + return 0; /* already thoroughly cancelled */ + ev_fd_disable(r->ev, ev_read, r->fd); + return ev_timeout(r->ev, 0, 0, reader_shutdown, r); } /** @brief Temporarily disable a reader @@ -1253,7 +1360,7 @@ int ev_reader_cancel(ev_reader *r) { */ int ev_reader_disable(ev_reader *r) { D(("disable reader fd %d", r->fd)); - return r->eof ? 0 : ev_fd_disable(r->ev, ev_read, r->fd); + return ev_fd_disable(r->ev, ev_read, r->fd); } /** @brief Called from ev_run() for ev_reader_incomplete() */ @@ -1263,8 +1370,13 @@ static int reader_continuation(ev_source attribute((unused)) *ev, ev_reader *r = u; D(("reader continuation callback fd %d", r->fd)); - if(ev_fd_enable(r->ev, ev_read, r->fd)) return -1; - return r->callback(ev, r, r->fd, r->b.start, r->b.end - r->b.start, r->eof, r->u); + /* If not at EOF turn the FD back on */ + if(!r->eof) + if(ev_fd_enable(r->ev, ev_read, r->fd)) + return -1; + /* We're already in a timeout callback so there's no reason we can't call the + * user callback directly (compare ev_reader_enable()). */ + return r->callback(ev, r, r->b.start, r->b.end - r->b.start, r->eof, r->u); } /** @brief Arrange another callback @@ -1287,7 +1399,7 @@ static int reader_enabled(ev_source *ev, ev_reader *r = u; D(("reader enabled callback fd %d", r->fd)); - return r->callback(ev, r, r->fd, r->b.start, r->b.end - r->b.start, r->eof, r->u); + return r->callback(ev, r, r->b.start, r->b.end - r->b.start, r->eof, r->u); } /** @brief Re-enable reading @@ -1303,11 +1415,38 @@ static int reader_enabled(ev_source *ev, * re-enable. You'll automatically get another callback directly from the * event loop (i.e. not from inside ev_reader_enable()) so you can handle the * next line (or whatever) if the whole thing has in fact already arrived. + * + * The difference between this process and calling ev_reader_incomplete() is + * ev_reader_incomplete() deals with the case where you can process now but + * would rather yield to other clients of the event loop, while using + * ev_reader_disable() and ev_reader_enable() deals with the case where you + * cannot process input yet because some other process is actually not + * complete. */ int ev_reader_enable(ev_reader *r) { D(("enable reader fd %d", r->fd)); - return ((r->eof ? 0 : ev_fd_enable(r->ev, ev_read, r->fd)) - || ev_timeout(r->ev, 0, 0, reader_enabled, r)) ? -1 : 0; + + /* First if we're not at EOF then we re-enable reading */ + if(!r->eof) + if(ev_fd_enable(r->ev, ev_read, r->fd)) + return -1; + /* Arrange another callback next time round the event loop */ + return ev_timeout(r->ev, 0, 0, reader_enabled, r); +} + +/** @brief Tie a reader and a writer together + * @param r Reader + * @param w Writer + * @return 0 on success, non-0 on error + * + * This function must be called if @p r and @p w share a file descritptor. + */ +int ev_tie(ev_reader *r, ev_writer *w) { + assert(r->writer == 0); + assert(w->reader == 0); + r->writer = w; + w->reader = r; + return 0; } /*