Skip to content

Commit

Permalink
代码优化
Browse files Browse the repository at this point in the history
  • Loading branch information
xiyoo0812 committed Feb 6, 2024
1 parent f635cea commit d41a47e
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 21 deletions.
29 changes: 15 additions & 14 deletions server/gateway/gateway.lua
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ prop:reader("players", {}) --会话列表
prop:reader("counter", nil) --计数器
prop:reader("req_counter", nil) --计数器
prop:reader("ntf_counter", nil) --计数器
prop:reader("ignore_cmds", {}) --日志过滤
prop:reader("cache_cmds", {}) --缓存过滤
prop:reader("print_cmds", {}) --日志过滤
prop:reader("reenter_cmds", {}) --重入过滤

function Gateway:__init()
Expand Down Expand Up @@ -92,27 +93,25 @@ end

---日志忽略网络消息通知名
function Gateway:on_cfg_filter_changed()
self.ignore_cmds = {}
self.print_cmds = {}
self.cache_cmds = {}
self.reenter_cmds = {}
for cmd_name, conf in filter:iterator() do
local cmd_id = protobuf_mgr:msg_id(cmd_name)
if conf.log then
self.ignore_cmds[cmd_id] = true
self.ignore_cmds[cmd_name] = true
self.print_cmds[cmd_id] = true
self.print_cmds[cmd_name] = true
end
if conf.cache then
self.cache_cmds[cmd_id] = true
self.cache_cmds[cmd_name] = true
end
if conf.proto then
self.reenter_cmds[cmd_id] = true
end
end
end

---是否输出CMD消息的内容
function Gateway:is_print_cmd(cmd_id)
if self.ignore_cmds[cmd_id] then
return false
end
return true
end

--查找玩家
function Gateway:get_player(player_id)
if player_id then
Expand Down Expand Up @@ -177,7 +176,9 @@ function Gateway:rpc_forward_client(player_id, cmd_id, data)
log_warn("[Gateway][rpc_forward_client] cmd_id({}) player({}) not exist!", cmd_id, player_id)
return
end
player:send_message(cmd_id, data, self:is_print_cmd(cmd_id))
local cachable = self.cache_cmds[cmd_id]
local printble = self.print_cmds[cmd_id]
player:send_message(cmd_id, data, printble, cachable)
end

--群发消息
Expand Down Expand Up @@ -379,7 +380,7 @@ function Gateway:on_socket_cmd(session, service_type, cmd_id, body, session_id)
client_mgr:callback_errcode(session, cmd_id, FRAME_FAILED, session_id)
return
end
player:notify_command(service_type, cmd_id, body, session_id, self:is_print_cmd(cmd_id))
player:notify_command(service_type, cmd_id, body, session_id, self.print_cmds[cmd_id])
end

quanta.gateway = Gateway()
Expand Down
47 changes: 40 additions & 7 deletions server/gateway/player.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,26 @@
local log_err = logger.err
local log_info = logger.info
local log_debug = logger.debug
local tremove = table.remove
local qfailed = quanta.failed
local name2sid = service.name2sid

local group_mgr = quanta.get("group_mgr")
local router_mgr = quanta.get("router_mgr")
local client_mgr = quanta.get("client_mgr")
local protobuf_mgr = quanta.get("protobuf_mgr")

local SERVICE_LOBBY = name2sid("lobby")
local FRAME_FAILED = protobuf_mgr:error_code("FRAME_FAILED")

--创建角色数据
local GatePlayer = class()
local prop = property(GatePlayer)
prop:reader("open_id", 0) --open_id
prop:reader("player_id", 0) --player_id
prop:reader("cursor", 0) --消息游标
prop:reader("groups", {}) --分组列表
prop:reader("messages", {}) --消息队列
prop:accessor("token", 0) --token
prop:accessor("lobby_id", 0) --大厅id
prop:accessor("session", nil) --session
Expand Down Expand Up @@ -55,24 +60,52 @@ function GatePlayer:notify_disconnect()
router_mgr:forward_send(self.player_id, -1, "rpc_player_disconnect", self.player_id)
end

--查询缓存消息
function GatePlayer:find_cache_queue()
local size = #self.messages
local msg_queue = self.messages[size]
if not msg_queue or msg_queue.cursor ~= self.cursor then
msg_queue = { cursor = self.cursor, msgs = {} }
self.messages[size + 1] = msg_queue
end
return msg_queue
end

--检查缓存消息
function GatePlayer:check_cache_queue(cursor)
if cursor and cursor > 0 then
local mesages = self.messages
for i = #mesages, 1, -1 do
local msg_queue = mesages[i]
if msg_queue.cursor <= cursor then
tremove(mesages, i)
end
end
end
end

--通知心跳
function GatePlayer:notify_heartbeat(session, cmd_id, body, session_id)
-- 缓存服务
router_mgr:forward_send(self.player_id, -1, "rpc_player_heartbeat", self.player_id)
client_mgr:check_flow(session)
client_mgr:callback_by_id(session, cmd_id, { time = quanta.now_ms, error_code = 0 }, session_id)
router_mgr:forward_send(self.player_id, SERVICE_LOBBY, "rpc_player_heartbeat", self.player_id)
client_mgr:callback_by_id(session, cmd_id, { time = quanta.now_ms, error_code = 0, serial = self.cursor }, session_id)
self:check_cache_queue(body.serial)
end

--发送消息
function GatePlayer:send_message(cmd_id, data, display)
function GatePlayer:send_message(cmd_id, data, printable, cachable)
client_mgr:send(self.session, cmd_id, data)
if display then
if printable then
log_debug("[GatePlayer][send_message] player({}) send message({}-{}) !", self.player_id, cmd_id, data)
end
if cachable then
local msg_queue = self:find_cache_queue()
msg_queue[#msg_queue + 1] = { cmd_id, data }
end
end

--转发消息
function GatePlayer:notify_command(service_id, cmd_id, body, session_id, display)
function GatePlayer:notify_command(service_id, cmd_id, body, session_id, printable)
local pla_id = self.player_id
local ok, codeoe, res = router_mgr:forward_call(pla_id, service_id, "rpc_player_command", pla_id, cmd_id, body)
if not ok then
Expand All @@ -84,7 +117,7 @@ function GatePlayer:notify_command(service_id, cmd_id, body, session_id, display
client_mgr:callback_errcode(self.session, cmd_id, codeoe, session_id)
return
end
if display then
if printable then
log_debug("[GatePlayer][notify_command] player({}) response message({}-{}) !", pla_id, cmd_id, res)
end
client_mgr:callback_by_id(self.session, cmd_id, res, session_id)
Expand Down
38 changes: 38 additions & 0 deletions server/robot/nodes/node_parall.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
--node_parall.lua
local log_warn = logger.warn
local makechan = quanta.make_channel

local NodeBase = import("robot/nodes/node_base.lua")

local NodeParall = class(NodeBase)
local prop = property(NodeParall)
prop:reader("childs", {}) --childs
prop:reader("channel", nil) --channel

function NodeParall:__init(case)
end

function NodeParall:on_load(conf)
local channel = makechan("node_parall")
for _, child in piars(conf.childs or {}) do
channel:push(function()

end)
end
self.channel = channel
return true
end

function NodeParall:on_action()
if self.channel then
local ok, code = self.channel:execute()
if not ok then
log_warn("[NodeParall][on_action] robot:{} code {}", self.actor.open_id, code)
self:failed("cond error")
return false
end
end
return true
end

return NodeParall
41 changes: 41 additions & 0 deletions server/robot/nodes/node_rand.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
--node_rand.lua
local log_warn = logger.warn
local makechan = quanta.make_channel

local NodeBase = import("robot/nodes/node_base.lua")

local NodeRand = class(NodeBase)
local prop = property(NodeRand)
prop:reader("channel", nil) --channel

function NodeRand:__init(case)
end

function NodeRand:on_load(conf)
self.channel = makechan("node")
self.failed = conf.result.failed
self.success = conf.result.success
return true
end

function NodeRand:go_next()
if self.result then
self.case:run_next(self.success)
else
self.case:run_next(self.failed)
end
end

function NodeRand:on_action()
local role = self.actor
local cond = self:call_script(self.cond)
if cond == nil then
log_warn("[NodeRand][on_action] robot:{} cond {} id null", role.open_id, self.cond)
self:failed("cond error")
return false
end
self.result = cond
return true
end

return NodeRand
40 changes: 40 additions & 0 deletions server/robot/nodes/node_repeat.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
--node_repeat.lua
local log_warn = logger.warn

local NodeBase = import("robot/nodes/node_base.lua")

local NodeRepeat = class(NodeBase)
local prop = property(NodeRepeat)
prop:reader("childs", {}) --childs

function NodeRepeat:__init(case)
end

function NodeRepeat:on_load(conf)
self.cond = conf.cond
self.failed = conf.result.failed
self.success = conf.result.success
return true
end

function NodeRepeat:go_next()
if self.result then
self.case:run_next(self.success)
else
self.case:run_next(self.failed)
end
end

function NodeRepeat:on_action()
local role = self.actor
local cond = self:call_script(self.cond)
if cond == nil then
log_warn("[NodeRepeat][on_action] robot:{} cond {} id null", role.open_id, self.cond)
self:failed("cond error")
return false
end
self.result = cond
return true
end

return NodeRepeat

0 comments on commit d41a47e

Please sign in to comment.