123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181 |
- local skynet = require "skynet"
- local snax = require "skynet.snax"
- local cluster = require "skynet.cluster"
- local nodename = skynet.getenv("nodeName")
- local root = {}
- -- 集群推送
- function root.send(node, service, cmd, ...)
- if not service or not cmd then
- log.error("node[%s] service[%s] cmd[%s]", node, service, cmd)
- return
- end
- if node == nil or node == "" or nodename == node then
- -- 本地服务
- skynet.send(service, "lua", cmd, ...)
- else
- -- 集群服务
- cluster.send(node, service, cmd, ...)
- end
- end
- -- 集群请求
- function root.call(node, service, cmd, ...)
- if not service or not cmd then
- log.error("call node[%s] service[%s] cmd[%s] is nil", tostring(node), tostring(service), tostring(cmd))
- return false
- end
- if node == nil or node == "" or nodename == node then
- -- 本地服务
- local args = {...}
- local ret1, ret2, ret3, ret4
- local ok, msg =
- xpcall(
- function()
- ret1, ret2, ret3, ret4 = skynet.call(service, "lua", cmd, table.unpack(args))
- end,
- debug.traceback
- )
- if not ok then
- log.error("call failed msg:%s service:%s cmd:%s", msg, tostring(service), cmd)
- end
- return ok, ret1, ret2, ret3, ret4
- else
- -- 集群服务
- local args = {...}
- local ret1, ret2, ret3, ret4
- local ok, msg =
- xpcall(
- function()
- ret1, ret2, ret3, ret4 = cluster.call(node, service, cmd, table.unpack(args))
- end,
- debug.traceback
- )
- if not ok then
- log.error("call failed node:%s service:%s cmd:%s", node, tostring(service), cmd)
- end
- return ok, ret1, ret2, ret3, ret4
- end
- end
- function root.rpsend(node, serv, cmd, ...)
- if not node or not serv or not cmd then
- error("rpsend fatal err")
- end
- return root.send(node, serv, "redirectS2S", cmd, ...)
- end
- function root.rpcall(node, serv, cmd, ...)
- if not node or not serv or not cmd then
- error("rpcall fatal err")
- end
- return root.call(node, serv, "redirootrectS2S", cmd, ...)
- end
- -- 查询节点服务地址
- function root.query_node_service_addr(node, name)
- if is_empty(node) or is_empty(name) then
- return
- end
- local ok, addr = pcall(cluster.query, node, name)
- if not ok then
- log.error("Node:%s or srv_name:%s donot exist!!!", node, name)
- return
- end
- return addr
- end
- --- 请求类型 req 等待返回值 post
- function root.snaxcall(node, name, address, reqType, func, ...)
- local bind
- if node == nil or node == "" or nodename == node then -- 当前节点
- bind = snax.queryservice(name)
- else
- bind = cluster.snax(node, name, address)
- end
- if not reqType or not func then
- return bind
- end
- if reqType == "req" then
- return bind[reqType][func](...)
- end
- bind[reqType][func](...)
- return nil
- end
- -- 向所有agent进行广播
- function root.broadcast_game_agent(mdl, cmd, ...)
- local nodeInfoList = root.get_type_node_list("game")
- if is_empty(nodeInfoList) then
- return
- end
- for _, v in ipairs(nodeInfoList) do
- root.send(v, ".srvAgentMgr", "broadcast_agents", mdl, cmd, ...)
- end
- end
- -- 获取某种类型的所有节点
- function root.get_type_node_list(nodeType)
- local ok, nodes = root.call("master", ".srvNodeMgr", "get_type_node_list", tostring(nodeType))
- if not ok or not nodes then
- return
- end
- return nodes
- end
- -- 分配战斗节点/服务
- function root.dispatch_battle_node_and_agent()
- local ok, server = root.call("battlemaster", ".srvNodeMgr", "dispatch_battle_node")
- if not ok or server == nil then
- return
- end
- if is_empty(server.battleNode) or server.battleId == nil then
- log.error(string.format("分配对战服失败, newBattleId=%s", server.battleId))
- return
- end
- local ok, handle = root.call(server.battleNode, ".BattleMgr", "getBattleAgent")
- if not ok or handle == nil then
- return
- end
- local info = {}
- info.nodeName = server.battleNode
- info.agent = handle
- info.battleId = server.battleId
- return info
- end
- -- 节点信息是否合法
- function root.is_node_info_valid(nodeInfo)
- if nodeInfo == nil then
- return false
- end
- if is_empty(nodeInfo.nodeName) then
- return false
- end
- if nodeInfo.agent == nil and is_empty(nodeInfo.srvName) then
- return false
- end
- return true
- end
- -- 发送 - match节点 - 堵塞
- function root.call_with_node_info(nodeInfo, cmd, ...)
- if not root.is_node_info_valid(nodeInfo) then
- return false
- end
- return root.call(nodeInfo.nodeName, nodeInfo.agent or nodeInfo.srvName, cmd, ...)
- end
- -- 发送 - match节点 - 非堵塞
- function root.send_with_node_info(nodeInfo, cmd, ...)
- if not root.is_node_info_valid(nodeInfo) then
- return false
- end
- root.send(nodeInfo.nodeName, nodeInfo.agent or nodeInfo.srvName, cmd, ...)
- end
- return root
|