Simulation Core
Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

sockcompat.h 4.3 KiB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. * Copyright (c) 2019, Marcus Geelnard <m at bitsnbites dot eu>
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * * Neither the name of Redis nor the names of its contributors may be used
  15. * to endorse or promote products derived from this software without
  16. * specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  19. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  20. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  21. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  22. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  23. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  24. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  25. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  26. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  27. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  28. * POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #ifndef __SOCKCOMPAT_H
  31. #define __SOCKCOMPAT_H
  32. #ifndef _WIN32
  33. /* For POSIX systems we use the standard BSD socket API. */
  34. #include <unistd.h>
  35. #include <sys/socket.h>
  36. #include <sys/select.h>
  37. #include <sys/un.h>
  38. #include <netinet/in.h>
  39. #include <netinet/tcp.h>
  40. #include <arpa/inet.h>
  41. #include <netdb.h>
  42. #include <poll.h>
  43. #else
  44. /* For Windows we use winsock. */
  45. #undef _WIN32_WINNT
  46. #define _WIN32_WINNT 0x0600 /* To get WSAPoll etc. */
  47. #include <winsock2.h>
  48. #include <ws2tcpip.h>
  49. #include <stddef.h>
  50. #include <errno.h>
  51. #include <mstcpip.h>
  52. #ifdef _MSC_VER
  53. typedef long long ssize_t;
  54. #endif
  55. /* Emulate the parts of the BSD socket API that we need (override the winsock signatures). */
  56. int win32_getaddrinfo(const char *node, const char *service, const struct addrinfo *hints, struct addrinfo **res);
  57. const char *win32_gai_strerror(int errcode);
  58. void win32_freeaddrinfo(struct addrinfo *res);
  59. SOCKET win32_socket(int domain, int type, int protocol);
  60. int win32_ioctl(SOCKET fd, unsigned long request, unsigned long *argp);
  61. int win32_bind(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen);
  62. int win32_connect(SOCKET sockfd, const struct sockaddr *addr, socklen_t addrlen);
  63. int win32_getsockopt(SOCKET sockfd, int level, int optname, void *optval, socklen_t *optlen);
  64. int win32_setsockopt(SOCKET sockfd, int level, int optname, const void *optval, socklen_t optlen);
  65. int win32_close(SOCKET fd);
  66. ssize_t win32_recv(SOCKET sockfd, void *buf, size_t len, int flags);
  67. ssize_t win32_send(SOCKET sockfd, const void *buf, size_t len, int flags);
  68. typedef ULONG nfds_t;
  69. int win32_poll(struct pollfd *fds, nfds_t nfds, int timeout);
  70. int win32_redisKeepAlive(SOCKET sockfd, int interval_ms);
  71. #ifndef REDIS_SOCKCOMPAT_IMPLEMENTATION
  72. #define getaddrinfo(node, service, hints, res) win32_getaddrinfo(node, service, hints, res)
  73. #undef gai_strerror
  74. #define gai_strerror(errcode) win32_gai_strerror(errcode)
  75. #define freeaddrinfo(res) win32_freeaddrinfo(res)
  76. #define socket(domain, type, protocol) win32_socket(domain, type, protocol)
  77. #define ioctl(fd, request, argp) win32_ioctl(fd, request, argp)
  78. #define bind(sockfd, addr, addrlen) win32_bind(sockfd, addr, addrlen)
  79. #define connect(sockfd, addr, addrlen) win32_connect(sockfd, addr, addrlen)
  80. #define getsockopt(sockfd, level, optname, optval, optlen) win32_getsockopt(sockfd, level, optname, optval, optlen)
  81. #define setsockopt(sockfd, level, optname, optval, optlen) win32_setsockopt(sockfd, level, optname, optval, optlen)
  82. #define close(fd) win32_close(fd)
  83. #define recv(sockfd, buf, len, flags) win32_recv(sockfd, buf, len, flags)
  84. #define send(sockfd, buf, len, flags) win32_send(sockfd, buf, len, flags)
  85. #define poll(fds, nfds, timeout) win32_poll(fds, nfds, timeout)
  86. #endif /* REDIS_SOCKCOMPAT_IMPLEMENTATION */
  87. #endif /* _WIN32 */
  88. #endif /* __SOCKCOMPAT_H */