nodeMgr.lua 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  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.nodeName, ".srvAgentMgr", "broadcast_agents", mdl, cmd, ...)
  107. end
  108. end
  109. -- 广播
  110. function root.broadcast_proto_notify(protoName, msg)
  111. if protoName == nil then
  112. log.error("广播消息推送失败 protoName[%s]", tostring(protoName))
  113. return false
  114. end
  115. local nodeInfoList = root.get_type_node_list("gate")
  116. if is_empty(nodeInfoList) then
  117. return false
  118. end
  119. for _, v in ipairs(nodeInfoList) do
  120. root.send(v.nodeName, ".wsSprotoWatchdog", "s2c_broadcast", protoName, msg)
  121. end
  122. return true
  123. end
  124. -- 获取某种类型的所有节点
  125. function root.get_type_node_list(nodeType)
  126. local ok, nodes = root.call("master", ".srvNodeMgr", "get_type_node_list", tostring(nodeType))
  127. if not ok or not nodes then
  128. return
  129. end
  130. return nodes
  131. end
  132. -- 节点信息是否合法
  133. function root.is_node_info_valid(nodeInfo)
  134. if nodeInfo == nil then
  135. return false
  136. end
  137. if is_empty(nodeInfo.nodeName) then
  138. return false
  139. end
  140. if nodeInfo.agent == nil and is_empty(nodeInfo.srvName) then
  141. return false
  142. end
  143. return true
  144. end
  145. -- 发送 - match节点 - 堵塞
  146. function root.call_with_node_info(nodeInfo, cmd, ...)
  147. if not root.is_node_info_valid(nodeInfo) then
  148. return false
  149. end
  150. return root.call(nodeInfo.nodeName, nodeInfo.agent or nodeInfo.srvName, cmd, ...)
  151. end
  152. -- 发送 - match节点 - 非堵塞
  153. function root.send_with_node_info(nodeInfo, cmd, ...)
  154. if not root.is_node_info_valid(nodeInfo) then
  155. return false
  156. end
  157. root.send(nodeInfo.nodeName, nodeInfo.agent or nodeInfo.srvName, cmd, ...)
  158. end
  159. return root