Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
tqchen committed May 28, 2015
1 parent fd8920c commit ceedf4e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
22 changes: 19 additions & 3 deletions src/socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
/*!
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -337,7 +353,7 @@ class TCPSocket : public Socket{
while (ndone < len) {
ssize_t ret = send(sockfd, buf, static_cast<ssize_t>(len - ndone), 0);
if (ret == -1) {
if (errno == EAGAIN || errno == EWOULDBLOCK) return ndone;
if (LastErrorWouldBlock()) return ndone;
Socket::Error("SendAll");
}
buf += ret;
Expand All @@ -359,7 +375,7 @@ class TCPSocket : public Socket{
ssize_t ret = recv(sockfd, buf,
static_cast<sock_size_t>(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;
Expand Down

0 comments on commit ceedf4e

Please sign in to comment.