Google Protobuf——实现跨平台跨语言的序列化/反序列化

Google Protobuf——实现跨平台跨语言的序列化/反序列化

0 Overview

Google Protocol Buffer 是一个平台无关、语言无关的结构化数据的序列化与反序列化工具。

1 Establish dev environment

wget http://protobuf.googlecode.com/files/protobuf-2.4.1.tar.gz
tar zxvf protobuf-2.4.1.tar.gz
cd protobuf-2.4.1
mkdir /Users/michael/Development/opt/protobuf-2.4.1
./configure --prefix=/Users/michael/Development/opt/protobuf-2.4.1
make
make check
make install

2 Protocol file

创建一个名为 lm.helloworld.proto 的文件。

package lm;
message helloworld
{
    required int32  id = 1;     // ID
    required string str = 2;    // str
    optional int32  opt = 3;    // optional field
}

一般 Google Protobuf 的 protocol 文件名形如:

packageName.MessageName.proto

3 Generate protocol classes

此时的目录结构:

protobuf-2.4.1
|---bin
|---include
|---lib
|---test
    |---lm.helloworld.proto

编译生成 Google Protobuf 类文件:

alias protoc='/Users/michael/Development/opt/protobuf-2.4.1/bin/protoc'
SRC_DIR=.
DST_DIR=.
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/lm.helloworld.proto

此时的目录结构为:

protobuf-2.4.1
|---bin
|---include
|---lib
|---test
    |---lm.helloworld.proto
    |---lm.helloworld.pb.cc
    |---lm.helloworld.pb.h

4 Application files

此处以 File Stream 为例。

4.1 Structured data -> Stream

#include "lm.helloworld.pb.h"
#include <iostream>
#include <fstream>

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))
    {
        cerr << "Failed to write msg." << endl;
        return -1;
    }
    return 0;
}

跟已有的结构化数据结构(依据 Google Protobuf 的格式)创建数据,将结构化数据序列化到流中。

4.2 Stream -> Structured data

#include "lm.helloworld.pb.h"                                                                                                                                                                             
#include <iostream>
#include <fstream>

using namespace std;

void ListMsg(const lm::helloworld & msg)
{
    cout << msg.id() << endl; 
    cout << msg.str() << endl; 
} 

int main(int argc, char* argv[])
{ 
    lm::helloworld msg1;
    {   
        fstream input("./log", ios::in | ios::binary);
        if (!msg1.ParseFromIstream(&input))
        {   
            cerr << "Failed to parse address book." << endl;
            return -1; 
        }   
    }   
    ListMsg(msg1);
}

将流中的序列化数据,读取到依据 Google Protobuf 的格式创建的对象中。

5 Compile executable files

5.1 Directories and file tree

protobuf-2.4.1
|---bin
|---include
|---lib
|---test
    |---lm.helloworld.proto
    |---lm.helloworld.pb.cc
    |---lm.helloworld.ph.h
    |---write.cpp
    |---read.cpp

5.2 Compile

g++ lm.helloworld.pb.cc write.cpp -o write.out -I ../include -L../lib -lprotobuf

Notice:

  • 编译应用程序源文件时,要记得同时编译 lm.helloworld.pb.cc 源文件;
  • 记得 Include Google Protobuf headers(-I ../include)
  • 记得 Link 路径以及相应的 google protobuf libraries(-L../lib -lprotobuf)

6 Run

运行 write:

./write

会观察到生成如下文件(参见源程序):

log

运行 read:

./read

输出结果:

$ ./read.out 
101
hello

7 Review

  1. 环境:搭建 Google Protobuf 的开发环境;
  2. 协议:根据 Google Protobuf 格式要求,创建 Protocol 文件;
  3. 生成:利用 protoc 编译生成所定义的 Protocol 的类源文件与头文件;
  4. 应用:编写利用所生成的 Protocol 的类源文件与头文件;
  5. 编译:编译所编写的应用的源文件与头文件,注意头文件路径、库文件路径及库;
  6. 运行。

Reference

  1. http://stackoverflow.com/questions/8875867/linking-errors-when-using-proto-tutorial
  2. http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/
  3. http://code.google.com/intl/zh-CN/apis/protocolbuffers/docs/overview.html

-

Happy Coding, enjoy sharing!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值