设置 protobuf 对象的最大大小 [英] Set maximum size of protobuf object

查看:110
本文介绍了设置 protobuf 对象的最大大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些 protobuf 对象,我们称之为 Msg,有一些重复的字段.我想序列化它并发送.但是我有包裹的最大限制,我可以发送.那么是否有可能将这个对象与一些小对象分开?或者可以设置序列化对象的最大大小?

I have some protobuf object, let's call it's Msg, with some repeated fields. I want to serialize it and send. But i have max limit for package, witch i can send. So is it possible to separate this object to some little object's? Or may be set maximum size of serialized objects?

推荐答案

文档,protobuf 不处理消息的开始和停止位置,因此您必须自己做.

As described in the documentation, protobuf does not take care of where a message start and stops, so you'll have to do that yourself.

protobuf 序列化器的输出只是一个序列化的缓冲区.这意味着没有实用的方法来设置您发送的消息的最大大小,因此需要某种机制.

The output of the protobuf serializer is just a serialized buffer. This means that there is no practical way of setting the maximum size of messages you are sending, so some sort of mechanism is needed.

来自文档:

[...] 如果您想将多条消息写入单个文件或流,则由您来跟踪一条消息的结束位置和下一条消息的开始位置.Protocol Buffer 有线格式不是自定界的,因此 Protocol Buffer 解析器无法自行确定消息的结束位置.解决这个问题最简单的方法是在写消息本身之前写下每条消息的大小.当您读回消息时,您读取大小,然后将字节读入单独的缓冲区,然后从该缓冲区解析.[...]

[...] If you want to write multiple messages to a single file or stream, it is up to you to keep track of where one message ends and the next begins. The Protocol Buffer wire format is not self-delimiting, so protocol buffer parsers cannot determine where a message ends on their own. The easiest way to solve this problem is to write the size of each message before you write the message itself. When you read the messages back in, you read the size, then read the bytes into a separate buffer, then parse from that buffer. [...]

根据您正在编写的语言,有些库可以为例如您可以使用的 TCP.

Depending on what language you're writing, there are libraries that does length-prefixing for e.g. TCP that you can use.

您还应该注意,在 TCP 套接字上发送多个 protobuf 时,它们可能会被缓冲以进行网络优化(连接)并一起发送.这意味着无论如何都需要某种分隔符.

You should also be aware of that when sending multiple protobufs on a TCP socket, they are likely to be buffered for network optimizations (concatenated) and sent together. Meaning some sort of delimiter is needed anyway.

这篇关于设置 protobuf 对象的最大大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆