本文整理汇总了C++中DataBuf::data方法的典型用法代码示例。如果您正苦于以下问题:C++ DataBuf::data方法的具体用法?C++ DataBuf::data怎么用?C++ DataBuf::data使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类DataBuf
的用法示例。
在下文中一共展示了DataBuf::data方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: encode
bool encode(DataBuf& buf, const OutBoundRpcMessage& msg) {
using exec::rpc::RpcHeader;
using google::protobuf::io::CodedOutputStream;
// Todo:
//
// - let a context manager to allocate a buffer `ByteBuf buf = ctx.alloc().buffer();`
// - builder pattern
//
#ifdef CODER_DEBUG
std::cerr << "Encoding outbound message " << msg << std::endl;
#endif
RpcHeader header;
header.set_mode(msg.m_mode);
header.set_coordination_id(msg.m_coord_id);
header.set_rpc_type(msg.m_rpc_type);
// calcute the length of the message
int header_length = header.ByteSize();
int proto_body_length = msg.m_pbody->ByteSize();
int full_length = HEADER_TAG_LENGTH + CodedOutputStream::VarintSize32(header_length) + header_length + \
PROTOBUF_BODY_TAG_LENGTH + CodedOutputStream::VarintSize32(proto_body_length) + proto_body_length;
/*
if(raw_body_length > 0) {
full_length += (RAW_BODY_TAG_LENGTH + getRawVarintSize(raw_body_length) + raw_body_length);
}
*/
buf.resize(full_length + CodedOutputStream::VarintSize32(full_length));
uint8_t* data = buf.data();
#ifdef CODER_DEBUG
std::cerr << "Writing full length " << full_length << std::endl;
#endif
data = CodedOutputStream::WriteVarint32ToArray(full_length, data);
#ifdef CODER_DEBUG
std::cerr << "Writing header length " << header_length << std::endl;
#endif
data = CodedOutputStream::WriteVarint32ToArray(HEADER_TAG, data);
data = CodedOutputStream::WriteVarint32ToArray(header_length, data);
data = header.SerializeWithCachedSizesToArray(data);
// write protobuf body length and body
#ifdef CODER_DEBUG
std::cerr << "Writing protobuf body length " << proto_body_length << std::endl;
#endif
data = CodedOutputStream::WriteVarint32ToArray(PROTOBUF_BODY_TAG, data);
data = CodedOutputStream::WriteVarint32ToArray(proto_body_length, data);
msg.m_pbody->SerializeWithCachedSizesToArray(data);
// Done! no read to write data body for client
return true;
}