From 78ca72b9c709cd7a9c68c8db4b11427616022293 Mon Sep 17 00:00:00 2001 From: tqchen Date: Sun, 23 Nov 2014 21:34:15 -0800 Subject: [PATCH] start work on win --- src/utils/socket.h | 36 +++++++++++++++++------------------- 1 file changed, 17 insertions(+), 19 deletions(-) diff --git a/src/utils/socket.h b/src/utils/socket.h index eb23a5908..104ebaef2 100644 --- a/src/utils/socket.h +++ b/src/utils/socket.h @@ -26,8 +26,8 @@ namespace utils { #if defined(_WIN32) typedef int ssize_t; #else -typedef int SOCKET; -const int INVALID_SOCKET = -1; +typedef int sock_t; +const int INVALID_sock_t = -1; #endif /*! \brief data structure for network address */ @@ -74,18 +74,18 @@ struct SockAddr { class TCPSocket { public: /*! \brief the file descriptor of socket */ - SOCKET sockfd; + sock_t sockfd; // constructor - TCPSocket(void) : sockfd(INVALID_SOCKET) { + TCPSocket(void) : sockfd(INVALID_sock_t) { } - explicit TCPSocket(SOCKET sockfd) : sockfd(sockfd) { + explicit TCPSocket(sock_t sockfd) : sockfd(sockfd) { } ~TCPSocket(void) { // do nothing in destructor // user need to take care of close } // default conversion to int - inline operator SOCKET() const { + inline operator sock_t() const { return sockfd; } /*! @@ -94,7 +94,7 @@ class TCPSocket { */ inline void Create(int af = PF_INET) { sockfd = socket(PF_INET, SOCK_STREAM, 0); - if (sockfd == INVALID_SOCKET) { + if (sockfd == INVALID_sock_t) { SockError("Create"); } } @@ -118,7 +118,7 @@ class TCPSocket { #ifdef _WIN32 u_long mode = non_block ? 1 : 0; if (ioctlsocket(sockfd, FIONBIO, &mode) != NO_ERROR) { - SockError("SetNonBlock", WSAGetLastError()); + SockError("SetNonBlock"); } #else int flag = fcntl(sockfd, F_GETFL, 0); @@ -144,8 +144,8 @@ class TCPSocket { } /*! \brief get a new connection */ TCPSocket Accept(void) { - SOCKET newfd = accept(sockfd, NULL, NULL); - if (newfd == INVALID_SOCKET) { + sock_t newfd = accept(sockfd, NULL, NULL); + if (newfd == INVALID_sock_t) { SockError("Accept"); } return TCPSocket(newfd); @@ -195,7 +195,7 @@ class TCPSocket { #else close(sockfd); #endif - sockfd = INVALID_SOCKET; + sockfd = INVALID_sock_t; } else { Error("TCPSocket::Close double close the socket or close without create"); } @@ -280,8 +280,7 @@ class TCPSocket { // report an socket error inline static void SockError(const char *msg) { int errsv = errno; - char buf[256]; - Error("Socket %s Error:%s", msg, strerror_r(errsv, buf, sizeof(buf))); + Error("Socket %s Error:%s", msg, strerror(errsv)); } }; /*! \brief helper data structure to perform select */ @@ -294,7 +293,7 @@ struct SelectHelper { * \brief add file descriptor to watch for read * \param fd file descriptor to be watched */ - inline void WatchRead(int fd) { + inline void WatchRead(sock_t fd) { read_fds.push_back(fd); if (fd > maxfd) maxfd = fd; } @@ -302,7 +301,7 @@ struct SelectHelper { * \brief add file descriptor to watch for write * \param fd file descriptor to be watched */ - inline void WatchWrite(int fd) { + inline void WatchWrite(sock_t fd) { write_fds.push_back(fd); if (fd > maxfd) maxfd = fd; } @@ -310,14 +309,14 @@ struct SelectHelper { * \brief Check if the descriptor is ready for read * \param fd file descriptor to check status */ - inline bool CheckRead(int fd) const { + inline bool CheckRead(sock_t fd) const { return FD_ISSET(fd, &read_set); } /*! * \brief Check if the descriptor is ready for write * \param fd file descriptor to check status */ - inline bool CheckWrite(int fd) const { + inline bool CheckWrite(sock_t fd) const { return FD_ISSET(fd, &write_set); } /*! @@ -353,8 +352,7 @@ struct SelectHelper { } if (ret == -1) { int errsv = errno; - char buf[256]; - Error("Select Error: %s", strerror_r(errsv, buf, sizeof(buf))); + Error("Select Error: %s", strerror(errsv)); } return ret; }