1
0
Fork 0
mirror of https://github.com/juce-framework/JUCE.git synced 2026-01-10 23:44:24 +00:00

Refactored some StreamingSocket connection code to iterate addresses and clean up failed handles

This commit is contained in:
jules 2016-04-05 10:18:57 +01:00
parent 22215d8279
commit 9eb54629f2

View file

@ -333,27 +333,21 @@ namespace SocketHelpers
const int portNumber, const int portNumber,
const int timeOutMillisecs) noexcept const int timeOutMillisecs) noexcept
{ {
struct addrinfo* info = getAddressInfo (false, hostName, portNumber); bool success = false;
if (info == nullptr) if (struct addrinfo* info = getAddressInfo (false, hostName, portNumber))
return false;
if (handle < 0)
handle = (int) socket (info->ai_family, info->ai_socktype, 0);
if (handle < 0)
{ {
freeaddrinfo (info); for (struct addrinfo* i = info; i != nullptr; i = i->ai_next)
return false; {
} const SocketHandle newHandle = socket (i->ai_family, i->ai_socktype, 0);
setSocketBlockingState (handle, false); if (newHandle >= 0)
const int result = ::connect (handle, info->ai_addr, (socklen_t) info->ai_addrlen); {
freeaddrinfo (info); setSocketBlockingState (newHandle, false);
const int result = ::connect (newHandle, i->ai_addr, (socklen_t) i->ai_addrlen);
success = (result >= 0);
bool retval = (result >= 0); if (! success)
if (result < 0)
{ {
#if JUCE_WINDOWS #if JUCE_WINDOWS
if (result == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK) if (result == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
@ -361,17 +355,35 @@ namespace SocketHelpers
if (errno == EINPROGRESS) if (errno == EINPROGRESS)
#endif #endif
{ {
if (waitForReadiness (handle, readLock, false, timeOutMillisecs) == 1) if (waitForReadiness (newHandle, readLock, false, timeOutMillisecs) == 1)
retval = true; success = true;
} }
} }
if (success)
{
handle = (int) newHandle;
break;
}
#if JUCE_WINDOWS
closesocket (newHandle);
#else
::close (newHandle);
#endif
}
}
freeaddrinfo (info);
if (success)
{
setSocketBlockingState (handle, true); setSocketBlockingState (handle, true);
if (retval)
resetSocketOptions (handle, false, false); resetSocketOptions (handle, false, false);
}
}
return retval; return success;
} }
static void makeReusable (int handle) noexcept static void makeReusable (int handle) noexcept