manager.lua 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. local skynet = require "skynet"
  2. local c = require "skynet.core"
  3. function skynet.launch(...)
  4. local addr = c.command("LAUNCH", table.concat({...}," "))
  5. if addr then
  6. return tonumber("0x" .. string.sub(addr , 2))
  7. end
  8. end
  9. function skynet.kill(name)
  10. if type(name) == "number" then
  11. skynet.send(".launcher","lua","REMOVE",name, true)
  12. name = skynet.address(name)
  13. end
  14. c.command("KILL",name)
  15. end
  16. function skynet.abort()
  17. c.command("ABORT")
  18. end
  19. local function globalname(name, handle)
  20. local c = string.sub(name,1,1)
  21. assert(c ~= ':')
  22. if c == '.' then
  23. return false
  24. end
  25. assert(#name <= 16) -- GLOBALNAME_LENGTH is 16, defined in skynet_harbor.h
  26. assert(tonumber(name) == nil) -- global name can't be number
  27. local harbor = require "skynet.harbor"
  28. harbor.globalname(name, handle)
  29. return true
  30. end
  31. function skynet.register(name)
  32. if not globalname(name) then
  33. c.command("REG", name)
  34. end
  35. end
  36. function skynet.name(name, handle)
  37. if not globalname(name, handle) then
  38. c.command("NAME", name .. " " .. skynet.address(handle))
  39. end
  40. end
  41. local dispatch_message = skynet.dispatch_message
  42. function skynet.forward_type(map, start_func)
  43. c.callback(function(ptype, msg, sz, ...)
  44. local prototype = map[ptype]
  45. if prototype then
  46. dispatch_message(prototype, msg, sz, ...)
  47. else
  48. local ok, err = pcall(dispatch_message, ptype, msg, sz, ...)
  49. c.trash(msg, sz)
  50. if not ok then
  51. error(err)
  52. end
  53. end
  54. end, true)
  55. skynet.timeout(0, function()
  56. skynet.init_service(start_func)
  57. end)
  58. end
  59. function skynet.filter(f ,start_func)
  60. c.callback(function(...)
  61. dispatch_message(f(...))
  62. end)
  63. skynet.timeout(0, function()
  64. skynet.init_service(start_func)
  65. end)
  66. end
  67. function skynet.monitor(service, query)
  68. local monitor
  69. if query then
  70. monitor = skynet.queryservice(true, service)
  71. else
  72. monitor = skynet.uniqueservice(true, service)
  73. end
  74. assert(monitor, "Monitor launch failed")
  75. c.command("MONITOR", string.format(":%08x", monitor))
  76. return monitor
  77. end
  78. return skynet