From d15e78a517f0db1fecf237dd887ff80a013036b3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Wed, 7 Mar 2018 10:59:31 +0100 Subject: [PATCH 1/2] Test for proper connection state after a successful accept/connect. Test for #54. --- tests/0-tcp.d | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/0-tcp.d b/tests/0-tcp.d index fa79fdf..75cc479 100644 --- a/tests/0-tcp.d +++ b/tests/0-tcp.d @@ -30,6 +30,7 @@ void main() server.waitForConnections!((incoming_, addr) { incoming = incoming_; // work around ref counting issue + assert(incoming.state == ConnectionState.connected); print("Got incoming, reading data"); incoming.read!((status, bts) { print("Got data"); @@ -67,6 +68,7 @@ void main() connectStream!((sock, status) { client = sock; assert(status == ConnectStatus.connected); + assert(sock.state == ConnectionState.connected); print("Initial write"); client.write!((wstatus, bytes) { print("Initial write done"); From 0b02c84b2b2ba2b8a49e42408ef0fcf4a58d81c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=B6nke=20Ludwig?= Date: Wed, 7 Mar 2018 11:00:03 +0100 Subject: [PATCH 2/2] Set ConnectionState.connected when accepting a connection. Fixes #54. --- source/eventcore/drivers/winapi/sockets.d | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/source/eventcore/drivers/winapi/sockets.d b/source/eventcore/drivers/winapi/sockets.d index 0922693..ebf6e9e 100644 --- a/source/eventcore/drivers/winapi/sockets.d +++ b/source/eventcore/drivers/winapi/sockets.d @@ -55,7 +55,7 @@ final class WinAPIEventDriverSockets : EventDriverSockets { return StreamSocketFD.invalid; } - auto sock = adoptStreamInternal(fd); + auto sock = adoptStreamInternal(fd, ConnectionState.connecting); auto ret = .connect(fd, peer_address.name, peer_address.nameLen); //auto ret = WSAConnect(m_socket, peer_address.name, peer_address.nameLen, null, null, null, null); @@ -70,7 +70,6 @@ final class WinAPIEventDriverSockets : EventDriverSockets { if (err == WSAEWOULDBLOCK) { with (m_sockets[sock].streamSocket) { connectCallback = on_connect; - state = ConnectionState.connecting; } m_core.addWaiter(); @@ -99,10 +98,10 @@ final class WinAPIEventDriverSockets : EventDriverSockets { override StreamSocketFD adoptStream(int socket) { - return adoptStreamInternal(socket); + return adoptStreamInternal(socket, ConnectionState.connected); } - private StreamSocketFD adoptStreamInternal(SOCKET socket) + private StreamSocketFD adoptStreamInternal(SOCKET socket, ConnectionState state) { auto fd = StreamSocketFD(socket); if (m_sockets[fd].common.refCount) // FD already in use? @@ -123,6 +122,7 @@ final class WinAPIEventDriverSockets : EventDriverSockets { initSocketSlot(fd); with (m_sockets[socket]) { specific = StreamSocketSlot.init; + streamSocket.state = state; setupOverlapped(streamSocket.write.overlapped); setupOverlapped(streamSocket.read.overlapped); } @@ -833,7 +833,7 @@ final class WinAPIEventDriverSockets : EventDriverSockets { socklen_t addr_len = addr.sizeof; auto clientsockfd = () @trusted { return WSAAccept(sock, cast(sockaddr*)&addr, &addr_len, null, 0); } (); if (clientsockfd == INVALID_SOCKET) return 0; - auto clientsock = driver.adoptStreamInternal(clientsockfd); + auto clientsock = driver.adoptStreamInternal(clientsockfd, ConnectionState.connected); scope RefAddress addrc = new RefAddress(() @trusted { return cast(sockaddr*)&addr; } (), addr_len); slot.streamListen.acceptCallback(cast(StreamListenSocketFD)sock, clientsock, addrc); }