标签:sage name str nil resume return cti skynet ==
????接上一篇分析《skynet服务之launcher》,本篇我们继续来分析一下lua中的协程如何与服务有机结合的,关于lua中协程的解释参见本文《lua中协程的理解》;
上一篇分析到,当一个lua服务收到消息后,在lua层,最终会执行到raw_dispatch_message函数,代码如下:
local
function raw_dispatch_message(prototype, msg, sz, session, source)
????-- skynet.PTYPE_RESPONSE = 1, read skynet.h
????if prototype ==
1
then
--回应包
????????local co = session_id_coroutine[session]
????????if co ==
"BREAK"
then
????????????session_id_coroutine[session]
=
nil
????????elseif co ==
nil
then
????????????unknown_response(session, source, msg, sz)
????????else
????????????session_id_coroutine[session]
=
nil
????????????suspend(co, coroutine_resume(co,
true, msg, sz))
????????end
????else
????????local p = proto[prototype]
????????if p ==
nil
then
????????????if session ~=
0
then
????????????????c.send(source, skynet.PTYPE_ERROR, session,
"")
????????????else
????????????????unknown_request(session, source, msg, sz, prototype)
????????????end
????????????return
????????end
????????local f = p.dispatch
????????if f then
????????????local ref = watching_service[source]
????????????if ref then
????????????????watching_service[source]
= ref +
1
????????????else
????????????????watching_service[source]
=
1
????????????end
????????????local co = co_create(f)
????????????session_coroutine_id[co]
= session
????????????session_coroutine_address[co]
= source
????????????suspend(co, coroutine_resume(co, session,source, p.unpack(msg,sz)))
????????elseif session ~=
0
then
????????????c.send(source, skynet.PTYPE_ERROR, session,
"")
????????else
????????????unknown_request(session, source, msg, sz, proto[prototype].name)
????????end
????