srvLoadProtoMgr.lua 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. --[[
  2. Descripttion:
  3. version:
  4. Author: Neo,Huang
  5. Date: 2022-07-28 09:16:34
  6. LastEditors: Neo,Huang
  7. LastEditTime: 2022-07-28 09:17:33
  8. --]]
  9. local skynet = require "skynet"
  10. require "skynet.manager"
  11. local proto_type, proto_service_num = ...
  12. proto_service_num = tonumber(proto_service_num)
  13. local balance = 1
  14. local proto_handles = {}
  15. local CMD = {}
  16. local function service_init()
  17. for i = 1, proto_service_num do
  18. local handle = skynet.newservice("srvLoadProto", proto_type)
  19. if handle then
  20. table.insert(proto_handles, handle)
  21. end
  22. end
  23. end
  24. function CMD.get_proto_handle()
  25. local handle = proto_handles[balance]
  26. if handle then
  27. balance = balance + 1
  28. if balance > proto_service_num then
  29. balance = 1
  30. end
  31. end
  32. assert(handle, "get proto handle error!")
  33. return handle
  34. end
  35. function CMD.update_proto()
  36. for _, v in ipairs(proto_handles) do
  37. skynet.send(v, "lua", "update_proto")
  38. end
  39. end
  40. function CMD.exit()
  41. for _, v in ipairs(proto_handles) do
  42. skynet.call(v, "lua", "exit")
  43. end
  44. skynet.retpack(nil)
  45. skynet.exit()
  46. end
  47. skynet.dispatch(
  48. "lua",
  49. function(session, _, cmd, ...)
  50. local func = assert(CMD[cmd], cmd .. " not found")
  51. local ret = func(...)
  52. if session > 0 and cmd ~= "exit" then
  53. skynet.retpack(ret)
  54. end
  55. end
  56. )
  57. skynet.start(
  58. function()
  59. service_init()
  60. skynet.register(".proto_mgr")
  61. end
  62. )