I am trying to use Google protocol buffers (protobuf) with Python in a network program using bare sockets. My question is: after the sending party sends the message, how does the receiving party know which message was sent? For example, let's say I have message definitions:
message StrMessage {
required string str = 1;
}
message IntMessage {
required int32 num = 1;
}
Now the transmitter does StrMessage, serializes it and sends serialized bytes over the network. How does the receiver know byte deserialization using StrMessage rather than IntMessage? I tried to do two things:
// Proposal 1: send one byte header to indicate type
enum MessageType {
STR_MESSAGE = 1;
INT_MESSAGE = 2;
}
// Proposal 2: use a wrapper message
message Packet {
optional StrMessage m_str = 1;
optional IntMessage m_int = 2;
}
None of them seem very clean, and both require me to list all types of messages manually. Is there a canonical / best way to handle this problem?
Thank!
source