pbc icon indicating copy to clipboard operation
pbc copied to clipboard

pbc 解析 decode context error

Open dongxie opened this issue 9 years ago • 3 comments

从c++发来的一段数据,0x0a,0x2b,0x08,0xb1,0xe1,0xe2,0x04,0x10,0x00,0x18,0x00,0x20,0x00,0x28,0xe7,0xa3,0xe9,0xdc,0x03,0x32,0x06,0x47,0x75,0x6f,0x72,0x75,0x69,0x3a,0x01,0x66,0x40,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0x01,0x48,0x01,0x50,0x00,0x00 在pbc中无法完成解析,基本上所有的repeated message都解析不成功,c++版本2.41不知道是否有问题,结构如下 message control_user_list //REQ|OGID_GAME_USERLIST 12313 { message user_attr { required int32 uid = 1; optional int32 pos = 2 [default = 0]; optional int32 level = 3 [default = 0]; optional int32 vip = 4 [default = 0]; optional int64 golds = 5 [default = 0]; optional string nn = 6; optional string sex = 7; optional int32 seat = 8 [default = -1]; optional int32 sign = 9 [default = 0];
optional int32 lockdealer = 10 [default = 0]; optional int32 firstbet = 11; optional int64 chips = 12 [default = 0]; optional int64 lstchips = 13 [default = 0]; optional int32 action = 14 [default = 0]; optional int32 laction = 15 [default = 0]; optional string cs = 16 [default = '']; optional int32 pstate = 17 [default = 0]; } repeated user_attr users = 1; }

dongxie avatar Apr 09 '16 17:04 dongxie

你的数据有问题。

ox0a, 0x2b 指一个带长度的数据段。但后面一共有. 2c 个字节,多出来的最后一个字节是 0 , 后面必须有一个 varint.

所以不是你多了个字节,就是少了东西。

建议读一下 pb 的编码协议,人肉分析一下数据。

cloudwu avatar Apr 10 '16 16:04 cloudwu

我也是报这个错:decode context error go发过来的数据:\n\x03Tom\x10\x1e\x1a\x05hello\x1a\x05world\x1a\x01!, 其字节数组为:10,3,84,111,109,16,30,26,5,104,101,108,108,111,26,5,119,111,114,108,100,26,1,33 proto结构: message Lua { string Name = 1; int32 Age = 2; repeated string Arr = 3; } Lua里解析代码: function OnData(data,_url,_type,_tag,length) local byteBuffer = ByteBuffer.New(data); local _data = byteBuffer:ReadBuffer() local path = dataPath .. "/config/lua/3rd/pbc/test.pb" local test = io.open(path,"rb") local buffer = test:read "*a" test:close() protobuf.register(buffer) local a,b = protobuf.decode("main.Lua",_data,length) if b then print(b) end end 请求指导。

softwareyangsong avatar Jul 19 '17 09:07 softwareyangsong

搞定了,原来是protobuf.lua版本问题

softwareyangsong avatar Jul 20 '17 08:07 softwareyangsong