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
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);
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;
}
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. */
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);
}
}
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);
}
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;
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);
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. */
FD_ZERO(&write_fds);
max_fd = 0;
for (nn=0; nn<fds_size; nn++) {
- if (!(fd = fds[nn])) continue;
+ if (!(fd = fds[nn]))
+ continue;
max_fd = nn;
- if (fd->wants_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. */
/* Call back anybody that has connect or read activity and wants to know. */
for (nn=0; nn<fds_size; nn++) {
- if (!(fd = fds[nn])) continue;
+ if (!(fd = fds[nn]))
+ continue;
if (FD_ISSET(nn, &read_fds)) {
- if (fd->line_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. */
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);
}
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