| 366 | } |
| 367 | |
| 368 | static bool connectSocket (std::atomic<int>& handle, |
| 369 | CriticalSection& readLock, |
| 370 | const String& hostName, |
| 371 | int portNumber, |
| 372 | int timeOutMillisecs) noexcept |
| 373 | { |
| 374 | bool success = false; |
| 375 | |
| 376 | if (auto* info = getAddressInfo (false, hostName, portNumber)) |
| 377 | { |
| 378 | for (auto* i = info; i != nullptr; i = i->ai_next) |
| 379 | { |
| 380 | auto newHandle = socket (i->ai_family, i->ai_socktype, 0); |
| 381 | |
| 382 | if (newHandle != invalidSocket) |
| 383 | { |
| 384 | setSocketBlockingState (newHandle, false); |
| 385 | auto result = ::connect (newHandle, i->ai_addr, (socklen_t) i->ai_addrlen); |
| 386 | success = (result >= 0); |
| 387 | |
| 388 | if (! success) |
| 389 | { |
| 390 | #if JUCE_WINDOWS |
| 391 | if (result == SOCKET_ERROR && WSAGetLastError() == WSAEWOULDBLOCK) |
| 392 | #else |
| 393 | if (errno == EINPROGRESS) |
| 394 | #endif |
| 395 | { |
| 396 | std::atomic<int> cvHandle { (int) newHandle }; |
| 397 | |
| 398 | if (waitForReadiness (cvHandle, readLock, false, timeOutMillisecs) == 1) |
| 399 | success = true; |
| 400 | } |
| 401 | } |
| 402 | |
| 403 | if (success) |
| 404 | { |
| 405 | handle = (int) newHandle; |
| 406 | break; |
| 407 | } |
| 408 | |
| 409 | #if JUCE_WINDOWS |
| 410 | closesocket (newHandle); |
| 411 | #else |
| 412 | ::close (newHandle); |
| 413 | #endif |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | freeaddrinfo (info); |
| 418 | |
| 419 | if (success) |
| 420 | { |
| 421 | auto h = (SocketHandle) handle.load(); |
| 422 | setSocketBlockingState (h, true); |
| 423 | resetSocketOptions (h, false, false); |
| 424 | } |
| 425 | } |
no test coverage detected