From 1349500d5726aff5e9b7f067dab972490e9fed23 Mon Sep 17 00:00:00 2001 From: Nathan Straz Date: Wed, 20 Aug 2014 14:30:39 -0400 Subject: [sockutil] Rewrite send_packet() so it retries If the writev() returned less than expected, we would never check it. In order to make retrying the write simpler, I modelled the new send_packet() after recv_packet(). --- sockutil.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/sockutil.c b/sockutil.c index c180eb3..a86290d 100644 --- a/sockutil.c +++ b/sockutil.c @@ -143,23 +143,36 @@ int send_packet(int fd, struct qa_packet *qp) { char buf[QARSH_MAX_PACKET_SIZE]; - uint32_t netsize; + uint32_t packetsize; + void *psbuf = &packetsize; int len; ssize_t ret = -1; - struct iovec iovs[2]; + int bufused = 0; qp->qp_seq = packet_seq++; - len = qptostr(qp, buf, QARSH_MAX_PACKET_SIZE - sizeof netsize); + len = qptostr(qp, buf, QARSH_MAX_PACKET_SIZE - sizeof packetsize); - if (len > 0) { - netsize = htonl(len); - iovs[0].iov_base = &netsize; - iovs[0].iov_len = sizeof netsize; - iovs[1].iov_base = buf; - iovs[1].iov_len = len; + if (len == 0) return 0; + + packetsize = htonl(len); + + do { + if ((ret = write(fd, (char *)psbuf+bufused, sizeof packetsize - bufused)) < 0) { + lprintf(LOG_ERR, "Write error while sending packet size: %s\n", strerror(errno)); + return -1; + } + bufused += ret; + } while (bufused < sizeof packetsize); + + bufused = 0; + do { + if ((ret = write(fd, buf+bufused, len - bufused)) < 0) { + lprintf(LOG_ERR, "Write error while sending packet data: %s\n", strerror(errno)); + return -1; + } + bufused += ret; + } while (bufused < len); - ret = writev(fd, iovs, 2); - } return ret; } -- cgit