Browse Source

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

tags/2021-05-28
jules 9 years ago
parent
commit
9eb54629f2
1 changed files with 41 additions and 29 deletions
  1. +41
    -29
      modules/juce_core/network/juce_Socket.cpp

+ 41
- 29
modules/juce_core/network/juce_Socket.cpp View File

@@ -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 (struct addrinfo* info = getAddressInfo (false, hostName, portNumber))
{
for (struct addrinfo* i = info; i != nullptr; i = i->ai_next)
{
const SocketHandle newHandle = socket (i->ai_family, i->ai_socktype, 0);
if (handle < 0)
handle = (int) socket (info->ai_family, info->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 (handle < 0)
{
freeaddrinfo (info);
return false;
}
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;
}
}
setSocketBlockingState (handle, false);
const int result = ::connect (handle, info->ai_addr, (socklen_t) info->ai_addrlen);
freeaddrinfo (info);
if (success)
{
handle = (int) newHandle;
break;
}
#if JUCE_WINDOWS
closesocket (newHandle);
#else
::close (newHandle);
#endif
}
}
bool retval = (result >= 0);
freeaddrinfo (info);
if (result < 0)
{
#if JUCE_WINDOWS
if (result == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
#else
if (errno == EINPROGRESS)
#endif
if (success)
{
if (waitForReadiness (handle, readLock, false, timeOutMillisecs) == 1)
retval = true;
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…
Cancel
Save