From df5c619a29c2d89adf7bffeb18eacb3d156539fb Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 16 Apr 2014 20:12:17 +0800 Subject: [PATCH 1/5] snax : a simple skynet auxliliary framework --- examples/config | 1 + lualib/snax.lua | 98 +++++++++++++++++++++++++++++++++++++++ lualib/snax_interface.lua | 84 +++++++++++++++++++++++++++++++++ service/snaxd.lua | 33 +++++++++++++ test/pingserver.lua | 28 +++++------ test/testping.lua | 13 +++--- 6 files changed, 235 insertions(+), 22 deletions(-) create mode 100644 lualib/snax.lua create mode 100644 lualib/snax_interface.lua create mode 100644 service/snaxd.lua diff --git a/examples/config b/examples/config index 4243a214..a6d9f5ad 100644 --- a/examples/config +++ b/examples/config @@ -7,5 +7,6 @@ master = "127.0.0.1:2013" start = "main" standalone = "0.0.0.0:2013" luaservice = root.."service/?.lua;"..root.."test/?.lua;"..root.."examples/?.lua" +snax = root.."examples/?.lua;"..root.."test/?.lua" cpath = root.."cservice/?.so" redis = root .. "examples/redisconf" diff --git a/lualib/snax.lua b/lualib/snax.lua new file mode 100644 index 00000000..cca3032e --- /dev/null +++ b/lualib/snax.lua @@ -0,0 +1,98 @@ +local skynet = require "skynet" +local snax_interface = require "snax_interface" + +local snax = {} +local typeclass = {} + +local G = { require = function() end } + +function snax.interface(name) + if typeclass[name] then + return typeclass[name] + end + + local si = snax_interface(name, G) + + local ret = { + subscribe = {}, + response = {}, + system = {}, + } + + for _,v in ipairs(si) do + local id, group, name, f = table.unpack(v) + if group == "system" then + if f then + ret.system[name] = id + end + else + ret[group][name] = id + end + end + + typeclass[name] = ret + return ret +end + +local meta = { __tostring = function(v) return string.format("[%s:%x]", v.type, v.handle) end} + +local skynet_send = skynet.send +local skynet_call = skynet.call + +local function gen_pub(type, handle) + return setmetatable({} , { + __index = function( t, k ) + local id = assert(type.subscribe[k] , string.format("publish %s no exist", k)) + return function(...) + skynet_send(handle, "lua", id, ...) + end + end }) +end + +local function gen_req(type, handle) + return setmetatable({} , { + __index = function( t, k ) + local id = assert(type.response[k] , string.format("request %s no exist", k)) + return function(...) + return skynet_call(handle, "lua", id, ...) + end + end }) +end + +local function wrapper(handle, name, type) + return setmetatable ({ + pub = gen_pub(type, handle), + req = gen_req(type, handle), + type = name, + handle = handle, + kill = function(...) return skynet_call(handle, "lua", type.system.exit, ...) end, + }, meta) +end + +local handle_cache = setmetatable( {} , { __mode = "kv" } ) + +function snax.newservice(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 +end + +function snax.bind(handle, type) + local ret = handle_cache[handle] + if ret then + assert(ret.type == type) + return ret + end + local t = snax.interface(type) + ret = wrapper(handle, type, t) + handle_cache[handle] = ret + return ret +end + +return snax diff --git a/lualib/snax_interface.lua b/lualib/snax_interface.lua new file mode 100644 index 00000000..7f4c24dc --- /dev/null +++ b/lualib/snax_interface.lua @@ -0,0 +1,84 @@ +local skynet = require "skynet" + +return function (name , G) + local mainfunc + + local function func_id(id, group) + local tmp = {} + local function count( _, name, func) + if type(name) ~= "string" then + error (string.format("%s method only support string", group)) + end + if type(func) ~= "function" then + error (string.format("%s.%s must be function"), group, name) + end + if tmp[name] then + error (string.format("%s.%s duplicate definition", group, name)) + end + tmp[name] = true + table.insert(id, { #id + 1, group, name, func} ) + end + return setmetatable({}, { __newindex = count }) + end + + do + assert(getmetatable(G) == nil) + assert(G.init == nil) + assert(G.exit == nil) + + assert(G.subscribe == nil) + assert(G.response == nil) + end + + local env = {} + local func = {} + + local system = { "init", "exit" } + + do + for k, v in ipairs(system) do + system[v] = k + func[k] = { k , "system", v } + end + end + + env.subscribe = func_id(func, "subscribe") + env.response = func_id(func, "response") + + local function init_system(t, name, f) + local index = assert(system[name] , string.format("Not support global var %s", name)) + if type(f) ~= "function" then + error (string.format("%s must be a function", name)) + end + func[index][4] = f + end + + setmetatable(G, { __index = env , __newindex = init_system }) + + do + local path = skynet.getenv "snax" + + local errlist = {} + + for pat in string.gmatch(path,"[^;]+") do + local filename = string.gsub(pat, "?", name) + local f , err = loadfile(filename, "bt", G) + if f then + mainfunc = f + break + else + table.insert(errlist, err) + end + end + + if mainfunc == nil then + error(table.concat(errlist, "\n")) + end + end + + mainfunc() + + setmetatable(G, nil) + + return func +end diff --git a/service/snaxd.lua b/service/snaxd.lua new file mode 100644 index 00000000..6bd68086 --- /dev/null +++ b/service/snaxd.lua @@ -0,0 +1,33 @@ +local skynet = require "skynet" +local snax_interface = require "snax_interface" + +local func = snax_interface(tostring(...), _ENV) + +skynet.start(function() + local init = false + skynet.dispatch("lua", function ( _, _, id, ...) + local method = func[id] + if method[2] == "system" then + if method[3] == "init" then + assert(not init, "Already init") + local initfunc = method[4] or function() end + skynet.ret(skynet.pack(initfunc(...))) + init = true + else + assert(init, "Never init") + local exitfunc = method[4] or function() end + skynet.ret(skynet.pack(exitfunc(...))) + init = false + skynet.exit() + end + else + assert(init, "Init first") + if method[2] == "subscribe" then + -- no return + method[4](...) + else + skynet.ret(skynet.pack(method[4](...))) + end + end + end) +end) diff --git a/test/pingserver.lua b/test/pingserver.lua index 49496608..aeeda6cb 100644 --- a/test/pingserver.lua +++ b/test/pingserver.lua @@ -1,27 +1,23 @@ local skynet = require "skynet" -local command = {} - -function command.PING(hello) +function response.ping(hello) + skynet.sleep(100) return hello end -function command.HELLO() - skynet.sleep(100) - return "hello" +function subscribe.hello() + print "hello" end -function command.EXIT() - skynet.exit() -end - -function command.ERROR() +function response.error() error "throw an error" end +function init( ... ) + print ("ping server start:", ...) +end -skynet.start(function() - skynet.dispatch("lua", function(session,addr, cmd, ...) - skynet.ret(skynet.pack(command[cmd](...))) - end) -end) +function exit(...) + print ("ping server exit:", ...) + return "Exit" +end diff --git a/test/testping.lua b/test/testping.lua index 59a2ad5d..9a141b43 100644 --- a/test/testping.lua +++ b/test/testping.lua @@ -1,10 +1,11 @@ local skynet = require "skynet" +local snax = require "snax" skynet.start(function() - local ps = skynet.uniqueservice("pingserver") - skynet.watch(ps) - print(pcall(skynet.call,ps,"lua","ERROR")) - print(skynet.call(ps, "lua", "PING", "hello")) - print(skynet.call(ps, "lua", "PING", "hay")) - skynet.call(ps, "lua", "EXIT") + local ps = snax.newservice ("pingserver", "hello world") + print(ps.pub.hello()) + print(ps.req.ping("foobar")) + print(pcall(ps.req.error)) + print(ps.kill("exit")) + skynet.exit() end) From 58dfe38c4b27db5dc85b764e9fd8a89260c82e17 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Wed, 16 Apr 2014 23:23:51 +0800 Subject: [PATCH 2/5] hotfix snax supported --- lualib/snax.lua | 27 +++++++--- lualib/snax_hotfix.lua | 107 ++++++++++++++++++++++++++++++++++++++ lualib/snax_interface.lua | 7 +-- service/snaxd.lua | 13 ++++- test/pingserver.lua | 6 ++- test/testping.lua | 4 +- 6 files changed, 150 insertions(+), 14 deletions(-) create mode 100644 lualib/snax_hotfix.lua diff --git a/lualib/snax.lua b/lualib/snax.lua index cca3032e..6af7fbd6 100644 --- a/lualib/snax.lua +++ b/lualib/snax.lua @@ -21,13 +21,7 @@ function snax.interface(name) for _,v in ipairs(si) do local id, group, name, f = table.unpack(v) - if group == "system" then - if f then - ret.system[name] = id - end - else - ret[group][name] = id - end + ret[group][name] = id end typeclass[name] = ret @@ -65,7 +59,6 @@ local function wrapper(handle, name, type) req = gen_req(type, handle), type = name, handle = handle, - kill = function(...) return skynet_call(handle, "lua", type.system.exit, ...) end, }, meta) end @@ -95,4 +88,22 @@ function snax.bind(handle, type) return ret end +function snax.kill(obj, ...) + local t = snax.interface(obj.type) + return skynet_call(obj.handle, "lua", t.system.exit, ...) +end + +local function test_result(ok, ...) + if ok then + return ... + else + error(...) + end +end + +function snax.hotfix(obj, ...) + local t = snax.interface(obj.type) + test_result(skynet_call(obj.handle, "lua", t.system.hotfix, obj.type, ...)) +end + return snax diff --git a/lualib/snax_hotfix.lua b/lualib/snax_hotfix.lua new file mode 100644 index 00000000..bbe5c214 --- /dev/null +++ b/lualib/snax_hotfix.lua @@ -0,0 +1,107 @@ +local si = require "snax_interface" +local io = io + +local hotfix = {} + +local function loader(filename, ...) + local f = io.open(filename, "rb") + if not f then + return false, string.format("Can't open %s", filename) + end + local source = f:read "*a" + f:close() + + return load(source, "@"..filename, ...) +end + +local function check_funcs(f1,f2) + for k,a in pairs(f1) do + local b = f2[k] + assert(a[1] == b[1] and a[2] == b[2] and a[3] == b[3] , + string.format("%s.%s %s.%s , function mismatch", a[2], a[3] , b[2], b[3])) + end +end + +local function get_upvalues(f, global) + local uv = {} + local i = 1 + while true do + local name = debug.getupvalue(f, i) + if name then + local uid = debug.upvalueid(f, i) + uv[name] = i + if global[name] and global[name][1] ~= uid then + error(string.format("ambiguity upvalue %s", name)) + end + global[name] = { uid , f , i } + else + break + end + i = i + 1 + end + return uv +end + +local function join_upvalues(f, new_f, uv) + local i = 1 + while true do + local name = debug.getupvalue(new_f, i) + if name then + if uv[name] then + debug.upvaluejoin(new_f, i, f, uv[name]) + end + else + break + end + i = i + 1 + end +end + +local function join_global_upvalues(f, global) + local i = 1 + while true do + local name = debug.getupvalue(f, i) + if name then + local uv = global[name] + if uv then + debug.upvaluejoin(f, i, uv[2], uv[3]) + end + else + break + end + i = i + 1 + end +end + +local function update_upvalues(funcs, newfuncs) + local global = {} + for id,v in pairs(funcs) do + local f = v[4] + local new_f = newfuncs[id][4] + if f and new_f then + local uv = get_upvalues(f, global) + v[4] = new_f + join_upvalues(f, new_f, uv) + newfuncs[id] = nil + end + end + for id,v in pairs(newfuncs) do + if v[2] == "system" then + funcs[id] = v + local f = v[4] + join_global_upvalues(f, global) + else + error(string.format("Detect unkown function %s.%s", v[2], v[3])) + end + end +end + +local function inject(funcs, new) + check_funcs(funcs, new) + update_upvalues(funcs, new) +end + +return function (funcs, modname) + local new_funcs = si(modname, _ENV, loader) + return pcall(inject, funcs, new_funcs) +end diff --git a/lualib/snax_interface.lua b/lualib/snax_interface.lua index 7f4c24dc..2dbb06b2 100644 --- a/lualib/snax_interface.lua +++ b/lualib/snax_interface.lua @@ -1,6 +1,7 @@ local skynet = require "skynet" -return function (name , G) +return function (name , G, loader) + loader = loader or loadfile local mainfunc local function func_id(id, group) @@ -33,7 +34,7 @@ return function (name , G) local env = {} local func = {} - local system = { "init", "exit" } + local system = { "init", "exit", "hotfix" } do for k, v in ipairs(system) do @@ -62,7 +63,7 @@ return function (name , G) for pat in string.gmatch(path,"[^;]+") do local filename = string.gsub(pat, "?", name) - local f , err = loadfile(filename, "bt", G) + local f , err = loader(filename, "bt", G) if f then mainfunc = f break diff --git a/service/snaxd.lua b/service/snaxd.lua index 6bd68086..2b62ddee 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -8,13 +8,24 @@ skynet.start(function() skynet.dispatch("lua", function ( _, _, id, ...) local method = func[id] if method[2] == "system" then - if method[3] == "init" then + local command = method[3] + if command == "hotfix" then + local hotfix = require "snax_hotfix" + local ok, msg = hotfix(func, ...) + if ok then + local hf = func[id][4] + skynet.ret(skynet.pack(pcall(hf, select(2,...)))) + else + skynet.ret(skynet.pack(ok, msg)) + end + elseif command == "init" then assert(not init, "Already init") local initfunc = method[4] or function() end skynet.ret(skynet.pack(initfunc(...))) init = true else assert(init, "Never init") + assert(command == "exit") local exitfunc = method[4] or function() end skynet.ret(skynet.pack(exitfunc(...))) init = false diff --git a/test/pingserver.lua b/test/pingserver.lua index aeeda6cb..cedc26d3 100644 --- a/test/pingserver.lua +++ b/test/pingserver.lua @@ -1,12 +1,16 @@ local skynet = require "skynet" +local i = 0 +local hello = "hello" + function response.ping(hello) skynet.sleep(100) return hello end function subscribe.hello() - print "hello" + i = i + 1 + print (i, hello) end function response.error() diff --git a/test/testping.lua b/test/testping.lua index 9a141b43..512cbe31 100644 --- a/test/testping.lua +++ b/test/testping.lua @@ -6,6 +6,8 @@ skynet.start(function() print(ps.pub.hello()) print(ps.req.ping("foobar")) print(pcall(ps.req.error)) - print(ps.kill("exit")) + snax.hotfix(ps) + print(ps.pub.hello()) + print(snax.kill(ps,"exit")) skynet.exit() end) From 6d4d9f87585f00c7220eca63c5de8f16a2b14735 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Apr 2014 13:49:25 +0800 Subject: [PATCH 3/5] use a patch to hotfix --- lualib/snax.lua | 4 +- lualib/snax_hotfix.lua | 149 +++++++++++++++++++---------------------- service/snaxd.lua | 8 +-- test/testping.lua | 18 ++++- 4 files changed, 88 insertions(+), 91 deletions(-) diff --git a/lualib/snax.lua b/lualib/snax.lua index 6af7fbd6..fd476528 100644 --- a/lualib/snax.lua +++ b/lualib/snax.lua @@ -101,9 +101,9 @@ local function test_result(ok, ...) end end -function snax.hotfix(obj, ...) +function snax.hotfix(obj, source, ...) local t = snax.interface(obj.type) - test_result(skynet_call(obj.handle, "lua", t.system.hotfix, obj.type, ...)) + return test_result(skynet_call(obj.handle, "lua", t.system.hotfix, source, ...)) end return snax diff --git a/lualib/snax_hotfix.lua b/lualib/snax_hotfix.lua index bbe5c214..f42ab0c6 100644 --- a/lualib/snax_hotfix.lua +++ b/lualib/snax_hotfix.lua @@ -3,105 +3,92 @@ local io = io local hotfix = {} -local function loader(filename, ...) - local f = io.open(filename, "rb") - if not f then - return false, string.format("Can't open %s", filename) - end - local source = f:read "*a" - f:close() - - return load(source, "@"..filename, ...) -end - -local function check_funcs(f1,f2) - for k,a in pairs(f1) do - local b = f2[k] - assert(a[1] == b[1] and a[2] == b[2] and a[3] == b[3] , - string.format("%s.%s %s.%s , function mismatch", a[2], a[3] , b[2], b[3])) - end -end - -local function get_upvalues(f, global) - local uv = {} +local function collect_uv(f , uv) local i = 1 while true do - local name = debug.getupvalue(f, i) - if name then - local uid = debug.upvalueid(f, i) - uv[name] = i - if global[name] and global[name][1] ~= uid then - error(string.format("ambiguity upvalue %s", name)) - end - global[name] = { uid , f , i } - else + local name, value = debug.getupvalue(f, i) + if name == nil then break end - i = i + 1 - end - return uv -end + local id = debug.upvalueid(f, i) -local function join_upvalues(f, new_f, uv) - local i = 1 - while true do - local name = debug.getupvalue(new_f, i) - if name then - if uv[name] then - debug.upvaluejoin(new_f, i, f, uv[name]) - end + if uv[name] then + assert(uv[name].id == id, string.format("ambiguity local value %s", name)) else - break + uv[name] = { func = f, index = i, id = id } + + if type(value) == "function" then + collect_uv(value, uv) + end end + i = i + 1 end end -local function join_global_upvalues(f, global) - local i = 1 - while true do - local name = debug.getupvalue(f, i) - if name then - local uv = global[name] - if uv then - debug.upvaluejoin(f, i, uv[2], uv[3]) - end - else - break - end - i = i + 1 - end -end - -local function update_upvalues(funcs, newfuncs) +local function collect_all_uv(funcs) local global = {} - for id,v in pairs(funcs) do - local f = v[4] - local new_f = newfuncs[id][4] - if f and new_f then - local uv = get_upvalues(f, global) - v[4] = new_f - join_upvalues(f, new_f, uv) - newfuncs[id] = nil + for _, v in pairs(funcs) do + if v[4] then + collect_uv(v[4], global) end end - for id,v in pairs(newfuncs) do - if v[2] == "system" then - funcs[id] = v - local f = v[4] - join_global_upvalues(f, global) - else - error(string.format("Detect unkown function %s.%s", v[2], v[3])) + + return global +end + +local function loader(source) + return function (filename, ...) + return load(source, "=patch", ...) + end +end + +local function find_func(funcs, group , name) + for _, desc in pairs(funcs) do + local _, g, n = table.unpack(desc) + if group == g and name == n then + return desc end end end -local function inject(funcs, new) - check_funcs(funcs, new) - update_upvalues(funcs, new) +local dummy_env = {} + +local function patch_func(funcs, global, group, name, f) + local desc = assert(find_func(funcs, group, name) , string.format("Patch mismatch %s.%s", group, name)) + local i = 1 + while true do + local name, value = debug.getupvalue(f, i) + if name == nil then + break + elseif value == nil or value == dummy_env then + local old_uv = global[name] + if old_uv then + debug.upvaluejoin(f, i, old_uv.func, old_uv.index) + end + end + i = i + 1 + end + desc[4] = f end -return function (funcs, modname) - local new_funcs = si(modname, _ENV, loader) - return pcall(inject, funcs, new_funcs) +local function inject(funcs, source, ...) + local patch = si("patch", dummy_env, loader(source)) + local global = collect_all_uv(funcs) + + for _, v in pairs(patch) do + local _, group, name, f = table.unpack(v) + if f then + patch_func(funcs, global, group, name, f) + end + end + + local hf = find_func(patch, "system", "hotfix") + if hf and hf[4] then + return hf[4](...) + end +end + +return function (funcs, source, ...) + return pcall(inject, funcs, source, ...) end diff --git a/service/snaxd.lua b/service/snaxd.lua index 2b62ddee..519c45e4 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -11,13 +11,7 @@ skynet.start(function() local command = method[3] if command == "hotfix" then local hotfix = require "snax_hotfix" - local ok, msg = hotfix(func, ...) - if ok then - local hf = func[id][4] - skynet.ret(skynet.pack(pcall(hf, select(2,...)))) - else - skynet.ret(skynet.pack(ok, msg)) - end + skynet.ret(skynet.pack(hotfix(func, ...))) elseif command == "init" then assert(not init, "Already init") local initfunc = method[4] or function() end diff --git a/test/testping.lua b/test/testping.lua index 512cbe31..7952f20b 100644 --- a/test/testping.lua +++ b/test/testping.lua @@ -6,7 +6,23 @@ skynet.start(function() print(ps.pub.hello()) print(ps.req.ping("foobar")) print(pcall(ps.req.error)) - snax.hotfix(ps) + print("Hotfix (i) :", snax.hotfix(ps, [[ + +local i +local hello + +function subscribe.hello() + i = i + 1 + print ("fix", i, hello) +end + +function hotfix(...) + local temp = i + i = 100 + return temp +end + + ]])) print(ps.pub.hello()) print(snax.kill(ps,"exit")) skynet.exit() From 129b1cd90e03598b74bc179dc0d5a0c211b4017f Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Apr 2014 15:37:01 +0800 Subject: [PATCH 4/5] queue mode --- lualib/snax.lua | 2 +- service/snaxd.lua | 77 ++++++++++++++++++++++++++++++++++++++++++--- test/pingserver.lua | 3 ++ test/testping.lua | 2 +- 4 files changed, 77 insertions(+), 7 deletions(-) diff --git a/lualib/snax.lua b/lualib/snax.lua index fd476528..e60aa293 100644 --- a/lualib/snax.lua +++ b/lualib/snax.lua @@ -90,7 +90,7 @@ end function snax.kill(obj, ...) local t = snax.interface(obj.type) - return skynet_call(obj.handle, "lua", t.system.exit, ...) + skynet_call(obj.handle, "lua", t.system.exit, ...) end local function test_result(ok, ...) diff --git a/service/snaxd.lua b/service/snaxd.lua index 519c45e4..59fe756a 100644 --- a/service/snaxd.lua +++ b/service/snaxd.lua @@ -1,12 +1,70 @@ local skynet = require "skynet" +local c = require "skynet.c" local snax_interface = require "snax_interface" local func = snax_interface(tostring(...), _ENV) +local mode +local thread_id +local message_queue = {} +local init = false + +local function do_func(f, msg) + return pcall(f, table.unpack(msg)) +end + +local function dispatch(f, ...) + return skynet.pack(f(...)) +end + +local function message_dispatch() + while true do + if #message_queue==0 then + thread_id = coroutine.running() + skynet.wait() + else + local msg = table.remove(message_queue,1) + local method = msg.method + local f = method[4] + if f then + if method[2] == "subscribe" then + -- no return + local ok, data = pcall(f, table.unpack(msg)) + if not ok then + print(string.format("Error on [:%x] to [:%x] %s", msg.source, skynet.self(), tostring(data))) + end + else + local ok, data, size = pcall(dispatch, f, table.unpack(msg)) + if ok then + -- skynet.PTYPE_RESPONSE == 1 + c.send(msg.source, 1, msg.session, data, size) + if method[2] == "system" then + init = false + skynet.exit() + break + end + else + -- Can't throw error, so print it directly + print(string.format("Error on [:%x] to [:%x] %s", msg.source, skynet.self(), tostring(data))) + c.send(msg.source, skynet.PTYPE_ERROR, msg.session, "") + end + end + end + end + end +end + +local function queue( session, source, method, ...) + table.insert(message_queue, {session = session, source = source, method = method, ... }) + if thread_id then + skynet.wakeup(thread_id) + thread_id = nil + end +end skynet.start(function() - local init = false - skynet.dispatch("lua", function ( _, _, id, ...) + skynet.dispatch("lua", function ( session , source , id, ...) local method = func[id] + if method[2] == "system" then local command = method[3] if command == "hotfix" then @@ -15,19 +73,28 @@ skynet.start(function() elseif command == "init" then assert(not init, "Already init") local initfunc = method[4] or function() end - skynet.ret(skynet.pack(initfunc(...))) + mode = initfunc(...) + if mode == "queue" then + skynet.fork(message_dispatch) + end + skynet.ret() init = true + elseif mode == "queue" then + queue( session, source, method , ...) else assert(init, "Never init") assert(command == "exit") local exitfunc = method[4] or function() end - skynet.ret(skynet.pack(exitfunc(...))) + exitfunc(...) + skynet.ret() init = false skynet.exit() end else assert(init, "Init first") - if method[2] == "subscribe" then + if mode == "queue" then + queue(session, source, method , ...) + elseif method[2] == "subscribe" then -- no return method[4](...) else diff --git a/test/pingserver.lua b/test/pingserver.lua index cedc26d3..2a266860 100644 --- a/test/pingserver.lua +++ b/test/pingserver.lua @@ -19,6 +19,9 @@ end function init( ... ) print ("ping server start:", ...) + +-- You can return "queue" for queue service mode +-- return "queue" end function exit(...) diff --git a/test/testping.lua b/test/testping.lua index 7952f20b..1244551e 100644 --- a/test/testping.lua +++ b/test/testping.lua @@ -3,8 +3,8 @@ local snax = require "snax" skynet.start(function() local ps = snax.newservice ("pingserver", "hello world") - print(ps.pub.hello()) print(ps.req.ping("foobar")) + print(ps.pub.hello()) print(pcall(ps.req.error)) print("Hotfix (i) :", snax.hotfix(ps, [[ From 6c6f9d6facdd3e0829a94fa7d8e960689fd80246 Mon Sep 17 00:00:00 2001 From: Cloud Wu Date: Thu, 17 Apr 2014 15:37:46 +0800 Subject: [PATCH 5/5] no return value for exit() --- test/pingserver.lua | 1 - 1 file changed, 1 deletion(-) diff --git a/test/pingserver.lua b/test/pingserver.lua index 2a266860..f36166bb 100644 --- a/test/pingserver.lua +++ b/test/pingserver.lua @@ -26,5 +26,4 @@ end function exit(...) print ("ping server exit:", ...) - return "Exit" end