summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorNathan Straz <nstraz@redhat.com>2014-08-20 14:30:39 -0400
committerNathan Straz <nstraz@redhat.com>2014-08-20 14:30:39 -0400
commit1349500d5726aff5e9b7f067dab972490e9fed23 (patch)
treea5a6f81e3701679a2a388f5fb04e1164acb7b93c
parent8229f8ff0cd40a96ca2d28de2c2b199fb20c8ab8 (diff)
downloadqarsh-1349500d5726aff5e9b7f067dab972490e9fed23.tar.gz
qarsh-1349500d5726aff5e9b7f067dab972490e9fed23.tar.xz
qarsh-1349500d5726aff5e9b7f067dab972490e9fed23.zip
[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().
-rw-r--r--sockutil.c35
1 files 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;
}