美文网首页
protobuf 3的安装与使用

protobuf 3的安装与使用

作者: DDDDAIPING | 来源:发表于2020-04-09 18:05 被阅读0次
安装

从官网提供的下载地址下载你想要使用的版本,此处我下载了protobuf-all-3.11.4.tar.gz

1. ./configure
2.  make    //可以省略
3.  make check   //可以省略
4.  sudo make install
5.  sudo ldconfig # refresh shared library cache.
常规字段说明
字段描述符:
package         包名      //package tutorial;
message         定义消息
required        字段必须
optional        字段可选
enum            枚举
repeated        字段可重复(会被定义成类似数组或list一类的结构)
syntax          指定.proto的风格  //syntax = "proto3"
service         rpc服务,不在此次学习范围内

数据类型:
bool
int32
uint32
uint64
sint32      使用变长编码,这些编码在负值时比int32高效的多
sint64      使用变长编码,有符号的整型值。编码时比通常的int64高效。
float
double
string
map<key_type, value_type> 
关于枚举:
enum EnumAllowingAlias {
    option allow_alias = true;      //允许枚举值重复
    UNKNOWN = 0;
    STARTED = 1;
    RUNNING = 1;
}
常用的API接口 序列化/返序列化
bool ParseFromString(const string& data):
          Parse the message from the given serialized binary string (also known as wire format).
bool SerializeToString(string* output) const: 
          Serialize the given message to a binary string.
string DebugString(): 
          Return a string giving the `text_format` representation of the proto (should only be used for debugging).
proto文件的定义
  1 syntax = "proto3";  //IMesage.proto
  2 package IMessage;
  3 
  4 enum MessageType {
  5    SUCCESS = 0;
  6    HEARTBEAT = 0x01;
  7    CHATMSG = 0x02;
  8    UNKNOWN = 0xFA;
  9 }
 10 
 11 message Header
 12 {
 13     MessageType Type = 1;
 14     int32 BodyLen = 2;
 15     int32 CrcNum = 3;
 16 }
 17 
 18 message UserInfo
 19 {
 20     uint64 sUserID = 1;
 21     uint64 dUserID = 2;
 22     string sUserName = 3;
 23     string dUserName = 4;
 24 }
 25 
 26 message Message
 27 {
 28     Header Head = 1;
 29     UserInfo User = 2;
 30     uint32 TimeOut = 3;
 31     string MessageBody = 4;
 32 }
生成代码
protoc --proto_path=protopath --cpp_out=cppout protopath/IMessage.proto
代码的使用
  1 #include<stdio.h>
  2 #include<unistd.h>
  3 #include<iostream>
  4 #include<string>
  5 #include "IMessage.pb.h"
  6 using namespace std;
  7 using namespace IMessage;
  8 
  9 int main(int argc, char** argv)
 10 {
 11    Message testMsg;
 12    testMsg.set_messagebody("Hello Protobuf!");
 13    //在c++中使用嵌套的消息,需要用mutable_xxx或是set_allocated_xxx这种方式
 14    //其它语言,类似python则可以直接访问成员对象
 15    UserInfo* uinfo = testMsg.mutable_user();
 16    Header* head = testMsg.mutable_head();
 17    Header* head2 = testMsg.mutable_head();
 18    printf("%p<====>%p\n", head, head2);
 19    uinfo->set_dusername("dst user name");
 20    head->set_bodylen(0);
 21    head->set_type(MessageType::HEARTBEAT);
 22    head->set_crcnum(-1);
 23    
 24    std::string out;
 25    //序列化
 26    if(!testMsg.SerializeToString(&out)){
 27       printf("Message SerializeToString Failed, Please Check\n");
 28       return -1;
 29    }  
 30    Message testMsg2;
 31    //反序列化
 32    if(!testMsg2.ParseFromString(out)){
 33       printf("Message ParseFromString Failed, Please Check\n");
 34       return -2;
 35    }  
 36    printf("[%s]\n", out.c_str());
 37    return 0;
 38 }  

编译测试程序 -lprotobuf
编译命令 : g++ -std=c++11 test.cpp IMessage.pb.cc  -lprotobuf
注意点
在c++中使用嵌套的消息,需要用mutable_xxx或是set_allocated_xxx这种方式
其它语言,类似golang,python则可以直接访问成员对象

举例:
message A{ string stra = 1;}
message B{ A ra = 1;}
B b;
b.ra.stra = "xxx"; //在c++ 当中,这种方法是不行的

方式1:
A* ptra = b.multable_ra();
a->set_stra("xxx");

方式2:
A* ptra2 = new A();
ptra2->set_stra("xxx");
b.set_allocated_ra(ptra2);

相关文章

网友评论

      本文标题:protobuf 3的安装与使用

      本文链接:https://www.haomeiwen.com/subject/lfxhmhtx.html