srvLoadProto.lua 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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:19:49
  8. --]]
  9. local skynet = require "skynet"
  10. local protobufUtil = require "utils.protobufUtil"
  11. local protobuf = require "protobuf"
  12. local proto_type = ...
  13. -- 注册协议文件
  14. local function register_protos()
  15. local pb_path, protos_tbl
  16. if proto_type == "game_proto" then
  17. pb_path, protos_tbl = protobufUtil.get_game_protos()
  18. else
  19. pb_path, protos_tbl = protobufUtil.get_http_protos()
  20. end
  21. for _, file in ipairs(protos_tbl) do
  22. protobuf.register_file(pb_path .. file)
  23. end
  24. end
  25. local CMD = {}
  26. -- 编码协议
  27. function CMD.encode(name, msg)
  28. if not name or not msg then
  29. return
  30. end
  31. return protobuf.encode(name, msg)
  32. end
  33. -- 解码协议
  34. function CMD.decode(name, msg)
  35. if not name or not msg then
  36. return
  37. end
  38. return protobuf.decode(name, msg)
  39. end
  40. -- 更新协议(注:只能热更协议增加字段或新增协议,其他均会异常!!!!!!!)
  41. function CMD.update_proto()
  42. protobuf.clear_register_file()
  43. register_protos()
  44. end
  45. -- 服务退出
  46. function CMD.exit()
  47. skynet.retpack(nil)
  48. skynet.exit()
  49. end
  50. skynet.dispatch(
  51. "lua",
  52. function(session, _, cmd, ...)
  53. local func = assert(CMD[cmd], cmd .. " not found")
  54. local ret = func(...)
  55. if session > 0 and cmd ~= "exit" then
  56. skynet.retpack(ret)
  57. end
  58. end
  59. )
  60. skynet.start(
  61. function()
  62. register_protos()
  63. end
  64. )