X-Git-Url: http://git.pk910.de/?p=srvx.git;a=blobdiff_plain;f=src%2Fioset.c;h=4e8caf137d280e53311d1df31ec5cae223c75cf0;hp=beb4504322c5d230e2ac74f022e1b2075b9b4bae;hb=d2048f43a8d603f4d758c685f88eceaaebb2b606;hpb=222e1b0003536cf7b47858961d4b56d45c6d6606 diff --git a/src/ioset.c b/src/ioset.c index beb4504..4e8caf1 100644 --- a/src/ioset.c +++ b/src/ioset.c @@ -58,13 +58,12 @@ static unsigned int ioq_put_avail(const struct ioq *ioq) { /* Subtract 1 from ioq->get to be sure we don't fill the buffer * and make it look empty even when there's data in it. */ - if (ioq->put < ioq->get) { + if (ioq->put < ioq->get) return ioq->get - ioq->put - 1; - } else if (ioq->get == 0) { + else if (ioq->get == 0) return ioq->size - ioq->put - 1; - } else { + else return ioq->size - ioq->put; - } } static unsigned int @@ -191,32 +190,38 @@ ioset_try_write(struct io_fd *fd) { res = write(fd->fd, fd->send.buf+fd->send.get, req); if (res < 0) { switch (errno) { - case EAGAIN: break; + case EAGAIN: + break; default: log_module(MAIN_LOG, LOG_ERROR, "write() on fd %d error %d: %s", fd->fd, errno, strerror(errno)); } } else { fd->send.get += res; - if (fd->send.get == fd->send.size) fd->send.get = 0; + if (fd->send.get == fd->send.size) + fd->send.get = 0; } } void ioset_close(int fd, int os_close) { struct io_fd *fdp; - if (!(fdp = fds[fd])) return; + if (!(fdp = fds[fd])) + return; fds[fd] = NULL; - if (fdp->destroy_cb) fdp->destroy_cb(fdp); + if (fdp->destroy_cb) + fdp->destroy_cb(fdp); if (fdp->send.get != fdp->send.put) { int flags = fcntl(fd, F_GETFL); fcntl(fd, F_SETFL, flags&~O_NONBLOCK); ioset_try_write(fdp); /* it may need to send the beginning of the buffer now.. */ - if (fdp->send.get != fdp->send.put) ioset_try_write(fdp); + if (fdp->send.get != fdp->send.put) + ioset_try_write(fdp); } free(fdp->send.buf); free(fdp->recv.buf); - if (os_close) close(fd); + if (os_close) + close(fd); free(fdp); FD_CLR(fd, &read_fds); FD_CLR(fd, &write_fds); @@ -227,14 +232,13 @@ ioset_find_line_length(struct io_fd *fd) { unsigned int pos, max, len; len = 0; max = (fd->recv.put < fd->recv.get) ? fd->recv.size : fd->recv.put; - for (pos = fd->recv.get; pos < max; ++pos, ++len) { - if (IS_EOL(fd->recv.buf[pos])) return fd->line_len = len + 1; - } - if (fd->recv.put < fd->recv.get) { - for (pos = 0; pos < fd->recv.put; ++pos, ++len) { - if (IS_EOL(fd->recv.buf[pos])) return fd->line_len = len + 1; - } - } + for (pos = fd->recv.get; pos < max; ++pos, ++len) + if (IS_EOL(fd->recv.buf[pos])) + return fd->line_len = len + 1; + if (fd->recv.put < fd->recv.get) + for (pos = 0; pos < fd->recv.put; ++pos, ++len) + if (IS_EOL(fd->recv.buf[pos])) + return fd->line_len = len + 1; return fd->line_len = 0; } @@ -242,11 +246,13 @@ static void ioset_buffered_read(struct io_fd *fd) { int put_avail, nbr, fdnum; - if (!(put_avail = ioq_put_avail(&fd->recv))) put_avail = ioq_grow(&fd->recv); + if (!(put_avail = ioq_put_avail(&fd->recv))) + put_avail = ioq_grow(&fd->recv); nbr = read(fd->fd, fd->recv.buf + fd->recv.put, put_avail); if (nbr < 0) { switch (errno) { - case EAGAIN: break; + case EAGAIN: + break; default: log_module(MAIN_LOG, LOG_ERROR, "Unexpected read() error %d on fd %d: %s", errno, fd->fd, strerror(errno)); /* Just flag it as EOF and call readable_cb() to notify the fd's owner. */ @@ -263,21 +269,22 @@ ioset_buffered_read(struct io_fd *fd) { unsigned int pos; for (pos = fd->recv.put; pos < fd->recv.put + nbr; ++pos) { if (IS_EOL(fd->recv.buf[pos])) { - if (fd->recv.put < fd->recv.get) { + if (fd->recv.put < fd->recv.get) fd->line_len = fd->recv.size + pos + 1 - fd->recv.get; - } else { + else fd->line_len = pos + 1 - fd->recv.get; - } break; } } } fd->recv.put += nbr; - if (fd->recv.put == fd->recv.size) fd->recv.put = 0; + if (fd->recv.put == fd->recv.size) + fd->recv.put = 0; fdnum = fd->fd; while (fd->wants_reads && (fd->line_len > 0)) { fd->readable_cb(fd); - if (!fds[fdnum]) break; /* make sure they didn't close on us */ + if (!fds[fdnum]) + break; /* make sure they didn't close on us */ ioset_find_line_length(fd); } } @@ -286,9 +293,12 @@ ioset_buffered_read(struct io_fd *fd) { int ioset_line_read(struct io_fd *fd, char *dest, int max) { int avail, done; - if (fd->eof && (!ioq_get_avail(&fd->recv) || (fd->line_len < 0))) return 0; - if (fd->line_len < 0) return -1; - if (fd->line_len < max) max = fd->line_len; + if (fd->eof && (!ioq_get_avail(&fd->recv) || (fd->line_len < 0))) + return 0; + if (fd->line_len < 0) + return -1; + if (fd->line_len < max) + max = fd->line_len; avail = ioq_get_avail(&fd->recv); if (max > avail) { memcpy(dest, fd->recv.buf + fd->recv.get, avail); @@ -301,7 +311,8 @@ ioset_line_read(struct io_fd *fd, char *dest, int max) { } memcpy(dest + done, fd->recv.buf + fd->recv.get, max - done); fd->recv.get += max - done; - if (fd->recv.get == fd->recv.size) fd->recv.get = 0; + if (fd->recv.get == fd->recv.size) + fd->recv.get = 0; dest[max] = 0; ioset_find_line_length(fd); return max; @@ -321,7 +332,8 @@ debug_fdsets(const char *msg, int nfds, fd_set *read_fds, fd_set *write_fds, fd_ flags = (read_fds && FD_ISSET(ii, read_fds)) ? 1 : 0; flags |= (write_fds && FD_ISSET(ii, write_fds)) ? 2 : 0; flags |= (except_fds && FD_ISSET(ii, except_fds)) ? 4 : 0; - if (!flags) continue; + if (!flags) + continue; pos += sprintf(buf+pos, " %d%s", ii, flag_text[flags]); } gettimeofday(&now, NULL); @@ -343,15 +355,15 @@ ioset_run(void) { struct io_fd *fd; while (!quit_services) { - while (!socket_io_fd) uplink_connect(); + while (!socket_io_fd) + uplink_connect(); /* How long to sleep? (fill in select_timeout) */ wakey = timeq_next(); - if ((wakey - now) < 0) { + if ((wakey - now) < 0) select_timeout.tv_sec = 0; - } else { + else select_timeout.tv_sec = wakey - now; - } select_timeout.tv_usec = 0; /* Set up read_fds and write_fds fdsets. */ @@ -359,10 +371,13 @@ ioset_run(void) { FD_ZERO(&write_fds); max_fd = 0; for (nn=0; nnwants_reads) FD_SET(nn, &read_fds); - if ((fd->send.get != fd->send.put) || !fd->connected) FD_SET(nn, &write_fds); + if (fd->wants_reads) + FD_SET(nn, &read_fds); + if ((fd->send.get != fd->send.put) || !fd->connected) + FD_SET(nn, &write_fds); } /* Check for activity, update time. */ @@ -380,27 +395,28 @@ ioset_run(void) { /* Call back anybody that has connect or read activity and wants to know. */ for (nn=0; nnline_reads) { + if (fd->line_reads) ioset_buffered_read(fd); - } else { + else fd->readable_cb(fd); - } } if (FD_ISSET(nn, &write_fds) && !fd->connected) { int rc, arglen = sizeof(rc); - if (getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &rc, &arglen) < 0) rc = errno; + if (getsockopt(fd->fd, SOL_SOCKET, SO_ERROR, &rc, &arglen) < 0) + rc = errno; fd->connected = 1; - if (fd->connect_cb) fd->connect_cb(fd, rc); + if (fd->connect_cb) + fd->connect_cb(fd, rc); } /* Note: check whether write FD is still set, since the * connect_cb() might close the FD, making us dereference * a free()'d pointer for the fd. */ - if (FD_ISSET(nn, &write_fds) && (fd->send.get != fd->send.put)) { + if (FD_ISSET(nn, &write_fds) && (fd->send.get != fd->send.put)) ioset_try_write(fd); - } } /* Call any timeq events we need to call. */ @@ -420,9 +436,8 @@ ioset_run(void) { void ioset_write(struct io_fd *fd, const char *buf, unsigned int nbw) { unsigned int avail; - while (ioq_used(&fd->send) + nbw >= fd->send.size) { + while (ioq_used(&fd->send) + nbw >= fd->send.size) ioq_grow(&fd->send); - } avail = ioq_put_avail(&fd->send); if (nbw > avail) { memcpy(fd->send.buf + fd->send.put, buf, avail); @@ -432,7 +447,8 @@ ioset_write(struct io_fd *fd, const char *buf, unsigned int nbw) { } memcpy(fd->send.buf + fd->send.put, buf, nbw); fd->send.put += nbw; - if (fd->send.put == fd->send.size) fd->send.put = 0; + if (fd->send.put == fd->send.size) + fd->send.put = 0; } void