srvAgent.lua 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208
  1. -- 游戏 agent 服务
  2. local skynet = require "skynet"
  3. local code = require "code"
  4. local jmService = require "jmService"
  5. local nodeUtil = require("utils.nodeUtil")
  6. local lib_queue = require("lib_queue")
  7. local util_event = require("utils.util_event")
  8. local mapItf2Module = {}
  9. local root = {
  10. mapUid2Agent = {}
  11. }
  12. -- 登录
  13. function root.login(uid, session)
  14. root.mapUid2Agent[uid] = session
  15. -- 通知 agentmgr
  16. skynet.call(".srvAgentMgr", "lua", "login", uid, session)
  17. nodeUtil:user_master_band_node(uid)
  18. return true
  19. end
  20. -- 登出
  21. function root.logout(uid, session)
  22. root.mapUid2Agent[uid] = nil
  23. skynet.call(".srvAgentMgr", "lua", "logout", uid, session)
  24. nodeUtil:user_master_unband_node(uid)
  25. return true
  26. end
  27. -- 回应请求协议
  28. local function l_c2s_response(uid, protoName, errCode, res, ...)
  29. res = res or {}
  30. res.code = errCode
  31. local session = root.mapUid2Agent[uid]
  32. if session == nil or session.gateNode == nil or session.gateAgent == nil then
  33. return
  34. end
  35. nodeUtil:send_to_node(session.gateNode, session.gateAgent, "c2s_response", uid, protoName, res, ...)
  36. end
  37. -- 格式化协议
  38. local function l_get_interface_and_func(protoName)
  39. -- return protoName:match "([^_]*)_(.*)"
  40. local data = string.split(protoName, "_")
  41. local itfName = table.remove(data, 1)
  42. local funcName = table.concat(data, "_")
  43. return itfName, funcName
  44. end
  45. -- 获取接口对象
  46. local function l_get_interface_module(itfName)
  47. local fileName = string.format("interface.%s", itfName)
  48. mapItf2Module[itfName] = mapItf2Module[itfName] or require(fileName)
  49. return mapItf2Module[itfName]
  50. end
  51. -- 处理客户端协议数据
  52. function root.c2s_forward(uid, protoName, args, ...)
  53. local session = root.mapUid2Agent[uid]
  54. if session == nil then
  55. log.warning("c2s_forward not find role! uid:%s", uid)
  56. return l_c2s_response(uid, protoName, code.INNER_SERVER_ERROR)
  57. end
  58. local itfName, funcName = l_get_interface_and_func(protoName)
  59. local itfModule = l_get_interface_module(itfName)
  60. if itfModule == nil then
  61. log.error("dont find module ! itfName:%s, funcName:%s", itfName, funcName)
  62. return l_c2s_response(uid, protoName, code.INNER_SERVER_ERROR, nil, ...)
  63. end
  64. local func = itfModule[funcName]
  65. if not func then
  66. log.error("dont find func ! itfName:%s, funcName:%s", itfName, funcName)
  67. return l_c2s_response(uid, protoName, code.INNER_SERVER_ERROR, nil, ...)
  68. end
  69. local ok, errCode, rs =
  70. xpcall(
  71. func,
  72. function()
  73. skynet.error(debug.traceback())
  74. end,
  75. itfModule,
  76. uid,
  77. args
  78. )
  79. if not ok then
  80. log.error(string.format("itfName:%s, funcName:%s, args:%s", itfName, funcName, tostring(args)))
  81. return l_c2s_response(uid, protoName, code.INNER_SERVER_ERROR, nil, ...)
  82. end
  83. l_c2s_response(uid, protoName, errCode, rs, ...)
  84. -- 协议处理完后进行处理
  85. local afterFuncName = string.format("after_%s", funcName)
  86. local afterFunc = itfModule[afterFuncName]
  87. if not afterFunc then
  88. return
  89. end
  90. ok, errCode, rs =
  91. xpcall(
  92. afterFunc,
  93. function()
  94. skynet.error(debug.traceback())
  95. end,
  96. itfModule,
  97. uid,
  98. args
  99. )
  100. if not ok then
  101. log.error(string.format("after itfName:%s, funcName:%s, args:%s", itfName, funcName, tostring(args)))
  102. return
  103. end
  104. end
  105. function root.doCmd(uid, itfName, cmd, args)
  106. local itfModule = l_get_interface_module(itfName)
  107. if itfModule == nil then
  108. log.error("doCmd dont find module ! itfName:%s, cmd:%s", itfName, cmd)
  109. return 400
  110. end
  111. local func = itfModule[cmd]
  112. if not func then
  113. log.error("doCmd dont find func ! itfName:%s, cmd:%s", itfName, cmd)
  114. return 401
  115. end
  116. local ok, errCode, rs =
  117. xpcall(
  118. func,
  119. function()
  120. skynet.error(debug.traceback())
  121. end,
  122. uid,
  123. args
  124. )
  125. if not ok then
  126. log.error(string.format("doCmd itfName:%s, cmd:%s, args:%s", itfName, cmd, tostring(args)))
  127. return
  128. end
  129. return errCode, rs
  130. end
  131. -- 玩家事件
  132. function root.user_trigge_event(uid, eventId, evtParams)
  133. log.debug("user_trigge_event uid[%s] eventId[%s] eventParams[%s]", uid, eventId, tostring(evtParams))
  134. lib_queue.critical_section(uid)(util_event.user_event, util_event, uid, eventId, evtParams)
  135. end
  136. -- 启动服务
  137. function root.onStart()
  138. return true
  139. end
  140. -- 服务关闭
  141. function root.onStop()
  142. -- 向agent管理服务发送退出成功消息
  143. skynet.send(".srvAgentMgr", "lua", "exit", skynet.self())
  144. return true
  145. end
  146. function root.recycle()
  147. skynet.exit()
  148. end
  149. -- 更新配置表
  150. function root.update_config(...)
  151. jmService.update_config(...)
  152. util_event:init()
  153. end
  154. -- 更新逻辑
  155. function root.update_logic(...)
  156. jmService.update_logic(...)
  157. util_event:init()
  158. end
  159. -- 在线玩家数
  160. function root.get_player_count()
  161. return #root.mapUid2Agent
  162. end
  163. -- 打印 - 玩家列表
  164. function root.print_uid_list()
  165. local uidList = {}
  166. for uid, _ in pairs(root.mapUid2Agent) do
  167. table.insert(uidList, uid)
  168. end
  169. log.warning("print_uid_list uidList:%s", tostring(uidList))
  170. end
  171. jmService.start(root, "srvAgent_" .. skynet.self())