diff --git a/src/agent.lua b/src/agent.lua index 1552306..fe486a8 100644 --- a/src/agent.lua +++ b/src/agent.lua @@ -24,6 +24,8 @@ local agent_util, cs _hotfixActions = _hotfixActions or {} _hotfixClass = _hotfixClass or {} +_codeSession = {} + --- {{{ 定时器相关 local function handle_timeout() if not agentInfo.open_timer then return end @@ -48,6 +50,7 @@ function cancel_agent_timer() end ---- 定时器相关 }}} + local _pipelinings = {} --可同时多个序列 栈的形式保证嵌套不出错 function SendPacket(actionCode, bin, client_fd) -- 内部消息不扩散出去 @@ -57,22 +60,31 @@ function SendPacket(actionCode, bin, client_fd) end return end - - + client_fd = client_fd or agentInfo.client_fd local handlerName = actionHandlers[actionCode] - if string.sub(handlerName, -3, -1) == "Rpc" then + local isRpc = string.sub(handlerName, -3, -1) == "Rpc" + local session = _codeSession[actionCode] or 0 + if isRpc then actionCode = actionCode + rpcResponseBegin end + -- 查看是否是在 流水线操作中 if #_pipelinings > 0 then local _curPipelining = _pipelinings[#_pipelinings] _curPipelining[client_fd] = _curPipelining[client_fd] or {} --区分不同客户端 - table.insert(_curPipelining[client_fd], {actionCode, bin}) + if not isRpc then + session = nil + end + table.insert(_curPipelining[client_fd], {actionCode, bin, session}) else if #bin > 0 then bin = xxtea.encrypt(bin, XXTEA_KEY) end local head = string.pack("H", actionCode) + if isRpc then + -- 是回复 -- 有session号 + head = head .. string.pack("H", session) + end return socket.write(client_fd, netpack.pack(head .. bin)) end end @@ -153,6 +165,7 @@ function rpcOtherUnion(id, funcName, ...) end end + skynet.register_protocol { name = "client", id = skynet.PTYPE_CLIENT, @@ -175,6 +188,13 @@ skynet.register_protocol { return end + if string.sub(actionName, -3, -1) == "Rpc" then + -- 是请求 -- 有session号 + local codeSession = string.unpack("H", string.sub(data, 1, 2)) + _codeSession[cmd] = codeSession + data = string.sub(data, 3) + end + local method if _hotfixActions[actionName] then method = _hotfixActions[actionName] @@ -207,6 +227,9 @@ skynet.register_protocol { agentInfo.role:endActionUcode() end + -- 清掉请求session + _codeSession[cmd] = nil + if not result or type(result) == "number" then SendPacket(actionCodes.Sys_innerErrorMsg, MsgPack.pack({id = cmd * 100 + (result or 0)})) end -- libgit2 0.21.2