From ceedf4ea964c4c60f469c3b0be4d6b62e6157b34 Mon Sep 17 00:00:00 2001 From: tqchen Date: Thu, 28 May 2015 12:37:06 -0700 Subject: [PATCH] fix --- Makefile | 2 +- src/socket.h | 22 +++++++++++++++++++--- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 2260148de..d2a642c2d 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ export CXX = g++ endif export MPICXX = mpicxx export LDFLAGS= -Llib -lrt -export WARNFLAGS= -Wall -Wextra -Wno-unused-parameter -Wno-unknown-pragmas -pedantic +export WARNFLAGS= -Wall -Wextra -Wno-unused-parameter -Wno-unknown-pragmas export CFLAGS = -O3 -msse2 $(WARNFLAGS) ifndef WITH_FPIC diff --git a/src/socket.h b/src/socket.h index 4a00016f8..49fbcb3e7 100644 --- a/src/socket.h +++ b/src/socket.h @@ -94,11 +94,23 @@ class Socket { inline operator SOCKET() const { return sockfd; } + /*! + * \return last error of socket operation + */ inline static int GetLastError(void) { #ifdef _WIN32 return WSAGetLastError(); #else return errno; +#endif + } + /*! \return whether last error was would block */ + inline static bool LastErrorWouldBlock(void) { + int errsv = GetLastError(); +#ifdef _WIN32 + return errsv == WSAEWOULDBLOCK; +#else + return errsv == EAGAIN || errsv == EWOULDBLOCK; #endif } /*! @@ -223,8 +235,12 @@ class Socket { } // report an socket error inline static void Error(const char *msg) { - int errsv = errno; + int errsv = GetLastError(); +#ifdef _WIN32 + utils::Error("Socket %s Error:WSAError-code=%d", msg, errsv); +#else utils::Error("Socket %s Error:%s", msg, strerror(errsv)); +#endif } protected: @@ -337,7 +353,7 @@ class TCPSocket : public Socket{ while (ndone < len) { ssize_t ret = send(sockfd, buf, static_cast(len - ndone), 0); if (ret == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) return ndone; + if (LastErrorWouldBlock()) return ndone; Socket::Error("SendAll"); } buf += ret; @@ -359,7 +375,7 @@ class TCPSocket : public Socket{ ssize_t ret = recv(sockfd, buf, static_cast(len - ndone), MSG_WAITALL); if (ret == -1) { - if (errno == EAGAIN || errno == EWOULDBLOCK) return ndone; + if (LastErrorWouldBlock()) return ndone; Socket::Error("RecvAll"); } if (ret == 0) return ndone;