I am using the following code to parse a message that was SerializedwithCodedStream
on to the socket:
if ( socket->read(databuffer, size) != -1)
{
google::protobuf::io::ArrayInputStream array_input(databuffer,size);
google::protobuf::io::CodedInputStream coded_input(&array_input);
data_model::terminal_data* tData = new data_model::terminal_data();
if (!tData->ParseFromCodedStream(&coded_input))
{
return;
}
else
std::cout << tData->symbol_name() << std::endl;
}
Here is how I serialized it:
data_model::terminal_data tData;
tData.set_type(1);
tData.set_client_id("C109");
tData.set_expiry("20140915");
tData.set_quantity(3500);
tData.set_strat_id("056");
tData.set_symbol_name("BANKNIFTY");
tData.set_time("145406340");
tData.set_trade_id(16109234);
int total_size = tData.ByteSize() + sizeof(int);
char *buffer = new char[total_size];
memset(buffer, '\0', total_size);
google::protobuf::io::ArrayOutputStream aos(buffer,total_size);
google::protobuf::io::CodedOutputStream *coded_output = new google::protobuf::io::CodedOutputStream(&aos);
google::protobuf::uint32 s = tData.ByteSize();
coded_output->WriteVarint32(s);
tData.SerializeToCodedStream(coded_output);
int sent_bytes = 0;
if ( (sent_bytes = send(liveConnections.at(i), buffer, total_size, MSG_NOSIGNAL)) == -1 )
liveConnections.erase(liveConnections.begin() + i);
else
std::cout << "sent " << sent_bytes << " bytes to " << i << std::endl;
delete coded_output;
delete buffer;
When I try to parse, it gives the following error at runtime:
[libprotobuf ERROR google/protobuf/message_lite.cc:123] Can't parse message of type "data_model.terminal_data" because it is missing required fields: type
But as you can see (in the second code snippet) I have set the type field. What is the problem ?