这个原型缓冲解码有什么问题

What's wrong with this protobuffer decode?

本文关键字:什么 问题 解码 缓冲 原型      更新时间:2023-09-26

我正在使用Google protobuffer解码一些缓冲区。我正在使用出色的@decodeIO ProtoBuf.js模块。

当我尝试解码此缓冲区时:

<Buffer 0a 0b 47 57 5f 2d 31 38 5f 39 39 32 39>

从此消息:

message PaymentResponseElement {
  optional int64  pnPaymentId       = 1;
  optional string messageCode       = 2; //won't use pb enum here
  optional int64  balanceAfterTransaction   = 3;
  optional int32  version           = 4;
}

我收到此错误:

Error: Illegal wire type for field Message.Field .core.comm.PaymentResponseElement.messageCode: 2 (0 expected)
    at ProtoBuf.Reflect.Field.decode (/home/joojo/node_modules/protobufjs/ProtoBuf.js:2095:27)
    at ProtoBuf.Reflect.Message.decode (/home/joojo/node_modules/protobufjs/ProtoBuf.js:1748:51)
    at ProtoBuf.Reflect.Field.decode (/home/joojo/node_modules/protobufjs/ProtoBuf.js:2196:46)
    at ProtoBuf.Reflect.Message.decode (/home/joojo/node_modules/protobufjs/ProtoBuf.js:1746:51)
    at Function.Message.decode (/home/joojo/node_modules/protobufjs/ProtoBuf.js:1630:41)
    at decode (/home/joojo/public_html/api/control/protoBuffer.js:94:52)
    at Object.module.exports.decode (/home/joojo/public_html/cageapi/control/protoBuffer.js:110:10)
    at decode (/home/joojo/public_html/api/control/messageStructurer.js:82:33)
    at Object.module.exports.decode (/home/joojo/public_html/api/control/messageStructurer.js:94:10
    at CleartextStream.month (/home/joojo/public_html/api/connectionHandler.js:83:32)

不知道发生了什么,而且已经发生了几次。如何解决?

我必须同意实现。该数据只有 1 个字段 - 字段编号 1,以长度为前缀(可以是字符串、二进制或子消息编辑或打包数组);如果我们推测性地将其解释为一个字符串(在 protouf 中始终是 UTF-8),它会显示为 "GW_-18_9929" ,这看起来很理智。

但是,您的消息将字段 1 声明为 int64 。字符串/长度前缀线型(线类型 2)对 int64 无效 - 事实上,唯一有效的线型是 64 位(线型 1)。

因此:您的数据有效,但它与您声明的架构不匹配。

要解密:

  • 0a 是二进制 1010 - 最后三位 (010) 是导线型,2 = 长度分隔;剩下的 1 是字段编号:1
  • 0b 是十进制 11,即后面字段的长度
  • 47 57 5f 2d 31 38 5f 39 39 32 39 是有效载荷,UTF-8 表示"GW_-18_9929"