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).
This commit is contained in:
Sönke Ludwig 2018-03-07 10:52:01 +01:00
parent 43ef41d4fa
commit 008163e913

View file

@ -182,11 +182,13 @@ final class WinAPIEventDriverSockets : EventDriverSockets {
override ConnectionState getConnectionState(StreamSocketFD sock) override ConnectionState getConnectionState(StreamSocketFD sock)
{ {
assert(sock != StreamSocketFD.invalid, "Invalid socket handle");
return m_sockets[sock].streamSocket.state; return m_sockets[sock].streamSocket.state;
} }
override bool getLocalAddress(SocketFD sock, scope RefAddress dst) override bool getLocalAddress(SocketFD sock, scope RefAddress dst)
{ {
assert(sock != StreamSocketFD.invalid, "Invalid socket handle");
socklen_t addr_len = dst.nameLen; socklen_t addr_len = dst.nameLen;
if (() @trusted { return getsockname(sock, dst.name, &addr_len); } () != 0) if (() @trusted { return getsockname(sock, dst.name, &addr_len); } () != 0)
return false; return false;
@ -196,6 +198,7 @@ final class WinAPIEventDriverSockets : EventDriverSockets {
override bool getRemoteAddress(SocketFD sock, scope RefAddress dst) override bool getRemoteAddress(SocketFD sock, scope RefAddress dst)
{ {
assert(sock != StreamSocketFD.invalid, "Invalid socket handle");
socklen_t addr_len = dst.nameLen; socklen_t addr_len = dst.nameLen;
if (() @trusted { return getpeername(sock, dst.name, &addr_len); } () != 0) if (() @trusted { return getpeername(sock, dst.name, &addr_len); } () != 0)
return false; return false;
@ -206,13 +209,13 @@ final class WinAPIEventDriverSockets : EventDriverSockets {
override void setTCPNoDelay(StreamSocketFD socket, bool enable) override void setTCPNoDelay(StreamSocketFD socket, bool enable)
@trusted { @trusted {
BOOL eni = enable; 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) override void setKeepAlive(StreamSocketFD socket, bool enable)
@trusted { @trusted {
BOOL eni = enable; 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) override void read(StreamSocketFD socket, ubyte[] buffer, IOMode mode, IOCallback on_read_finish)