RCFProto

- Main Page
- Classes
- Files

- File List

All Classes Functions Typedefs
Unix/BsdSockets.hpp
1
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2013, 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: 2.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18
19 #ifndef INCLUDE_UTIL_PLATFORM_OS_UNIX_BSDSOCKETS_HPP
20 #define INCLUDE_UTIL_PLATFORM_OS_UNIX_BSDSOCKETS_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 #if defined(__MACH__) && defined(__APPLE__)
28
29 // Incompatibilites in the 10.2.8 and 10.3.9 headers
30 #ifndef _BSD_SOCKLEN_T_
31 #define _BSD_SOCKLEN_T_ int32_t
32 #endif
33
34 #if defined( __GNUC__ ) && !defined(MSG_NOSIGNAL)
35 #define MSG_NOSIGNAL 0
36 #endif
37
38 #endif // defined(__MACH__) && defined(__APPLE__)
39
40 #include <stdio.h>
41 #include <unistd.h>
42 #include <fcntl.h>
43 #include <sys/uio.h>
44 #include <sys/stat.h>
45 #include <sys/types.h>
46 #include <sys/socket.h>
47 #include <sys/ioctl.h>
48 #include <netinet/in.h>
49 #include <arpa/inet.h>
50 #include <netdb.h>
51 #include <signal.h>
52
53 #include <cerrno>
54
55 #include <string>
56 #include <string.h>
57
58 #ifndef __UNIX__
59 #define __UNIX__
60 #endif
61
62 // O_BINARY is not defined in Unix (all files are "binary" anyway)
63 #ifndef O_BINARY
64 #define O_BINARY 0
65 #endif
66
67 #ifndef SOCKET
68 #define SOCKET int
69 #endif
70
71 #ifndef INVALID_SOCKET
72 #define INVALID_SOCKET -1
73 #endif
74
75 // Solaris doesn't define INADDR_NONE, for some reason
76 #ifndef INADDR_NONE
77 #define INADDR_NONE ((unsigned long) -1)
78 #endif
79
80 namespace Platform {
81
82 namespace OS {
83
84 inline std::string GetErrorString(int err) { return std::string( strerror(err) ); }
85
86 inline std::string GetErrorString() { return GetErrorString( errno ); }
87
88 namespace BsdSockets {
89
90 typedef ::socklen_t socklen_t;
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 sendto(int fd, const char *buf, int len, int flags, const sockaddr *to, int tolen)
103 {
104 return ::sendto(fd, buf, len, flags, to, tolen);
105 }
106
107 inline int recvfrom(int fd, char *buf, int len, int flags, sockaddr *from, int *fromlen)
108 {
109 if (from)
110 {
111 socklen_t fromlen_ = *fromlen;
112 int ret = ::recvfrom(fd, buf, len, flags, from, &fromlen_);
113 *fromlen = fromlen_;
114 return ret;
115 }
116 else
117 {
118 return ::recvfrom(fd, buf, len, flags, NULL, NULL);
119 }
120 }
121
122 inline int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, const struct timeval *timeout)
123 {
124 return ::select( nfds, readfds, writefds, exceptfds, const_cast<struct timeval *>(timeout) );
125 }
126
127 inline int accept(int fd, sockaddr *addr, int *addrlen)
128 {
129 socklen_t addrlen_ = *addrlen;
130 int ret = ::accept(fd, addr, &addrlen_);
131 *addrlen = addrlen_;
132 return ret;
133 }
134
135 inline int connect(int fd, const sockaddr *name, int namelen)
136 {
137 return ::connect(fd, name, namelen);
138 }
139
140 inline int closesocket(int fd)
141 {
142 return ::close(fd);
143 }
144
145 inline void setblocking(SOCKET fd, bool bBlocking)
146 {
147 int arg = bBlocking ? 0 : 1;
148 ::ioctl(fd, FIONBIO, &arg);
149 }
150
151 inline int GetLastError()
152 {
153 return errno;
154 }
155
156 #if (defined(__MACH__) && defined(__APPLE__)) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__)
157
158 inline void disableBrokenPipeSignals()
159 {
160 signal(SIGPIPE, SIG_IGN);
161 }
162
163 #else
164
165 inline void disableBrokenPipeSignals()
166 {
167 sigignore(SIGPIPE);
168 }
169
170 #endif
171
172 static const int ERR_EWOULDBLOCK = EWOULDBLOCK;
173 static const int ERR_EINPROGRESS = EINPROGRESS;
174 static const int ERR_ECONNRESET = ECONNRESET;
175 static const int ERR_ECONNABORTED = ECONNABORTED;
176 static const int ERR_ECONNREFUSED = ECONNREFUSED;
177 static const int ERR_EMSGSIZE = EMSGSIZE;
178 static const int ERR_EADDRINUSE = EADDRINUSE;
179
180 } //namespace BsdSockets
181
182 } // namespace OS;
183
184 } // namespace Platform
185
186 #endif