multiplexing/demultiplexing protobuf 流中的不同消息类型
multiplexing/demultiplexing different messages types in a protobuf stream
我想通过线路发送一连串不同的 protobuf 消息,并能够在它们到达时区分它们。
假设我有一个这样的 *.proto:
message Book {
//...
}
message BlueRay{
//...
}
然后是发送方,我序列化假设这个序列(C# 中的伪代码):
Book1.WriteDelimitedTo(myStream);
BlueRay1.WriteDelimitedTo(myStream);
Book2.WriteDelimitedTo(myStream);
我怎样才能知道接收方接收到的 order/types 消息? (当然,合同在发送方和接收方都可用)
根据发件人的状态,我无法 presume/tell 将要发送什么以及发送顺序...
我知道没有像 the documentation 中所述的内置方法来做到这一点,但是例如对于消息的大小有一个帮助程序(C# API 帮助程序 WriteDelimited 方法嵌入尺寸)。
如何 get/map 接收到的消息类型?
我的服务器将用给定的语言(实际上是 C#)编写,但我的客户端应该是 "implementable" 在任何支持 protobuf 的目标中,所以我不想设置会序列化 C#/CLR 的东西介于两者之间的具体内容...
我可能以一种奇怪的方式使用了 protobuf?我正在尝试建立一种协议。
我想我终于找到了如何做到这一点(目前仅在 C# 中)。
基本上,我正在将以下内容写入流:
- 即将到来的消息描述符的目标名称(在 proto 文件中,因为两端共享此定义),使用原语进行字符串序列化
- 即将到来的消息大小,使用原语进行大小序列化
- 序列化消息入流
这会产生如下所示的方法:
public static void WriteToStream(Stream outputStream, IMessage message)
{
MessageDescriptor stateMsgDescriptor = message.Descriptor;
using (CodedOutputStream codedOutStr = new CodedOutputStream(outputStream, true))
{
codedOutStr.WriteString(stateMsgDescriptor.FullName);
int size = message.CalculateSize();
codedOutStr.WriteLength(size);
message.WriteTo(codedOutStr);
codedOutStr.Flush();
}
}
The Protocol Buffer wire format is not self-delimiting, so protocol
buffer parsers cannot determine where a message ends on their own
我想通过线路发送一连串不同的 protobuf 消息,并能够在它们到达时区分它们。
假设我有一个这样的 *.proto:
message Book {
//...
}
message BlueRay{
//...
}
然后是发送方,我序列化假设这个序列(C# 中的伪代码):
Book1.WriteDelimitedTo(myStream);
BlueRay1.WriteDelimitedTo(myStream);
Book2.WriteDelimitedTo(myStream);
我怎样才能知道接收方接收到的 order/types 消息? (当然,合同在发送方和接收方都可用) 根据发件人的状态,我无法 presume/tell 将要发送什么以及发送顺序...
我知道没有像 the documentation 中所述的内置方法来做到这一点,但是例如对于消息的大小有一个帮助程序(C# API 帮助程序 WriteDelimited 方法嵌入尺寸)。
如何 get/map 接收到的消息类型?
我的服务器将用给定的语言(实际上是 C#)编写,但我的客户端应该是 "implementable" 在任何支持 protobuf 的目标中,所以我不想设置会序列化 C#/CLR 的东西介于两者之间的具体内容...
我可能以一种奇怪的方式使用了 protobuf?我正在尝试建立一种协议。
我想我终于找到了如何做到这一点(目前仅在 C# 中)。
基本上,我正在将以下内容写入流:
- 即将到来的消息描述符的目标名称(在 proto 文件中,因为两端共享此定义),使用原语进行字符串序列化
- 即将到来的消息大小,使用原语进行大小序列化
- 序列化消息入流
这会产生如下所示的方法:
public static void WriteToStream(Stream outputStream, IMessage message)
{
MessageDescriptor stateMsgDescriptor = message.Descriptor;
using (CodedOutputStream codedOutStr = new CodedOutputStream(outputStream, true))
{
codedOutStr.WriteString(stateMsgDescriptor.FullName);
int size = message.CalculateSize();
codedOutStr.WriteLength(size);
message.WriteTo(codedOutStr);
codedOutStr.Flush();
}
}
The Protocol Buffer wire format is not self-delimiting, so protocol buffer parsers cannot determine where a message ends on their own