【Skynet】解析skynet.call方法

解析skynet.call方法
1、call用調用註冊表裏面的

function skynet.call(addr, typename, ...)
    local tag = session_coroutine_tracetag[running_thread]
    if tag then
        c.trace(tag, "call", 2)
        c.send(addr, skynet.PTYPE_TRACE, 0, tag)
    end

    local p = proto[typename]
    local session = c.send(addr, p.id , nil , p.pack(...))
    if session == nil then
        error("call to invalid address " .. skynet.address(addr))
    end
    return p.unpack(yield_call(addr, session))
end
LUAMOD_API int
luaopen_skynet_core(lua_State *L) {}

2、lua調用C的接口

c.send(addr, skynet.PTYPE_TRACE, 0, tag)

3、send調用skynet.core的lsend方法=》send_message

static int
send_message(lua_State *L, int source, int idx_type) {
     struct skynet_context * context = lua_touserdata(L, lua_upvalueindex(1));
	.....
}
lua_upvalueindex(1) 壓入棧頂

取註冊表的值

lua_getfield(L, LUA_REGISTRYINDEX, "skynet_context");

註冊表

skynet.register_protocol {
    name = "text",
    id = skynet.PTYPE_TEXT,
    unpack = skynet.tostring,
    pack = function(text) return text end,
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章