Protobuf介绍
Protobuf提供一种灵活、高效、自动序列化结构数据的机制,可以联想XML,但是比XML更小、更快、更简单。仅需要自定义一次所有的数据格式,然后用户就可以使用Protobuf编译器自动生成各种语言的源码,方便的读写用户自定义的格式化的数据。与语言无关,与平台无关,还可以在不破坏原数据格式的基础上,依据老的数据格式,更新现有的数据格式。
Protobuf特点
- 作用与XML、json类似,他是二进制格式,性能好,效率高
- 代码生成机制,易于使用
- 解析速度快
- 支持多种语言
- 向后兼容、向前兼容
- 缺点:可读性差
下载并安装
1)下载
https://github.com/google/protobuf/releases
2)解压
tar -xvf protobuf-3.6.1.tar.gz
3)编译安装
cd protobuf-3.6.1
./autogen.sh
./configure
make
make install即可。
注意:
configure: WARNING: no configuration information is in third_party/googletest
需要下载googletest,下载地址:https://github.com/google/googletest/releases,解压后放在./protobuf-3.6.1/third_party/googletest,然后执行./autogen.sh之后的
4)简单测试
// Helloworld.proto
syntax = "proto3";
package lm;
message helloworld
{
int32 id = 1;
string str = 2;
}
编译执行如下命令:protoc -I=./ --cpp_out=./ helloworld.proto
编译成功后生成.cc和.h文件,对应为helloworld.pb.cc和helloworld.pb.h
// main.cpp
#include <iostream>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <string>
#include <pthread.h>
#include <fstream>
#include "helloworld.pb.h"
using namespace std;
#define BUFFSIZE 1024
int main()
{
GOOGLE_PROTOBUF_VERIFY_VERSION;
// 写文件
lm::helloworld msg1;
msg1.set_id(1);
msg1.set_str("test1");
fstream output("./log", ios::out | ios::trunc | ios::binary);
if (!msg1.SerializeToOstream(&output)) {
cerr << "Failed to write msg." << endl;
return -1;
}
output.close();
cout << msg1.id() << endl;
cout << msg1.str() << endl;
// 读文件
lm::helloworld msg2;
fstream input("./log", ios::in | ios::binary);
if (!input)
{
cerr << "open file failed!\n";
return -1;
}
if (!msg2.ParseFromIstream(&input)) {
cerr << "Parse file failed!" << endl;
return -1;
}
input.close();
cout << msg2.id() << endl;
cout << msg2.str() << endl;
// 写buf, protobuf序列化
lm::helloworld msg3;
msg3.set_id(2);
msg3.set_str("test2");
char buf[BUFFSIZE];
memset(buf, 0, BUFFSIZE);
msg3.SerializeToArray(buf, BUFFSIZE);
//读buf, protobuf反序列化
lm::helloworld msg4;
msg4.ParseFromArray(buf, BUFFSIZE);
cout << msg4.id() << endl;
cout << msg4.str() << endl;
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
编译执行如下命令:g++ helloworld.pb.cc main.cpp -o main -lprotobuf -lpthread -std=c++11
程序执行:./main
执行结果:
1
test1
1
test1
2
test2