18 #ifndef _UTIL_PLATFORM_OS_UNIX_COMMON_HPP_ 19 #define _UTIL_PLATFORM_OS_UNIX_COMMON_HPP_ 21 #if defined(sun) || defined(__sun) || defined(__sun__) 22 #define BSD_COMP // Needs to be defined in order to use FIONBIO in ioctl() 23 #define MSG_NOSIGNAL 0 // MSG_NOSIGNAL flag for send() not implemented on Solaris 32 #include <sys/types.h> 33 #include <sys/socket.h> 34 #include <sys/ioctl.h> 35 #include <netinet/in.h> 36 #include <arpa/inet.h> 60 #define INADDR_NONE ((unsigned long) -1) 67 inline void OutputDebugString(
const char *sz)
70 #ifdef OUTPUTDEBUGSTRING_TO_STDERR 71 fprintf(stderr,
"%s", sz);
74 #ifdef OUTPUTDEBUGSTRING_TO_STDOUT 75 fprintf(stdout,
"%s", sz);
78 #ifdef OUTPUTDEBUGSTRING_TO_FILE 79 static FILE *file = fopen(
"OutputDebugString.txt",
"w" );
80 fprintf(file,
"%s", sz);
85 inline std::string GetErrorString(
int err) {
return std::string( strerror(err) ); }
86 inline std::string GetErrorString() {
return GetErrorString( errno ); }
88 inline void Sleep(
unsigned int seconds) { ::sleep(seconds); }
92 inline int recv(
int fd,
char *buf,
int len,
int flags)
94 return ::recv(fd, buf, len, flags);
97 inline int send(
int fd,
const char *buf,
int len,
int flags)
99 return ::send(fd, buf, len, flags | MSG_NOSIGNAL);
102 inline int select(
int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
const struct timeval *timeout)
104 return ::select( nfds, readfds, writefds, exceptfds, const_cast<struct timeval *>(timeout) );
107 inline int accept(
int fd, sockaddr *addr,
int *addrlen)
109 socklen_t addrlen_ = *addrlen;
110 int ret = ::accept(fd, addr, &addrlen_);
115 inline int connect(
int fd,
const sockaddr *name,
int namelen)
117 return ::connect(fd, name, namelen);
120 inline int closesocket(
int fd)
125 inline void setblocking(SOCKET fd,
bool bBlocking)
127 int arg = bBlocking ? 0 : 1;
128 ::ioctl(fd, FIONBIO, &arg);
131 inline int GetLastError()
136 static const int ERR_EWOULDBLOCK = EWOULDBLOCK;
137 static const int ERR_EINPROGRESS = EINPROGRESS;
138 static const int ERR_ECONNRESET = ECONNRESET;
139 static const int ERR_ECONNABORTED = ECONNABORTED;
140 static const int ERR_ECONNREFUSED = ECONNREFUSED;