PbManager.lua 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. local PbManager = class("PbManager")
  2. local pb = require "pb"
  3. local protoc = require("3rd/lua-protobuf/protoc")
  4. local p
  5. function PbManager:ctor()
  6. p = protoc.new()
  7. --pb.option("int64_as_string")
  8. local list = LuaMgr.Instance:GetPbFiles()
  9. for i = 0, list.Count - 1 do
  10. --local content = pb.io.read(pbs[i])
  11. --local content = LuaMgr.Instance:ReadFile()
  12. pb.load(list[i])
  13. --pb.loadfile(list[i])
  14. end
  15. self.protoRequestName2Type = {}
  16. for name, number, type in pb.fields "serverproto.Request" do
  17. self.protoRequestName2Type[number] = type
  18. --print(name, number, type)
  19. end
  20. self.protoResponseId2Types = {}
  21. for name, number, type in pb.fields "serverproto.Response" do
  22. self.protoResponseId2Types[number] = type
  23. --print(name, number, type)
  24. end
  25. end
  26. function PbManager:GetEnumValue(enumTypeName, enumName)
  27. if pb then
  28. return pb.enum("serverproto." .. tostring(enumTypeName) , enumName)
  29. end
  30. return nil
  31. end
  32. function PbManager:EncodePb(id, param)
  33. local reqType = self.protoRequestName2Type[id]
  34. local bytes = nil;
  35. if reqType == nil then
  36. LogError(id.." net pb isnt exist")
  37. return bytes
  38. end
  39. if param ~= nil then
  40. bytes = assert(pb.encode(reqType, param))
  41. end
  42. if id == 1116 then
  43. local testData = pb.decode(reqType, bytes)
  44. --DebugHelper.LogError(tostring(param.t_uid));
  45. --DebugHelper.LogError(tostring(testData.t_uid));
  46. end
  47. --print(pb.tohex(bytes))
  48. return bytes
  49. end
  50. local function CheckUintType(pbType, data)
  51. for name, number, type1 in pb.fields(pbType) do
  52. if data[name] ~= nil then
  53. if type1 == "uint64" then
  54. if type(data[name]) == "table" then
  55. --local list = {}
  56. --for _,v in pairs(data[name]) do
  57. -- local number = string.gsub(v, "#", "")
  58. -- local a = uint64.new(number)
  59. -- list[#list + 1] = a
  60. --end
  61. --data[name] = list
  62. else
  63. --local number = string.gsub(data[name], "#", "")
  64. --local a = uint64.new(number)
  65. --data[name] = a
  66. --LogError(tostring(data[name]))
  67. end
  68. elseif type1 == "sint64" then
  69. --if type(data[name]) == "table" then
  70. -- local list = {}
  71. -- for _,v in pairs(data[name]) do
  72. -- local number = string.gsub(v, "#", "")
  73. -- local a = int64.new(number)
  74. -- list[#list + 1] = a
  75. -- end
  76. -- data[name] = list
  77. --else
  78. -- local number = string.gsub(data[name], "#", "")
  79. -- local a = int64.new(number)
  80. -- data[name] = a
  81. --end
  82. else
  83. --CheckUintType(type1, data[name])
  84. end
  85. --if string.find(type1, ".serverproto.") ~= nil then
  86. -- CheckUintType(type1, data[name])
  87. --elseif type1 == "uint32"then
  88. -- local bytes = LuaUInt32.FromString(data[name])
  89. -- data[name] = LuaUInt32.BytesToUInt32(bytes)
  90. --elseif type1 == "uint64" then
  91. -- local bytes = LuaUInt64.FromString(data[name])
  92. -- data[name] = LuaUInt64.BytesToUInt64(bytes)
  93. --end
  94. end
  95. end
  96. end
  97. function PbManager:DecodePb(msgId, msgBody_)
  98. if self.protoResponseId2Types[msgId] == nil then
  99. LogError("ERROR...No Register Protocol: " .. msgId)
  100. return
  101. end
  102. local repType = self.protoResponseId2Types[msgId]
  103. local data = assert(pb.decode(repType, msgBody_))
  104. --CheckUintType(repType, data)
  105. return data
  106. end
  107. function PbManager:Destroy()
  108. self.protoRequestName2Type = nil
  109. self.protoResponseId2Types = nil
  110. if tolua.getpeer(self) ~= nil then
  111. tolua.setpeer(self, nil)
  112. end
  113. end
  114. function PbManager:check_msg(name, data, r)
  115. LogError(string.format("%d",data.u64v))
  116. local chunk2, _ = pb.encode(name, data)
  117. local data2 = pb.decode(name, chunk2)
  118. --print("msg: ", name, "<"..chunk2:gsub(".", function(s)
  119. --return ("%02X "):format(s:byte())
  120. --end)..">")
  121. --eq(data2, r or data)
  122. LogError("data = "..Inspect(data))
  123. LogError("data2 = "..Inspect(data2))
  124. end
  125. --local buffer = require "pb.buffer"
  126. function PbManager:check_load(chunk, name)
  127. local pbdata = protoc.new():compile(chunk, name)
  128. local ret, offset = pb.load(pbdata)
  129. if not ret then
  130. error("load error at "..offset..
  131. "\nproto: "..chunk..
  132. "\ndata: ")--.buffer(pbdata):tohex()
  133. end
  134. end
  135. local testpbMsg = [[
  136. message TestTypes {
  137. optional int64 u64v = 1;
  138. } ]]
  139. function PbManager:TestMsgIs()
  140. self:check_load(testpbMsg)
  141. self:check_msg("TestTypes",{u64v = 7574286654560534})
  142. end
  143. return PbManager