I'm looking at the advantages and disadvantages for two the different TCP message framing methods I'm aware of.
EG: User [Username]\nPassword [Password]\n
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:
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With