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