From de7b5b177b8ad295820bd14c00b049fd8a5ec21f Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 25 Apr 2010 19:34:35 +0200 Subject: [PATCH] dccp_recv/udp_recv: Use the new nonblock API. This simplifies the code quite a bit. In particular it allows to kill the extra check for EAGAIN that was necessary to deal with unreliable return value of FD_ISSET(). --- dccp_recv.c | 33 +++++++++++++-------------------- fd.c | 21 --------------------- fd.h | 1 - udp_recv.c | 43 ++++++++++++++++++------------------------- 4 files changed, 31 insertions(+), 67 deletions(-) diff --git a/dccp_recv.c b/dccp_recv.c index f71a7253..2ab9fcab 100644 --- a/dccp_recv.c +++ b/dccp_recv.c @@ -153,34 +153,27 @@ static void dccp_recv_post_select(struct sched *s, struct task *t) struct btr_node *btrn = rn->btrn; struct iovec iov[2]; int ret, iovcnt; + size_t num_bytes; ret = btr_node_status(btrn, 0, BTR_NT_ROOT); - if (ret < 0) - goto err; - if (ret == 0) - return; - if (!FD_ISSET(pdd->fd, &s->rfds)) - return; /* nothing to do */ + if (ret <= 0) + goto out; iovcnt = btr_pool_get_buffers(pdd->btrp, iov); ret = -E_DCCP_OVERRUN; if (iovcnt == 0) - goto err; - ret = para_readv(pdd->fd, iov, iovcnt); - /* EAGAIN is possible even if FD_ISSET */ - if (ret < 0 && is_errno(-ret, EAGAIN)) - return; - if (ret == 0) - ret = -E_RECV_EOF; - if (ret < 0) - goto err; - if (ret <= iov[0].iov_len) /* only the first buffer was filled */ - btr_add_output_pool(pdd->btrp, ret, btrn); + goto out; + ret = readv_nonblock(pdd->fd, iov, iovcnt, &s->rfds, &num_bytes); + if (num_bytes == 0) + goto out; + if (num_bytes <= iov[0].iov_len) /* only the first buffer was filled */ + btr_add_output_pool(pdd->btrp, num_bytes, btrn); else { /* both buffers contain data */ btr_add_output_pool(pdd->btrp, iov[0].iov_len, btrn); - btr_add_output_pool(pdd->btrp, ret - iov[0].iov_len, btrn); + btr_add_output_pool(pdd->btrp, num_bytes - iov[0].iov_len, btrn); } - return; -err: +out: + if (ret >= 0) + return; btr_remove_node(rn->btrn); t->error = ret; } diff --git a/fd.c b/fd.c index d56a3110..2e59bc95 100644 --- a/fd.c +++ b/fd.c @@ -177,27 +177,6 @@ int read_nonblock(int fd, void *buf, size_t sz, fd_set *rfds, size_t *num_bytes) return readv_nonblock(fd, &iov, 1, rfds, num_bytes); } -/** - * Simple wrapper for readv(). - * - * \param fd The file descriptor to read from. - * \param iov Scatter/gather array used in readv(). - * \param iovcnt Number of elements in \a iov. - * - * \return A negative error code on errors, the return value of the underlying - * call to readv() otherwise. - * - * \sa readv(2). - */ -int para_readv(int fd, struct iovec *iov, int iovcnt) -{ - int ret = readv(fd, iov, iovcnt); - - if (ret < 0) - return -ERRNO_TO_PARA_ERROR(errno); - return ret; -} - /** * Check whether a file exists. * diff --git a/fd.h b/fd.h index b9558820..787784d3 100644 --- a/fd.h +++ b/fd.h @@ -7,7 +7,6 @@ /** \file fd.h exported symbols from fd.c */ int write_all(int fd, const char *buf, size_t *len); -int para_readv(int fd, struct iovec *iov, int iovcnt); int file_exists(const char *); int para_select(int n, fd_set *readfds, fd_set *writefds, struct timeval *timeout_tv); diff --git a/udp_recv.c b/udp_recv.c index 00ad3e27..5520c6fb 100644 --- a/udp_recv.c +++ b/udp_recv.c @@ -67,43 +67,36 @@ static void udp_recv_post_select(__a_unused struct sched *s, struct task *t) struct receiver_node *rn = container_of(t, struct receiver_node, task); struct private_udp_recv_data *purd = rn->private_data; struct btr_node *btrn = rn->btrn; - size_t packet_size; + size_t num_bytes; struct iovec iov[2]; - int ret, iovcnt; + int ret, readv_ret, iovcnt; t->error = 0; ret = btr_node_status(btrn, 0, BTR_NT_ROOT); - if (ret < 0) - goto err; - if (ret == 0) - return; - if (!FD_ISSET(purd->fd, &s->rfds)) - return; + if (ret <= 0) + goto out; iovcnt = btr_pool_get_buffers(purd->btrp, iov); ret = -E_UDP_OVERRUN; if (iovcnt == 0) - goto err; - ret = para_readv(purd->fd, iov, iovcnt); - /* EAGAIN is possible even if FD_ISSET */ - if (ret < 0 && is_errno(-ret, EAGAIN)) - return; - if (ret == 0) - ret = -E_RECV_EOF; + goto out; + ret = readv_nonblock(purd->fd, iov, iovcnt, &s->rfds, &num_bytes); + if (num_bytes == 0) + goto out; + readv_ret = ret; + ret = udp_check_eof(num_bytes, iov); if (ret < 0) - goto err; - packet_size = ret; - ret = udp_check_eof(packet_size, iov); - if (ret < 0) - goto err; - if (iov[0].iov_len >= packet_size) - btr_add_output_pool(purd->btrp, packet_size, btrn); + goto out; + if (iov[0].iov_len >= num_bytes) + btr_add_output_pool(purd->btrp, num_bytes, btrn); else { /* both buffers contain data */ btr_add_output_pool(purd->btrp, iov[0].iov_len, btrn); - btr_add_output_pool(purd->btrp, packet_size - iov[0].iov_len, + btr_add_output_pool(purd->btrp, num_bytes - iov[0].iov_len, btrn); } - return; -err: + ret = readv_ret; +out: + if (ret >= 0) + return; btr_remove_node(btrn); t->error = ret; close(purd->fd); -- 2.39.5