Fixed Windows connect() bug for TcpSocket

The socket is put into non-blocking mode prior to connecting. Previously, errno was being checked after connecting to see if the socket would block. This is the correct behaviour on Linux. On Windows however, errno remains 0, and WSAGetLastError() needs to be compared against WSAEWOULDBLOCK instead.
This commit is contained in:
Fred Nicolson 2018-07-12 11:46:59 +01:00
parent 4dec5318f7
commit fb28a03c2b
2 changed files with 11 additions and 3 deletions

View File

@ -88,7 +88,7 @@ namespace fr
mbedtls_ssl_init(ssl.get()); mbedtls_ssl_init(ssl.get());
mbedtls_net_init(ssl_socket_descriptor.get()); mbedtls_net_init(ssl_socket_descriptor.get());
//Do to mbedtls not supporting connect timeouts, we have to use an fr::TcpSocket to //Due to mbedtls not supporting connect timeouts, we have to use an fr::TcpSocket to
//Open the descriptor, and then steal it. This is a hack. //Open the descriptor, and then steal it. This is a hack.
{ {
fr::TcpSocket socket; fr::TcpSocket socket;

View File

@ -125,10 +125,18 @@ namespace fr
//Try and connect //Try and connect
ret = ::connect(socket_descriptor, c->ai_addr, c->ai_addrlen); ret = ::connect(socket_descriptor, c->ai_addr, c->ai_addrlen);
#ifdef _WIN32
if(ret < 0 && WSAGetLastError() != WSAEWOULDBLOCK)
#else
if(ret < 0 && errno != EINPROGRESS) if(ret < 0 && errno != EINPROGRESS)
#endif
{
continue; continue;
}
else if(ret == 0) //If it connected immediately then break out of the connect loop else if(ret == 0) //If it connected immediately then break out of the connect loop
{
break; break;
}
//Wait for the socket to do something/expire //Wait for the socket to do something/expire
timeval tv = {}; timeval tv = {};