This commit is contained in:
tqchen 2015-05-28 12:37:06 -07:00
parent fd8920c71d
commit ceedf4ea96
2 changed files with 20 additions and 4 deletions

View File

@ -3,7 +3,7 @@ export CXX = g++
endif endif
export MPICXX = mpicxx export MPICXX = mpicxx
export LDFLAGS= -Llib -lrt 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) export CFLAGS = -O3 -msse2 $(WARNFLAGS)
ifndef WITH_FPIC ifndef WITH_FPIC

View File

@ -94,11 +94,23 @@ class Socket {
inline operator SOCKET() const { inline operator SOCKET() const {
return sockfd; return sockfd;
} }
/*!
* \return last error of socket operation
*/
inline static int GetLastError(void) { inline static int GetLastError(void) {
#ifdef _WIN32 #ifdef _WIN32
return WSAGetLastError(); return WSAGetLastError();
#else #else
return errno; 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 #endif
} }
/*! /*!
@ -223,8 +235,12 @@ class Socket {
} }
// report an socket error // report an socket error
inline static void Error(const char *msg) { 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)); utils::Error("Socket %s Error:%s", msg, strerror(errsv));
#endif
} }
protected: protected:
@ -337,7 +353,7 @@ class TCPSocket : public Socket{
while (ndone < len) { while (ndone < len) {
ssize_t ret = send(sockfd, buf, static_cast<ssize_t>(len - ndone), 0); ssize_t ret = send(sockfd, buf, static_cast<ssize_t>(len - ndone), 0);
if (ret == -1) { if (ret == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) return ndone; if (LastErrorWouldBlock()) return ndone;
Socket::Error("SendAll"); Socket::Error("SendAll");
} }
buf += ret; buf += ret;
@ -359,7 +375,7 @@ class TCPSocket : public Socket{
ssize_t ret = recv(sockfd, buf, ssize_t ret = recv(sockfd, buf,
static_cast<sock_size_t>(len - ndone), MSG_WAITALL); static_cast<sock_size_t>(len - ndone), MSG_WAITALL);
if (ret == -1) { if (ret == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) return ndone; if (LastErrorWouldBlock()) return ndone;
Socket::Error("RecvAll"); Socket::Error("RecvAll");
} }
if (ret == 0) return ndone; if (ret == 0) return ndone;