Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Message Framing Method advantages and disadvantages in .NET

Tags:

.net

sockets

I'm looking at the advantages and disadvantages for two the different TCP message framing methods I'm aware of.

  1. Delimited: The TCP stream is broken into messages of a non fixed length by using a delimiter byte. When sending data the routine must check the message data for the delimiter and escape it to ensure safe transmission of the message frame. When receiving data the routine must read through the stream looking for the delimiter byte to break the frame into a message.

EG: User [Username]\nPassword [Password]\n

  1. Length-Prefixed: The TCP stream is broken into messages of a predetermined size by using a prefix of say 4 bytes to state the length of the message. When receiving data the routine will first read the prefix to determine the length of the message frame. When sending data the routine must prepend the length prefix to the message before transmission.

EG: [MessageLength]User [Username][MessageLength]Password [Password]

Both of these methods allow transmission of message frames that can vary in size and contain a stream of bytes to be interpreted. The higher level message structure or protocol is not relevant.


So I focus my attention towards scalability and performance efficiency. I find myself needing to run benchmark tests to see which method I can receive the greatest efficiency throughput without any message processing involved.


My current thoughts, I'm no expert by any means.

Delimited message frames would have lower efficiency during the receive routine as each byte in the stream needs to be checked for a message frame delimiter. Length-Prefixed message frames will always read the prefix bytes and the rest of the message frame stream will go straight into the buffer without processing until the entire message frame is received.

Where as Length-Prefixed message frames would have a lower efficiency during the send routine as the message prefix as to be transmitted before the message itself.


Other factors I can think could include:

  • Lots of small message frames would result in more packets being transmitted for a Length-Prefixed structure.
  • Lots of larger message frames would be process much more efficiently with a Length-Prefixed structure as each byte is not being read to check for a delimiter.

Any light across this topic would be awesome. I find it very hard to find good resources on the difference between Message Frame structures for TCP.

like image 523
Matthew Dunn Avatar asked Aug 17 '12 04:08

Matthew Dunn


1 Answers

From my experience length prefixing is preferred, the parsing code of messages tends to be easier to write.

Besides, with message delimiters you need to figure out an escaping scheme if message payload may contain the delimiter character.

I have come across a third scheme that is not payload agnostic. It defines different message types with known format, the different parts of the message may be either fixed or variable length. (Fixed for simple types and variable is arrays and strings). The structure is shared between client and server beforehand. When sending a message the message is prefixed with the message type number. From the message type number the receiving part can deduce how to parse the message.
An example of this is the protocol for the LysKOM messaging system.
This protocol has a formal specification that can be used to generate parser code.

like image 139
Albin Sunnanbo Avatar answered Oct 19 '22 23:10

Albin Sunnanbo