Ejemplo n.º 1
0
/**
This function loads a protobuf object from a file using the assumption that it is proceeded with a 64 bit Poco::Int64 in network byte order that holds the size of the object to be loaded and deserialized.
@param inputPath: The path to the file to load from
@param inputMessageBuffer: The protobuf object to deserialize to
@return: true if the message could be loaded and deserialized and false otherwise
*/
bool pylongps::loadProtobufObjectFromFile(const std::string &inputPath, google::protobuf::Message &inputMessageBuffer)
{
//Read size of object in file
std::string sizeString;
if(readStringFromFile(sizeString, sizeof(Poco::Int64), inputPath) == false) 
{
return false;
}

Poco::Int64 objectSizeNetworkOrder = *((Poco::Int64 *) sizeString.c_str());
Poco::Int64 objectSize = Poco::ByteOrder::fromNetwork(objectSizeNetworkOrder);

//Read size+object
std::string sizeAndObjectString;
if(readStringFromFile(sizeAndObjectString, sizeof(Poco::Int64)+objectSize, inputPath) == false) 
{
return false;
}

//Clip off size
std::string objectString = sizeAndObjectString.substr(sizeof(Poco::Int64));

//Deserialize the protobuf object
inputMessageBuffer.ParseFromString(objectString);

if(!inputMessageBuffer.IsInitialized())
{
printf("Message not initialized\n");
return false;
}

return true;
}
Ejemplo n.º 2
0
bool read_message(boost::asio::ip::tcp::socket& s, ::google::protobuf::Message& msg)
{
   bling_pb::header hdr;
   string str = read_string(s, header_length);
   if (!hdr.ParseFromString(str))
      return false;
   //cout << "hdr::" << hdr.ShortDebugString() << endl;

   str = read_string(s, hdr.len());
   //hexdump(str);
   if (hdr.msg_id() == bling_pb::header::SLAVE_LIST && 
       (typeid(msg) == typeid(bling_pb::slave_list)))
      return msg.ParseFromString(str);

   cout << "Received message doesn't match expected." << endl;
   return false;
}
Ejemplo n.º 3
0
 void decode(google::protobuf::Message& message, const std::string& data, Client* client) {
   if (!message.ParseFromString(data))
     throw DecodeFailed("Failed to decode message");
 }