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