本文整理汇总了C++中MessagePtr::ParseFromString方法的典型用法代码示例。如果您正苦于以下问题:C++ MessagePtr::ParseFromString方法的具体用法?C++ MessagePtr::ParseFromString怎么用?C++ MessagePtr::ParseFromString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类MessagePtr
的用法示例。
在下文中一共展示了MessagePtr::ParseFromString方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: parse
MessagePtr protobuf_codec::parse(const char* buf, const uint32_t len, uint32_t& error)
{
MessagePtr message;
do
{
//msg_name_len include the end character '\0'
const uint16_t msg_name_len = *(const uint16_t*)&buf[0]; //TODO: ntoh()
if (msg_name_len > 128 || msg_name_len < MIN_MSG_NAME_SIZE || msg_name_len + MSG_NAME_LENGTH_SIZE > len)
{
LOG(ERROR)<<"message name length error length = "<<msg_name_len<<", total="<<len;
error = 1;
break;
}
char msgname[128] = {'\0'};
strncpy(msgname, (const char*)(buf + MSG_NAME_LENGTH_SIZE), msg_name_len);
const uint16_t n = strlen(msgname) + 1;
if (n != msg_name_len)
{
LOG(ERROR)<<"message name length error (received="<<n<<", expected="<<msg_name_len<<", msgname="<<msgname<<")";
error = 2;
break;
}
LOG(INFO)<<"[protobuf_codec],parse, msg_name_len="<<msg_name_len<<", msgname="<<msgname;
message.reset(createMessage(msgname));
if (!message)
{
LOG(ERROR)<<"unrecognized message name = "<<msgname;
error = 3;
break;
}
if (!message->ParseFromString(std::string(buf + MSG_NAME_LENGTH_SIZE + msg_name_len, len - MSG_NAME_LENGTH_SIZE - msg_name_len)))
{
LOG(ERROR)<<"Parse error msgname="<<msgname;
error = 4;
break;
}
}while(false);
return message;
}