I'm using protocol buffers for c++ to generate a string that should be send over the network. But when calling ParseFromString
I receive false
as return and no error message!?
I create the object to be send as follows:
Messages::RequestMessage msg;
msg.set_type(1);
msg.set_epid(4);
string s;
if (msg.SerializeToString(&s)) {
Log("serialized");
return s;
} else {
Log("error serializing", log::error);
return "";
}
The generated string should now be send via async_write
from boost::asio over the network. I have tried different approaches here:
max_length = 1024;
char data_[max_length];
memset(data_, '\0', sizeof(char)*max_length);
//first tried
//const char *msg = s.c_str();
//memcpy(data_, msg, strlen(msg));
memcpy(data_, s.data(), s.size());
boost::asio::async_write(socket_, boost::asio::buffer(data_, max_length),
boost::bind(&Session::handle_write, this,
boost::asio::placeholders::error));
And on the receiver side:
char reply_[max_length];
boost::asio::async_read(socket_, boost::asio::buffer(reply_, max_length),
boost::bind(&Client::handle_read, this,
boost::asio::placeholders::error,
boost::asio::placeholders::bytes_transferred));
string str(reply_);
Messages::RequestMessage m;
bool ret = m.ParseFromString(str);
//which gives ret=false but no error msg is displayed!?
I think there might be something wrong with transforming the string into char* and back?