diff --git a/CMakeLists.txt b/CMakeLists.txt index 02d257b5..8b13e6f2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -26,6 +26,7 @@ set( IXWEBSOCKET_SOURCES ixwebsocket/IXSocketFactory.cpp ixwebsocket/IXDNSLookup.cpp ixwebsocket/IXCancellationRequest.cpp + ixwebsocket/IXNetSystem.cpp ixwebsocket/IXWebSocket.cpp ixwebsocket/IXWebSocketServer.cpp ixwebsocket/IXWebSocketTransport.cpp @@ -49,6 +50,7 @@ set( IXWEBSOCKET_HEADERS ixwebsocket/IXSetThreadName.h ixwebsocket/IXDNSLookup.h ixwebsocket/IXCancellationRequest.h + ixwebsocket/IXNetSystem.h ixwebsocket/IXProgressCallback.h ixwebsocket/IXWebSocket.h ixwebsocket/IXWebSocketServer.h diff --git a/ixwebsocket/IXDNSLookup.cpp b/ixwebsocket/IXDNSLookup.cpp index 9bdd78c7..18c5879a 100644 --- a/ixwebsocket/IXDNSLookup.cpp +++ b/ixwebsocket/IXDNSLookup.cpp @@ -27,7 +27,7 @@ namespace ix _done(false), _id(_nextId++) { - + initNetSystem(); } DNSLookup::~DNSLookup() @@ -137,6 +137,11 @@ namespace ix return nullptr; } + if (!_errMsg.empty()) + { + errMsg = _errMsg; + } + std::unique_lock rlock(_resMutex); return _res; } diff --git a/ixwebsocket/IXNetSystem.cpp b/ixwebsocket/IXNetSystem.cpp new file mode 100644 index 00000000..ff290ee1 --- /dev/null +++ b/ixwebsocket/IXNetSystem.cpp @@ -0,0 +1,39 @@ +/* + * IXNetSystem.cpp + * Author: Benjamin Sergeant + * Copyright (c) 2019 Machine Zone. All rights reserved. + */ + +#include "IXNetSystem.h" + +namespace ix +{ + bool initNetSystem() + { +#ifdef _WIN32 + WORD wVersionRequested; + WSADATA wsaData; + int err; + + /* Use the MAKEWORD(lowbyte, highbyte) macro declared in Windef.h */ + wVersionRequested = MAKEWORD(2, 2); + + err = WSAStartup(wVersionRequested, &wsaData); + + return err == 0; +#else + return true; +#endif + } + + bool uninitNetSystem() + { +#ifdef _WIN32 + int err = WSACleanup(); + + return err == 0; +#else + return true; +#endif + } +} diff --git a/ixwebsocket/IXNetSystem.h b/ixwebsocket/IXNetSystem.h index 40522fb4..e8993e1a 100644 --- a/ixwebsocket/IXNetSystem.h +++ b/ixwebsocket/IXNetSystem.h @@ -23,3 +23,9 @@ # include # include #endif + +namespace ix +{ + bool initNetSystem(); + bool uninitNetSystem(); +} diff --git a/ixwebsocket/IXSocketConnect.cpp b/ixwebsocket/IXSocketConnect.cpp index 7944fa40..08011a33 100644 --- a/ixwebsocket/IXSocketConnect.cpp +++ b/ixwebsocket/IXSocketConnect.cpp @@ -57,10 +57,10 @@ namespace ix SocketConnect::configure(fd); if (::connect(fd, address->ai_addr, address->ai_addrlen) == -1 - && errno != EINPROGRESS) + && errno != EINPROGRESS && errno != 0) { - closeSocket(fd); errMsg = strerror(errno); + closeSocket(fd); return -1; } diff --git a/test/IXTest.cpp b/test/IXTest.cpp index 2d575a67..9808994a 100644 --- a/test/IXTest.cpp +++ b/test/IXTest.cpp @@ -113,7 +113,7 @@ namespace ix } struct sockaddr_in sa; // server address information - socklen_t len; + socklen_t len = sizeof(sa); if (getsockname(sockfd, (struct sockaddr *) &sa, &len) < 0) { log("Cannot compute a free port. getsockname error."); diff --git a/test/IXTest.h b/test/IXTest.h index 89bb57cf..c438406f 100644 --- a/test/IXTest.h +++ b/test/IXTest.h @@ -52,6 +52,5 @@ namespace ix void log(const std::string& msg); - bool computeFreePorts(int count); int getFreePort(); }