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