【问题标题】:Protobuf declaring i'm missing required fields when i'm clearly not当我显然不是时,Protobuf 声明我缺少必填字段
【发布时间】:2013-09-06 20:49:59
【问题描述】:

我正在编写一个使用 protobuf 作为序列化系统的轻量级服务器-客户端系统。我在 protobufs 抱怨缺少必填字段时遇到问题,但 1)这些字段显然存在于数据流中,并且 2)错误消息无法识别缺少的字段。

首先是一段代码:

myMessages::DataRequest cData;  // this is declared outside of the function in the global
                                // so it can be reused each time to avoid unnecessary
                                // memory allocation. Per the documentation in protobufs
.
.
.

bool processStream(u_int8_t *pStream, u_int32_t dwLength)
{
try
{
    if (!cData.ParseFromArray(pStream, dwLength))
    {
        printf("Failed to parse data stream\n");
        hexdump(pStream, dwLength);
        return false;
    }
}
catch(exception e)
{
    printf("Exception in ParseFromArray: %s", e.what());
    hexdump(pStream, dwLength);
    return false;
}

这是我获取完整数据流并尝试让 protobufs 对其进行解码的代码。大多数情况下,这工作正常。但是通过这段代码的每一次迭代我都会得到这个错误:

[libprotobuf ERROR google/protobuf/message_lite.cc:123] Can't parse message of type "myMessages.DataRequest" because it is missing required fields: 

由于失败,我的代码吐出了这个,向我展示了分配给 protobuf 解析器的缓冲区:

Failed to parse data stream
000000: 08 86 ad 80 a5 f0 e7 aa e7 01 12 06 64 37 32 36  ............d726
000010: 31 37

DataRequest .proto 非常简单...

message DataRequest {
required uint64 timeStamp = 1;
required string strid= 2;
optional bool checkin= 3;

// event messages
repeated internalMessage1 iEvents = 5;
repeated internalMessage2 oEvents = 6;
repeated internalMessage3 eEvents = 7;
repeated internalMessage4 qEvents = 8;

// aux messages
repeated auxMessages auxEvents = 20;

}

这个 proto 表明唯一需要的字段是 1 和 2(分别是时间戳和 strid)。解码在 hexdump 中看到的数据的有线格式,您可以清楚地看到两个必填字段都存在。此外,您可以看到缺少重复的字段(所以它不像我在其中一个字段中缺少必填字段!)。

知道为什么 protobufs 在这里是个混蛋吗?

谢谢!

tjac.

【问题讨论】:

    标签: c++ c protocol-buffers


    【解决方案1】:

    事实证明,protobufs 不喜欢使用全局声明的变量跨多个线程使用。我遇到的错误是我的服务器正在处理多个数据流的结果。我将上面的 protobuf 处理程序代码声明为每个线程调用的独立函数。由于没有排他性检查,因为我正处于一个调用的中间(例如 ParseFromArray),来自另一个线程的另一个调用可能正在修改对象中的数据。

    长话短说,我没有关注多线程编程的基本租户,因此我注定要花几个小时来弄清楚 protobufs 是如何失败的。我只能希望这对将来的其他人有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-02
      • 2013-04-03
      • 1970-01-01
      • 2014-01-19
      • 2014-08-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多