This commit is contained in:
tqchen 2014-11-23 21:36:22 -08:00
parent 78ca72b9c7
commit 77ffd0465b

View File

@ -26,8 +26,8 @@ namespace utils {
#if defined(_WIN32) #if defined(_WIN32)
typedef int ssize_t; typedef int ssize_t;
#else #else
typedef int sock_t; typedef int SOCKET;
const int INVALID_sock_t = -1; const int INVALID_SOCKET = -1;
#endif #endif
/*! \brief data structure for network address */ /*! \brief data structure for network address */
@ -74,18 +74,18 @@ struct SockAddr {
class TCPSocket { class TCPSocket {
public: public:
/*! \brief the file descriptor of socket */ /*! \brief the file descriptor of socket */
sock_t sockfd; SOCKET sockfd;
// constructor // constructor
TCPSocket(void) : sockfd(INVALID_sock_t) { TCPSocket(void) : sockfd(INVALID_SOCKET) {
} }
explicit TCPSocket(sock_t sockfd) : sockfd(sockfd) { explicit TCPSocket(SOCKET sockfd) : sockfd(sockfd) {
} }
~TCPSocket(void) { ~TCPSocket(void) {
// do nothing in destructor // do nothing in destructor
// user need to take care of close // user need to take care of close
} }
// default conversion to int // default conversion to int
inline operator sock_t() const { inline operator SOCKET() const {
return sockfd; return sockfd;
} }
/*! /*!
@ -94,7 +94,7 @@ class TCPSocket {
*/ */
inline void Create(int af = PF_INET) { inline void Create(int af = PF_INET) {
sockfd = socket(PF_INET, SOCK_STREAM, 0); sockfd = socket(PF_INET, SOCK_STREAM, 0);
if (sockfd == INVALID_sock_t) { if (sockfd == INVALID_SOCKET) {
SockError("Create"); SockError("Create");
} }
} }
@ -144,8 +144,8 @@ class TCPSocket {
} }
/*! \brief get a new connection */ /*! \brief get a new connection */
TCPSocket Accept(void) { TCPSocket Accept(void) {
sock_t newfd = accept(sockfd, NULL, NULL); SOCKET newfd = accept(sockfd, NULL, NULL);
if (newfd == INVALID_sock_t) { if (newfd == INVALID_SOCKET) {
SockError("Accept"); SockError("Accept");
} }
return TCPSocket(newfd); return TCPSocket(newfd);
@ -195,7 +195,7 @@ class TCPSocket {
#else #else
close(sockfd); close(sockfd);
#endif #endif
sockfd = INVALID_sock_t; sockfd = INVALID_SOCKET;
} else { } else {
Error("TCPSocket::Close double close the socket or close without create"); Error("TCPSocket::Close double close the socket or close without create");
} }
@ -293,7 +293,7 @@ struct SelectHelper {
* \brief add file descriptor to watch for read * \brief add file descriptor to watch for read
* \param fd file descriptor to be watched * \param fd file descriptor to be watched
*/ */
inline void WatchRead(sock_t fd) { inline void WatchRead(SOCKET fd) {
read_fds.push_back(fd); read_fds.push_back(fd);
if (fd > maxfd) maxfd = fd; if (fd > maxfd) maxfd = fd;
} }
@ -301,7 +301,7 @@ struct SelectHelper {
* \brief add file descriptor to watch for write * \brief add file descriptor to watch for write
* \param fd file descriptor to be watched * \param fd file descriptor to be watched
*/ */
inline void WatchWrite(sock_t fd) { inline void WatchWrite(SOCKET fd) {
write_fds.push_back(fd); write_fds.push_back(fd);
if (fd > maxfd) maxfd = fd; if (fd > maxfd) maxfd = fd;
} }
@ -309,14 +309,14 @@ struct SelectHelper {
* \brief Check if the descriptor is ready for read * \brief Check if the descriptor is ready for read
* \param fd file descriptor to check status * \param fd file descriptor to check status
*/ */
inline bool CheckRead(sock_t fd) const { inline bool CheckRead(SOCKET fd) const {
return FD_ISSET(fd, &read_set); return FD_ISSET(fd, &read_set);
} }
/*! /*!
* \brief Check if the descriptor is ready for write * \brief Check if the descriptor is ready for write
* \param fd file descriptor to check status * \param fd file descriptor to check status
*/ */
inline bool CheckWrite(sock_t fd) const { inline bool CheckWrite(SOCKET fd) const {
return FD_ISSET(fd, &write_set); return FD_ISSET(fd, &write_set);
} }
/*! /*!