PikaPlatform_socket.c 6.1 KB

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