add error handle in skynet.fork and mqueue

This commit is contained in:
云风
2013-10-30 21:19:13 +08:00
parent 4291e0e4a1
commit 91b097cbbf
2 changed files with 21 additions and 19 deletions

View File

@@ -20,21 +20,8 @@ skynet.register_protocol {
end end
} }
-- todo: write to log system
local function write_log(...)
return print(...)
end
local function report_error(succ, ...)
if succ then
return ...
else
write_log("Message queue dispatch error: ", ...)
end
end
local function do_func(f, msg) local function do_func(f, msg)
return report_error(pcall(f, table.unpack(msg))) return pcall(f, table.unpack(msg))
end end
local function message_dispatch(f) local function message_dispatch(f)
@@ -47,7 +34,8 @@ local function message_dispatch(f)
local session = msg.session local session = msg.session
if session == 0 then if session == 0 then
if do_func(f, msg) ~= nil then if do_func(f, msg) ~= nil then
write_log("Message queue returns value, maybe need call this service") skynet.fork(message_dispatch,f)
error(string.format("[:%x] send a message to [:%x] return something", msg.addr, skynet.self()))
end end
else else
local data, size = skynet.pack(do_func(f,msg)) local data, size = skynet.pack(do_func(f,msg))
@@ -65,7 +53,7 @@ function mqueue.register(f)
end end
function mqueue.call(addr, ...) function mqueue.call(addr, ...)
return skynet.call(addr, "queue", ...) return assert(skynet.call(addr, "queue", ...))
end end
function mqueue.send(addr, ...) function mqueue.send(addr, ...)

View File

@@ -5,6 +5,7 @@ local tonumber = tonumber
local coroutine = coroutine local coroutine = coroutine
local assert = assert local assert = assert
local pairs = pairs local pairs = pairs
local pcall = pcall
local proto = {} local proto = {}
local skynet = {} local skynet = {}
@@ -292,7 +293,7 @@ function skynet.fork(func,...)
table.insert(fork_queue, co) table.insert(fork_queue, co)
end end
local function dispatch_message(prototype, msg, sz, session, source, ...) local function raw_dispatch_message(prototype, msg, sz, session, source, ...)
-- PTYPE_RESPONSE = 1, read skynet.h -- PTYPE_RESPONSE = 1, read skynet.h
if prototype == 1 then if prototype == 1 then
local co = session_id_coroutine[session] local co = session_id_coroutine[session]
@@ -318,14 +319,27 @@ local function dispatch_message(prototype, msg, sz, session, source, ...)
error(string.format("Can't dispatch type %s : ", p.name)) error(string.format("Can't dispatch type %s : ", p.name))
end end
end end
end
local function dispatch_message(...)
local succ, err = pcall(raw_dispatch_message,...)
while true do while true do
local key,co = next(fork_queue) local key,co = next(fork_queue)
if co == nil then if co == nil then
return break
end end
fork_queue[key] = nil fork_queue[key] = nil
suspend(co,coroutine.resume(co)) local fork_succ, fork_err = pcall(suspend,co,coroutine.resume(co))
if not fork_succ then
if succ then
succ = false
err = fork_err
else
err = err .. "\n" .. fork_err
end
end
end end
assert(succ, err)
end end
function skynet.newservice(name, ...) function skynet.newservice(name, ...)