Google Protocol Buffer(1) - hellowrold

Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 个 .proto 文件。他们用于 RPC 系统和持续数据存储系统。

Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,或者说序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。

如何简单的构建一个 pb 的例子呢?
安装:
wget http://protobuf.googlecode.com/files/protobuf-2.5.0.tar.gz
tar -zxvf protobuf-2.5.0.tar.gz
./configure
make && make install
ldconfig
可能要编译一会,耐心等待

创建.proto文件
首先我们需要编写一个 proto 文件,定义我们程序中需要处理的结构化数据,在 protobuf 的术语中,结构化数据被称为 Message。proto 文件非常类似 java 或者 C 语言的数据定义。
我们创建的message.proto文件:
package lm;
message helloworld
{
required int32 id = 1; // ID
required string str = 2; // str
optional int32 opt = 3; //optional field
}
一个比较好的习惯是认真对待 proto 文件的文件名。比如将命名规则定于如:packageName.MessageName.proto
在上例中,package 名字叫做 lm,定义了一个消息 helloworld,该消息有三个成员,类型为 int32 的 id,另一个为类型为 string 的成员 str。opt 是一个可选的成员,即消息中可以不包含该成员。

编译.proto文件
安装成功后,可以看到多了一个命令 protoc。
protoc lm.helloworld.proto –cpp_out=./
会在当前目录生成2个文件如下:
[root@performance pbhello]# ls
lm.helloworld.pb.cc lm.helloworld.pb.h lm.helloworld.proto
lm.helloworld.pb.h , 定义了 C++ 类的头文件
lm.helloworld.pb.cc , C++ 类的实现文件

打开lm.helloworld.pb.发现里面申明了好多helloworld类成员,同时命名空间就是我们之前定义的 package lm。

hello读取和解析代码

#include “./lm.helloworld.pb.h”

#include

int main(void)
{
//——–创建一个简单的pb string
lm::helloworld msg1;
msg1.set_id(101);
msg1.set_str(“hello”);
std::string str;
msg1.SerializeToString(&str);
std::cout<<str<<std::endl;
//——-接受pb string
lm::helloworld msg2;
msg2.ParseFromString(str); // 从字符串反序列化
std::cout<<msg2.id()<<std::endl;
std::cout<<msg2.str()<<std::endl;
return 0;
}

保存为hello.cpp
运行编译命令
g++ -g -o hello hello.cpp lm.helloworld.pb.cc -I. -I /usr/local/protobuf-2.5.0/include -L /usr/local/protobuf-2.5.0/lib -lprotobuf -pthread
成功以后会有hello.o在当前目录,运行就看看到输出结果了:
[root@performance pbhello]# g++ -g -o hello hello.cpp lm.helloworld.pb.cc -I. -I /usr/local/protobuf-2.5.0/include -L /usr/local/protobuf-2.5.0/lib -lprotobuf -pthread
[root@performance pbhello]# ls
hello hello.cpp lm.helloworld.pb.cc lm.helloworld.pb.h lm.helloworld.proto
[root@performance pbhello]# ./hello
ehello
101
hello
[root@performance pbhello]#
输出正是我们之前所输入的内容。

更复杂的结构
我们继续看下更加复杂的消息格式,
message Person {
required string name = 1;
required int32 id = 2; // Unique ID number for this person.
optional string email = 3;

enum PhoneType {
MOBILE = 0;
HOME = 1;
WORK = 2;
}

message PhoneNumber {
required string number = 1;
optional PhoneType type = 2 [default = HOME];
}
repeated PhoneNumber phone = 4;
}
上面的东西很容易理解,不多解释了,我们还是可以使用相同的方法进行编码和解码