cdummy.lua 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. local skynet = require "skynet"
  2. require "skynet.manager" -- import skynet.launch, ...
  3. local globalname = {}
  4. local queryname = {}
  5. local harbor = {}
  6. local harbor_service
  7. skynet.register_protocol {
  8. name = "harbor",
  9. id = skynet.PTYPE_HARBOR,
  10. pack = function(...) return ... end,
  11. unpack = skynet.tostring,
  12. }
  13. skynet.register_protocol {
  14. name = "text",
  15. id = skynet.PTYPE_TEXT,
  16. pack = function(...) return ... end,
  17. unpack = skynet.tostring,
  18. }
  19. local function response_name(name)
  20. local address = globalname[name]
  21. if queryname[name] then
  22. local tmp = queryname[name]
  23. queryname[name] = nil
  24. for _,resp in ipairs(tmp) do
  25. resp(true, address)
  26. end
  27. end
  28. end
  29. function harbor.REGISTER(name, handle)
  30. assert(globalname[name] == nil)
  31. globalname[name] = handle
  32. response_name(name)
  33. skynet.redirect(harbor_service, handle, "harbor", 0, "N " .. name)
  34. end
  35. function harbor.QUERYNAME(name)
  36. if name:byte() == 46 then -- "." , local name
  37. skynet.ret(skynet.pack(skynet.localname(name)))
  38. return
  39. end
  40. local result = globalname[name]
  41. if result then
  42. skynet.ret(skynet.pack(result))
  43. return
  44. end
  45. local queue = queryname[name]
  46. if queue == nil then
  47. queue = { skynet.response() }
  48. queryname[name] = queue
  49. else
  50. table.insert(queue, skynet.response())
  51. end
  52. end
  53. function harbor.LINK(id)
  54. skynet.ret()
  55. end
  56. function harbor.CONNECT(id)
  57. skynet.error("Can't connect to other harbor in single node mode")
  58. end
  59. skynet.start(function()
  60. local harbor_id = tonumber(skynet.getenv "harbor")
  61. assert(harbor_id == 0)
  62. skynet.dispatch("lua", function (session,source,command,...)
  63. local f = assert(harbor[command])
  64. f(...)
  65. end)
  66. skynet.dispatch("text", function(session,source,command)
  67. -- ignore all the command
  68. end)
  69. harbor_service = assert(skynet.launch("harbor", harbor_id, skynet.self()))
  70. end)