From 753fc845ac69d7b8f20cd0b41f1f0c80fe27294f Mon Sep 17 00:00:00 2001 From: Dimon4eg Date: Mon, 6 May 2019 19:13:42 +0300 Subject: [PATCH] Fix for windows (#50) --- ixwebsocket/IXSocket.cpp | 30 +++++++++++++++++++++------- ixwebsocket/IXSocket.h | 3 +-- ixwebsocket/IXSocketServer.cpp | 2 +- ixwebsocket/IXWebSocketTransport.cpp | 6 ++---- test/CMakeLists.txt | 5 ++--- 5 files changed, 29 insertions(+), 17 deletions(-) diff --git a/ixwebsocket/IXSocket.cpp b/ixwebsocket/IXSocket.cpp index ffe2a3d3..0d8a4dfe 100644 --- a/ixwebsocket/IXSocket.cpp +++ b/ixwebsocket/IXSocket.cpp @@ -196,6 +196,25 @@ namespace ix #endif } + bool Socket::isWaitNeeded() + { + int err = getErrno(); + + if (err == EWOULDBLOCK || err == EAGAIN) + { + return true; + } + +#ifdef _WIN32 + if (err == WSAEWOULDBLOCK || err == WSATRY_AGAIN) + { + return true; + } +#endif + + return false; + } + void Socket::closeSocket(int fd) { #ifdef _WIN32 @@ -228,8 +247,7 @@ namespace ix return ret == len; } // There is possibly something to be writen, try again - else if (ret < 0 && (getErrno() == EWOULDBLOCK || - getErrno() == EAGAIN)) + else if (ret < 0 && Socket::isWaitNeeded()) { continue; } @@ -257,8 +275,7 @@ namespace ix return true; } // There is possibly something to be read, try again - else if (ret < 0 && (getErrno() == EWOULDBLOCK || - getErrno() == EAGAIN)) + else if (ret < 0 && Socket::isWaitNeeded()) { // Wait with a 1ms timeout until the socket is ready to read. // This way we are not busy looping @@ -317,13 +334,12 @@ namespace ix size_t size = std::min(kChunkSize, length - output.size()); ssize_t ret = recv((char*)&_readBuffer[0], size); - if (ret <= 0 && (getErrno() != EWOULDBLOCK && - getErrno() != EAGAIN)) + if (ret <= 0 && !Socket::isWaitNeeded()) { // Error return std::make_pair(false, std::string()); } - else if (ret > 0) + else { output.insert(output.end(), _readBuffer.begin(), diff --git a/ixwebsocket/IXSocket.h b/ixwebsocket/IXSocket.h index 0167f5b7..db047f52 100644 --- a/ixwebsocket/IXSocket.h +++ b/ixwebsocket/IXSocket.h @@ -41,8 +41,6 @@ namespace ix virtual ~Socket(); bool init(std::string& errorMsg); - void configure(); - // Functions to check whether there is activity on the socket PollResultType poll(int timeoutSecs = kDefaultPollTimeout); bool wakeUpFromPoll(uint8_t wakeUpCode); @@ -76,6 +74,7 @@ namespace ix const CancellationRequest& isCancellationRequested); static int getErrno(); + static bool isWaitNeeded(); // Used as special codes for pipe communication static const uint64_t kSendRequest; diff --git a/ixwebsocket/IXSocketServer.cpp b/ixwebsocket/IXSocketServer.cpp index f72f363e..0a7966e9 100644 --- a/ixwebsocket/IXSocketServer.cpp +++ b/ixwebsocket/IXSocketServer.cpp @@ -247,7 +247,7 @@ namespace ix if ((clientFd = accept(_serverFd, (struct sockaddr *)&client, &addressLen)) < 0) { - if (Socket::getErrno() != EWOULDBLOCK) + if (!Socket::isWaitNeeded()) { // FIXME: that error should be propagated std::stringstream ss; diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 455702d6..91c89241 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -296,8 +296,7 @@ namespace ix { ssize_t ret = _socket->recv((char*)&_readbuf[0], _readbuf.size()); - if (ret < 0 && (_socket->getErrno() == EWOULDBLOCK || - _socket->getErrno() == EAGAIN)) + if (ret < 0 && Socket::isWaitNeeded()) { break; } @@ -844,8 +843,7 @@ namespace ix { ssize_t ret = _socket->send((char*)&_txbuf[0], _txbuf.size()); - if (ret < 0 && (_socket->getErrno() == EWOULDBLOCK || - _socket->getErrno() == EAGAIN)) + if (ret < 0 && Socket::isWaitNeeded()) { break; } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 51ad5bfd..f1038ae9 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -8,12 +8,11 @@ project (ixwebsocket_unittest) set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/../third_party/sanitizers-cmake/cmake" ${CMAKE_MODULE_PATH}) find_package(Sanitizers) -set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") -set(CMAKE_LD_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") - set (CMAKE_CXX_STANDARD 14) if (NOT WIN32) + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") + set(CMAKE_LD_FLAGS "${CMAKE_CXX_FLAGS} -fsanitize=thread") option(USE_TLS "Add TLS support" ON) endif()