mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-24 20:23:06 +00:00
add module skynet.harbor
This commit is contained in:
@@ -190,12 +190,33 @@ function skynet.wait()
|
||||
session_id_coroutine[session] = nil
|
||||
end
|
||||
|
||||
local function globalname(name, handle)
|
||||
local c = string.sub(name,1,1)
|
||||
assert(c ~= ':')
|
||||
if c == '.' then
|
||||
return false
|
||||
end
|
||||
|
||||
assert(#name <= 16) -- GLOBALNAME_LENGTH is 16, defined in skynet_harbor.h
|
||||
assert(tonumber(name) == nil) -- global name can't be number
|
||||
|
||||
local harbor = require "skynet.harbor"
|
||||
|
||||
harbor.globalname(name, handle)
|
||||
|
||||
return true
|
||||
end
|
||||
|
||||
function skynet.register(name)
|
||||
c.command("REG", name)
|
||||
if not globalname(name) then
|
||||
c.command("REG", name)
|
||||
end
|
||||
end
|
||||
|
||||
function skynet.name(name, handle)
|
||||
c.command("NAME", name .. " " .. skynet.address(handle))
|
||||
if not globalname(name, handle) then
|
||||
c.command("NAME", name .. " " .. skynet.address(handle))
|
||||
end
|
||||
end
|
||||
|
||||
local self_handle
|
||||
|
||||
40
lualib/skynet/harbor.lua
Normal file
40
lualib/skynet/harbor.lua
Normal file
@@ -0,0 +1,40 @@
|
||||
local skynet = require "skynet"
|
||||
|
||||
local harbor = {}
|
||||
|
||||
local HARBOR = skynet.getenv "harbor_address"
|
||||
|
||||
if HARBOR then
|
||||
HARBOR = tonumber("0x" .. string.sub(HARBOR , 2))
|
||||
end
|
||||
|
||||
function harbor.globalname(name, handle)
|
||||
assert(HARBOR)
|
||||
handle = handle or skynet.self()
|
||||
skynet.redirect(HARBOR, handle, "system", 0, "R " .. name)
|
||||
end
|
||||
|
||||
function harbor.init(h)
|
||||
assert(HARBOR == nil)
|
||||
HARBOR = h
|
||||
skynet.setenv("harbor_address", skynet.address(h))
|
||||
end
|
||||
|
||||
function harbor.link(id)
|
||||
assert(HARBOR)
|
||||
skynet.call(HARBOR, "system", "M " .. tostring(id))
|
||||
end
|
||||
|
||||
function harbor.connect(id)
|
||||
assert(HARBOR)
|
||||
skynet.call(HARBOR, "system", "C " .. tostring(id))
|
||||
end
|
||||
|
||||
skynet.register_protocol {
|
||||
name = "system",
|
||||
id = skynet.PTYPE_SYSTEM,
|
||||
pack = function(...) return ... end,
|
||||
unpack = skynet.tostring,
|
||||
}
|
||||
|
||||
return harbor
|
||||
Reference in New Issue
Block a user