|
3 | 3 | // Distributed under the MIT software license, see the accompanying |
4 | 4 | // file COPYING or http://www.opensource.org/licenses/mit-license.php. |
5 | 5 |
|
6 | | -#ifndef BITCOIN_COMPAT_H |
7 | | -#define BITCOIN_COMPAT_H |
| 6 | +#ifndef BITCOIN_COMPAT_COMPAT_H |
| 7 | +#define BITCOIN_COMPAT_COMPAT_H |
8 | 8 |
|
9 | 9 | #if defined(HAVE_CONFIG_H) |
10 | 10 | #include <config/bitcoin-config.h> |
11 | 11 | #endif |
12 | 12 |
|
| 13 | +// Windows defines FD_SETSIZE to 64 (see _fd_types.h in mingw-w64), |
| 14 | +// which is too small for our usage, but allows us to redefine it safely. |
| 15 | +// We redefine it to be 1024, to match glibc, see typesizes.h. |
13 | 16 | #ifdef WIN32 |
14 | 17 | #ifdef FD_SETSIZE |
15 | | -#undef FD_SETSIZE // prevent redefinition compiler warning |
| 18 | +#undef FD_SETSIZE |
16 | 19 | #endif |
17 | | -#define FD_SETSIZE 1024 // max number of fds in fd_set |
| 20 | +#define FD_SETSIZE 1024 |
18 | 21 | #include <winsock2.h> |
19 | 22 | #include <ws2tcpip.h> |
20 | | -#include <stdint.h> |
| 23 | +#include <cstdint> |
21 | 24 | #else |
22 | 25 | #include <fcntl.h> |
23 | 26 | #include <sys/mman.h> |
|
34 | 37 | #include <unistd.h> |
35 | 38 | #endif |
36 | 39 |
|
| 40 | +// We map Linux / BSD error functions and codes, to the equivalent |
| 41 | +// Windows definitions, and use the WSA* names throughout our code. |
| 42 | +// Note that glibc defines EWOULDBLOCK as EAGAIN (see errno.h). |
37 | 43 | #ifndef WIN32 |
38 | 44 | typedef unsigned int SOCKET; |
39 | | -#include <errno.h> |
| 45 | +#include <cerrno> |
40 | 46 | #define WSAGetLastError() errno |
41 | 47 | #define WSAEINVAL EINVAL |
42 | | -#define WSAEALREADY EALREADY |
43 | 48 | #define WSAEWOULDBLOCK EWOULDBLOCK |
44 | 49 | #define WSAEAGAIN EAGAIN |
45 | 50 | #define WSAEMSGSIZE EMSGSIZE |
46 | 51 | #define WSAEINTR EINTR |
47 | 52 | #define WSAEINPROGRESS EINPROGRESS |
48 | 53 | #define WSAEADDRINUSE EADDRINUSE |
49 | | -#define WSAENOTSOCK EBADF |
50 | 54 | #define INVALID_SOCKET (SOCKET)(~0) |
51 | 55 | #define SOCKET_ERROR -1 |
52 | 56 | #else |
53 | | -#ifndef WSAEAGAIN |
| 57 | +// WSAEAGAIN doesn't exist on Windows |
54 | 58 | #ifdef EAGAIN |
55 | 59 | #define WSAEAGAIN EAGAIN |
56 | 60 | #else |
57 | 61 | #define WSAEAGAIN WSAEWOULDBLOCK |
58 | 62 | #endif |
59 | 63 | #endif |
60 | | -#endif |
61 | 64 |
|
| 65 | +// Windows doesn't define S_IRUSR or S_IWUSR. We define both |
| 66 | +// here, with the same values as glibc (see stat.h). |
62 | 67 | #ifdef WIN32 |
63 | 68 | #ifndef S_IRUSR |
64 | 69 | #define S_IRUSR 0400 |
65 | 70 | #define S_IWUSR 0200 |
66 | 71 | #endif |
67 | | -#else |
| 72 | +#endif |
| 73 | + |
| 74 | +// Windows defines MAX_PATH as it's maximum path length. |
| 75 | +// We define MAX_PATH for use on non-Windows systems. |
| 76 | +#ifndef WIN32 |
68 | 77 | #define MAX_PATH 1024 |
69 | 78 | #endif |
| 79 | + |
| 80 | +// ssize_t is POSIX, and not present when using MSVC. |
70 | 81 | #ifdef _MSC_VER |
71 | | -#if !defined(ssize_t) |
72 | | -#ifdef _WIN64 |
73 | | -typedef int64_t ssize_t; |
74 | | -#else |
75 | | -typedef int32_t ssize_t; |
76 | | -#endif |
77 | | -#endif |
| 82 | +#include <BaseTsd.h> |
| 83 | +typedef SSIZE_T ssize_t; |
78 | 84 | #endif |
79 | 85 |
|
| 86 | +// The type of the option value passed to getsockopt & setsockopt |
| 87 | +// differs between Windows and non-Windows. |
80 | 88 | #ifndef WIN32 |
81 | 89 | typedef void* sockopt_arg_type; |
82 | 90 | #else |
@@ -119,4 +127,4 @@ bool static inline IsSelectableSocket(const SOCKET& s) { |
119 | 127 | #define MSG_DONTWAIT 0 |
120 | 128 | #endif |
121 | 129 |
|
122 | | -#endif // BITCOIN_COMPAT_H |
| 130 | +#endif // BITCOIN_COMPAT_COMPAT_H |
0 commit comments