Network/TcpClient: Fix QueryState() when connection failed

Former-commit-id: 15824535922186c0601ade8bfbbe76866b7cd5ff
This commit is contained in:
Lynix 2015-11-13 14:01:36 +01:00
parent 5325513bd8
commit a324f44901
1 changed files with 41 additions and 35 deletions

View File

@ -94,50 +94,56 @@ namespace Nz
SocketState TcpClient::QueryState() SocketState TcpClient::QueryState()
{ {
// Check our state depending on our last state // Only check state if we have a valid handle, else we're not connected.
switch (m_state) 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 case SocketState_Connecting:
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)
{ {
// No error yet, we're still connecting or connected, check that by calling Connect again // If we were connecting, check how it's going
return Connect(m_peerAddress); SocketError getError;
} SocketError error = SocketImpl::GetLastError(m_handle, &getError);
else
{
// Our connection attempt failed
m_lastError = error;
UpdateState(SocketState_NotConnected);
}
break; if (getError != SocketError_NoError)
} break; //< Do not update state if we cannot get socket state
default: if (error == SocketError_NoError)
{ {
// Check our peer address, if it works we're connected // No error yet, we're still connecting or connected, check that by calling Connect again
SocketError error; return Connect(m_peerAddress);
m_peerAddress = SocketImpl::QueryPeerAddress(m_handle, &error); }
if (m_peerAddress == IpAddress::Invalid) else
{ {
// Other errors mean a problem while getting the peer address // Our connection attempt failed
if (error == SocketError_ConnectionClosed) m_lastError = error;
UpdateState(SocketState_NotConnected); 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; return m_state;
} }