protobuf的基本使用示例
1、编写msgtype文件
test.proto
syntax = "proto3";
package msgType;
enum EnumTest
{
TEST0 = 0x00;
TEST1 = 0x01;
TEST2 = 0x02;
TEST3 = 0x03;
}
message ProtoTestSub
{
int32 test1 = 1;
string test2 = 2;
}
message ProtoTest
{
int32 int32_test = 1;
string str_test = 2;
repeated double dou_test = 3;
repeated ProtoTestSub sub_test = 4;
EnumTest eunm_test = 5;
bytes bytes_test = 6;
}
2、生成对应的头文件以及源文件
root@root:/root/protobuf# protoc --cpp_out=./ ./test.proto.proto
root@root:/root/protobuf# ls
a.out include mybuild protobuf_main.cpp test.pb.cc test.pb.h test.proto
生成对应的*.pb.cc *.pb.h 文件 只需将这两个文件加到自己的工程目录中去
3、示例程序
protobuf_main.cpp
#include <iostream>
#include <vector>
#include <algorithm>
#include "test.pb.h"
void MyPrint(unsigned char temp)
{
printf("%02x ",(temp)&0xFF);
}
int main(void )
{
//1、序列化 protobuf的基本使用
msgType:: ProtoTest cls_proto_test1;
cls_proto_test1.set_int32_test(123);
cls_proto_test1.set_str_test("FlyInCoding");
//多个 repeated使用
cls_proto_test1.add_dou_test(56.023);
cls_proto_test1.add_dou_test(78.023);
//嵌套类的使用
msgType:: ProtoTestSub * pcls_temp = cls_proto_test1.add_sub_test();
pcls_temp->set_test1(12);
pcls_temp->set_test2("zxcvbnm");
pcls_temp = cls_proto_test1.add_sub_test();
pcls_temp->set_test1(34);
pcls_temp->set_test2("asdfghjkl");
cls_proto_test1.set_eunm_test(msgType::TEST0);
//protobuf调试打印函数 打印成字符串
cls_proto_test1.PrintDebugString();
//序列化之后的大小
int ilen = cls_proto_test1.ByteSize();
//序列化成数组
std::vector <unsigned char> vec_test1;
vec_test1.resize(ilen);
cls_proto_test1.SerializePartialToArray(vec_test1.data(),ilen);
printf("cls_proto_test1 len is %d\n",ilen);
//容器类遍历的通用办法
printf("**************************************************\n");
std::for_each(vec_test1.begin(),vec_test1.end(),MyPrint);
printf("**************************************************\n");
//序列化成字符串
std::string str_temp;
cls_proto_test1.SerializeToString(&str_temp);
printf("**************************************************\n");
std::for_each(str_temp.begin(),str_temp.end(),MyPrint);
printf("**************************************************\n");
/*********************************************************
protobuf有个问题 如果里面数据都为整型 且都为0时 序列化之后的数据大小也为0
整型为0的数据 不计入整个序列化的数据长度
doubule float 空的字符串则没有此规律
**********************************************************/
msgType:: ProtoTest cls_proto_test2;
cls_proto_test2.set_int32_test(0);
//cls_proto_test2.set_str_test("");
//cls_proto_test2.add_dou_test(0);
//cls_proto_test2.add_dou_test(0);
//msgType:: ProtoTestSub * pcls_temp2 = cls_proto_test2.add_sub_test();
//pcls_temp2->set_test1(0);
//pcls_temp2->set_test2("");
//pcls_temp2 = cls_proto_test2.add_sub_test();
// pcls_temp2->set_test1(0);
//pcls_temp2->set_test2("");
//cls_proto_test2.set_eunm_test(msgType::TEST0);
//protobuf调试打印函数 打印成字符串
cls_proto_test2.PrintDebugString();
int ilen2 = cls_proto_test2.ByteSize();
printf("cls_proto_test1 ilen2 is %d\n",ilen2);
// 反序列化 从数组进行反序列化
msgType::ProtoTest cls_proto_test3;
cls_proto_test3.ParseFromArray(vec_test1.data(),vec_test1.size());
cls_proto_test3.PrintDebugString();
printf("int32_test %d \n",cls_proto_test3.int32_test());
printf("eunm_test %d \n",cls_proto_test3.eunm_test());
printf("double count %d \n",cls_proto_test3.dou_test_size());
printf("double0 %f \n",cls_proto_test3.dou_test(0));
printf("double1 %f \n",cls_proto_test3.dou_test(1));
msgType:: ProtoTestSub cls_temp ;
for(int i = 0;i < cls_proto_test3.sub_test_size();i++)
{
cls_temp = cls_proto_test3.sub_test(i);
cls_temp.PrintDebugString();
}
return 0;
}
4、编译及运行
root@root:/root/protobuf# g++ protobuf_main.cpp test.pb.cc -lprotobuf
root@root:/root/protobuf# ./a.out
int32_test: 123
str_test: "FlyInCoding"
dou_test: 56.023
dou_test: 78.023
sub_test {
test1: 12
test2: "zxcvbnm"
}
sub_test {
test1: 34
test2: "asdfghjkl"
}
cls_proto_test1 len is 61
**************************************************
08 7b 12 0b 77 61 6e 78 75 65 78 69 61 6e 67 1a 10 6d e7 fb a9 f1 02 4c 40 b6 f3 fd d4 78 81 53 40 22 0b 08 0c 12 07 7a 78 63 76 62 6e 6d 22 0d 08 22 12 09 61 73 64 66 67 68 6a 6b 6c **************************************************
**************************************************
08 7b 12 0b 77 61 6e 78 75 65 78 69 61 6e 67 1a 10 6d e7 fb a9 f1 02 4c 40 b6 f3 fd d4 78 81 53 40 22 0b 08 0c 12 07 7a 78 63 76 62 6e 6d 22 0d 08 22 12 09 61 73 64 66 67 68 6a 6b 6c **************************************************
cls_proto_test1 ilen2 is 0
int32_test: 123
str_test: "FlyInCoding"
dou_test: 56.023
dou_test: 78.023
sub_test {
test1: 12
test2: "zxcvbnm"
}
sub_test {
test1: 34
test2: "asdfghjkl"
}
int32_test 123
eunm_test 0
double count 2
double0 56.023000
double1 78.023000
test1: 12
test2: "zxcvbnm"
test1: 34
test2: "asdfghjkl"
root@root:/root/protobuf#