mirror of
https://github.com/cloudwu/skynet.git
synced 2026-07-25 12:43:09 +00:00
add debug_console service
This commit is contained in:
@@ -324,7 +324,7 @@ function skynet.call(addr, typename, ...)
|
|||||||
end
|
end
|
||||||
local session = c.send(addr, p.id , nil , p.pack(...))
|
local session = c.send(addr, p.id , nil , p.pack(...))
|
||||||
if session == nil then
|
if session == nil then
|
||||||
error("call to invalid address " .. tostring(addr))
|
error("call to invalid address " .. skynet.address(addr))
|
||||||
end
|
end
|
||||||
return p.unpack(yield_call(addr, session))
|
return p.unpack(yield_call(addr, session))
|
||||||
end
|
end
|
||||||
@@ -335,7 +335,7 @@ function skynet.blockcall(addr, typename , ...)
|
|||||||
local session = c.send(addr, p.id , nil , p.pack(...))
|
local session = c.send(addr, p.id , nil , p.pack(...))
|
||||||
if session == nil then
|
if session == nil then
|
||||||
c.command("UNLOCK")
|
c.command("UNLOCK")
|
||||||
error("call to invalid address " .. tostring(addr))
|
error("call to invalid address " .. skynet.address(addr))
|
||||||
end
|
end
|
||||||
return p.unpack(yield_call(addr, session))
|
return p.unpack(yield_call(addr, session))
|
||||||
end
|
end
|
||||||
@@ -510,7 +510,11 @@ function skynet.query_group(handle)
|
|||||||
end
|
end
|
||||||
|
|
||||||
function skynet.address(addr)
|
function skynet.address(addr)
|
||||||
return string.format(":%x",addr)
|
if type(addr) == "number" then
|
||||||
|
return string.format(":%x",addr)
|
||||||
|
else
|
||||||
|
return tostring(addr)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function skynet.harbor(addr)
|
function skynet.harbor(addr)
|
||||||
@@ -566,6 +570,7 @@ function dbgcmd.TIMING()
|
|||||||
else
|
else
|
||||||
-- turn on timing
|
-- turn on timing
|
||||||
timing_call = {}
|
timing_call = {}
|
||||||
|
skynet.ret(skynet.pack(timing_call))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
80
service/debug_console.lua
Normal file
80
service/debug_console.lua
Normal file
@@ -0,0 +1,80 @@
|
|||||||
|
local skynet = require "skynet"
|
||||||
|
local socket = require "socket"
|
||||||
|
|
||||||
|
local port = tonumber(...)
|
||||||
|
|
||||||
|
local function format_table(t)
|
||||||
|
local index = {}
|
||||||
|
for k in pairs(t) do
|
||||||
|
table.insert(index, k)
|
||||||
|
end
|
||||||
|
table.sort(index)
|
||||||
|
local result = {}
|
||||||
|
for _,v in ipairs(index) do
|
||||||
|
table.insert(result, string.format("%s:%s",v,tostring(t[v])))
|
||||||
|
end
|
||||||
|
return table.concat(result,"\t")
|
||||||
|
end
|
||||||
|
|
||||||
|
local function dump_line(print, key, value)
|
||||||
|
if type(value) == "table" then
|
||||||
|
print(key, format_table(value))
|
||||||
|
else
|
||||||
|
print(key,tostring(value))
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function dump_list(print, list)
|
||||||
|
local index = {}
|
||||||
|
for k in pairs(list) do
|
||||||
|
table.insert(index, k)
|
||||||
|
end
|
||||||
|
table.sort(index)
|
||||||
|
for _,v in ipairs(index) do
|
||||||
|
dump_line(print, v, list[v])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function docmd(cmdline, print)
|
||||||
|
local split = {}
|
||||||
|
for i in string.gmatch(cmdline, "%S+") do
|
||||||
|
table.insert(split,i)
|
||||||
|
end
|
||||||
|
local ok, list = pcall(skynet.call,".launcher","lua", table.unpack(split))
|
||||||
|
if ok then
|
||||||
|
if list then
|
||||||
|
dump_list(print, list)
|
||||||
|
end
|
||||||
|
else
|
||||||
|
print("Error:", list)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
local function console_main_loop(stdin, print)
|
||||||
|
socket.lock(stdin)
|
||||||
|
print("Welcome to skynet console")
|
||||||
|
while true do
|
||||||
|
local cmdline = socket.readline(stdin, "\n")
|
||||||
|
if not cmdline then
|
||||||
|
break
|
||||||
|
end
|
||||||
|
if cmdline ~= "" then
|
||||||
|
docmd(cmdline, print)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
socket.unlock(stdin)
|
||||||
|
end
|
||||||
|
|
||||||
|
skynet.start(function()
|
||||||
|
local listen_socket = socket.listen ("127.0.0.1", port)
|
||||||
|
print("Start debug console at 127.0.0.1",port)
|
||||||
|
socket.start(listen_socket , function(id, addr)
|
||||||
|
local function print(...)
|
||||||
|
local t = { ... }
|
||||||
|
socket.write(id, table.concat(t,"\t"))
|
||||||
|
socket.write(id, "\n")
|
||||||
|
end
|
||||||
|
socket.start(id)
|
||||||
|
skynet.fork(console_main_loop, id , print)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
@@ -9,12 +9,14 @@ local function handle_to_address(handle)
|
|||||||
return tonumber("0x" .. string.sub(handle , 2))
|
return tonumber("0x" .. string.sub(handle , 2))
|
||||||
end
|
end
|
||||||
|
|
||||||
|
local NORET = {}
|
||||||
|
|
||||||
function command.LIST()
|
function command.LIST()
|
||||||
local list = {}
|
local list = {}
|
||||||
for k,v in pairs(services) do
|
for k,v in pairs(services) do
|
||||||
list[skynet.address(k)] = v
|
list[skynet.address(k)] = v
|
||||||
end
|
end
|
||||||
skynet.ret(skynet.pack(list))
|
return list
|
||||||
end
|
end
|
||||||
|
|
||||||
function command.RELOAD(handle)
|
function command.RELOAD(handle)
|
||||||
@@ -23,9 +25,9 @@ function command.RELOAD(handle)
|
|||||||
print(services[handle],cmd)
|
print(services[handle],cmd)
|
||||||
if cmd then
|
if cmd then
|
||||||
skynet.send(handle,"debug","RELOAD",cmd)
|
skynet.send(handle,"debug","RELOAD",cmd)
|
||||||
skynet.ret(skynet.pack({cmd}))
|
return {cmd}
|
||||||
else
|
else
|
||||||
skynet.ret(skynet.pack({"Support only snlua"}))
|
return {"Support only snlua"}
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -35,23 +37,23 @@ function command.STAT()
|
|||||||
local stat = skynet.call(k,"debug","STAT")
|
local stat = skynet.call(k,"debug","STAT")
|
||||||
list[skynet.address(k)] = stat
|
list[skynet.address(k)] = stat
|
||||||
end
|
end
|
||||||
skynet.ret(skynet.pack(list))
|
return list
|
||||||
end
|
end
|
||||||
|
|
||||||
function command.INFO(handle)
|
function command.INFO(handle)
|
||||||
handle = handle_to_address(handle)
|
handle = handle_to_address(handle)
|
||||||
if services[handle] == nil then
|
if services[handle] == nil then
|
||||||
skynet.ret(skynet.pack(nil))
|
return
|
||||||
else
|
else
|
||||||
local result = skynet.call(handle,"debug","INFO")
|
local result = skynet.call(handle,"debug","INFO")
|
||||||
skynet.ret(skynet.pack(result))
|
return result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function command.TIMING(handle)
|
function command.TIMING(handle)
|
||||||
handle = handle_to_address(handle)
|
handle = handle_to_address(handle)
|
||||||
if services[handle] == nil then
|
if services[handle] == nil then
|
||||||
skynet.ret(skynet.pack(nil))
|
return
|
||||||
else
|
else
|
||||||
local r = skynet.call(handle,"debug","TIMING")
|
local r = skynet.call(handle,"debug","TIMING")
|
||||||
local result = {}
|
local result = {}
|
||||||
@@ -60,15 +62,16 @@ function command.TIMING(handle)
|
|||||||
v.avg = v.ti/v.n
|
v.avg = v.ti/v.n
|
||||||
result[skynet.address(k)] = v
|
result[skynet.address(k)] = v
|
||||||
end
|
end
|
||||||
skynet.ret(skynet.pack(result))
|
return result
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
function command.KILL(handle)
|
function command.KILL(handle)
|
||||||
handle = handle_to_address(handle)
|
handle = handle_to_address(handle)
|
||||||
skynet.kill(handle)
|
skynet.kill(handle)
|
||||||
skynet.ret( skynet.pack({ [handle] = tostring(services[handle]) }))
|
local ret = { [skynet.address(handle)] = tostring(services[handle]) }
|
||||||
services[handle] = nil
|
services[handle] = nil
|
||||||
|
return ret
|
||||||
end
|
end
|
||||||
|
|
||||||
function command.MEM()
|
function command.MEM()
|
||||||
@@ -77,18 +80,20 @@ function command.MEM()
|
|||||||
local kb, bytes = skynet.call(k,"debug","MEM")
|
local kb, bytes = skynet.call(k,"debug","MEM")
|
||||||
list[skynet.address(k)] = string.format("%d Kb (%s)",kb,v)
|
list[skynet.address(k)] = string.format("%d Kb (%s)",kb,v)
|
||||||
end
|
end
|
||||||
skynet.ret(skynet.pack(list))
|
return list
|
||||||
end
|
end
|
||||||
|
|
||||||
function command.GC()
|
function command.GC()
|
||||||
for k,v in pairs(services) do
|
for k,v in pairs(services) do
|
||||||
skynet.send(k,"debug","GC")
|
skynet.send(k,"debug","GC")
|
||||||
end
|
end
|
||||||
command.MEM()
|
return command.MEM()
|
||||||
end
|
end
|
||||||
|
|
||||||
function command.REMOVE(handle)
|
function command.REMOVE(handle)
|
||||||
services[handle] = nil
|
services[handle] = nil
|
||||||
|
-- don't return (skynet.ret) because the handle may exit
|
||||||
|
return NORET
|
||||||
end
|
end
|
||||||
|
|
||||||
local instance = {}
|
local instance = {}
|
||||||
@@ -124,7 +129,15 @@ end)
|
|||||||
|
|
||||||
skynet.dispatch("lua", function(session, address, cmd , ...)
|
skynet.dispatch("lua", function(session, address, cmd , ...)
|
||||||
cmd = string.upper(cmd)
|
cmd = string.upper(cmd)
|
||||||
command[cmd](...)
|
local f = command[cmd]
|
||||||
|
if f then
|
||||||
|
local ret = f(...)
|
||||||
|
if ret ~= NORET then
|
||||||
|
skynet.ret(skynet.pack(ret))
|
||||||
|
end
|
||||||
|
else
|
||||||
|
skynet.ret(skynet.pack {"Unknown command"} )
|
||||||
|
end
|
||||||
end)
|
end)
|
||||||
|
|
||||||
skynet.start(function() end)
|
skynet.start(function() end)
|
||||||
|
|||||||
@@ -8,6 +8,7 @@ skynet.start(function()
|
|||||||
skynet.monitor "simplemonitor"
|
skynet.monitor "simplemonitor"
|
||||||
local lualog = skynet.newservice("lualog")
|
local lualog = skynet.newservice("lualog")
|
||||||
local console = skynet.newservice("console")
|
local console = skynet.newservice("console")
|
||||||
|
-- skynet.newservice("debug_console",8000)
|
||||||
local watchdog = skynet.newservice("watchdog","8888", max_client, 0)
|
local watchdog = skynet.newservice("watchdog","8888", max_client, 0)
|
||||||
local db = skynet.newservice("simpledb")
|
local db = skynet.newservice("simpledb")
|
||||||
-- skynet.launch("snlua","testgroup")
|
-- skynet.launch("snlua","testgroup")
|
||||||
|
|||||||
@@ -594,10 +594,10 @@ _filter_args(struct skynet_context * context, int type, int *session, void ** da
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (needcopy && *data) {
|
if (needcopy && *data) {
|
||||||
char * msg = malloc(*sz+1);
|
char * msg = malloc(*sz+1);
|
||||||
memcpy(msg, *data, *sz);
|
memcpy(msg, *data, *sz);
|
||||||
msg[*sz] = '\0';
|
msg[*sz] = '\0';
|
||||||
*data = msg;
|
*data = msg;
|
||||||
}
|
}
|
||||||
|
|
||||||
assert((*sz & HANDLE_MASK) == *sz);
|
assert((*sz & HANDLE_MASK) == *sz);
|
||||||
@@ -630,7 +630,7 @@ skynet_send(struct skynet_context * context, uint32_t source, uint32_t destinati
|
|||||||
|
|
||||||
if (skynet_context_push(destination, &smsg)) {
|
if (skynet_context_push(destination, &smsg)) {
|
||||||
free(data);
|
free(data);
|
||||||
skynet_error(NULL, "Drop message from %x to %x (type=%d)(size=%d)", source, destination, type, (int)(sz & HANDLE_MASK));
|
skynet_error(NULL, "Drop message from %x to %x (type=%d)(size=%d)", source, destination, type&0xff, (int)(sz & HANDLE_MASK));
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user