add cluster.proxy and cluster.ncall

This commit is contained in:
Cloud Wu
2014-08-13 17:49:30 +08:00
parent cde423cb73
commit 978e010e67
11 changed files with 79 additions and 9 deletions

View File

@@ -4,7 +4,7 @@ local clusterd
local cluster = {}
function cluster.call(node, address, ...)
-- skynet.pack(...) will free by cluster.c.packrequest
-- skynet.pack(...) will free by cluster.core.packrequest
return skynet.call(clusterd, "lua", "req", node, address, skynet.pack(...))
end
@@ -20,6 +20,23 @@ function cluster.reload()
skynet.call(clusterd, "lua", "reload")
end
function cluster.proxy(node, name)
return skynet.call(clusterd, "lua", "proxy", node, name)
end
local namecache = {}
function cluster.ncall(name, ...)
local s = namecache[name]
if not s then
local node , lname = name:match "(.-)(%..+)"
assert(node and lname)
s = cluster.proxy(node, lname)
namecache[name] = assert(s)
end
return skynet.call(s, "lua", ...)
end
skynet.init(function()
clusterd = skynet.uniqueservice("clusterd")
end)

View File

@@ -99,6 +99,7 @@ local coroutine_yield = coroutine.yield
local function co_create(f)
local co = table.remove(coroutine_pool)
if co == nil then
local print = print
co = coroutine.create(function(...)
f(...)
while true do
@@ -548,7 +549,7 @@ end
function skynet.address(addr)
if type(addr) == "number" then
return string.format(":%x",addr)
return string.format(":%08x",addr)
else
return tostring(addr)
end
@@ -648,6 +649,21 @@ function skynet.filter(f ,start_func)
end)
end
function skynet.forward_type(map, start_func)
c.callback(function(ptype, msg, sz, ...)
local prototype = map[ptype]
if prototype then
dispatch_message(prototype, msg, sz, ...)
else
dispatch_message(ptype, msg, sz, ...)
c.trash(msg, sz)
end
end, true)
skynet.timeout(0, function()
init_service(start_func)
end)
end
function skynet.endless()
return c.command("ENDLESS")~=nil
end