I'm working on a project where I was originally using winsock but wanted to do everything in Juce possible so I transitioned it to StreamingSockets instead; however I've run into an issue. Previously when I was using getaddrinfo the ai_next pointer would be populated and that is where I needed to make my connection but Juce does not iterate through those pointers. I've made a modification to juce_Socket.cpp to handle this case but I'm no Juce or Socket master so I just wanted to post the code here for review. The base Juce code does not successfully make a connection but the modified code below does.
static bool connectSocket (int volatile& handle,
CriticalSection& readLock,
const String& hostName,
const int portNumber,
const int timeOutMillisecs) noexcept
{
struct addrinfo* info = getAddressInfo (false, hostName, portNumber);
if (info == nullptr)
return false;
bool retval = false;
struct addrinfo* iterator;
for (iterator = info; iterator != NULL ; iterator = iterator->ai_next)
{
handle = (int) socket (iterator->ai_family, iterator->ai_socktype, 0);
if (handle < 0)
{
freeaddrinfo (info);
return false;
}
setSocketBlockingState (handle, false);
const int result = ::connect (handle, iterator->ai_addr, (socklen_t) iterator->ai_addrlen);
retval = (result >= 0);
if (result < 0)
{
#if JUCE_WINDOWS
if (result == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK)
#else
if (errno == EINPROGRESS)
#endif
{
if (waitForReadiness (handle, readLock, false, timeOutMillisecs) == 1)
retval = true;
}
continue;
}
break;
}
freeaddrinfo (info);
setSocketBlockingState (handle, true);
if (retval)
resetSocketOptions (handle, false, false);
return retval;
}
Just want to make sure this isn't going to cause some issue down the line I don't forsee or if there is a better way to achieve my desired effect.
