int protobuf_msg_from_msgb_file(google::protobuf::Message & msg, const std::string & msgbfile) { string sfile; int sz = dcs::file_size(msgbfile); sfile.reserve(sz); int n = dcs::readfile(msgbfile.c_str(), (char*)sfile.data(), sfile.capacity()); if (n <= 0) { GLOG_ERR("readfile :%s error ret :%d", msgbfile.c_str(), n); return -1; } if (!msg.ParseFromArray(sfile.data(), n)) { GLOG_ERR("protobuf parse from array ereror ! buff size:%d", n); return -1; } return 0; }
bool default_protocol::parse_raw_message(const std::string &mess, google::protobuf::Message &out ) { const size_t hash_length = hash_checker_->hash_size( ); return out.ParseFromArray( mess.c_str( ) + hash_length, mess.size( ) - hash_length ); }
static void unmarshalMsg(int8_t* pBuffer, int size, ::google::protobuf::Message& msg) { bool parsedOk = msg.ParseFromArray(pBuffer, size); assert(parsedOk); }
/** This function is used to receive and deserialize a protobuf object from a ZMQ socket. @param inputSocketToReceiveFrom: This is the socket to receive the object from @param inputMessageBuffer: This is the buffer to place the received object in @param inputFlags: The flags to pass to the ZMQ socket @param inputPreappendedDataBuffer: The buffer to place x bytes before the message in @param inputPreappendedDataSize: How much data to expect to be preappended @param inputPostappendedDataBuffer: The buffer to place x bytes before the message in @param inputPostappendedDataSize: How much data to expect to be postappended @return: <true if message received, true if message deserialized correctly> @throws: This function can throw exceptions */ std::tuple<bool, bool> pylongps::receiveProtobufMessage(zmq::socket_t &inputSocketToReceiveFrom, google::protobuf::Message &inputMessageBuffer, int inputFlags, char *inputPreappendedDataBuffer, int inputPreappendedDataSize, char *inputPostappendedDataBuffer, int inputPostappendedDataSize) { if(inputPreappendedDataSize < 0 || inputPostappendedDataSize < 0) { throw SOMException("Negative data size\n", INVALID_FUNCTION_INPUT, __FILE__, __LINE__); } if((inputPreappendedDataSize > 0 && inputPreappendedDataBuffer == nullptr) || (inputPostappendedDataSize < 0 && inputPostappendedDataBuffer == nullptr)) { throw SOMException("Data size > 0 but buffer is nullptr\n", INVALID_FUNCTION_INPUT, __FILE__, __LINE__); } bool messageReceived = false; bool messageDeserialized = false; std::unique_ptr<zmq::message_t> messageBuffer; SOM_TRY messageBuffer.reset(new zmq::message_t); SOM_CATCH("Error initializing ZMQ message") SOM_TRY messageReceived = inputSocketToReceiveFrom.recv(messageBuffer.get(), inputFlags); SOM_CATCH("Error, unable to receive message\n") if(messageReceived == false) { //Didn't get a message return std::tuple<bool, bool>(messageReceived, messageDeserialized); } //Attempt to retrieve any preappended data if(messageBuffer->size() < inputPreappendedDataSize) { //Message isn't valid (smaller than expected preappended data) return std::tuple<bool, bool>(messageReceived, messageDeserialized); } else if(inputPreappendedDataSize > 0) { //Preappended data is expected and the message is large enough to get it memcpy((void *) inputPreappendedDataBuffer, messageBuffer->data(), inputPreappendedDataSize); } //Deserialize message inputMessageBuffer.ParseFromArray(((char *) messageBuffer->data())+inputPreappendedDataSize, messageBuffer->size()-inputPreappendedDataSize); if(!inputMessageBuffer.IsInitialized()) { return std::tuple<bool, bool>(messageReceived, messageDeserialized); } if(inputPostappendedDataSize > 0) { int protobufMessageSize = inputMessageBuffer.ByteSize(); if((protobufMessageSize+inputPreappendedDataSize+inputPostappendedDataSize) > messageBuffer->size()) { return std::tuple<bool, bool>(messageReceived, messageDeserialized); } else { memcpy((void *) inputPostappendedDataBuffer, (void *) (((char *) messageBuffer->data()) + protobufMessageSize + inputPreappendedDataSize), inputPostappendedDataSize); messageDeserialized = true; } } else { messageDeserialized = true; } return std::tuple<bool, bool>(messageReceived, messageDeserialized); }