_socket.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include "_socket.h"
  2. #include "_socket_socket.h"
  3. #ifdef __linux__
  4. #include <arpa/inet.h>
  5. #include <unistd.h>
  6. #else
  7. /*
  8. You need to create the __platform_socket.h for your platform.
  9. For example:
  10. You can #include "lwip/socket.h" in the __platform_socket.h
  11. */
  12. #include "__platform_socket.h"
  13. #endif
  14. #if !PIKASCRIPT_VERSION_REQUIRE_MINIMUN(1, 10, 4)
  15. #error "This library requires PikaScript version 1.10.4 or higher"
  16. #endif
  17. /*
  18. The functinos start with PIKA_WEAK are weak functions,
  19. you need to override them in your platform.
  20. */
  21. PIKA_WEAK int __platform_socket(int __domain, int __type, int __protocol) {
  22. #ifdef __linux__
  23. return socket(__domain, __type, __protocol);
  24. #else
  25. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  26. #endif
  27. }
  28. PIKA_WEAK int __platform_bind(int __fd,
  29. const struct sockaddr* __addr,
  30. socklen_t __addr_len) {
  31. #ifdef __linux__
  32. return bind(__fd, __addr, __addr_len);
  33. #else
  34. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  35. #endif
  36. }
  37. PIKA_WEAK int __platform_listen(int __fd, int __n) {
  38. #ifdef __linux__
  39. return listen(__fd, __n);
  40. #else
  41. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  42. #endif
  43. }
  44. PIKA_WEAK int __platform_accept(int __fd,
  45. struct sockaddr* __addr,
  46. socklen_t* __addr_len) {
  47. #ifdef __linux__
  48. return accept(__fd, __addr, __addr_len);
  49. #else
  50. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  51. #endif
  52. }
  53. PIKA_WEAK int __platform_connect(int __fd,
  54. const struct sockaddr* __addr,
  55. socklen_t __addr_len) {
  56. #ifdef __linux__
  57. return connect(__fd, __addr, __addr_len);
  58. #else
  59. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  60. #endif
  61. }
  62. PIKA_WEAK int __platform_send(int __fd,
  63. const void* __buf,
  64. size_t __n,
  65. int __flags) {
  66. #ifdef __linux__
  67. return send(__fd, __buf, __n, __flags);
  68. #else
  69. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  70. #endif
  71. }
  72. PIKA_WEAK int __platform_recv(int __fd, void* __buf, size_t __n, int __flags) {
  73. #ifdef __linux__
  74. return recv(__fd, __buf, __n, __flags);
  75. #else
  76. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  77. #endif
  78. }
  79. PIKA_WEAK int __platform_close(int __fd) {
  80. #ifdef __linux__
  81. return close(__fd);
  82. #else
  83. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  84. #endif
  85. }
  86. /* gethostname */
  87. PIKA_WEAK int __platform_gethostname(char* __name, size_t __len) {
  88. #ifdef __linux__
  89. return gethostname(__name, __len);
  90. #else
  91. WEAK_FUNCTION_NEED_OVERRIDE_ERROR();
  92. #endif
  93. }
  94. void _socket_socket__init(PikaObj* self) {
  95. int family = obj_getInt(self, "family");
  96. int type = obj_getInt(self, "type");
  97. int protocol = obj_getInt(self, "protocol");
  98. int sockfd = 0;
  99. sockfd = __platform_socket(family, type, protocol);
  100. if (sockfd < 0) {
  101. obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
  102. __platform_printf("socket error\n");
  103. return;
  104. }
  105. obj_setInt(self, "sockfd", sockfd);
  106. }
  107. void _socket_socket__close(PikaObj* self) {
  108. int sockfd = obj_getInt(self, "sockfd");
  109. __platform_close(sockfd);
  110. }
  111. void _socket_socket__send(PikaObj* self, Arg* data) {
  112. uint8_t* data_send = NULL;
  113. int len = 0;
  114. if (arg_getType(data) == ARG_TYPE_STRING) {
  115. data_send = (uint8_t*)arg_getStr(data);
  116. len = strGetSize((char*)data_send);
  117. }
  118. if (arg_getType(data) == ARG_TYPE_BYTES) {
  119. data_send = (uint8_t*)arg_getBytes(data);
  120. len = arg_getBytesSize(data);
  121. }
  122. int sockfd = obj_getInt(self, "sockfd");
  123. int ret = 0;
  124. ret = __platform_send(sockfd, data_send, len, 0);
  125. if (ret < 0) {
  126. obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
  127. __platform_printf("send error\n");
  128. return;
  129. }
  130. }
  131. void _socket_socket__accept(PikaObj* self) {
  132. int sockfd = obj_getInt(self, "sockfd");
  133. int client_sockfd = 0;
  134. struct sockaddr_in client_addr;
  135. socklen_t client_addr_len = sizeof(client_addr);
  136. client_sockfd = __platform_accept(sockfd, (struct sockaddr*)&client_addr,
  137. &client_addr_len);
  138. if (client_sockfd < 0) {
  139. obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
  140. __platform_printf("accept error\n");
  141. return;
  142. }
  143. obj_setInt(self, "client_sockfd", client_sockfd);
  144. obj_setStr(self, "client_addr", inet_ntoa(client_addr.sin_addr));
  145. }
  146. Arg* _socket_socket__recv(PikaObj* self, int num) {
  147. int sockfd = obj_getInt(self, "sockfd");
  148. int ret = 0;
  149. uint8_t* data_recv = NULL;
  150. Arg* res = arg_newBytes(NULL, num);
  151. data_recv = arg_getBytes(res);
  152. ret = __platform_recv(sockfd, data_recv, num, 0);
  153. if (ret < 0) {
  154. obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
  155. __platform_printf("recv error\n");
  156. return NULL;
  157. }
  158. return res;
  159. }
  160. void _socket_socket__listen(PikaObj* self, int num) {
  161. int sockfd = obj_getInt(self, "sockfd");
  162. __platform_listen(sockfd, num);
  163. }
  164. void _socket_socket__connect(PikaObj* self, char* host, int port) {
  165. int sockfd = obj_getInt(self, "sockfd");
  166. struct sockaddr_in server_addr;
  167. server_addr.sin_family = AF_INET;
  168. server_addr.sin_port = htons(port);
  169. server_addr.sin_addr.s_addr = inet_addr(host);
  170. __platform_connect(sockfd, (struct sockaddr*)&server_addr,
  171. sizeof(server_addr));
  172. }
  173. void _socket_socket__bind(PikaObj* self, char* host, int port) {
  174. int sockfd = obj_getInt(self, "sockfd");
  175. struct sockaddr_in server_addr;
  176. server_addr.sin_family = AF_INET;
  177. server_addr.sin_port = htons(port);
  178. server_addr.sin_addr.s_addr = inet_addr(host);
  179. int res = __platform_bind(sockfd, (struct sockaddr*)&server_addr,
  180. sizeof(server_addr));
  181. if (res < 0) {
  182. obj_setErrorCode(self, PIKA_RES_ERR_RUNTIME_ERROR);
  183. __platform_printf("bind error\n");
  184. return;
  185. }
  186. }
  187. char* _socket__gethostname(PikaObj* self) {
  188. char hostname_buff[128] = {0};
  189. char* hostname = (char*)hostname_buff;
  190. __platform_gethostname(hostname_buff, 128);
  191. return obj_cacheStr(self, hostname);
  192. }