]> granicus.if.org Git - libevent/commitdiff
allow the write callback to called even if there is no buffered data
authorNiels Provos <provos@gmail.com>
Tue, 23 Mar 2004 04:05:37 +0000 (04:05 +0000)
committerNiels Provos <provos@gmail.com>
Tue, 23 Mar 2004 04:05:37 +0000 (04:05 +0000)
svn:r96

evbuffer.c
event.h

index 80e7318778bb3875b7b5715cc7f5c6fc4c5fff29..d765ce5fed86ce824fa7f452803c000eebb52a46 100644 (file)
@@ -85,7 +85,7 @@ static void
 bufferevent_writecb(int fd, short event, void *arg)
 {
        struct bufferevent *bufev = arg;
-       int res;
+       int res = 0;
        short what = EVBUFFER_WRITE;
 
        if (event == EV_TIMEOUT) {
@@ -93,29 +93,33 @@ bufferevent_writecb(int fd, short event, void *arg)
                goto error;
        }
 
-       res = evbuffer_write(bufev->input, fd);
-       if (res == -1) {
-               if (errno == EAGAIN || errno == EINTR)
-                       goto reschedule;
-               /* error case */
-               what |= EVBUFFER_ERROR;
-       } else if (res == 0) {
-               /* eof case */
-               what |= EVBUFFER_EOF;
+       if (EVBUFFER_LENGTH(bufev->output)) {
+           res = evbuffer_write(bufev->output, fd);
+           if (res == -1) {
+                   if (errno == EAGAIN || errno == EINTR)
+                           goto reschedule;
+                   /* error case */
+                   what |= EVBUFFER_ERROR;
+           } else if (res == 0) {
+                   /* eof case */
+                   what |= EVBUFFER_EOF;
+           }
+           if (res <= 0)
+                   goto error;
        }
 
-       if (res <= 0)
-               goto error;
+       if (EVBUFFER_LENGTH(bufev->output) != 0)
+               bufferevent_add(&bufev->ev_write, bufev->timeout_write);
 
        /* Invoke the user callback if our buffer is drained */
        if (EVBUFFER_LENGTH(bufev->output) == 0)
                (*bufev->writecb)(bufev, bufev->cbarg);
 
+       return;
+
  reschedule:
-       /* Do not call if we call user callback before, we may be deleted */
-       if (EVBUFFER_LENGTH(bufev->output) != 0) {
+       if (EVBUFFER_LENGTH(bufev->output) != 0)
                bufferevent_add(&bufev->ev_write, bufev->timeout_write);
-       }
        return;
 
  error:
diff --git a/event.h b/event.h
index ebb716531fc684aec111f72099c38812a572e1fe..dd03c538d0e056d60d4cde6d9dc28cd4be91eb66 100644 (file)
--- a/event.h
+++ b/event.h
@@ -175,6 +175,7 @@ struct evbuffer {
        size_t off;
 };
 
+/* Just for error reporting - use other constants otherwise */
 #define EVBUFFER_READ          0x01
 #define EVBUFFER_WRITE         0x02
 #define EVBUFFER_EOF           0x10