如何检测时已被完全接收一个协议缓冲区的消息? [英] How to detect when a Protocol Buffer message is fully received?

查看:189
本文介绍了如何检测时已被完全接收一个协议缓冲区的消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是怎样的一个分支了我的其他问题。读它,如果你喜欢,但它不是必需的。

This is kind of a branch off of my other question. Read it if you like, but it's not necessary.

基本上,我意识到,为了有效地使用C#的大消息BeginReceive(),我需要是(a )先读取数据包长度,然后阅读正是许多字节或(b)使用结束分组的分隔符。我的问题是,要么这些存在于协议缓冲区?我没有用他们尚未,但去的文档在它似乎并不像有一个长头或分隔符。

Basically, I realized that in order to effectively use C#'s BeginReceive() on large messages, I need to either (a) read the packet length first, then read exactly that many bytes or (b) use an end-of-packet delimiter. My question is, are either of these present in protocol buffers? I haven't used them yet, but going over the documentation it doesn't seem like there is a length header or a delimiter.

如果不是,我应该怎么办?我应该只是建立消息的长度页眉/ EOP分隔符然后前缀/后缀呢?

If not, what should I do? Should I just build the message then prefix/suffix it with the length header/EOP delimiter?

推荐答案

您需要包括大小或结束标志在你的协议。没有内置于基于流套接字(TCP / IP),比支承八位任意分成单独的数据包的不确定流等(和数据包可以在运输过程中以及被溢出)。

You need to include the size or end marker in your protocol. Nothing is built into stream based sockets (TCP/IP) other than supporting an indefinite stream of octets arbitrarily broken up into separate packets (and packets can be spilt in transit as well).

一个简单的方法是对每一个信息有一个固定大小标头,包括一个协议版本和净荷大小和任何其它固定的数据。然后,该消息内容(负载)。

A simple approach would be for each "message" to have a fixed size header, include both a protocol version and a payload size and any other fixed data. Then the message content (payload).

可选的消息页脚(固定大小)可以用一个校验和,甚至是密码签名加入(取决于你的可靠性/安全性要求)。

Optionally a message footer (fixed size) could be added with a checksum or even a cryptographic signature (depending on your reliability/security requirements).

知道了有效载荷的大小可​​以让你保持读取的字节数,这将对于消息的其余部分是足够(如果读取与少,做完成另一种阅读,直到整个消息已收到剩余的字节)。

Knowing the payload size allows you to keep reading a number of bytes that will be enough for the rest of the message (and if a read completes with less, doing another read for the remaining bytes until the whole message has been received).

有一个结束消息的指标也可以,但是你需要定义如何处理包含您的留言同样的八位顺序...

Having a end message indicator also works, but you need to define how to handle your message containing that same octet sequence...

这篇关于如何检测时已被完全接收一个协议缓冲区的消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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