skynet_socket.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. #include "skynet.h"
  2. #include "skynet_socket.h"
  3. #include "socket_server.h"
  4. #include "skynet_server.h"
  5. #include "skynet_mq.h"
  6. #include "skynet_harbor.h"
  7. #include <assert.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <stdbool.h>
  11. static struct socket_server * SOCKET_SERVER = NULL;
  12. void
  13. skynet_socket_init() {
  14. SOCKET_SERVER = socket_server_create(skynet_now());
  15. }
  16. void
  17. skynet_socket_exit() {
  18. socket_server_exit(SOCKET_SERVER);
  19. }
  20. void
  21. skynet_socket_free() {
  22. socket_server_release(SOCKET_SERVER);
  23. SOCKET_SERVER = NULL;
  24. }
  25. void
  26. skynet_socket_updatetime() {
  27. socket_server_updatetime(SOCKET_SERVER, skynet_now());
  28. }
  29. // mainloop thread
  30. static void
  31. forward_message(int type, bool padding, struct socket_message * result) {
  32. struct skynet_socket_message *sm;
  33. size_t sz = sizeof(*sm);
  34. if (padding) {
  35. if (result->data) {
  36. size_t msg_sz = strlen(result->data);
  37. if (msg_sz > 128) {
  38. msg_sz = 128;
  39. }
  40. sz += msg_sz;
  41. } else {
  42. result->data = "";
  43. }
  44. }
  45. sm = (struct skynet_socket_message *)skynet_malloc(sz);
  46. sm->type = type;
  47. sm->id = result->id;
  48. sm->ud = result->ud;
  49. if (padding) {
  50. sm->buffer = NULL;
  51. memcpy(sm+1, result->data, sz - sizeof(*sm));
  52. } else {
  53. sm->buffer = result->data;
  54. }
  55. struct skynet_message message;
  56. message.source = 0;
  57. message.session = 0;
  58. message.data = sm;
  59. message.sz = sz | ((size_t)PTYPE_SOCKET << MESSAGE_TYPE_SHIFT);
  60. if (skynet_context_push((uint32_t)result->opaque, &message)) {
  61. // todo: report somewhere to close socket
  62. // don't call skynet_socket_close here (It will block mainloop)
  63. skynet_free(sm->buffer);
  64. skynet_free(sm);
  65. }
  66. }
  67. int
  68. skynet_socket_poll() {
  69. struct socket_server *ss = SOCKET_SERVER;
  70. assert(ss);
  71. struct socket_message result;
  72. int more = 1;
  73. int type = socket_server_poll(ss, &result, &more);
  74. switch (type) {
  75. case SOCKET_EXIT:
  76. return 0;
  77. case SOCKET_DATA:
  78. forward_message(SKYNET_SOCKET_TYPE_DATA, false, &result);
  79. break;
  80. case SOCKET_CLOSE:
  81. forward_message(SKYNET_SOCKET_TYPE_CLOSE, false, &result);
  82. break;
  83. case SOCKET_OPEN:
  84. forward_message(SKYNET_SOCKET_TYPE_CONNECT, true, &result);
  85. break;
  86. case SOCKET_ERR:
  87. forward_message(SKYNET_SOCKET_TYPE_ERROR, true, &result);
  88. break;
  89. case SOCKET_ACCEPT:
  90. forward_message(SKYNET_SOCKET_TYPE_ACCEPT, true, &result);
  91. break;
  92. case SOCKET_UDP:
  93. forward_message(SKYNET_SOCKET_TYPE_UDP, false, &result);
  94. break;
  95. case SOCKET_WARNING:
  96. forward_message(SKYNET_SOCKET_TYPE_WARNING, false, &result);
  97. break;
  98. default:
  99. skynet_error(NULL, "Unknown socket message type %d.",type);
  100. return -1;
  101. }
  102. if (more) {
  103. return -1;
  104. }
  105. return 1;
  106. }
  107. int
  108. skynet_socket_sendbuffer(struct skynet_context *ctx, struct socket_sendbuffer *buffer) {
  109. return socket_server_send(SOCKET_SERVER, buffer);
  110. }
  111. int
  112. skynet_socket_sendbuffer_lowpriority(struct skynet_context *ctx, struct socket_sendbuffer *buffer) {
  113. return socket_server_send_lowpriority(SOCKET_SERVER, buffer);
  114. }
  115. int
  116. skynet_socket_listen(struct skynet_context *ctx, const char *host, int port, int backlog) {
  117. uint32_t source = skynet_context_handle(ctx);
  118. return socket_server_listen(SOCKET_SERVER, source, host, port, backlog);
  119. }
  120. int
  121. skynet_socket_connect(struct skynet_context *ctx, const char *host, int port) {
  122. uint32_t source = skynet_context_handle(ctx);
  123. return socket_server_connect(SOCKET_SERVER, source, host, port);
  124. }
  125. int
  126. skynet_socket_bind(struct skynet_context *ctx, int fd) {
  127. uint32_t source = skynet_context_handle(ctx);
  128. return socket_server_bind(SOCKET_SERVER, source, fd);
  129. }
  130. void
  131. skynet_socket_close(struct skynet_context *ctx, int id) {
  132. uint32_t source = skynet_context_handle(ctx);
  133. socket_server_close(SOCKET_SERVER, source, id);
  134. }
  135. void
  136. skynet_socket_shutdown(struct skynet_context *ctx, int id) {
  137. uint32_t source = skynet_context_handle(ctx);
  138. socket_server_shutdown(SOCKET_SERVER, source, id);
  139. }
  140. void
  141. skynet_socket_start(struct skynet_context *ctx, int id) {
  142. uint32_t source = skynet_context_handle(ctx);
  143. socket_server_start(SOCKET_SERVER, source, id);
  144. }
  145. void
  146. skynet_socket_pause(struct skynet_context *ctx, int id) {
  147. uint32_t source = skynet_context_handle(ctx);
  148. socket_server_pause(SOCKET_SERVER, source, id);
  149. }
  150. void
  151. skynet_socket_nodelay(struct skynet_context *ctx, int id) {
  152. socket_server_nodelay(SOCKET_SERVER, id);
  153. }
  154. int
  155. skynet_socket_udp(struct skynet_context *ctx, const char * addr, int port) {
  156. uint32_t source = skynet_context_handle(ctx);
  157. return socket_server_udp(SOCKET_SERVER, source, addr, port);
  158. }
  159. int
  160. skynet_socket_udp_connect(struct skynet_context *ctx, int id, const char * addr, int port) {
  161. return socket_server_udp_connect(SOCKET_SERVER, id, addr, port);
  162. }
  163. int
  164. skynet_socket_udp_sendbuffer(struct skynet_context *ctx, const char * address, struct socket_sendbuffer *buffer) {
  165. return socket_server_udp_send(SOCKET_SERVER, (const struct socket_udp_address *)address, buffer);
  166. }
  167. const char *
  168. skynet_socket_udp_address(struct skynet_socket_message *msg, int *addrsz) {
  169. if (msg->type != SKYNET_SOCKET_TYPE_UDP) {
  170. return NULL;
  171. }
  172. struct socket_message sm;
  173. sm.id = msg->id;
  174. sm.opaque = 0;
  175. sm.ud = msg->ud;
  176. sm.data = msg->buffer;
  177. return (const char *)socket_server_udp_address(SOCKET_SERVER, &sm, addrsz);
  178. }
  179. struct socket_info *
  180. skynet_socket_info() {
  181. return socket_server_info(SOCKET_SERVER);
  182. }