From a324f44901795baaeee8c76e09b8f846d80ab05f Mon Sep 17 00:00:00 2001 From: Lynix Date: Fri, 13 Nov 2015 14:01:36 +0100 Subject: [PATCH] Network/TcpClient: Fix QueryState() when connection failed Former-commit-id: 15824535922186c0601ade8bfbbe76866b7cd5ff --- src/Nazara/Network/TcpClient.cpp | 76 +++++++++++++++++--------------- 1 file changed, 41 insertions(+), 35 deletions(-) diff --git a/src/Nazara/Network/TcpClient.cpp b/src/Nazara/Network/TcpClient.cpp index 87094cb36..6e4dec7b8 100644 --- a/src/Nazara/Network/TcpClient.cpp +++ b/src/Nazara/Network/TcpClient.cpp @@ -94,50 +94,56 @@ namespace Nz SocketState TcpClient::QueryState() { - // Check our state depending on our last state - switch (m_state) + // Only check state if we have a valid handle, else we're not connected. + if (m_handle != SocketImpl::InvalidHandle) { - case SocketState_Connecting: + // Check our state depending on our last state + switch (m_state) { - // If we were connecting, check how it's going - SocketError getError; - SocketError error = SocketImpl::GetLastError(m_handle, &getError); - - if (getError != SocketError_NoError) - break; //< Do not update state if we cannot get socket state - - if (error == SocketError_NoError) + case SocketState_Connecting: { - // No error yet, we're still connecting or connected, check that by calling Connect again - return Connect(m_peerAddress); - } - else - { - // Our connection attempt failed - m_lastError = error; - UpdateState(SocketState_NotConnected); - } + // If we were connecting, check how it's going + SocketError getError; + SocketError error = SocketImpl::GetLastError(m_handle, &getError); - break; - } + if (getError != SocketError_NoError) + break; //< Do not update state if we cannot get socket state - default: - { - // Check our peer address, if it works we're connected - SocketError error; - m_peerAddress = SocketImpl::QueryPeerAddress(m_handle, &error); - if (m_peerAddress == IpAddress::Invalid) - { - // Other errors mean a problem while getting the peer address - if (error == SocketError_ConnectionClosed) + if (error == SocketError_NoError) + { + // No error yet, we're still connecting or connected, check that by calling Connect again + return Connect(m_peerAddress); + } + else + { + // Our connection attempt failed + m_lastError = error; UpdateState(SocketState_NotConnected); - } - else - UpdateState(SocketState_Connected); // If we are not connecting and have a peer address, we are connected + } - break; + break; + } + + default: + { + // Check our peer address, if it works we're connected + SocketError error; + m_peerAddress = SocketImpl::QueryPeerAddress(m_handle, &error); + if (m_peerAddress == IpAddress::Invalid) + { + // Other errors mean a problem while getting the peer address + if (error == SocketError_ConnectionClosed) + UpdateState(SocketState_NotConnected); + } + else + UpdateState(SocketState_Connected); // If we are not connecting and have a peer address, we are connected + + break; + } } } + else + UpdateState(SocketState_NotConnected); return m_state; }