allow the write callback to called even if there is no buffered data

svn:r96
This commit is contained in:
Niels Provos 2004-03-23 04:05:37 +00:00
parent 5908bd7213
commit 246d8583c0
2 changed files with 20 additions and 15 deletions

View File

@ -85,7 +85,7 @@ static void
bufferevent_writecb(int fd, short event, void *arg) bufferevent_writecb(int fd, short event, void *arg)
{ {
struct bufferevent *bufev = arg; struct bufferevent *bufev = arg;
int res; int res = 0;
short what = EVBUFFER_WRITE; short what = EVBUFFER_WRITE;
if (event == EV_TIMEOUT) { if (event == EV_TIMEOUT) {
@ -93,29 +93,33 @@ bufferevent_writecb(int fd, short event, void *arg)
goto error; goto error;
} }
res = evbuffer_write(bufev->input, fd); if (EVBUFFER_LENGTH(bufev->output)) {
if (res == -1) { res = evbuffer_write(bufev->output, fd);
if (errno == EAGAIN || errno == EINTR) if (res == -1) {
goto reschedule; if (errno == EAGAIN || errno == EINTR)
/* error case */ goto reschedule;
what |= EVBUFFER_ERROR; /* error case */
} else if (res == 0) { what |= EVBUFFER_ERROR;
/* eof case */ } else if (res == 0) {
what |= EVBUFFER_EOF; /* eof case */
what |= EVBUFFER_EOF;
}
if (res <= 0)
goto error;
} }
if (res <= 0) if (EVBUFFER_LENGTH(bufev->output) != 0)
goto error; bufferevent_add(&bufev->ev_write, bufev->timeout_write);
/* Invoke the user callback if our buffer is drained */ /* Invoke the user callback if our buffer is drained */
if (EVBUFFER_LENGTH(bufev->output) == 0) if (EVBUFFER_LENGTH(bufev->output) == 0)
(*bufev->writecb)(bufev, bufev->cbarg); (*bufev->writecb)(bufev, bufev->cbarg);
return;
reschedule: 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); bufferevent_add(&bufev->ev_write, bufev->timeout_write);
}
return; return;
error: error:

View File

@ -175,6 +175,7 @@ struct evbuffer {
size_t off; size_t off;
}; };
/* Just for error reporting - use other constants otherwise */
#define EVBUFFER_READ 0x01 #define EVBUFFER_READ 0x01
#define EVBUFFER_WRITE 0x02 #define EVBUFFER_WRITE 0x02
#define EVBUFFER_EOF 0x10 #define EVBUFFER_EOF 0x10