I'm looking at the advantages and disadvantages for two the different TCP message framing methods I'm aware of.
- 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
- 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.