From 008163e913ca1567dd746965dda26759f7ef0fd6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Wed, 7 Mar 2018 10:52:01 +0100 Subject: [PATCH] Fix setTCPNoDelay/setKeepAlive and add assertions. Assertions make sure that socket query functions are not called with an invalid handle (resulting in strange out-of-memory errors). --- source/eventcore/drivers/winapi/sockets.d | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/source/eventcore/drivers/winapi/sockets.d b/source/eventcore/drivers/winapi/sockets.d index 550efca..0922693 100644 --- a/source/eventcore/drivers/winapi/sockets.d +++ b/source/eventcore/drivers/winapi/sockets.d @@ -182,11 +182,13 @@ final class WinAPIEventDriverSockets : EventDriverSockets { override ConnectionState getConnectionState(StreamSocketFD sock) { + assert(sock != StreamSocketFD.invalid, "Invalid socket handle"); return m_sockets[sock].streamSocket.state; } override bool getLocalAddress(SocketFD sock, scope RefAddress dst) { + assert(sock != StreamSocketFD.invalid, "Invalid socket handle"); socklen_t addr_len = dst.nameLen; if (() @trusted { return getsockname(sock, dst.name, &addr_len); } () != 0) return false; @@ -196,6 +198,7 @@ final class WinAPIEventDriverSockets : EventDriverSockets { override bool getRemoteAddress(SocketFD sock, scope RefAddress dst) { + assert(sock != StreamSocketFD.invalid, "Invalid socket handle"); socklen_t addr_len = dst.nameLen; if (() @trusted { return getpeername(sock, dst.name, &addr_len); } () != 0) return false; @@ -206,13 +209,13 @@ final class WinAPIEventDriverSockets : EventDriverSockets { override void setTCPNoDelay(StreamSocketFD socket, bool enable) @trusted { BOOL eni = enable; - setsockopt(INVALID_SOCKET, IPPROTO_TCP, TCP_NODELAY, &eni, eni.sizeof); + setsockopt(socket, IPPROTO_TCP, TCP_NODELAY, &eni, eni.sizeof); } override void setKeepAlive(StreamSocketFD socket, bool enable) @trusted { BOOL eni = enable; - setsockopt(INVALID_SOCKET, SOL_SOCKET, SO_KEEPALIVE, &eni, eni.sizeof); + setsockopt(socket, SOL_SOCKET, SO_KEEPALIVE, &eni, eni.sizeof); } override void read(StreamSocketFD socket, ubyte[] buffer, IOMode mode, IOCallback on_read_finish)