catch EINPROGRESS and WSAEWOULDBLOCK

This commit is contained in:
MihailRis
2024-11-26 20:32:57 +03:00
parent 992cfefc8e
commit 85d94017c7
+21 -4
View File
@@ -19,10 +19,14 @@
#include <arpa/inet.h> #include <arpa/inet.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h>
using SOCKET = int; using SOCKET = int;
#endif // _WIN32 #endif // _WIN32
#include <chrono>
#include <thread>
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
#include "util/stringutil.hpp" #include "util/stringutil.hpp"
@@ -361,11 +365,24 @@ public:
int res = connectsocket(descriptor, addrinfo->ai_addr, addrinfo->ai_addrlen); int res = connectsocket(descriptor, addrinfo->ai_addr, addrinfo->ai_addrlen);
if (res == -1) { if (res == -1) {
auto error = handle_socket_error("Connect failed"); # ifdef _WIN32
closesocket(descriptor); if (WSAGetLastError() != WSAEWOULDBLOCK) {
freeaddrinfo(addrinfo); auto error = handle_socket_error("Connect failed");
throw error; 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 << " [" logger.info() << "connected to " << address << " ["
<< to_string(addrinfo) << ":" << port << "]"; << to_string(addrinfo) << ":" << port << "]";
return std::make_shared<SocketImpl>(descriptor, addrinfo); return std::make_shared<SocketImpl>(descriptor, addrinfo);