catch EINPROGRESS and WSAEWOULDBLOCK

This commit is contained in:
MihailRis 2024-11-26 20:32:57 +03:00
parent 992cfefc8e
commit 85d94017c7

View File

@ -19,10 +19,14 @@
#include <arpa/inet.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
using SOCKET = int;
#endif // _WIN32
#include <chrono>
#include <thread>
#include "debug/Logger.hpp"
#include "util/stringutil.hpp"
@ -361,11 +365,24 @@ public:
int res = connectsocket(descriptor, addrinfo->ai_addr, addrinfo->ai_addrlen);
if (res == -1) {
auto error = handle_socket_error("Connect failed");
closesocket(descriptor);
freeaddrinfo(addrinfo);
throw error;
# ifdef _WIN32
if (WSAGetLastError() != WSAEWOULDBLOCK) {
auto error = handle_socket_error("Connect failed");
closesocket(descriptor);
freeaddrinfo(addrinfo);
throw error;
}
# else
if (errno != EINPROGRESS) {
auto error = handle_socket_error("Connect failed");
closesocket(descriptor);
freeaddrinfo(addrinfo);
throw error;
}
# endif
}
std::this_thread::sleep_for(std::chrono::milliseconds(1000));
logger.info() << "connected to " << address << " ["
<< to_string(addrinfo) << ":" << port << "]";
return std::make_shared<SocketImpl>(descriptor, addrinfo);