How to detect when a Protocol Buffer message is fully received?

后端 未结 4 1626
再見小時候
再見小時候 2020-12-30 13:35

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

Basically, I realized that in order to effectively use C#\'s BeginRe

4条回答
  •  我在风中等你
    2020-12-30 13:50

    I agree with Matt that a header is better than a footer for Protocol Buffers, for the primary reason that as PB is a binary protocol it's problematic to come up with a footer that would not also be a valid message sequence. A lot of footer-based protocols (typically EOL ones) work because the message content is in a defined range (typically 0x20 - 0x7F ASCII).

    A useful approach is to have your lowest level code just read buffers off of the socket and present them up to a framing layer that assembles complete messages and remembers partial ones (I present an async approach to this (using the CCR) here, albeit for a line protocol).

    For consistency, you could always define your message as a PB message with three fields: a fixed-int as the length, an enum as the type, and a byte sequence that contains the actual data. This keeps your entire network protocol transparent.

提交回复
热议问题