When designing our wire protocol, we realized that when transmitting raw data bytes on a socket, there’s no inherent concept of “response matching a request”, unlike working with the HTTP protocol that we’re familiar with. (This is apparent because HTTP itself is a protocol built on top of TCP/IP socket layer.)
So we want to include sufficient information in our protocol message, so that the receiver can use them to figure out the state and perform the corresponding behavior.
Each message contains a header with the following structure:
HEADER_FORMAT = '!BHI' # "!": Network byte order (big-endian)
# version (1 byte), message_type (2 bytes), body_length (4 bytes)
The message body follows a hierarchical structure supporting:
Each message type and their fields are explicitly defined, so we know how to deserialize and parse it when we receive a specific type of data.
Definition of MessageField: