19 #ifndef _UTIL_PLATFORM_OS_UNIX_COMMON_HPP_ 20 #define _UTIL_PLATFORM_OS_UNIX_COMMON_HPP_ 22 #if defined(sun) || defined(__sun) || defined(__sun__) 23 #define BSD_COMP // Needs to be defined in order to use FIONBIO in ioctl() 24 #define MSG_NOSIGNAL 0 // MSG_NOSIGNAL flag for send() not implemented on Solaris 33 #include <sys/types.h> 34 #include <sys/socket.h> 35 #include <sys/ioctl.h> 36 #include <netinet/in.h> 37 #include <arpa/inet.h> 61 #define INADDR_NONE ((unsigned long) -1) 68 inline void OutputDebugString(
const char *sz)
71 #ifdef OUTPUTDEBUGSTRING_TO_STDERR 72 fprintf(stderr,
"%s", sz);
75 #ifdef OUTPUTDEBUGSTRING_TO_STDOUT 76 fprintf(stdout,
"%s", sz);
79 #ifdef OUTPUTDEBUGSTRING_TO_FILE 80 static FILE *file = fopen(
"OutputDebugString.txt",
"w" );
81 fprintf(file,
"%s", sz);
86 inline std::string GetErrorString(
int err) {
return std::string( strerror(err) ); }
87 inline std::string GetErrorString() {
return GetErrorString( errno ); }
89 inline void Sleep(
unsigned int seconds) { ::sleep(seconds); }
93 inline int recv(
int fd,
char *buf,
int len,
int flags)
95 return ::recv(fd, buf, len, flags);
98 inline int send(
int fd,
const char *buf,
int len,
int flags)
100 return ::send(fd, buf, len, flags | MSG_NOSIGNAL);
103 inline int select(
int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
const struct timeval *timeout)
105 return ::select( nfds, readfds, writefds, exceptfds, const_cast<struct timeval *>(timeout) );
108 inline int accept(
int fd, sockaddr *addr,
int *addrlen)
110 socklen_t addrlen_ = *addrlen;
111 int ret = ::accept(fd, addr, &addrlen_);
116 inline int connect(
int fd,
const sockaddr *name,
int namelen)
118 return ::connect(fd, name, namelen);
121 inline int closesocket(
int fd)
126 inline void setblocking(SOCKET fd,
bool bBlocking)
128 int arg = bBlocking ? 0 : 1;
129 ::ioctl(fd, FIONBIO, &arg);
132 inline int GetLastError()
137 static const int ERR_EWOULDBLOCK = EWOULDBLOCK;
138 static const int ERR_EINPROGRESS = EINPROGRESS;
139 static const int ERR_ECONNRESET = ECONNRESET;
140 static const int ERR_ECONNABORTED = ECONNABORTED;
141 static const int ERR_ECONNREFUSED = ECONNREFUSED;