mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-23 19:43:09 +00:00
add snax.uniqueservice (and others)
This commit is contained in:
@@ -410,12 +410,20 @@ function skynet.newservice(name, ...)
|
||||
end
|
||||
end
|
||||
|
||||
function skynet.uniqueservice(...)
|
||||
return assert(skynet.call(".service", "lua", "LAUNCH", ...))
|
||||
function skynet.uniqueservice(global, ...)
|
||||
if global == true then
|
||||
return assert(skynet.call(".service", "lua", "GLAUNCH", ...))
|
||||
else
|
||||
return assert(skynet.call(".service", "lua", "LAUNCH", global, ...))
|
||||
end
|
||||
end
|
||||
|
||||
function skynet.queryservice(...)
|
||||
return assert(skynet.call(".service", "lua", "QUERY", ...))
|
||||
function skynet.queryservice(global, ...)
|
||||
if global == true then
|
||||
return assert(skynet.call(".service", "lua", "GQUERY", ...))
|
||||
else
|
||||
return assert(skynet.call(".service", "lua", "QUERY", global, ...))
|
||||
end
|
||||
end
|
||||
|
||||
local function group_command(cmd, handle, address)
|
||||
|
||||
@@ -64,16 +64,14 @@ end
|
||||
|
||||
local handle_cache = setmetatable( {} , { __mode = "kv" } )
|
||||
|
||||
function snax.newservice(name, ...)
|
||||
function snax.rawnewservice(name, ...)
|
||||
local t = snax.interface(name)
|
||||
local handle = skynet.newservice("snaxd", name)
|
||||
assert(handle_cache[handle] == nil)
|
||||
if t.system.init then
|
||||
skynet.call(handle, "lua", t.system.init, ...)
|
||||
end
|
||||
local ret = wrapper(handle, name, t)
|
||||
handle_cache[handle] = ret
|
||||
return ret
|
||||
return handle
|
||||
end
|
||||
|
||||
function snax.bind(handle, type)
|
||||
@@ -88,6 +86,39 @@ function snax.bind(handle, type)
|
||||
return ret
|
||||
end
|
||||
|
||||
function snax.newservice(name, ...)
|
||||
local handle = snax.rawnewservice(name, ...)
|
||||
return snax.bind(handle, name)
|
||||
end
|
||||
|
||||
local function service_name(global, name, ...)
|
||||
if global == true then
|
||||
return name
|
||||
else
|
||||
return global
|
||||
end
|
||||
end
|
||||
|
||||
function snax.uniqueservice(name, ...)
|
||||
local handle = assert(skynet.call(".service", "lua", "LAUNCH", "snaxd", name, ...))
|
||||
return snax.bind(handle, name)
|
||||
end
|
||||
|
||||
function snax.globalservice(name, ...)
|
||||
local handle = assert(skynet.call(".service", "lua", "GLAUNCH", "snaxd", name, ...))
|
||||
return snax.bind(handle, name)
|
||||
end
|
||||
|
||||
function snax.queryservice(name)
|
||||
local handle = assert(skynet.call(".service", "lua", "QUERY", "snaxd", name))
|
||||
return snax.bind(handle, name)
|
||||
end
|
||||
|
||||
function snax.queryglobal(name)
|
||||
local handle = assert(skynet.call(".service", "lua", "GQUERY", "snaxd", name))
|
||||
return snax.bind(handle, name)
|
||||
end
|
||||
|
||||
function snax.kill(obj, ...)
|
||||
local t = snax.interface(obj.type)
|
||||
skynet_call(obj.handle, "lua", t.system.exit, ...)
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
local skynet = require "skynet"
|
||||
local snax = require "snax"
|
||||
|
||||
local cmd = {}
|
||||
local service = {}
|
||||
@@ -57,33 +58,47 @@ local function waitfor(name , func, ...)
|
||||
return s
|
||||
end
|
||||
|
||||
function cmd.LAUNCH(service_name, ...)
|
||||
return waitfor(service_name, skynet.newservice, service_name, ...)
|
||||
function cmd.LAUNCH(service_name, subname, ...)
|
||||
if service_name == "snaxd" then
|
||||
return waitfor("snax."..subname, snax.rawnewservice, subname, ...)
|
||||
else
|
||||
return waitfor(service_name, skynet.newservice, service_name, subname, ...)
|
||||
end
|
||||
end
|
||||
|
||||
function cmd.QUERY(service_name)
|
||||
return waitfor(service_name)
|
||||
function cmd.QUERY(service_name, subname)
|
||||
if service_name == "snaxd" then
|
||||
return waitfor("snax."..subname)
|
||||
else
|
||||
return waitfor(service_name)
|
||||
end
|
||||
end
|
||||
|
||||
function cmd.GLAUNCH(...)
|
||||
if GLOBAL then
|
||||
return cmd.LAUNCH(...)
|
||||
else
|
||||
return waitfor(service_name, skynet.call, "SERVICE", "lua", "LAUNCH", ...)
|
||||
end
|
||||
end
|
||||
|
||||
function cmd.GQUERY(...)
|
||||
if GLOBAL then
|
||||
return cmd.QUERY(...)
|
||||
else
|
||||
return waitfor(service_name, skynet.call, "SERVICE", "lua", "QUERY", ...)
|
||||
end
|
||||
end
|
||||
|
||||
skynet.start(function()
|
||||
skynet.dispatch("lua", function(session, address, command, global, service_name , ...)
|
||||
skynet.dispatch("lua", function(session, address, command, ...)
|
||||
local f = cmd[command]
|
||||
if f == nil then
|
||||
skynet.ret(skynet.pack(nil, "Invalid command " .. command))
|
||||
return
|
||||
end
|
||||
|
||||
local ok, r
|
||||
if global == true then
|
||||
if GLOBAL then
|
||||
ok, r = pcall(f, service_name, ...)
|
||||
else
|
||||
return waitfor(service_name, skynet.call, "SERVICE", "lua", command, service_name, ...)
|
||||
end
|
||||
else
|
||||
assert(type(global) == "string") -- global is service_name
|
||||
ok, r = pcall(f, global, service_name, ...)
|
||||
end
|
||||
local ok, r = pcall(f, ...)
|
||||
|
||||
if ok then
|
||||
skynet.ret(skynet.pack(r))
|
||||
|
||||
Reference in New Issue
Block a user