service_harbor.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. #include "skynet.h"
  2. #include "skynet_harbor.h"
  3. #include "skynet_socket.h"
  4. #include "skynet_handle.h"
  5. /*
  6. harbor listen the PTYPE_HARBOR (in text)
  7. N name : update the global name
  8. S fd id: connect to new harbor , we should send self_id to fd first , and then recv a id (check it), and at last send queue.
  9. A fd id: accept new harbor , we should send self_id to fd , and then send queue.
  10. If the fd is disconnected, send message to slave in PTYPE_TEXT. D id
  11. If we don't known a globalname, send message to slave in PTYPE_TEXT. Q name
  12. */
  13. #include <stdio.h>
  14. #include <stdlib.h>
  15. #include <stdbool.h>
  16. #include <string.h>
  17. #include <assert.h>
  18. #include <stdint.h>
  19. #include <unistd.h>
  20. #define HASH_SIZE 4096
  21. #define DEFAULT_QUEUE_SIZE 1024
  22. // 12 is sizeof(struct remote_message_header)
  23. #define HEADER_COOKIE_LENGTH 12
  24. /*
  25. message type (8bits) is in destination high 8bits
  26. harbor id (8bits) is also in that place , but remote message doesn't need harbor id.
  27. */
  28. struct remote_message_header {
  29. uint32_t source;
  30. uint32_t destination;
  31. uint32_t session;
  32. };
  33. struct harbor_msg {
  34. struct remote_message_header header;
  35. void * buffer;
  36. size_t size;
  37. };
  38. struct harbor_msg_queue {
  39. int size;
  40. int head;
  41. int tail;
  42. struct harbor_msg * data;
  43. };
  44. struct keyvalue {
  45. struct keyvalue * next;
  46. char key[GLOBALNAME_LENGTH];
  47. uint32_t hash;
  48. uint32_t value;
  49. struct harbor_msg_queue * queue;
  50. };
  51. struct hashmap {
  52. struct keyvalue *node[HASH_SIZE];
  53. };
  54. #define STATUS_WAIT 0
  55. #define STATUS_HANDSHAKE 1
  56. #define STATUS_HEADER 2
  57. #define STATUS_CONTENT 3
  58. #define STATUS_DOWN 4
  59. struct slave {
  60. int fd;
  61. struct harbor_msg_queue *queue;
  62. int status;
  63. int length;
  64. int read;
  65. uint8_t size[4];
  66. char * recv_buffer;
  67. };
  68. struct harbor {
  69. struct skynet_context *ctx;
  70. int id;
  71. uint32_t slave;
  72. struct hashmap * map;
  73. struct slave s[REMOTE_MAX];
  74. };
  75. // hash table
  76. static void
  77. push_queue_msg(struct harbor_msg_queue * queue, struct harbor_msg * m) {
  78. // If there is only 1 free slot which is reserved to distinguish full/empty
  79. // of circular buffer, expand it.
  80. if (((queue->tail + 1) % queue->size) == queue->head) {
  81. struct harbor_msg * new_buffer = skynet_malloc(queue->size * 2 * sizeof(struct harbor_msg));
  82. int i;
  83. for (i=0;i<queue->size-1;i++) {
  84. new_buffer[i] = queue->data[(i+queue->head) % queue->size];
  85. }
  86. skynet_free(queue->data);
  87. queue->data = new_buffer;
  88. queue->head = 0;
  89. queue->tail = queue->size - 1;
  90. queue->size *= 2;
  91. }
  92. struct harbor_msg * slot = &queue->data[queue->tail];
  93. *slot = *m;
  94. queue->tail = (queue->tail + 1) % queue->size;
  95. }
  96. static void
  97. push_queue(struct harbor_msg_queue * queue, void * buffer, size_t sz, struct remote_message_header * header) {
  98. struct harbor_msg m;
  99. m.header = *header;
  100. m.buffer = buffer;
  101. m.size = sz;
  102. push_queue_msg(queue, &m);
  103. }
  104. static struct harbor_msg *
  105. pop_queue(struct harbor_msg_queue * queue) {
  106. if (queue->head == queue->tail) {
  107. return NULL;
  108. }
  109. struct harbor_msg * slot = &queue->data[queue->head];
  110. queue->head = (queue->head + 1) % queue->size;
  111. return slot;
  112. }
  113. static struct harbor_msg_queue *
  114. new_queue() {
  115. struct harbor_msg_queue * queue = skynet_malloc(sizeof(*queue));
  116. queue->size = DEFAULT_QUEUE_SIZE;
  117. queue->head = 0;
  118. queue->tail = 0;
  119. queue->data = skynet_malloc(DEFAULT_QUEUE_SIZE * sizeof(struct harbor_msg));
  120. return queue;
  121. }
  122. static void
  123. release_queue(struct harbor_msg_queue *queue) {
  124. if (queue == NULL)
  125. return;
  126. struct harbor_msg * m;
  127. while ((m=pop_queue(queue)) != NULL) {
  128. skynet_free(m->buffer);
  129. }
  130. skynet_free(queue->data);
  131. skynet_free(queue);
  132. }
  133. static struct keyvalue *
  134. hash_search(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) {
  135. uint32_t *ptr = (uint32_t*) name;
  136. uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3];
  137. struct keyvalue * node = hash->node[h % HASH_SIZE];
  138. while (node) {
  139. if (node->hash == h && strncmp(node->key, name, GLOBALNAME_LENGTH) == 0) {
  140. return node;
  141. }
  142. node = node->next;
  143. }
  144. return NULL;
  145. }
  146. /*
  147. // Don't support erase name yet
  148. static struct void
  149. hash_erase(struct hashmap * hash, char name[GLOBALNAME_LENGTH) {
  150. uint32_t *ptr = name;
  151. uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3];
  152. struct keyvalue ** ptr = &hash->node[h % HASH_SIZE];
  153. while (*ptr) {
  154. struct keyvalue * node = *ptr;
  155. if (node->hash == h && strncmp(node->key, name, GLOBALNAME_LENGTH) == 0) {
  156. _release_queue(node->queue);
  157. *ptr->next = node->next;
  158. skynet_free(node);
  159. return;
  160. }
  161. *ptr = &(node->next);
  162. }
  163. }
  164. */
  165. static struct keyvalue *
  166. hash_insert(struct hashmap * hash, const char name[GLOBALNAME_LENGTH]) {
  167. uint32_t *ptr = (uint32_t *)name;
  168. uint32_t h = ptr[0] ^ ptr[1] ^ ptr[2] ^ ptr[3];
  169. struct keyvalue ** pkv = &hash->node[h % HASH_SIZE];
  170. struct keyvalue * node = skynet_malloc(sizeof(*node));
  171. memcpy(node->key, name, GLOBALNAME_LENGTH);
  172. node->next = *pkv;
  173. node->queue = NULL;
  174. node->hash = h;
  175. node->value = 0;
  176. *pkv = node;
  177. return node;
  178. }
  179. static struct hashmap *
  180. hash_new() {
  181. struct hashmap * h = skynet_malloc(sizeof(struct hashmap));
  182. memset(h,0,sizeof(*h));
  183. return h;
  184. }
  185. static void
  186. hash_delete(struct hashmap *hash) {
  187. int i;
  188. for (i=0;i<HASH_SIZE;i++) {
  189. struct keyvalue * node = hash->node[i];
  190. while (node) {
  191. struct keyvalue * next = node->next;
  192. release_queue(node->queue);
  193. skynet_free(node);
  194. node = next;
  195. }
  196. }
  197. skynet_free(hash);
  198. }
  199. ///////////////
  200. static void
  201. close_harbor(struct harbor *h, int id) {
  202. struct slave *s = &h->s[id];
  203. s->status = STATUS_DOWN;
  204. if (s->fd) {
  205. skynet_socket_close(h->ctx, s->fd);
  206. s->fd = 0;
  207. }
  208. if (s->queue) {
  209. release_queue(s->queue);
  210. s->queue = NULL;
  211. }
  212. }
  213. static void
  214. report_harbor_down(struct harbor *h, int id) {
  215. char down[64];
  216. int n = sprintf(down, "D %d",id);
  217. skynet_send(h->ctx, 0, h->slave, PTYPE_TEXT, 0, down, n);
  218. }
  219. struct harbor *
  220. harbor_create(void) {
  221. struct harbor * h = skynet_malloc(sizeof(*h));
  222. memset(h,0,sizeof(*h));
  223. h->map = hash_new();
  224. return h;
  225. }
  226. static void
  227. close_all_remotes(struct harbor *h) {
  228. int i;
  229. for (i=1;i<REMOTE_MAX;i++) {
  230. close_harbor(h,i);
  231. // don't call report_harbor_down.
  232. // never call skynet_send during module exit, because of dead lock
  233. }
  234. }
  235. void
  236. harbor_release(struct harbor *h) {
  237. close_all_remotes(h);
  238. hash_delete(h->map);
  239. skynet_free(h);
  240. }
  241. static inline void
  242. to_bigendian(uint8_t *buffer, uint32_t n) {
  243. buffer[0] = (n >> 24) & 0xff;
  244. buffer[1] = (n >> 16) & 0xff;
  245. buffer[2] = (n >> 8) & 0xff;
  246. buffer[3] = n & 0xff;
  247. }
  248. static inline void
  249. header_to_message(const struct remote_message_header * header, uint8_t * message) {
  250. to_bigendian(message , header->source);
  251. to_bigendian(message+4 , header->destination);
  252. to_bigendian(message+8 , header->session);
  253. }
  254. static inline uint32_t
  255. from_bigendian(uint32_t n) {
  256. union {
  257. uint32_t big;
  258. uint8_t bytes[4];
  259. } u;
  260. u.big = n;
  261. return u.bytes[0] << 24 | u.bytes[1] << 16 | u.bytes[2] << 8 | u.bytes[3];
  262. }
  263. static inline void
  264. message_to_header(const uint32_t *message, struct remote_message_header *header) {
  265. header->source = from_bigendian(message[0]);
  266. header->destination = from_bigendian(message[1]);
  267. header->session = from_bigendian(message[2]);
  268. }
  269. // socket package
  270. static void
  271. forward_local_messsage(struct harbor *h, void *msg, int sz) {
  272. const char * cookie = msg;
  273. cookie += sz - HEADER_COOKIE_LENGTH;
  274. struct remote_message_header header;
  275. message_to_header((const uint32_t *)cookie, &header);
  276. uint32_t destination = header.destination;
  277. int type = destination >> HANDLE_REMOTE_SHIFT;
  278. destination = (destination & HANDLE_MASK) | ((uint32_t)h->id << HANDLE_REMOTE_SHIFT);
  279. if (skynet_send(h->ctx, header.source, destination, type | PTYPE_TAG_DONTCOPY , (int)header.session, (void *)msg, sz-HEADER_COOKIE_LENGTH) < 0) {
  280. if (type != PTYPE_ERROR) {
  281. // don't need report error when type is error
  282. skynet_send(h->ctx, destination, header.source , PTYPE_ERROR, (int)header.session, NULL, 0);
  283. }
  284. skynet_error(h->ctx, "Unknown destination :%x from :%x type(%d)", destination, header.source, type);
  285. }
  286. }
  287. static void
  288. send_remote(struct skynet_context * ctx, int fd, const char * buffer, size_t sz, struct remote_message_header * cookie) {
  289. size_t sz_header = sz+sizeof(*cookie);
  290. if (sz_header > UINT32_MAX) {
  291. skynet_error(ctx, "remote message from :%08x to :%08x is too large.", cookie->source, cookie->destination);
  292. return;
  293. }
  294. uint8_t sendbuf[sz_header+4];
  295. to_bigendian(sendbuf, (uint32_t)sz_header);
  296. memcpy(sendbuf+4, buffer, sz);
  297. header_to_message(cookie, sendbuf+4+sz);
  298. struct socket_sendbuffer tmp;
  299. tmp.id = fd;
  300. tmp.type = SOCKET_BUFFER_RAWPOINTER;
  301. tmp.buffer = sendbuf;
  302. tmp.sz = sz_header+4;
  303. // ignore send error, because if the connection is broken, the mainloop will recv a message.
  304. skynet_socket_sendbuffer(ctx, &tmp);
  305. }
  306. static void
  307. dispatch_name_queue(struct harbor *h, struct keyvalue * node) {
  308. struct harbor_msg_queue * queue = node->queue;
  309. uint32_t handle = node->value;
  310. int harbor_id = handle >> HANDLE_REMOTE_SHIFT;
  311. struct skynet_context * context = h->ctx;
  312. struct slave *s = &h->s[harbor_id];
  313. int fd = s->fd;
  314. if (fd == 0) {
  315. if (s->status == STATUS_DOWN) {
  316. char tmp [GLOBALNAME_LENGTH+1];
  317. memcpy(tmp, node->key, GLOBALNAME_LENGTH);
  318. tmp[GLOBALNAME_LENGTH] = '\0';
  319. skynet_error(context, "Drop message to %s (in harbor %d)",tmp,harbor_id);
  320. } else {
  321. if (s->queue == NULL) {
  322. s->queue = node->queue;
  323. node->queue = NULL;
  324. } else {
  325. struct harbor_msg * m;
  326. while ((m = pop_queue(queue))!=NULL) {
  327. push_queue_msg(s->queue, m);
  328. }
  329. }
  330. if (harbor_id == (h->slave >> HANDLE_REMOTE_SHIFT)) {
  331. // the harbor_id is local
  332. struct harbor_msg * m;
  333. while ((m = pop_queue(s->queue)) != NULL) {
  334. int type = m->header.destination >> HANDLE_REMOTE_SHIFT;
  335. skynet_send(context, m->header.source, handle , type | PTYPE_TAG_DONTCOPY, m->header.session, m->buffer, m->size);
  336. }
  337. release_queue(s->queue);
  338. s->queue = NULL;
  339. }
  340. }
  341. return;
  342. }
  343. struct harbor_msg * m;
  344. while ((m = pop_queue(queue)) != NULL) {
  345. m->header.destination |= (handle & HANDLE_MASK);
  346. send_remote(context, fd, m->buffer, m->size, &m->header);
  347. skynet_free(m->buffer);
  348. }
  349. }
  350. static void
  351. dispatch_queue(struct harbor *h, int id) {
  352. struct slave *s = &h->s[id];
  353. int fd = s->fd;
  354. assert(fd != 0);
  355. struct harbor_msg_queue *queue = s->queue;
  356. if (queue == NULL)
  357. return;
  358. struct harbor_msg * m;
  359. while ((m = pop_queue(queue)) != NULL) {
  360. send_remote(h->ctx, fd, m->buffer, m->size, &m->header);
  361. skynet_free(m->buffer);
  362. }
  363. release_queue(queue);
  364. s->queue = NULL;
  365. }
  366. static void
  367. push_socket_data(struct harbor *h, const struct skynet_socket_message * message) {
  368. assert(message->type == SKYNET_SOCKET_TYPE_DATA);
  369. int fd = message->id;
  370. int i;
  371. int id = 0;
  372. struct slave * s = NULL;
  373. for (i=1;i<REMOTE_MAX;i++) {
  374. if (h->s[i].fd == fd) {
  375. s = &h->s[i];
  376. id = i;
  377. break;
  378. }
  379. }
  380. if (s == NULL) {
  381. skynet_error(h->ctx, "Invalid socket fd (%d) data", fd);
  382. return;
  383. }
  384. uint8_t * buffer = (uint8_t *)message->buffer;
  385. int size = message->ud;
  386. for (;;) {
  387. switch(s->status) {
  388. case STATUS_HANDSHAKE: {
  389. // check id
  390. uint8_t remote_id = buffer[0];
  391. if (remote_id != id) {
  392. skynet_error(h->ctx, "Invalid shakehand id (%d) from fd = %d , harbor = %d", id, fd, remote_id);
  393. close_harbor(h,id);
  394. return;
  395. }
  396. ++buffer;
  397. --size;
  398. s->status = STATUS_HEADER;
  399. dispatch_queue(h, id);
  400. if (size == 0) {
  401. break;
  402. }
  403. // go though
  404. }
  405. case STATUS_HEADER: {
  406. // big endian 4 bytes length, the first one must be 0.
  407. int need = 4 - s->read;
  408. if (size < need) {
  409. memcpy(s->size + s->read, buffer, size);
  410. s->read += size;
  411. return;
  412. } else {
  413. memcpy(s->size + s->read, buffer, need);
  414. buffer += need;
  415. size -= need;
  416. if (s->size[0] != 0) {
  417. skynet_error(h->ctx, "Message is too long from harbor %d", id);
  418. close_harbor(h,id);
  419. return;
  420. }
  421. s->length = s->size[1] << 16 | s->size[2] << 8 | s->size[3];
  422. s->read = 0;
  423. s->recv_buffer = skynet_malloc(s->length);
  424. s->status = STATUS_CONTENT;
  425. if (size == 0) {
  426. return;
  427. }
  428. }
  429. }
  430. // go though
  431. case STATUS_CONTENT: {
  432. int need = s->length - s->read;
  433. if (size < need) {
  434. memcpy(s->recv_buffer + s->read, buffer, size);
  435. s->read += size;
  436. return;
  437. }
  438. memcpy(s->recv_buffer + s->read, buffer, need);
  439. forward_local_messsage(h, s->recv_buffer, s->length);
  440. s->length = 0;
  441. s->read = 0;
  442. s->recv_buffer = NULL;
  443. size -= need;
  444. buffer += need;
  445. s->status = STATUS_HEADER;
  446. if (size == 0)
  447. return;
  448. break;
  449. }
  450. default:
  451. return;
  452. }
  453. }
  454. }
  455. static void
  456. update_name(struct harbor *h, const char name[GLOBALNAME_LENGTH], uint32_t handle) {
  457. struct keyvalue * node = hash_search(h->map, name);
  458. if (node == NULL) {
  459. node = hash_insert(h->map, name);
  460. }
  461. node->value = handle;
  462. if (node->queue) {
  463. dispatch_name_queue(h, node);
  464. release_queue(node->queue);
  465. node->queue = NULL;
  466. }
  467. }
  468. static int
  469. remote_send_handle(struct harbor *h, uint32_t source, uint32_t destination, int type, int session, const char * msg, size_t sz) {
  470. int harbor_id = destination >> HANDLE_REMOTE_SHIFT;
  471. struct skynet_context * context = h->ctx;
  472. if (harbor_id == h->id) {
  473. // local message
  474. skynet_send(context, source, destination , type | PTYPE_TAG_DONTCOPY, session, (void *)msg, sz);
  475. return 1;
  476. }
  477. struct slave * s = &h->s[harbor_id];
  478. if (s->fd == 0 || s->status == STATUS_HANDSHAKE) {
  479. if (s->status == STATUS_DOWN) {
  480. // throw an error return to source
  481. // report the destination is dead
  482. skynet_send(context, destination, source, PTYPE_ERROR, session, NULL, 0);
  483. skynet_error(context, "Drop message to harbor %d from %x to %x (session = %d, msgsz = %d)",harbor_id, source, destination,session,(int)sz);
  484. } else {
  485. if (s->queue == NULL) {
  486. s->queue = new_queue();
  487. }
  488. struct remote_message_header header;
  489. header.source = source;
  490. header.destination = (type << HANDLE_REMOTE_SHIFT) | (destination & HANDLE_MASK);
  491. header.session = (uint32_t)session;
  492. push_queue(s->queue, (void *)msg, sz, &header);
  493. return 1;
  494. }
  495. } else {
  496. struct remote_message_header cookie;
  497. cookie.source = source;
  498. cookie.destination = (destination & HANDLE_MASK) | ((uint32_t)type << HANDLE_REMOTE_SHIFT);
  499. cookie.session = (uint32_t)session;
  500. send_remote(context, s->fd, msg,sz,&cookie);
  501. }
  502. return 0;
  503. }
  504. static int
  505. remote_send_name(struct harbor *h, uint32_t source, const char name[GLOBALNAME_LENGTH], int type, int session, const char * msg, size_t sz) {
  506. struct keyvalue * node = hash_search(h->map, name);
  507. if (node == NULL) {
  508. node = hash_insert(h->map, name);
  509. }
  510. if (node->value == 0) {
  511. if (node->queue == NULL) {
  512. node->queue = new_queue();
  513. }
  514. struct remote_message_header header;
  515. header.source = source;
  516. header.destination = type << HANDLE_REMOTE_SHIFT;
  517. header.session = (uint32_t)session;
  518. push_queue(node->queue, (void *)msg, sz, &header);
  519. char query[2+GLOBALNAME_LENGTH+1] = "Q ";
  520. query[2+GLOBALNAME_LENGTH] = 0;
  521. memcpy(query+2, name, GLOBALNAME_LENGTH);
  522. skynet_send(h->ctx, 0, h->slave, PTYPE_TEXT, 0, query, strlen(query));
  523. return 1;
  524. } else {
  525. return remote_send_handle(h, source, node->value, type, session, msg, sz);
  526. }
  527. }
  528. static void
  529. handshake(struct harbor *h, int id) {
  530. struct slave *s = &h->s[id];
  531. uint8_t handshake[1] = { (uint8_t)h->id };
  532. struct socket_sendbuffer tmp;
  533. tmp.id = s->fd;
  534. tmp.type = SOCKET_BUFFER_RAWPOINTER;
  535. tmp.buffer = handshake;
  536. tmp.sz = 1;
  537. skynet_socket_sendbuffer(h->ctx, &tmp);
  538. }
  539. static void
  540. harbor_command(struct harbor * h, const char * msg, size_t sz, int session, uint32_t source) {
  541. const char * name = msg + 2;
  542. int s = (int)sz;
  543. s -= 2;
  544. switch(msg[0]) {
  545. case 'N' : {
  546. if (s <=0 || s>= GLOBALNAME_LENGTH) {
  547. skynet_error(h->ctx, "Invalid global name %s", name);
  548. return;
  549. }
  550. struct remote_name rn;
  551. memset(&rn, 0, sizeof(rn));
  552. memcpy(rn.name, name, s);
  553. rn.handle = source;
  554. update_name(h, rn.name, rn.handle);
  555. break;
  556. }
  557. case 'S' :
  558. case 'A' : {
  559. char buffer[s+1];
  560. memcpy(buffer, name, s);
  561. buffer[s] = 0;
  562. int fd=0, id=0;
  563. sscanf(buffer, "%d %d",&fd,&id);
  564. if (fd == 0 || id <= 0 || id>=REMOTE_MAX) {
  565. skynet_error(h->ctx, "Invalid command %c %s", msg[0], buffer);
  566. return;
  567. }
  568. struct slave * slave = &h->s[id];
  569. if (slave->fd != 0) {
  570. skynet_error(h->ctx, "Harbor %d alreay exist", id);
  571. return;
  572. }
  573. slave->fd = fd;
  574. skynet_socket_start(h->ctx, fd);
  575. handshake(h, id);
  576. if (msg[0] == 'S') {
  577. slave->status = STATUS_HANDSHAKE;
  578. } else {
  579. slave->status = STATUS_HEADER;
  580. dispatch_queue(h,id);
  581. }
  582. break;
  583. }
  584. default:
  585. skynet_error(h->ctx, "Unknown command %s", msg);
  586. return;
  587. }
  588. }
  589. static int
  590. harbor_id(struct harbor *h, int fd) {
  591. int i;
  592. for (i=1;i<REMOTE_MAX;i++) {
  593. struct slave *s = &h->s[i];
  594. if (s->fd == fd) {
  595. return i;
  596. }
  597. }
  598. return 0;
  599. }
  600. static int
  601. mainloop(struct skynet_context * context, void * ud, int type, int session, uint32_t source, const void * msg, size_t sz) {
  602. struct harbor * h = ud;
  603. switch (type) {
  604. case PTYPE_SOCKET: {
  605. const struct skynet_socket_message * message = msg;
  606. switch(message->type) {
  607. case SKYNET_SOCKET_TYPE_DATA:
  608. push_socket_data(h, message);
  609. skynet_free(message->buffer);
  610. break;
  611. case SKYNET_SOCKET_TYPE_ERROR:
  612. case SKYNET_SOCKET_TYPE_CLOSE: {
  613. int id = harbor_id(h, message->id);
  614. if (id) {
  615. report_harbor_down(h,id);
  616. } else {
  617. skynet_error(context, "Unknown fd (%d) closed", message->id);
  618. }
  619. break;
  620. }
  621. case SKYNET_SOCKET_TYPE_CONNECT:
  622. // fd forward to this service
  623. break;
  624. case SKYNET_SOCKET_TYPE_WARNING: {
  625. int id = harbor_id(h, message->id);
  626. if (id) {
  627. skynet_error(context, "message havn't send to Harbor (%d) reach %d K", id, message->ud);
  628. }
  629. break;
  630. }
  631. default:
  632. skynet_error(context, "recv invalid socket message type %d", type);
  633. break;
  634. }
  635. return 0;
  636. }
  637. case PTYPE_HARBOR: {
  638. harbor_command(h, msg,sz,session,source);
  639. return 0;
  640. }
  641. case PTYPE_SYSTEM : {
  642. // remote message out
  643. const struct remote_message *rmsg = msg;
  644. if (rmsg->destination.handle == 0) {
  645. if (remote_send_name(h, source , rmsg->destination.name, rmsg->type, session, rmsg->message, rmsg->sz)) {
  646. return 0;
  647. }
  648. } else {
  649. if (remote_send_handle(h, source , rmsg->destination.handle, rmsg->type, session, rmsg->message, rmsg->sz)) {
  650. return 0;
  651. }
  652. }
  653. skynet_free((void *)rmsg->message);
  654. return 0;
  655. }
  656. default:
  657. skynet_error(context, "recv invalid message from %x, type = %d", source, type);
  658. if (session != 0 && type != PTYPE_ERROR) {
  659. skynet_send(context,0,source,PTYPE_ERROR, session, NULL, 0);
  660. }
  661. return 0;
  662. }
  663. }
  664. int
  665. harbor_init(struct harbor *h, struct skynet_context *ctx, const char * args) {
  666. h->ctx = ctx;
  667. int harbor_id = 0;
  668. uint32_t slave = 0;
  669. sscanf(args,"%d %u", &harbor_id, &slave);
  670. if (slave == 0) {
  671. return 1;
  672. }
  673. h->id = harbor_id;
  674. h->slave = slave;
  675. if (harbor_id == 0) {
  676. close_all_remotes(h);
  677. }
  678. skynet_callback(ctx, h, mainloop);
  679. skynet_harbor_start(ctx);
  680. return 0;
  681. }