【C++】Google Protocol Buffer(protobuf)详解(一)

1、简介

Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,
Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。目前提供了 C++、Java、Python 三种语言的 API。

源码下载:

github地址 : https://github.com/protocolbuffers/protobuf 

编译:

tar zxvf protobuf*.tar.gz
cd protobuf*/ 
./configure –prefix=/usr/local/protobuf
make 
make check 
make install 
protoc –version

参考博客:
https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html
https://www.cnblogs.com/tohxyblog/p/8974763.html
https://blog.csdn.net/u014308482/article/details/52958148

2、ProtoBuf协议说明

proto文件定义了协议数据中的实体结构(message ,field)

关键字message: 代表了实体结构,由多个消息字段(field)组成。
消息字段(field): 包括数据类型、字段名、字段规则、字段唯一标识、默认值
字段规则:

required:必须初始化字段,如果没有赋值,在数据序列化时会抛出异常
optional:可选字段,可以不必初始化。
repeated:数据可以重复(相当于java 中的Array或List)

字段唯一标识:序列化和反序列化将会使用到。
数据类型:
在这里插入图片描述

3、ProtoBuf的使用流程
1> 定义.proto文件

网上都有如下的例子说明,皆出自IBM的学习社区:https://www.ibm.com/developerworks/cn/linux/l-cn-gpb/index.html
proto文件:first.proto

package lm; 
message helloworld 
{ 
   required int32     id = 1;  // ID 
   required string    str = 2;  // str 
   optional int32     opt = 3;  //optional field 
}
2> 生成*.pb.cc 和 *.pb.h

protoc命令格式:protoc -I=<proto文件所在路径> --cpp_out=<输出路径> proto文件名
如:protoc -I=. --cpp_out=. first.proto
将会生成:first.pb.cc first.pb.h

3> 序列化操作

将helloworld结构 序列化到文件log中:firstWrite.cpp

#include "first.pb.h"
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
int main(void) 
{ 
	lm::helloworld msg1; 
	msg1.set_id(101); 
	msg1.set_str("hello");

	fstream output("./log", ios::out | ios::trunc | ios::binary);
	if (!msg1.SerializeToOstream(&output)) {
		cout << "Failed to write msg." << endl;
		return -1;
	}
	return 0;
}

编译时需要链接protobuf库,并且使用first.pb.cc一起编译:
g++ firstWrite.cpp first.pb.cc -lprotobuf

4> 反序列化操作

从文件中,读取到helloworld结构中: firstRead.cpp

#include "first.pb.h"
#include <string>
#include <fstream>
#include <iostream>

using namespace std;

int main(void) 
{ 
	lm::helloworld msg; 
	msg.set_id(101); 
	msg.set_str("hello");

	fstream input("./log", ios::in | ios::binary);
	if (!msg.ParseFromIstream(&input)) {
		cout << "Failed to parse address book." << endl;
		return -1;
	}

	cout << msg.id() << endl;
	cout << msg.str() << endl;

	return 0;
}

编译时需要链接protobuf库,并且使用first.pb.cc一起编译:
g++ firstRead.cpp first.pb.cc -lprotobuf

5> cmake编译CMakeLists.txt(注意修改cmake版本)
cmake_minimum_required(VERSION 3.14.1)
project (protobuf_test)

set (PROJECT_LINK_LIBS libprotobuf.so) 

add_executable(firstWrite firstWrite.cpp first.pb.cc)
add_executable(firstRead firstRead.cpp first.pb.cc)

target_link_libraries(firstWrite ${PROJECT_LINK_LIBS} )
target_link_libraries(firstRead ${PROJECT_LINK_LIBS} )
  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

郭老二

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值