PikaPlatform_socket.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. #include "PikaPlatform_socket.h"
  2. /*
  3. The functinos start with PIKA_WEAK are weak functions,
  4. you need to override them in your platform.
  5. */
  6. #ifdef _WIN32
  7. #pragma comment(lib, "ws2_32.lib")
  8. static int pika_platform_winsock_initialized = 0;
  9. int pika_platform_init_winsock() {
  10. if (0 == pika_platform_winsock_initialized) {
  11. WSADATA wsaData;
  12. int res = WSAStartup(MAKEWORD(2, 2), &wsaData);
  13. if (res != 0) {
  14. __platform_printf("WSAStartup failed with error: %d\n", res);
  15. return 1;
  16. }
  17. pika_platform_winsock_initialized = 1;
  18. } else if (0 < pika_platform_winsock_initialized) {
  19. pika_platform_winsock_initialized++;
  20. }
  21. return 0;
  22. }
  23. int pika_platform_cleanup_winsock() {
  24. if (1 == pika_platform_winsock_initialized) {
  25. WSACleanup();
  26. pika_platform_winsock_initialized = 0;
  27. } else if (1 < pika_platform_winsock_initialized) {
  28. pika_platform_winsock_initialized--;
  29. }
  30. return 0;
  31. }
  32. #endif
  33. PIKA_WEAK int pika_platform_socket(int __domain, int __type, int __protocol) {
  34. #if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
  35. return socket(__domain, __type, __protocol);
  36. #else
  37. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  38. #endif
  39. }
  40. PIKA_WEAK int pika_platform_bind(int __fd,
  41. const struct sockaddr* __addr,
  42. socklen_t __addr_len) {
  43. #if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
  44. return bind(__fd, __addr, __addr_len);
  45. #else
  46. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  47. #endif
  48. }
  49. PIKA_WEAK int pika_platform_listen(int __fd, int __n) {
  50. #if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
  51. return listen(__fd, __n);
  52. #else
  53. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  54. #endif
  55. }
  56. PIKA_WEAK int pika_platform_accept(int __fd,
  57. struct sockaddr* __addr,
  58. socklen_t* __addr_len) {
  59. #if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
  60. return accept(__fd, __addr, __addr_len);
  61. #else
  62. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  63. #endif
  64. }
  65. PIKA_WEAK int pika_platform_connect(int __fd,
  66. const struct sockaddr* __addr,
  67. socklen_t __addr_len) {
  68. #if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
  69. return connect(__fd, __addr, __addr_len);
  70. #else
  71. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  72. #endif
  73. }
  74. PIKA_WEAK int pika_platform_htons(int __hostshort) {
  75. #if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
  76. return htons(__hostshort);
  77. #else
  78. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  79. #endif
  80. }
  81. /* gethostbyname */
  82. PIKA_WEAK struct hostent* pika_platform_gethostbyname(const char* __name) {
  83. #if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
  84. return gethostbyname(__name);
  85. #else
  86. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  87. #endif
  88. }
  89. /* inet_ntoa */
  90. PIKA_WEAK char* pika_platform_inet_ntoa(struct in_addr __addr) {
  91. #if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
  92. return inet_ntoa(__addr);
  93. #else
  94. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  95. #endif
  96. }
  97. PIKA_WEAK int pika_platform_send(int __fd,
  98. const void* __buf,
  99. size_t __n,
  100. int __flags) {
  101. #if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
  102. return send(__fd, __buf, __n, __flags);
  103. #else
  104. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  105. #endif
  106. }
  107. PIKA_WEAK int pika_platform_recv(int __fd,
  108. void* __buf,
  109. size_t __n,
  110. int __flags) {
  111. #if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
  112. return recv(__fd, __buf, __n, __flags);
  113. #else
  114. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  115. return -1;
  116. #endif
  117. }
  118. /* gethostname */
  119. PIKA_WEAK int pika_platform_gethostname(char* __name, size_t __len) {
  120. #if defined(__linux__) || defined(_WIN32)
  121. return gethostname(__name, __len);
  122. #else
  123. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  124. return -1;
  125. #endif
  126. }
  127. PIKA_WEAK int pika_platform_getaddrinfo(const char* __name,
  128. const char* __service,
  129. const struct addrinfo* __req,
  130. struct addrinfo** __pai) {
  131. #if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
  132. return getaddrinfo(__name, __service, __req, __pai);
  133. #else
  134. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  135. #endif
  136. }
  137. PIKA_WEAK void pika_platform_freeaddrinfo(struct addrinfo* __ai) {
  138. #if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
  139. freeaddrinfo(__ai);
  140. #else
  141. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  142. #endif
  143. }
  144. PIKA_WEAK int pika_platform_setsockopt(int __fd,
  145. int __level,
  146. int __optname,
  147. const void* __optval,
  148. socklen_t __optlen) {
  149. #if defined(__linux__) || defined(_WIN32) || PIKA_LWIP_ENABLE
  150. return setsockopt(__fd, __level, __optname, __optval, __optlen);
  151. #else
  152. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  153. #endif
  154. }
  155. PIKA_WEAK int pika_platform_fcntl(int fd, int cmd, long arg) {
  156. #if defined(__linux__) || PIKA_LWIP_ENABLE
  157. return fcntl(fd, cmd, arg);
  158. #elif defined(_WIN32)
  159. if (cmd == F_GETFL) {
  160. u_long mode = 0;
  161. ioctlsocket(fd, FIONBIO, &mode);
  162. return (mode ? O_NONBLOCK : 0);
  163. } else if (cmd == F_SETFL) {
  164. u_long mode = (arg & O_NONBLOCK) ? 1 : 0;
  165. return ioctlsocket(fd, FIONBIO, &mode);
  166. }
  167. return -1;
  168. #else
  169. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  170. #endif
  171. }
  172. /* os file API */
  173. PIKA_WEAK int pika_platform_close(int __fd) {
  174. #if defined(__linux__) || PIKA_LWIP_ENABLE
  175. return close(__fd);
  176. #elif defined(_WIN32)
  177. return closesocket(__fd);
  178. #elif PIKA_FREERTOS_ENABLE
  179. return closesocket(__fd);
  180. #else
  181. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  182. #endif
  183. }
  184. PIKA_WEAK int pika_platform_write(int __fd, const void* __buf, size_t __nbyte) {
  185. #if defined(__linux__) || PIKA_LWIP_ENABLE
  186. return write(__fd, __buf, __nbyte);
  187. #elif defined(_WIN32)
  188. return send(__fd, __buf, __nbyte, 0);
  189. #else
  190. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  191. #endif
  192. }