I have a protobuf message that contains a header and a body. The header has a member called size (reflecting the size of the entire message, i.e header plus body). Since protobuf (AFAIK) does not provide message boundary, I use the size (first element of header and fixed size of 4 bytes) to know how much to slurp from the socket.
Assuming I have read (or recv(2)ed) 4 bytes from the socket, I now need to convert (decode) this to an int so I can instruct recv(2) to slurp that much more.
How do I convert / decode this wire format to an int in C++ land ?
I have tried ParseFromString() but this function is returning a boolean and the doc does not tell me much.
I will show what I think is relevant...lets see
message PsdAgentMsg {
message Header {
fixed32 theSize = 1; // size includes header and message
uint32 theInstanceId = 2;
Type theMsgType = 3;
}
Header theHeader = 1;
oneof theMsg {
abc = 2;
def = 3;
ghi = 4;
klm = 5;
PsdAgentGPCMsg theGPCMsg = 6;
}
}
message PsdAgentGPCMsg {
int32 theCount = 1;
}
...few minutes later..
PsdAgentMsg *msg = new PsdAgentMsg(); // psd msg is header + body
...
rc = recv(sock, buf, 4, 0); // I am simplifying some stuff
std::string sizeString = buf;
...
size_t payloadSize = msg->ParseFromString(sizeString);
I am finding that payloadSize = 0. which is a false being assigned to a size_t. So ParseFromString() does not seem to be the right way to decode the 4 bytes. Again, I need to decode the sizeString to an int. so I can say recv(sock, buf, payloadSize,0)
int32field? What's the type ofpayloadSize? This question needs a minimal reproducible example to be answerable. - Miles Budnek