You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.9 KiB

4 months ago
  1. /*
  2. * IXNetSystem.h
  3. * Author: Benjamin Sergeant
  4. * Copyright (c) 2019 Machine Zone. All rights reserved.
  5. */
  6. #pragma once
  7. #ifdef _WIN32
  8. #ifndef WIN32_LEAN_AND_MEAN
  9. #define WIN32_LEAN_AND_MEAN
  10. #endif
  11. #include <ws2tcpip.h>
  12. #include <winsock2.h>
  13. #include <basetsd.h>
  14. #include <io.h>
  15. #include <ws2def.h>
  16. #include <cerrno>
  17. #undef EWOULDBLOCK
  18. #undef EAGAIN
  19. #undef EINPROGRESS
  20. #undef EBADF
  21. #undef EINVAL
  22. // map to WSA error codes
  23. #define EWOULDBLOCK WSAEWOULDBLOCK
  24. #define EAGAIN WSATRY_AGAIN
  25. #define EINPROGRESS WSAEINPROGRESS
  26. #define EBADF WSAEBADF
  27. #define EINVAL WSAEINVAL
  28. // Define our own poll on Windows, as a wrapper on top of select
  29. typedef unsigned long int nfds_t;
  30. // pollfd is not defined by some versions of mingw64 since _WIN32_WINNT is too low
  31. #if _WIN32_WINNT < 0x0600
  32. struct pollfd
  33. {
  34. int fd; /* file descriptor */
  35. short events; /* requested events */
  36. short revents; /* returned events */
  37. };
  38. #define POLLIN 0x001 /* There is data to read. */
  39. #define POLLOUT 0x004 /* Writing now will not block. */
  40. #define POLLERR 0x008 /* Error condition. */
  41. #define POLLHUP 0x010 /* Hung up. */
  42. #define POLLNVAL 0x020 /* Invalid polling request. */
  43. #endif
  44. #else
  45. #include <arpa/inet.h>
  46. #include <errno.h>
  47. #include <fcntl.h>
  48. #include <netdb.h>
  49. #include <netinet/in.h>
  50. #include <netinet/ip.h>
  51. #include <netinet/tcp.h>
  52. #include <poll.h>
  53. #include <sys/select.h>
  54. #include <sys/socket.h>
  55. #include <sys/stat.h>
  56. #include <sys/time.h>
  57. #include <unistd.h>
  58. #endif
  59. namespace ix
  60. {
  61. #ifdef _WIN32
  62. typedef SOCKET socket_t;
  63. #else
  64. typedef int socket_t;
  65. #endif
  66. bool initNetSystem();
  67. bool uninitNetSystem();
  68. int poll(struct pollfd* fds, nfds_t nfds, int timeout, void** event);
  69. const char* inet_ntop(int af, const void* src, char* dst, socklen_t size);
  70. int inet_pton(int af, const char* src, void* dst);
  71. unsigned short network_to_host_short(unsigned short value);
  72. } // namespace ix