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:
parent
22215d8279
commit
9eb54629f2
1 changed files with 45 additions and 33 deletions
|
|
@ -333,45 +333,57 @@ namespace SocketHelpers
|
|||
const int portNumber,
|
||||
const int timeOutMillisecs) noexcept
|
||||
{
|
||||
struct addrinfo* info = getAddressInfo (false, hostName, portNumber);
|
||||
bool success = false;
|
||||
|
||||
if (info == nullptr)
|
||||
return false;
|
||||
|
||||
if (handle < 0)
|
||||
handle = (int) socket (info->ai_family, info->ai_socktype, 0);
|
||||
|
||||
if (handle < 0)
|
||||
if (struct addrinfo* info = getAddressInfo (false, hostName, portNumber))
|
||||
{
|
||||
freeaddrinfo (info);
|
||||
return false;
|
||||
}
|
||||
|
||||
setSocketBlockingState (handle, false);
|
||||
const int result = ::connect (handle, info->ai_addr, (socklen_t) info->ai_addrlen);
|
||||
freeaddrinfo (info);
|
||||
|
||||
bool retval = (result >= 0);
|
||||
|
||||
if (result < 0)
|
||||
{
|
||||
#if JUCE_WINDOWS
|
||||
if (result == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
|
||||
#else
|
||||
if (errno == EINPROGRESS)
|
||||
#endif
|
||||
for (struct addrinfo* i = info; i != nullptr; i = i->ai_next)
|
||||
{
|
||||
if (waitForReadiness (handle, readLock, false, timeOutMillisecs) == 1)
|
||||
retval = true;
|
||||
const SocketHandle newHandle = socket (i->ai_family, i->ai_socktype, 0);
|
||||
|
||||
if (newHandle >= 0)
|
||||
{
|
||||
setSocketBlockingState (newHandle, false);
|
||||
const int result = ::connect (newHandle, i->ai_addr, (socklen_t) i->ai_addrlen);
|
||||
success = (result >= 0);
|
||||
|
||||
if (! success)
|
||||
{
|
||||
#if JUCE_WINDOWS
|
||||
if (result == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
|
||||
#else
|
||||
if (errno == EINPROGRESS)
|
||||
#endif
|
||||
{
|
||||
if (waitForReadiness (newHandle, readLock, false, timeOutMillisecs) == 1)
|
||||
success = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (success)
|
||||
{
|
||||
handle = (int) newHandle;
|
||||
break;
|
||||
}
|
||||
|
||||
#if JUCE_WINDOWS
|
||||
closesocket (newHandle);
|
||||
#else
|
||||
::close (newHandle);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
freeaddrinfo (info);
|
||||
|
||||
if (success)
|
||||
{
|
||||
setSocketBlockingState (handle, true);
|
||||
resetSocketOptions (handle, false, false);
|
||||
}
|
||||
}
|
||||
|
||||
setSocketBlockingState (handle, true);
|
||||
|
||||
if (retval)
|
||||
resetSocketOptions (handle, false, false);
|
||||
|
||||
return retval;
|
||||
return success;
|
||||
}
|
||||
|
||||
static void makeReusable (int handle) noexcept
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue