service_gate.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #include "skynet.h"
  2. #include "skynet_socket.h"
  3. #include "databuffer.h"
  4. #include "hashid.h"
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <assert.h>
  8. #include <stdint.h>
  9. #include <stdio.h>
  10. #include <stdarg.h>
  11. #define BACKLOG 128
  12. struct connection {
  13. int id; // skynet_socket id
  14. uint32_t agent;
  15. uint32_t client;
  16. char remote_name[32];
  17. struct databuffer buffer;
  18. };
  19. struct gate {
  20. struct skynet_context *ctx;
  21. int listen_id;
  22. uint32_t watchdog;
  23. uint32_t broker;
  24. int client_tag;
  25. int header_size;
  26. int max_connection;
  27. struct hashid hash;
  28. struct connection *conn;
  29. // todo: save message pool ptr for release
  30. struct messagepool mp;
  31. };
  32. struct gate *
  33. gate_create(void) {
  34. struct gate * g = skynet_malloc(sizeof(*g));
  35. memset(g,0,sizeof(*g));
  36. g->listen_id = -1;
  37. return g;
  38. }
  39. void
  40. gate_release(struct gate *g) {
  41. int i;
  42. struct skynet_context *ctx = g->ctx;
  43. for (i=0;i<g->max_connection;i++) {
  44. struct connection *c = &g->conn[i];
  45. if (c->id >=0) {
  46. skynet_socket_close(ctx, c->id);
  47. }
  48. }
  49. if (g->listen_id >= 0) {
  50. skynet_socket_close(ctx, g->listen_id);
  51. }
  52. messagepool_free(&g->mp);
  53. hashid_clear(&g->hash);
  54. skynet_free(g->conn);
  55. skynet_free(g);
  56. }
  57. static void
  58. _parm(char *msg, int sz, int command_sz) {
  59. while (command_sz < sz) {
  60. if (msg[command_sz] != ' ')
  61. break;
  62. ++command_sz;
  63. }
  64. int i;
  65. for (i=command_sz;i<sz;i++) {
  66. msg[i-command_sz] = msg[i];
  67. }
  68. msg[i-command_sz] = '\0';
  69. }
  70. static void
  71. _forward_agent(struct gate * g, int fd, uint32_t agentaddr, uint32_t clientaddr) {
  72. int id = hashid_lookup(&g->hash, fd);
  73. if (id >=0) {
  74. struct connection * agent = &g->conn[id];
  75. agent->agent = agentaddr;
  76. agent->client = clientaddr;
  77. }
  78. }
  79. static void
  80. _ctrl(struct gate * g, const void * msg, int sz) {
  81. struct skynet_context * ctx = g->ctx;
  82. char tmp[sz+1];
  83. memcpy(tmp, msg, sz);
  84. tmp[sz] = '\0';
  85. char * command = tmp;
  86. int i;
  87. if (sz == 0)
  88. return;
  89. for (i=0;i<sz;i++) {
  90. if (command[i]==' ') {
  91. break;
  92. }
  93. }
  94. if (memcmp(command,"kick",i)==0) {
  95. _parm(tmp, sz, i);
  96. int uid = strtol(command , NULL, 10);
  97. int id = hashid_lookup(&g->hash, uid);
  98. if (id>=0) {
  99. skynet_socket_close(ctx, uid);
  100. }
  101. return;
  102. }
  103. if (memcmp(command,"forward",i)==0) {
  104. _parm(tmp, sz, i);
  105. char * client = tmp;
  106. char * idstr = strsep(&client, " ");
  107. if (client == NULL) {
  108. return;
  109. }
  110. int id = strtol(idstr , NULL, 10);
  111. char * agent = strsep(&client, " ");
  112. if (client == NULL) {
  113. return;
  114. }
  115. uint32_t agent_handle = strtoul(agent+1, NULL, 16);
  116. uint32_t client_handle = strtoul(client+1, NULL, 16);
  117. _forward_agent(g, id, agent_handle, client_handle);
  118. return;
  119. }
  120. if (memcmp(command,"broker",i)==0) {
  121. _parm(tmp, sz, i);
  122. g->broker = skynet_queryname(ctx, command);
  123. return;
  124. }
  125. if (memcmp(command,"start",i) == 0) {
  126. _parm(tmp, sz, i);
  127. int uid = strtol(command , NULL, 10);
  128. int id = hashid_lookup(&g->hash, uid);
  129. if (id>=0) {
  130. skynet_socket_start(ctx, uid);
  131. }
  132. return;
  133. }
  134. if (memcmp(command, "close", i) == 0) {
  135. if (g->listen_id >= 0) {
  136. skynet_socket_close(ctx, g->listen_id);
  137. g->listen_id = -1;
  138. }
  139. return;
  140. }
  141. skynet_error(ctx, "[gate] Unknown command : %s", command);
  142. }
  143. static void
  144. _report(struct gate * g, const char * data, ...) {
  145. if (g->watchdog == 0) {
  146. return;
  147. }
  148. struct skynet_context * ctx = g->ctx;
  149. va_list ap;
  150. va_start(ap, data);
  151. char tmp[1024];
  152. int n = vsnprintf(tmp, sizeof(tmp), data, ap);
  153. va_end(ap);
  154. skynet_send(ctx, 0, g->watchdog, PTYPE_TEXT, 0, tmp, n);
  155. }
  156. static void
  157. _forward(struct gate *g, struct connection * c, int size) {
  158. struct skynet_context * ctx = g->ctx;
  159. int fd = c->id;
  160. if (fd <= 0) {
  161. // socket error
  162. return;
  163. }
  164. if (g->broker) {
  165. void * temp = skynet_malloc(size);
  166. databuffer_read(&c->buffer,&g->mp,(char *)temp, size);
  167. skynet_send(ctx, 0, g->broker, g->client_tag | PTYPE_TAG_DONTCOPY, fd, temp, size);
  168. return;
  169. }
  170. if (c->agent) {
  171. void * temp = skynet_malloc(size);
  172. databuffer_read(&c->buffer,&g->mp,(char *)temp, size);
  173. skynet_send(ctx, c->client, c->agent, g->client_tag | PTYPE_TAG_DONTCOPY, fd , temp, size);
  174. } else if (g->watchdog) {
  175. char * tmp = skynet_malloc(size + 32);
  176. int n = snprintf(tmp,32,"%d data ",c->id);
  177. databuffer_read(&c->buffer,&g->mp,tmp+n,size);
  178. skynet_send(ctx, 0, g->watchdog, PTYPE_TEXT | PTYPE_TAG_DONTCOPY, fd, tmp, size + n);
  179. }
  180. }
  181. static void
  182. dispatch_message(struct gate *g, struct connection *c, int id, void * data, int sz) {
  183. databuffer_push(&c->buffer,&g->mp, data, sz);
  184. for (;;) {
  185. int size = databuffer_readheader(&c->buffer, &g->mp, g->header_size);
  186. if (size < 0) {
  187. return;
  188. } else if (size > 0) {
  189. if (size >= 0x1000000) {
  190. struct skynet_context * ctx = g->ctx;
  191. databuffer_clear(&c->buffer,&g->mp);
  192. skynet_socket_close(ctx, id);
  193. skynet_error(ctx, "Recv socket message > 16M");
  194. return;
  195. } else {
  196. _forward(g, c, size);
  197. databuffer_reset(&c->buffer);
  198. }
  199. }
  200. }
  201. }
  202. static void
  203. dispatch_socket_message(struct gate *g, const struct skynet_socket_message * message, int sz) {
  204. struct skynet_context * ctx = g->ctx;
  205. switch(message->type) {
  206. case SKYNET_SOCKET_TYPE_DATA: {
  207. int id = hashid_lookup(&g->hash, message->id);
  208. if (id>=0) {
  209. struct connection *c = &g->conn[id];
  210. dispatch_message(g, c, message->id, message->buffer, message->ud);
  211. } else {
  212. skynet_error(ctx, "Drop unknown connection %d message", message->id);
  213. skynet_socket_close(ctx, message->id);
  214. skynet_free(message->buffer);
  215. }
  216. break;
  217. }
  218. case SKYNET_SOCKET_TYPE_CONNECT: {
  219. if (message->id == g->listen_id) {
  220. // start listening
  221. break;
  222. }
  223. int id = hashid_lookup(&g->hash, message->id);
  224. if (id<0) {
  225. skynet_error(ctx, "Close unknown connection %d", message->id);
  226. skynet_socket_close(ctx, message->id);
  227. }
  228. break;
  229. }
  230. case SKYNET_SOCKET_TYPE_CLOSE:
  231. case SKYNET_SOCKET_TYPE_ERROR: {
  232. int id = hashid_remove(&g->hash, message->id);
  233. if (id>=0) {
  234. struct connection *c = &g->conn[id];
  235. databuffer_clear(&c->buffer,&g->mp);
  236. memset(c, 0, sizeof(*c));
  237. c->id = -1;
  238. _report(g, "%d close", message->id);
  239. }
  240. break;
  241. }
  242. case SKYNET_SOCKET_TYPE_ACCEPT:
  243. // report accept, then it will be get a SKYNET_SOCKET_TYPE_CONNECT message
  244. assert(g->listen_id == message->id);
  245. if (hashid_full(&g->hash)) {
  246. skynet_socket_close(ctx, message->ud);
  247. } else {
  248. struct connection *c = &g->conn[hashid_insert(&g->hash, message->ud)];
  249. if (sz >= sizeof(c->remote_name)) {
  250. sz = sizeof(c->remote_name) - 1;
  251. }
  252. c->id = message->ud;
  253. memcpy(c->remote_name, message+1, sz);
  254. c->remote_name[sz] = '\0';
  255. _report(g, "%d open %d %s:0",c->id, c->id, c->remote_name);
  256. skynet_error(ctx, "socket open: %x", c->id);
  257. }
  258. break;
  259. case SKYNET_SOCKET_TYPE_WARNING:
  260. skynet_error(ctx, "fd (%d) send buffer (%d)K", message->id, message->ud);
  261. break;
  262. }
  263. }
  264. static int
  265. _cb(struct skynet_context * ctx, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
  266. struct gate *g = ud;
  267. switch(type) {
  268. case PTYPE_TEXT:
  269. _ctrl(g , msg , (int)sz);
  270. break;
  271. case PTYPE_CLIENT: {
  272. if (sz <=4 ) {
  273. skynet_error(ctx, "Invalid client message from %x",source);
  274. break;
  275. }
  276. // The last 4 bytes in msg are the id of socket, write following bytes to it
  277. const uint8_t * idbuf = msg + sz - 4;
  278. uint32_t uid = idbuf[0] | idbuf[1] << 8 | idbuf[2] << 16 | idbuf[3] << 24;
  279. int id = hashid_lookup(&g->hash, uid);
  280. if (id>=0) {
  281. // don't send id (last 4 bytes)
  282. skynet_socket_send(ctx, uid, (void*)msg, sz-4);
  283. // return 1 means don't free msg
  284. return 1;
  285. } else {
  286. skynet_error(ctx, "Invalid client id %d from %x",(int)uid,source);
  287. break;
  288. }
  289. }
  290. case PTYPE_SOCKET:
  291. // recv socket message from skynet_socket
  292. dispatch_socket_message(g, msg, (int)(sz-sizeof(struct skynet_socket_message)));
  293. break;
  294. }
  295. return 0;
  296. }
  297. static int
  298. start_listen(struct gate *g, char * listen_addr) {
  299. struct skynet_context * ctx = g->ctx;
  300. char * portstr = strrchr(listen_addr,':');
  301. const char * host = "";
  302. int port;
  303. if (portstr == NULL) {
  304. port = strtol(listen_addr, NULL, 10);
  305. if (port <= 0) {
  306. skynet_error(ctx, "Invalid gate address %s",listen_addr);
  307. return 1;
  308. }
  309. } else {
  310. port = strtol(portstr + 1, NULL, 10);
  311. if (port <= 0) {
  312. skynet_error(ctx, "Invalid gate address %s",listen_addr);
  313. return 1;
  314. }
  315. portstr[0] = '\0';
  316. host = listen_addr;
  317. }
  318. g->listen_id = skynet_socket_listen(ctx, host, port, BACKLOG);
  319. if (g->listen_id < 0) {
  320. return 1;
  321. }
  322. skynet_socket_start(ctx, g->listen_id);
  323. return 0;
  324. }
  325. int
  326. gate_init(struct gate *g , struct skynet_context * ctx, char * parm) {
  327. if (parm == NULL)
  328. return 1;
  329. int max = 0;
  330. int sz = strlen(parm)+1;
  331. char watchdog[sz];
  332. char binding[sz];
  333. int client_tag = 0;
  334. char header;
  335. int n = sscanf(parm, "%c %s %s %d %d", &header, watchdog, binding, &client_tag, &max);
  336. if (n<4) {
  337. skynet_error(ctx, "Invalid gate parm %s",parm);
  338. return 1;
  339. }
  340. if (max <=0 ) {
  341. skynet_error(ctx, "Need max connection");
  342. return 1;
  343. }
  344. if (header != 'S' && header !='L') {
  345. skynet_error(ctx, "Invalid data header style");
  346. return 1;
  347. }
  348. if (client_tag == 0) {
  349. client_tag = PTYPE_CLIENT;
  350. }
  351. if (watchdog[0] == '!') {
  352. g->watchdog = 0;
  353. } else {
  354. g->watchdog = skynet_queryname(ctx, watchdog);
  355. if (g->watchdog == 0) {
  356. skynet_error(ctx, "Invalid watchdog %s",watchdog);
  357. return 1;
  358. }
  359. }
  360. g->ctx = ctx;
  361. hashid_init(&g->hash, max);
  362. g->conn = skynet_malloc(max * sizeof(struct connection));
  363. memset(g->conn, 0, max *sizeof(struct connection));
  364. g->max_connection = max;
  365. int i;
  366. for (i=0;i<max;i++) {
  367. g->conn[i].id = -1;
  368. }
  369. g->client_tag = client_tag;
  370. g->header_size = header=='S' ? 2 : 4;
  371. skynet_callback(ctx,g,_cb);
  372. return start_listen(g,binding);
  373. }