diff --git a/ixwebsocket/IXSocket.cpp b/ixwebsocket/IXSocket.cpp index 3917100d..597f3bf9 100644 --- a/ixwebsocket/IXSocket.cpp +++ b/ixwebsocket/IXSocket.cpp @@ -6,22 +6,26 @@ #include "IXSocket.h" -#include -#include #include #include #include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include #include +#include +#include +#include + +#ifdef _WIN32 +# include +#else +# include +# include +# include +# include +# include +# include +# include +# include +#endif // // Linux/Android has a special type of virtual files. select(2) will react @@ -35,7 +39,7 @@ // cf Android/Kernel table here // https://android.stackexchange.com/questions/51651/which-android-runs-which-linux-kernel // -#ifndef __APPLE__ +#ifdef __linux__ # include #endif @@ -51,7 +55,7 @@ namespace ix _sockfd(-1), _eventfd(-1) { -#ifndef __APPLE__ +#ifdef __linux__ _eventfd = eventfd(0, 0); assert(_eventfd != -1 && "Panic - eventfd not functioning on this platform"); #endif @@ -61,7 +65,7 @@ namespace ix { close(); -#ifndef __APPLE__ +#ifdef __linux__ ::close(_eventfd); #endif } @@ -163,7 +167,7 @@ namespace ix FD_ZERO(&rfds); FD_SET(_sockfd, &rfds); -#ifndef __APPLE__ +#ifdef __linux__ FD_SET(_eventfd, &rfds); #endif @@ -191,7 +195,7 @@ namespace ix { #ifdef __APPLE__ wakeUpFromPollApple(); -#else +#elif defined(__linux__) wakeUpFromPollLinux(); #endif } @@ -202,7 +206,7 @@ namespace ix { std::lock_guard lock(_socketMutex); -#ifndef __APPLE__ +#ifdef __linux__ if (_eventfd == -1) { return false; // impossible to use this socket if eventfd is broken @@ -248,4 +252,12 @@ namespace ix return (int) ::recv(_sockfd, buffer, length, flags); } + int Socket::getErrno() const + { +#ifdef _WIN32 + return WSAGetLastError(); +#else + return errno; +#endif + } } diff --git a/ixwebsocket/IXSocket.h b/ixwebsocket/IXSocket.h index 8084e219..9dff2d93 100644 --- a/ixwebsocket/IXSocket.h +++ b/ixwebsocket/IXSocket.h @@ -38,6 +38,8 @@ namespace ix virtual int send(const std::string& buffer); virtual int recv(void* buffer, size_t length); + int getErrno() const; + protected: void wakeUpFromPollApple(); void wakeUpFromPollLinux(); diff --git a/ixwebsocket/IXWebSocketTransport.cpp b/ixwebsocket/IXWebSocketTransport.cpp index 5095077c..41efedd7 100644 --- a/ixwebsocket/IXWebSocketTransport.cpp +++ b/ixwebsocket/IXWebSocketTransport.cpp @@ -20,7 +20,6 @@ #endif #include -#include #include #include @@ -277,8 +276,8 @@ namespace ix { _rxbuf.resize(N + 1500); ret = _socket->recv((char*)&_rxbuf[0] + N, 1500); - if (ret < 0 && (errno == EWOULDBLOCK || - errno == EAGAIN)) { + if (ret < 0 && (_socket->getErrno() == EWOULDBLOCK || + _socket->getErrno() == EAGAIN)) { _rxbuf.resize(N); break; } @@ -575,8 +574,8 @@ namespace ix { { int ret = _socket->send((char*)&_txbuf[0], _txbuf.size()); - if (ret < 0 && (errno == EWOULDBLOCK || - errno == EAGAIN)) + if (ret < 0 && (_socket->getErrno() == EWOULDBLOCK || + _socket->getErrno() == EAGAIN)) { break; }