使用 Protocol Buffers 从文件中读取消息的问题

Problems using Protocol Buffers to read messages from file

我正在尝试通过 CodedInputStream 使用 Google Protocol Buffers to read multiple messages from a file. The documentation suggests

但是如果我尝试阅读的不仅仅是一条非常小的消息,我会从 MergeFromCodedStream

中收到失败消息

例如,如果我将消息定义为:

message Chunk {
  repeated int64 values = 1 [packed=true];
}

然后尝试将消息写入文件然后读回:

int main() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;
  {
      Chunk chunk;
      for (int i = 0; i != 26; ++i)
        chunk.add_values(i);

      std::ofstream output("D:\temp.bin");
      OstreamOutputStream raw_output(&output);

      if (!writeDelimitedTo(chunk, &raw_output)){
        std::cout << "Unable to write chunk\n";
        return 1;
      }
  }
  {
    std::ifstream input("D:\temp.bin");
    IstreamInputStream raw_input(&input);
    Chunk in_chunk;

    if (!readDelimitedFrom(&raw_input, &in_chunk)) { // <--- Fails here
      std::cout << "Unable to read chunk\n";
      return 1;
    }

    std::cout << "Num values in chunk " << in_chunk.values_size() << "\n";
  }

  google::protobuf::ShutdownProtobufLibrary();
}

其中 writeDelimitedToreadDelimitedFrom 来自 C++ protobuf 库作者的 this answer

bool writeDelimitedTo(
  const google::protobuf::MessageLite& message,
  google::protobuf::io::ZeroCopyOutputStream* rawOutput) {
  google::protobuf::io::CodedOutputStream output(rawOutput);

  const int size = message.ByteSize();
  output.WriteVarint32(size);

  uint8_t* buffer = output.GetDirectBufferForNBytesAndAdvance(size);
  if (buffer != NULL) {
    message.SerializeWithCachedSizesToArray(buffer);
  } else {
    message.SerializeWithCachedSizes(&output);
    if (output.HadError()) return false;
  }

  return true;
}

bool readDelimitedFrom(
  google::protobuf::io::ZeroCopyInputStream* rawInput,
  google::protobuf::MessageLite* message) {
  google::protobuf::io::CodedInputStream input(rawInput);

  uint32_t size;
  if (!input.ReadVarint32(&size)) return false;

  google::protobuf::io::CodedInputStream::Limit limit =
    input.PushLimit(size);

  if (!message->MergeFromCodedStream(&input)) return false; // <-- Fails here
  if (!input.ConsumedEntireMessage()) return false;

  input.PopLimit(limit);

  return true;
}

如果我只在我的消息中写入 25 个值它就可以工作,26 个就失败了。我已经在代码中展示了它失败的地方。

我试过调试 protobuf 库,它似乎无法将新数据读入缓冲区,但我不知道为什么。

我正在使用 Visual Studio 2013 和 protobuf 2.6.1。

正如@rashimoto 正确指出的那样,我未能以二进制模式打开我的文件!

修复后,我可以成功地将多条消息写入文件:

int main() {
  GOOGLE_PROTOBUF_VERIFY_VERSION;
  {
    std::vector<Chunk> chunks = createChunks(NUM_CHUNKS, CHUNK_SIZE);

    std::ofstream output("D:\temp.bin", std::ios::binary);
    OstreamOutputStream raw_output(&output);

    for (Chunk& chunk : chunks) {
      if (!writeDelimitedTo(chunk, &raw_output)){
        std::cout << "Unable to write chunk\n";
        return 1;
      }
    }
  }
  {
    std::ifstream input("D:\temp.bin", std::ios::binary);
    IstreamInputStream raw_input(&input);
    std::vector<Chunk> chunks(NUM_CHUNKS);

    for (auto& chunk : chunks) {
      if (!readDelimitedFrom(&raw_input, &chunk)) {
        std::cout << "Unable to read chunk\n";
        return 1;
      }
    }

    std::cout << "Num values in first chunk " << chunks[0].values_size() << "\n";
  }

  google::protobuf::ShutdownProtobufLibrary();
}