in win32/src/socket_transport_win32.c [52:114]
static int connect_to_endpoint(SOCKET client_socket, const ADDRINFO* addrInfo, uint32_t connection_timeout_ms)
{
int result;
// Codes_SOCKET_TRANSPORT_WIN32_09_019: [ connect_to_endpoint shall call connect using connection_timeout_ms as a timeout for the connection. ]
if (connect(client_socket, addrInfo->ai_addr, (int)addrInfo->ai_addrlen) != 0)
{
// Codes_ SOCKET_TRANSPORT_WIN32_09_020: [ If the connect call fails, connect_to_endpoint shall check to WSAGetLastError for WSAEWOULDBLOCK.
if (WSAGetLastError() == WSAEWOULDBLOCK)
{
fd_set write_fds;
FD_ZERO(&write_fds);
FD_SET(client_socket, &write_fds);
struct timeval tv;
tv.tv_sec = 0;
tv.tv_usec = connection_timeout_ms * MILLISECONDS_CONVERSION;
// Codes_SOCKET_TRANSPORT_WIN32_09_021: [ On WSAEWOULDBLOCK connect_to_endpoint shall call the select API with the connection_timeout_ms and check the return value: ]
int ret = select(0, NULL, &write_fds, NULL, &tv);
// Codes_SOCKET_TRANSPORT_WIN32_09_022: [ If the return is SOCKET_ERROR, this indicates a failure and connect_to_endpoint shall fail. ]
if (ret == SOCKET_ERROR)
{
LogError("Failure connecting error: %d", WSAGetLastError());
result = MU_FAILURE;
}
// Codes_SOCKET_TRANSPORT_WIN32_09_023: [ If the return value is 0, this indicates a timeout and connect_to_endpoint shall fail. ]
else if (ret == 0)
{
LogError("Failure timeout (%" PRId32 " us) attempting to connect", connection_timeout_ms);
result = MU_FAILURE;
}
// Codes_SOCKET_TRANSPORT_WIN32_09_024: [ Any other value this indicates a possible success and connect_to_endpoint shall test if the socket is writable by calling FD_ISSET. ]
else
{
if (FD_ISSET(client_socket, &write_fds))
{
result = 0;
}
else
{
LogError("Failure connection is not writable: %d", WSAGetLastError());
result = MU_FAILURE;
}
}
}
// Codes_SOCKET_TRANSPORT_WIN32_09_026: [ If any error is encountered connect_to_endpoint shall return a non-zero value. ]
else
{
LogLastError("Winsock connect failure");
result = MU_FAILURE;
}
}
// Codes_SOCKET_TRANSPORT_WIN32_09_025: [ If the socket is writable connect_to_endpoint shall succeed and return a 0 value. ]
else
{
result = 0;
}
return result;
}