Remote Call Framework 3.0
Windows/BsdSockets.hpp
1 
2 //******************************************************************************
3 // RCF - Remote Call Framework
4 //
5 // Copyright (c) 2005 - 2018, 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.0
15 // Contact: support <at> deltavsoft.com
16 //
17 //******************************************************************************
18 
19 #ifndef INCLUDE_UTIL_PLATFORM_OS_WINDOWS_BSDSOCKETS_HPP
20 #define INCLUDE_UTIL_PLATFORM_OS_WINDOWS_BSDSOCKETS_HPP
21 
22 // If none of these are defined, then Winsock thinks its WIN16 and redefines error messages etc.
23 #if !defined(WIN16) && !defined(WIN32) && !defined(_WIN64)
24 #define WIN32
25 #endif
26 
27 #include <winsock2.h>
28 #include <mswsock.h>
29 #include <ws2tcpip.h>
30 
31 #include <stdio.h>
32 #include <io.h>
33 #include <fcntl.h>
34 #include <sys/stat.h>
35 #include <assert.h>
36 
37 #include <string>
38 
39 #ifndef __WINDOWS__
40 #define __WINDOWS__
41 #endif
42 
43 #if !defined(NDEBUG) && !defined(_DEBUG)
44 #define _DEBUG
45 #endif
46 
47 // compensate for some things lacking in mingw's platform headers
48 #ifdef __MINGW32__
49 
50 #ifndef WSAID_ACCEPTEX
51 
52 typedef
53 BOOL
54 (PASCAL FAR * LPFN_ACCEPTEX)(
55  IN SOCKET sListenSocket,
56  IN SOCKET sAcceptSocket,
57  IN PVOID lpOutputBuffer,
58  IN DWORD dwReceiveDataLength,
59  IN DWORD dwLocalAddressLength,
60  IN DWORD dwRemoteAddressLength,
61  OUT LPDWORD lpdwBytesReceived,
62  IN LPOVERLAPPED lpOverlapped
63 );
64 
65 #define WSAID_ACCEPTEX \
66 {0xb5367df1,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
67 
68 #endif // ! WSAID_ACCEPTEX
69 
70 #ifndef WSAID_GETACCEPTEXSOCKADDRS
71 
72 typedef
73 VOID
74 (PASCAL FAR * LPFN_GETACCEPTEXSOCKADDRS)(
75  IN PVOID lpOutputBuffer,
76  IN DWORD dwReceiveDataLength,
77  IN DWORD dwLocalAddressLength,
78  IN DWORD dwRemoteAddressLength,
79  OUT struct sockaddr **LocalSockaddr,
80  OUT LPINT LocalSockaddrLength,
81  OUT struct sockaddr **RemoteSockaddr,
82  OUT LPINT RemoteSockaddrLength
83  );
84 
85 #define WSAID_GETACCEPTEXSOCKADDRS \
86 {0xb5367df2,0xcbac,0x11cf,{0x95,0xca,0x00,0x80,0x5f,0x48,0xa1,0x92}}
87 
88 #endif // ! WSAID_GETACCEPTEXSOCKADDRS
89 
90 #endif // __MINGW32__
91 
92 
93 namespace Platform {
94 
95  namespace OS {
96 
97  inline std::string GetErrorString(int err)
98  {
99  std::string errorString = "Error string lookup failed";
100  LPVOID lpMsgBuf;
101  if (FormatMessageA(
102  FORMAT_MESSAGE_ALLOCATE_BUFFER |
103  FORMAT_MESSAGE_FROM_SYSTEM |
104  FORMAT_MESSAGE_IGNORE_INSERTS,
105  NULL,
106  err,
107  MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
108  (char *) &lpMsgBuf,
109  0,
110  NULL ))
111  {
112  errorString = (const char *) lpMsgBuf;
113  LocalFree( lpMsgBuf );
114  }
115 
116  // strip trailing newline characters
117  while (
118  !errorString.empty() &&
119  errorString.at(errorString.size()-1) <= 13)
120  {
121  errorString = errorString.substr(0, errorString.size()-1);
122  }
123 
124  return errorString;
125  }
126 
127  inline std::string GetErrorString() { return GetErrorString( ::GetLastError() ); }
128 
129  namespace BsdSockets {
130 
131  typedef int socklen_t;
132 
133  inline int recv(int fd, char *buf, int len, int flags)
134  {
135  return ::recv(fd, buf, len, flags);
136  }
137 
138  inline int send(int fd, const char *buf, int len, int flags)
139  {
140  return ::send(fd, buf, len, flags);
141  }
142 
143  inline int sendto(int fd, const char *buf, int len, int flags, const sockaddr *to, int tolen)
144  {
145  return ::sendto(fd, buf, len, flags, to, tolen);
146  }
147 
148  inline int recvfrom(int fd, char *buf, int len, int flags, sockaddr *from, int *fromlen)
149  {
150  return ::recvfrom(fd, buf, len, flags, from, fromlen);
151  }
152 
153  inline int accept(int fd, sockaddr *addr, int *addrlen)
154  {
155  return (int) ::accept(fd, addr, addrlen);
156  }
157 
158  inline int connect(int fd, const sockaddr *name, int namelen)
159  {
160  return ::connect(fd, name, namelen);
161  }
162 
163  inline int select(int nfds, fd_set *readfds, fd_set *writefds, fd_set *exceptfds, struct timeval *timeout)
164  {
165  return ::select(nfds, readfds, writefds, exceptfds, const_cast<struct timeval *>(timeout) );
166  }
167 
168  inline int closesocket(int fd)
169  {
170  return ::closesocket(fd);
171  }
172 
173  inline void setblocking(int fd, bool bBlocking)
174  {
175  u_long arg = bBlocking ? 0 : 1;
176  ioctlsocket(fd, FIONBIO, &arg);
177  }
178 
179  inline int GetLastError()
180  {
181  return ::WSAGetLastError();
182  }
183 
184  inline void disableBrokenPipeSignals()
185  {
186  }
187 
188  static const int ERR_EWOULDBLOCK = WSAEWOULDBLOCK;
189  static const int ERR_EINPROGRESS = WSAEINPROGRESS;
190  static const int ERR_ECONNRESET = WSAECONNRESET;
191  static const int ERR_ECONNABORTED = WSAECONNABORTED;
192  static const int ERR_ECONNREFUSED = WSAECONNREFUSED;
193  static const int ERR_EMSGSIZE = WSAEMSGSIZE;
194  static const int ERR_EADDRINUSE = WSAEADDRINUSE;
195 
196  } //namespace BsdSockets
197 
198  } // namespace OS;
199 
200 } // namespace Platform
201 
202 #endif // ! INCLUDE_UTIL_PLATFORM_OS_WINDOWS_BSDSOCKETS_HPP
Definition: Unix/BsdSockets.hpp:80