简体   繁体   English

Protobuf ParseDelimitedFrom在C ++中的实现

[英]Protobuf ParseDelimitedFrom implementation in C++

C# Publisher is publishing continuos marketdata messages in custom protobuff format over the socket using "writeDelimitedTo" API. C#Publisher正在使用“ writeDelimitedTo” API通过套接字以自定义原型格式发布持续市场消息。 I have to read all messages in C++ and desearialize it. 我必须阅读C ++中的所有消息并对其进行反序列化。 Below is my code. 下面是我的代码。 Since C++ don't have "parseDelimitedFrom", so have coded something like below after going through multiple suggestions in this forum. 由于C ++没有“ parseDelimitedFrom”,因此在此论坛中经过多次建议后,已编写了如下所示的代码。

Now my question is - Refering to the code below, If the first message size is less than 1024 then in the first iteration, i will have full stream of the 1st message and part of the stream from the 2nd message. 现在我的问题是-参考下面的代码,如果第一个消息的大小小于1024,则在第一次迭代中,我将拥有第一条消息的完整流和第二条消息的部分流。 After deserializing first message, How can i read remaining streams of the second message from socket and merge it with the stream which i read in the previous iteration ? 反序列化第一条消息后,如何从套接字读取第二条消息的其余流并将其与上一次迭代中读取的流合并?

EDIT: Support for "delimited" format is now part of the official protobuf library. 编辑: 对“定界”格式的支持现在是官方protobuf库的一部分。 The post below predates it being added. 下面的帖子早于其添加。

I've written optimally-efficient versions of parseDelimitedFrom and writeDelimitedTo in C++ here (the read and write methods of Uncompressed ): 我写的优化高效的版本parseDelimitedFromwriteDelimitedTo在C ++这里(的readwrite的方法Uncompressed ):

https://github.com/capnproto/capnproto/blob/06a7136708955d91f8ddc1fa3d54e620eacba13e/c%2B%2B/src/benchmark/protobuf-common.h#L101 https://github.com/capnproto/capnproto/blob/06a7136708955d91f8ddc1fa3d54e620eacba13e/c%2B%2B/src/benchmark/protobuf-common.h#L101

Feel free to copy. 随时复制。

These implementations read from / write to a ZeroCopyInputStream / ZeroCopyOutputStream .(Hmm, for some reason my write is declared to use FileOutputStream , but you should be able to just change that to ZeroCopyOutputStream .) 这些实现从ZeroCopyInputStream / ZeroCopyOutputStream读取/写入。(嗯,出于某种原因,我的write被声明为使用FileOutputStream ,但是您应该能够将其更改为ZeroCopyOutputStream 。)

So, you'll need to create a ZeroCopyInputStream which reads from your StreamSocket , then pass it to my read() . 因此,您需要创建一个从您的StreamSocket读取的ZeroCopyInputStream ,然后将其传递给我的read()

It looks like StreamSocket is a classic copying-read interface. 看起来StreamSocket是一个经典的复制读取接口。 You should therefore use CopyingInputStreamAdaptor as your ZeroCopyInputStream , wrapping an implementation of CopyingInputStream which reads from your StreamSocket . 因此,应该将CopyingInputStreamAdaptor用作ZeroCopyInputStream ,并包装从StreamSocket读取的CopyingInputStream的实现。

https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.io.zero_copy_stream_impl_lite#CopyingInputStreamAdaptor https://developers.google.com/protocol-buffers/docs/reference/cpp/google.protobuf.io.zero_copy_stream_impl_lite#CopyingInputStreamAdaptor

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM