move lualib/snax_* to lualib/snax/

This commit is contained in:
Cloud Wu
2014-04-29 17:05:27 +08:00
parent f161dbb0ea
commit a4a21bd793
7 changed files with 124 additions and 119 deletions

View File

@@ -399,7 +399,7 @@ local function raw_dispatch_message(prototype, msg, sz, session, source, ...)
session_coroutine_address[co] = source
suspend(co, coroutine.resume(co, session,source, p.unpack(msg,sz, ...)))
else
unknown_request(session, source, msg, sz)
unknown_request(session, source, msg, sz)
end
end
end
@@ -426,8 +426,7 @@ local function dispatch_message(...)
end
function skynet.newservice(name, ...)
local param = table.concat({"snlua", name, ...}, " ")
local handle = skynet.call(".launcher", "text" , param)
local handle = skynet.tostring(skynet.rawcall(".launcher", "lua" , skynet.pack("LAUNCH", "snlua", name, ...)))
if handle == "" then
return nil
else
@@ -451,14 +450,6 @@ function skynet.queryservice(global, ...)
end
end
local function group_command(cmd, handle, address)
if address then
return string.format("%s %d :%x",cmd, handle, address)
else
return string.format("%s %d",cmd,handle)
end
end
function skynet.address(addr)
if type(addr) == "number" then
return string.format(":%x",addr)
@@ -479,66 +470,10 @@ function skynet.error(...)
return c.error(table.concat(t, " "))
end
----- debug
local internal_info_func
function skynet.info_func(func)
internal_info_func = func
end
local dbgcmd = {}
function dbgcmd.MEM()
local kb, bytes = collectgarbage "count"
skynet.ret(skynet.pack(kb,bytes))
end
function dbgcmd.GC()
coroutine_pool = {}
collectgarbage "collect"
end
function dbgcmd.STAT()
local stat = {}
stat.mqlen = skynet.mqlen()
skynet.ret(skynet.pack(stat))
end
function dbgcmd.INFO()
if internal_info_func then
skynet.ret(skynet.pack(internal_info_func()))
else
skynet.ret(skynet.pack(nil))
end
end
local function _debug_dispatch(session, address, cmd, ...)
local f = dbgcmd[cmd]
assert(f, cmd)
f(...)
end
----- register protocol
do
local REG = skynet.register_protocol
REG {
name = "text",
id = skynet.PTYPE_TEXT,
pack = function (...)
local n = select ("#" , ...)
if n == 0 then
return ""
elseif n == 1 then
return tostring(...)
else
return table.concat({...}," ")
end
end,
unpack = c.tostring
}
REG {
name = "lua",
id = skynet.PTYPE_LUA,
@@ -551,14 +486,6 @@ do
id = skynet.PTYPE_RESPONSE,
}
REG {
name = "debug",
id = skynet.PTYPE_DEBUG,
pack = skynet.pack,
unpack = skynet.unpack,
dispatch = _debug_dispatch,
}
REG {
name = "error",
id = skynet.PTYPE_ERROR,
@@ -603,10 +530,10 @@ local function init_service(start)
local ok, err = xpcall(init_template, debug.traceback, start)
if not ok then
print("init service failed:", err)
skynet.send(".launcher","text", "ERROR")
skynet.send(".launcher","lua", "ERROR")
skynet.exit()
else
skynet.send(".launcher","text", "")
skynet.send(".launcher","lua", "LAUNCHOK")
end
end
@@ -649,4 +576,8 @@ function skynet.mqlen()
return tonumber(c.command "MQLEN")
end
-- Inject internal debug framework
local debug = require "skynet.debug"
debug(skynet)
return skynet

49
lualib/skynet/debug.lua Normal file
View File

@@ -0,0 +1,49 @@
return function (skynet)
local internal_info_func
function skynet.info_func(func)
internal_info_func = func
end
local dbgcmd = {}
function dbgcmd.MEM()
local kb, bytes = collectgarbage "count"
skynet.ret(skynet.pack(kb,bytes))
end
function dbgcmd.GC()
coroutine_pool = {}
collectgarbage "collect"
end
function dbgcmd.STAT()
local stat = {}
stat.mqlen = skynet.mqlen()
skynet.ret(skynet.pack(stat))
end
function dbgcmd.INFO()
if internal_info_func then
skynet.ret(skynet.pack(internal_info_func()))
else
skynet.ret(skynet.pack(nil))
end
end
local function _debug_dispatch(session, address, cmd, ...)
local f = dbgcmd[cmd]
assert(f, cmd)
f(...)
end
skynet.register_protocol {
name = "debug",
id = assert(skynet.PTYPE_DEBUG),
pack = assert(skynet.pack),
unpack = assert(skynet.unpack),
dispatch = _debug_dispatch,
}
end

View File

@@ -1,5 +1,5 @@
local skynet = require "skynet"
local snax_interface = require "snax_interface"
local snax_interface = require "snax.interface"
local snax = {}
local typeclass = {}

View File

@@ -1,4 +1,4 @@
local si = require "snax_interface"
local si = require "snax.interface"
local io = io
local hotfix = {}

View File

@@ -2,8 +2,8 @@ local skynet = require "skynet"
local string = string
local services = {}
local command = {}
local instance = {} -- for confirm (function command.LAUNCH / command.ERROR / command.LAUNCHOK)
local function handle_to_address(handle)
return tonumber("0x" .. string.sub(handle , 2))
@@ -28,7 +28,7 @@ function command.STAT()
return list
end
function command.INFO(handle)
function command.INFO(_, _, handle)
handle = handle_to_address(handle)
if services[handle] == nil then
return
@@ -38,7 +38,7 @@ function command.INFO(handle)
end
end
function command.KILL(handle)
function command.KILL(_, _, handle)
handle = handle_to_address(handle)
skynet.kill(handle)
local ret = { [skynet.address(handle)] = tostring(services[handle]) }
@@ -62,48 +62,71 @@ function command.GC()
return command.MEM()
end
function command.REMOVE(handle)
function command.REMOVE(_,_, handle)
services[handle] = nil
-- don't return (skynet.ret) because the handle may exit
return NORET
end
local instance = {}
skynet.dispatch("text" , function(session, address , cmd)
if cmd == "" then
-- init notice
local reply = instance[address]
if reply then
skynet.redirect(reply.address , 0, "response", reply.session, skynet.address(address))
instance[address] = nil
end
elseif cmd == "ERROR" then
-- see serivce-src/service_lua.c
-- init failed
local reply = instance[address]
if reply then
skynet.redirect(reply.address , 0, "response", reply.session, "")
instance[address] = nil
end
function command.LAUNCH(address, session, service, ...)
local param = table.concat({...}, " ")
local inst = skynet.launch(service, param)
if inst then
services[inst] = service .. " " .. param
instance[inst] = { session = session, address = address }
else
-- launch request
local service, param = string.match(cmd,"([^ ]+) (.*)")
local inst = skynet.launch(service, param)
if inst then
services[inst] = cmd
instance[inst] = { session = session, address = address }
else
skynet.ret("")
end
skynet.ret("") -- launch failed
end
end)
return NORET
end
function command.ERROR(address)
-- see serivce-src/service_lua.c
-- init failed
local reply = instance[address]
if reply then
skynet.redirect(reply.address , 0, "response", reply.session, "")
instance[address] = nil
end
return NORET
end
function command.LAUNCHOK(address)
-- init notice
local reply = instance[address]
if reply then
skynet.redirect(reply.address , 0, "response", reply.session, skynet.address(address))
instance[address] = nil
end
return NORET
end
-- for historical reasons, launcher support text command (for C service)
skynet.register_protocol {
name = "text",
id = skynet.PTYPE_TEXT,
unpack = skynet.tostring,
dispatch = function(session, address , cmd)
if cmd == "" then
command.LAUNCHOK(address)
elseif cmd == "ERROR" then
command.ERROR(address)
else
-- launch request
local service, param = string.match(cmd,"([^ ]+) (.*)")
command.LAUNCH(address, session, service, param)
end
end,
}
skynet.dispatch("lua", function(session, address, cmd , ...)
cmd = string.upper(cmd)
local f = command[cmd]
if f then
local ret = f(...)
local ret = f(address, session, ...)
if ret ~= NORET then
skynet.ret(skynet.pack(ret))
end

View File

@@ -1,6 +1,6 @@
local skynet = require "skynet"
local c = require "skynet.c"
local snax_interface = require "snax_interface"
local snax_interface = require "snax.interface"
local profile = require "profile"
local snax = require "snax"
@@ -21,8 +21,10 @@ local function update_stat(name, ti)
t.time = t.time + ti
end
local traceback = debug.traceback
local function do_func(f, msg)
return pcall(f, table.unpack(msg))
return xpcall(f, traceback, table.unpack(msg))
end
local function dispatch(f, ...)
@@ -42,7 +44,7 @@ local function message_dispatch()
if method[2] == "accept" then
-- no return
profile.start()
local ok, data = pcall(f, table.unpack(msg))
local ok, data = xpcall(f, traceback, table.unpack(msg))
local ti = profile.stop()
update_stat(method[3], ti)
if not ok then
@@ -50,7 +52,7 @@ local function message_dispatch()
end
else
profile.start()
local ok, data, size = pcall(dispatch, f, table.unpack(msg))
local ok, data, size = xpcall(dispatch, traceback, f, table.unpack(msg))
local ti = profile.stop()
update_stat(method[3], ti)
if ok then
@@ -89,9 +91,9 @@ local function timing( method, ... )
profile.start()
if method[2] == "accept" then
-- no return
err,msg = pcall(method[4], ...)
err,msg = xpcall(method[4], traceback, ...)
else
err,msg = pcall(return_f, method[4], ...)
err,msg = xpcall(return_f, traceback, method[4], ...)
end
local ti = profile.stop()
update_stat(method[3], ti)
@@ -105,7 +107,7 @@ skynet.start(function()
if method[2] == "system" then
local command = method[3]
if command == "hotfix" then
local hotfix = require "snax_hotfix"
local hotfix = require "snax.hotfix"
skynet.ret(skynet.pack(hotfix(func, ...)))
elseif command == "init" then
assert(not init, "Already init")