From ad9171f2263b05ff28c4afc0656547d0483a1f73 Mon Sep 17 00:00:00 2001 From: David van Erkelens Date: Wed, 6 Jul 2016 14:08:45 +0200 Subject: [PATCH] Changed writev to sendmsg to prevent SIGPIPE errors --- src/tcpoutbuffer.h | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/src/tcpoutbuffer.h b/src/tcpoutbuffer.h index 6a84de40..0e10f639 100644 --- a/src/tcpoutbuffer.h +++ b/src/tcpoutbuffer.h @@ -224,19 +224,28 @@ class TcpOutBuffer // update counter for next iteration if (++index >= 64) break; } - + + // create the message header + struct msghdr header; + + // make sure the members of the header are empty + memset(&header, 0, sizeof(header)); + + // save the buffers in the message header + header.msg_iov = buffer; + header.msg_iovlen = index; + // send the data - // @todo use sendmsg() with a MSG_NOSIG flag - auto result = writev(socket, (const struct iovec *)&buffer, index); + auto result = sendmsg(socket, &header, MSG_NOSIGNAL); // skip on error, or when nothing was written if (result <= 0) return total > 0 ? total : result; - + // shrink the buffer shrink(result); - + // update total number of bytes written - total += 0; + total += result; } // done