skynet_harbor.c 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include "skynet.h"
  2. #include "skynet_harbor.h"
  3. #include "skynet_server.h"
  4. #include "skynet_mq.h"
  5. #include "skynet_handle.h"
  6. #include <string.h>
  7. #include <stdio.h>
  8. #include <assert.h>
  9. static struct skynet_context * REMOTE = 0;
  10. static unsigned int HARBOR = ~0;
  11. static inline int
  12. invalid_type(int type) {
  13. return type != PTYPE_SYSTEM && type != PTYPE_HARBOR;
  14. }
  15. void
  16. skynet_harbor_send(struct remote_message *rmsg, uint32_t source, int session) {
  17. assert(invalid_type(rmsg->type) && REMOTE);
  18. skynet_context_send(REMOTE, rmsg, sizeof(*rmsg) , source, PTYPE_SYSTEM , session);
  19. }
  20. int
  21. skynet_harbor_message_isremote(uint32_t handle) {
  22. assert(HARBOR != ~0);
  23. int h = (handle & ~HANDLE_MASK);
  24. return h != HARBOR && h !=0;
  25. }
  26. void
  27. skynet_harbor_init(int harbor) {
  28. HARBOR = (unsigned int)harbor << HANDLE_REMOTE_SHIFT;
  29. }
  30. void
  31. skynet_harbor_start(void *ctx) {
  32. // the HARBOR must be reserved to ensure the pointer is valid.
  33. // It will be released at last by calling skynet_harbor_exit
  34. skynet_context_reserve(ctx);
  35. REMOTE = ctx;
  36. }
  37. void
  38. skynet_harbor_exit() {
  39. struct skynet_context * ctx = REMOTE;
  40. REMOTE= NULL;
  41. if (ctx) {
  42. skynet_context_release(ctx);
  43. }
  44. }