From 1c666a0191eff99228364db59ff6ef859ac1ea2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Wed, 19 Jul 2017 10:42:33 +0200 Subject: [PATCH] Fix send() return value handling. A value of 0 doesn't indicate a closed connection as for recv(). --- source/eventcore/drivers/posix/sockets.d | 29 ++++++++---------------- 1 file changed, 9 insertions(+), 20 deletions(-) diff --git a/source/eventcore/drivers/posix/sockets.d b/source/eventcore/drivers/posix/sockets.d index ce6ce0e..0204d26 100644 --- a/source/eventcore/drivers/posix/sockets.d +++ b/source/eventcore/drivers/posix/sockets.d @@ -350,21 +350,16 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets on_write_finish(socket, IOStatus.error, 0); return; } + + if (mode == IOMode.immediate) { + on_write_finish(socket, IOStatus.wouldBlock, 0); + return; + } } size_t bytes_written = 0; - if (ret == 0) { - on_write_finish(socket, IOStatus.disconnected, 0); - return; - } - - if (ret < 0 && mode == IOMode.immediate) { - on_write_finish(socket, IOStatus.wouldBlock, 0); - return; - } - - if (ret > 0) { + if (ret >= 0) { bytes_written += ret; buffer = buffer[ret .. $]; if (mode != IOMode.all || buffer.length == 0) { @@ -380,7 +375,7 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets with (m_loop.m_fds[socket].streamSocket) { writeCallback = on_write_finish; writeMode = mode; - bytesWritten = ret > 0 ? ret : 0; + bytesWritten = ret >= 0 ? ret : 0; writeBuffer = buffer; } @@ -411,13 +406,7 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets } } - if (ret == 0) { - m_loop.setNotifyCallback!(EventType.write)(socket, null); - slot.writeCallback(cast(StreamSocketFD)socket, IOStatus.disconnected, slot.bytesWritten); - return; - } - - if (ret > 0) { + if (ret >= 0) { slot.bytesWritten += ret; slot.writeBuffer = slot.writeBuffer[ret .. $]; if (slot.writeMode != IOMode.all || slot.writeBuffer.length == 0) { @@ -524,7 +513,7 @@ final class PosixEventDriverSockets(Loop : PosixEventLoop) : EventDriverSockets ret = () @trusted { return connect(sockfd, cast(sockaddr*)&sa, addr_len); } (); } else ret = () @trusted { return connect(sockfd, target_address.name, target_address.nameLen); } (); - + if (ret != 0) { closeSocket(sockfd); return DatagramSocketFD.init;