pbuf.lua 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. -- local protobuf = require "protobuf" --引入文件protobuf.lua
  2. -- --注册protobuffer文件
  3. -- protobuf.register_file(protofile)
  4. -- ​
  5. -- --根据注册的protofile中的类定义进行序列化,返回得到一个stringbuffer
  6. -- protobuf.encode("package.message", { ... })
  7. -- ​
  8. -- --根据注册的protofile中的类定义进行反序列化
  9. -- protobuf.decode("package.message", stringbuffer)
  10. local skynet = require "skynet"
  11. local protobuf = require "protobuf" --引入文件protobuf.lua
  12. local stringify = require "stringify"
  13. protobuf.register_file "./proto/game.pb"
  14. local packname = "game." -- 包名
  15. local Sproto = {}
  16. local tmap = {}
  17. Sproto.enum_id = function (name)
  18. local ret = protobuf.enum_id("game.msg_cmd", "cmd_"..name)
  19. if not ret then
  20. assert(false, "not enum:cmd_"..name)
  21. end
  22. return ret
  23. end
  24. Sproto.decode = function(msg)
  25. local e_id = string.unpack("I4", msg)
  26. if not e_id then
  27. skynet.error("Sproto.decode not e_id")
  28. return
  29. end
  30. local name = tmap[e_id]
  31. if not name then
  32. skynet.error("Sproto.decode not name, e_id:"..e_id)
  33. return nil, nil
  34. end
  35. local str = string.sub(msg, 5, #msg)
  36. local ret = protobuf.decode(packname..name, str)
  37. local function fun(args)
  38. return Sproto.encode(name.."_rsp", args)
  39. end
  40. return "REQUEST", name, ret, fun
  41. end
  42. Sproto.encode = function (name, args, ...)
  43. local e_id = Sproto.enum_id(name)
  44. local ret = protobuf.encode(packname..name, args)
  45. if ret and e_id then
  46. return string.pack("I4", e_id)..ret
  47. end
  48. return ret
  49. end
  50. Sproto.register_msg = function(name)
  51. local e_id = Sproto.enum_id(name)
  52. if e_id then
  53. tmap[e_id] = name
  54. end
  55. end
  56. -- skynet.start(function()
  57. -- skynet.error("protobuf game.pb")
  58. -- --编码
  59. -- local stringbuffer = protobuf.encode("game.login", --对应person.proto协议的包名与类名
  60. -- {
  61. -- sid = 1,
  62. -- account = 3,
  63. -- })
  64. -- stringbuffer = string.pack("I4", 65536)..stringbuffer
  65. -- local msg = string.unpack("I4", stringbuffer)
  66. -- local str = string.sub(stringbuffer, 5, #stringbuffer)
  67. -- -- 解码
  68. -- local data = protobuf.decode("game.login",str)
  69. -- for k, v in pairs(data or {}) do
  70. -- skynet.error(k, v)
  71. -- end
  72. -- end)
  73. skynet.start(function()
  74. skynet.error("protobuf game.pb")
  75. Sproto.register_msg("login")
  76. Sproto.register_msg("login_rsp")
  77. --编码
  78. local code = Sproto.encode("login", {
  79. sid = 1,
  80. account = 3,
  81. })
  82. local req, name , args, func= Sproto.decode(code)
  83. skynet.error(req, name)
  84. skynet.error(stringify(args or {}))
  85. local code2 = func({
  86. errno = 3;
  87. })
  88. req, name , args, func= Sproto.decode(code2)
  89. skynet.error(req, name)
  90. skynet.error(stringify(args or {}))
  91. end)