Browse Source

Rework read_socket_line to keep reading while data is available

master
Con Kolivas 11 years ago
parent
commit
75f330d468
  1. 15
      src/ckpool.c

15
src/ckpool.c

@ -141,20 +141,23 @@ int read_socket_line(connsock_t *cs, int timeout)
cs->buf = ckzalloc(PAGESIZE); cs->buf = ckzalloc(PAGESIZE);
else if (cs->buflen) { else if (cs->buflen) {
memmove(cs->buf, cs->buf + cs->bufofs, cs->buflen); memmove(cs->buf, cs->buf + cs->bufofs, cs->buflen);
cs->buf[cs->buflen] = '\0'; memset(cs->buf + cs->buflen, 0, cs->bufofs);
cs->bufofs = cs->buflen; cs->bufofs = cs->buflen;
cs->buflen = 0; cs->buflen = 0;
eom = strchr(cs->buf, '\n');
} else } else
cs->bufofs = 0; cs->bufofs = 0;
while (!eom) { while (42) {
char readbuf[PAGESIZE] = {}; char readbuf[PAGESIZE] = {};
FD_ZERO(&rd); FD_ZERO(&rd);
FD_SET(cs->fd, &rd); FD_SET(cs->fd, &rd);
tv_timeout.tv_sec = timeout; tv_timeout.tv_sec = eom ? 0 : timeout;
tv_timeout.tv_usec = 0; tv_timeout.tv_usec = 0;
ret = select(cs->fd + 1, &rd, NULL, NULL, &tv_timeout); ret = select(cs->fd + 1, &rd, NULL, NULL, &tv_timeout);
if (eom && !ret)
break;
if (ret < 1) { if (ret < 1) {
if (!ret) if (!ret)
LOGDEBUG("Select timed out in read_socket_line"); LOGDEBUG("Select timed out in read_socket_line");
@ -172,18 +175,20 @@ int read_socket_line(connsock_t *cs, int timeout)
align_len(&buflen); align_len(&buflen);
cs->buf = realloc(cs->buf, buflen); cs->buf = realloc(cs->buf, buflen);
if (unlikely(!cs->buf)) if (unlikely(!cs->buf))
quit(1, "Failed to alloc rem of %d bytes in read_socket_line", (int)buflen); quit(1, "Failed to alloc buf of %d bytes in read_socket_line", (int)buflen);
memcpy(cs->buf + cs->bufofs, readbuf, ret); memcpy(cs->buf + cs->bufofs, readbuf, ret);
eom = strchr(cs->buf, '\n'); eom = strchr(cs->buf, '\n');
cs->bufofs += ret; cs->bufofs += ret;
cs->buf[cs->bufofs] = '\0'; cs->buf[cs->bufofs] = '\0';
} }
ret = eom - cs->buf;
if (eom < cs->buf + cs->bufofs) { if (eom < cs->buf + cs->bufofs) {
cs->buflen = cs->buf + cs->bufofs - eom - 1; cs->buflen = cs->buf + cs->bufofs - eom - 1;
cs->bufofs = eom - cs->buf + 1; cs->bufofs = eom - cs->buf + 1;
} else } else
cs->buflen = cs->bufofs = 0; cs->buflen = cs->bufofs = 0;
eom[0] = '\0'; *eom = '\0';
out: out:
if (ret < 1) { if (ret < 1) {
dealloc(cs->buf); dealloc(cs->buf);

Loading…
Cancel
Save