nodeMgr.lua 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. local skynet = require "skynet"
  2. local snax = require "skynet.snax"
  3. local cluster = require "skynet.cluster"
  4. local nodename = skynet.getenv("nodeName")
  5. local root = {}
  6. -- 集群推送
  7. function root.send(node, service, cmd, ...)
  8. if not service or not cmd then
  9. log.error("node[%s] service[%s] cmd[%s]", node, service, cmd)
  10. return
  11. end
  12. if node == nil or node == "" or nodename == node then
  13. -- 本地服务
  14. skynet.send(service, "lua", cmd, ...)
  15. else
  16. -- 集群服务
  17. cluster.send(node, service, cmd, ...)
  18. end
  19. end
  20. -- 集群请求
  21. function root.call(node, service, cmd, ...)
  22. if not service or not cmd then
  23. log.error("call node[%s] service[%s] cmd[%s] is nil", tostring(node), tostring(service), tostring(cmd))
  24. return false
  25. end
  26. if node == nil or node == "" or nodename == node then
  27. -- 本地服务
  28. local args = {...}
  29. local ret1, ret2, ret3, ret4
  30. local ok, msg =
  31. xpcall(
  32. function()
  33. ret1, ret2, ret3, ret4 = skynet.call(service, "lua", cmd, table.unpack(args))
  34. end,
  35. debug.traceback
  36. )
  37. if not ok then
  38. log.error("call failed msg:%s service:%s cmd:%s", msg, tostring(service), cmd)
  39. end
  40. return ok, ret1, ret2, ret3, ret4
  41. else
  42. -- 集群服务
  43. local args = {...}
  44. local ret1, ret2, ret3, ret4
  45. local ok, msg =
  46. xpcall(
  47. function()
  48. ret1, ret2, ret3, ret4 = cluster.call(node, service, cmd, table.unpack(args))
  49. end,
  50. debug.traceback
  51. )
  52. if not ok then
  53. log.error("call failed node:%s service:%s cmd:%s", node, tostring(service), cmd)
  54. end
  55. return ok, ret1, ret2, ret3, ret4
  56. end
  57. end
  58. function root.rpsend(node, serv, cmd, ...)
  59. if not node or not serv or not cmd then
  60. error("rpsend fatal err")
  61. end
  62. return root.send(node, serv, "redirectS2S", cmd, ...)
  63. end
  64. function root.rpcall(node, serv, cmd, ...)
  65. if not node or not serv or not cmd then
  66. error("rpcall fatal err")
  67. end
  68. return root.call(node, serv, "redirootrectS2S", cmd, ...)
  69. end
  70. -- 查询节点服务地址
  71. function root.query_node_service_addr(node, name)
  72. if is_empty(node) or is_empty(name) then
  73. return
  74. end
  75. local ok, addr = pcall(cluster.query, node, name)
  76. if not ok then
  77. log.error("Node:%s or srv_name:%s donot exist!!!", node, name)
  78. return
  79. end
  80. return addr
  81. end
  82. --- 请求类型 req 等待返回值 post
  83. function root.snaxcall(node, name, address, reqType, func, ...)
  84. local bind
  85. if node == nil or node == "" or nodename == node then -- 当前节点
  86. bind = snax.queryservice(name)
  87. else
  88. bind = cluster.snax(node, name, address)
  89. end
  90. if not reqType or not func then
  91. return bind
  92. end
  93. if reqType == "req" then
  94. return bind[reqType][func](...)
  95. end
  96. bind[reqType][func](...)
  97. return nil
  98. end
  99. -- 向所有agent进行广播
  100. function root.broadcast_game_agent(mdl, cmd, ...)
  101. local nodeInfoList = root.get_type_node_list("game")
  102. if is_empty(nodeInfoList) then
  103. return
  104. end
  105. for _, v in ipairs(nodeInfoList) do
  106. root.send(v, ".srvAgentMgr", "broadcast_agents", mdl, cmd, ...)
  107. end
  108. end
  109. -- 获取某种类型的所有节点
  110. function root.get_type_node_list(nodeType)
  111. local ok, nodes = root.call("master", ".srvNodeMgr", "get_type_node_list", tostring(nodeType))
  112. if not ok or not nodes then
  113. return
  114. end
  115. return nodes
  116. end
  117. -- 分配战斗节点/服务
  118. function root.dispatch_battle_node_and_agent()
  119. local ok, server = root.call("battlemaster", ".srvNodeMgr", "dispatch_battle_node")
  120. if not ok or server == nil then
  121. return
  122. end
  123. if is_empty(server.battleNode) or server.battleId == nil then
  124. log.error(string.format("分配对战服失败, newBattleId=%s", server.battleId))
  125. return
  126. end
  127. local ok, handle = root.call(server.battleNode, ".BattleMgr", "getBattleAgent")
  128. if not ok or handle == nil then
  129. return
  130. end
  131. local info = {}
  132. info.nodeName = server.battleNode
  133. info.agent = handle
  134. info.battleId = server.battleId
  135. return info
  136. end
  137. -- 节点信息是否合法
  138. function root.is_node_info_valid(nodeInfo)
  139. if nodeInfo == nil then
  140. return false
  141. end
  142. if is_empty(nodeInfo.nodeName) then
  143. return false
  144. end
  145. if nodeInfo.agent == nil and is_empty(nodeInfo.srvName) then
  146. return false
  147. end
  148. return true
  149. end
  150. -- 发送 - match节点 - 堵塞
  151. function root.call_with_node_info(nodeInfo, cmd, ...)
  152. if not root.is_node_info_valid(nodeInfo) then
  153. return false
  154. end
  155. return root.call(nodeInfo.nodeName, nodeInfo.agent or nodeInfo.srvName, cmd, ...)
  156. end
  157. -- 发送 - match节点 - 非堵塞
  158. function root.send_with_node_info(nodeInfo, cmd, ...)
  159. if not root.is_node_info_valid(nodeInfo) then
  160. return false
  161. end
  162. root.send(nodeInfo.nodeName, nodeInfo.agent or nodeInfo.srvName, cmd, ...)
  163. end
  164. return root