Remote Call Framework 3.1
Unix_Common.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2019, Delta V Software. All rights reserved.
6 // http://www.deltavsoft.com
7 //
8 // RCF is distributed under dual licenses - closed source or GPL.
9 // Consult your particular license for conditions of use.
10 //
11 // If you have not purchased a commercial license, you are using RCF
12 // under GPL terms.
13 //
14 // Version: 3.1
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef _UTIL_PLATFORM_OS_UNIX_COMMON_HPP_
20 #define _UTIL_PLATFORM_OS_UNIX_COMMON_HPP_
21 
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
25 #endif
26 
27 #include <iostream>
28 #include <stdio.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/uio.h>
32 #include <sys/stat.h>
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>
38 #include <netdb.h>
39 
40 #include <cerrno>
41 
42 #ifndef __UNIX__
43 #define __UNIX__
44 #endif
45 
46 // O_BINARY is not defined in Unix (all files are "binary" anyway)
47 #ifndef O_BINARY
48 #define O_BINARY 0
49 #endif
50 
51 //#ifndef SOCKET
52 //#define SOCKET int
53 //#endif
54 //
55 //#ifndef INVALID_SOCKET
56 //#define INVALID_SOCKET -1
57 //#endif
58 
59 // Solaris doesn't define INADDR_NONE, for some reason
60 #ifndef INADDR_NONE
61 #define INADDR_NONE ((unsigned long) -1)
62 #endif
63 
64 namespace Platform {
65 
66  namespace OS {
67 
68  inline void OutputDebugString(const char *sz)
69  {
70 
71 #ifdef OUTPUTDEBUGSTRING_TO_STDERR
72  fprintf(stderr, "%s", sz);
73 #endif
74 
75 #ifdef OUTPUTDEBUGSTRING_TO_STDOUT
76  fprintf(stdout, "%s", sz);
77 #endif
78 
79 #ifdef OUTPUTDEBUGSTRING_TO_FILE
80  static FILE *file = fopen( "OutputDebugString.txt", "w" );
81  fprintf(file, "%s", sz);
82 #endif
83 
84  }
85 
86  inline std::string GetErrorString(int err) { return std::string( strerror(err) ); }
87  inline std::string GetErrorString() { return GetErrorString( errno ); }
88 
89  inline void Sleep(unsigned int seconds) { ::sleep(seconds); }
90 
91  namespace Socket {
92 
93  inline int recv(int fd, char *buf, int len, int flags)
94  {
95  return ::recv(fd, buf, len, flags);
96  }
97 
98  inline int send(int fd, const char *buf, int len, int flags)
99  {
100  return ::send(fd, buf, len, flags | MSG_NOSIGNAL);
101  }
102 
103  inline int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
104  {
105  return ::select( nfds, readfds, writefds, exceptfds, const_cast<struct timeval *>(timeout) );
106  }
107 
108  inline int accept(int fd, sockaddr *addr, int *addrlen)
109  {
110  socklen_t addrlen_ = *addrlen;
111  int ret = ::accept(fd, addr, &addrlen_);
112  *addrlen = addrlen_;
113  return ret;
114  }
115 
116  inline int connect(int fd, const sockaddr *name, int namelen)
117  {
118  return ::connect(fd, name, namelen);
119  }
120 
121  inline int closesocket(int fd)
122  {
123  return ::close(fd);
124  }
125 
126  inline void setblocking(SOCKET fd, bool bBlocking)
127  {
128  int arg = bBlocking ? 0 : 1;
129  ::ioctl(fd, FIONBIO, &arg);
130  }
131 
132  inline int GetLastError()
133  {
134  return errno;
135  }
136 
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;
142 
143  } //namespace Socket
144 
145  } // namespace OS;
146 
147 } // namespace Platform
148 
149 
150 #endif
Definition: Unix/BsdSockets.hpp:80