ProtoBuf



       Dongdong Deng <LibFetion@gmail.com>
OverView

� 1、简介
� 2、示例
� 3、.proto语法
1、ProtoBuf
  ProtoBuf
Google开源的,用于序列结构化数据的协议

http://code.google.com/apis/protocolbu
  ffers
� 跨编程语言
� 跨系统平台
� 可扩展
1、ProtoBuf vs XML
  ProtoBuf
� 简单
� 比XML小3-10倍
� 比XML快20-100倍
� 避免歧义
� 自动产生数据操作类,让开发者更容易使
  用
1、ProtoBuf 缺点
  ProtoBuf
� 目前使用不够广泛
ProtoBuf目前没有广泛使用,如果系统需要提供若干对外
   的接口给第三方系统调用,XML目前是更好的选择


� 二进制格式导致可读性差
二进制格式进行编码,通信协议出现问题不好跟踪

� 缺乏自描述
如果不配合相应的proto文件,ProtoBuf基本是看不懂,
在配置文件方面,XML还是很有优势的
2、ProtoBuf 示例
  ProtoBuf
1: Write .proto 文件

2:protoc --cpp_out --java_out --python_out

3: *.pb.h *.pb.cc

4: using above *.pb.h in your program
2、.proto
  .proto

message Test
{
  required int32 id = 1;
  required string pwd = 2;
  optional string others = 3;
}
2、protoc
  protoc


protoc --cpp_out=./test test.proto

$ls ./test
test.pb.h test.pb.cc
2、progam-产生方
  progam-
产生方:

Test test;
test.set_id(344300);
test.set_pwd(123);
test.set_others("othersothers");

string sTest;
test.SerailzeToString(&sTest);
2、progam-读取方
  progam-
读取方:
string sTest; //得到协议数据,存放到sTest
Test test;
if(test.ParseFromString(sTest))
{ cout << "ID:" << test.id() << endl
       << "PWD:" << test.pwd() << endl
       << "others:" << test.others() << endl;
} else {
  cerr << "parse error!" << endl;
}
3、proto 语法
  proto
package tutorial;

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;
} Make Presentation much more fun

// Our address book file is just one of these.                     @WPS官方微博
                                                                   @WPS官方微博
message AddressBook {                                              @kingsoftwps
  repeated Person person = 1;
}
Protobuf

More Related Content

PPTX
Google雲端工作術 demo版
DOC
Dostoevskiy bratiy karamazovi
DOCX
England in 50
PDF
Muradyan - HR Congress - RU
PPS
Crown Long Ppt
DOC
индия и европейцы
DOCX
Особенности и развитие Латинской Америки
PPTX
What, beyond the curriculum, does your school teach? Administrators Conferen...
Google雲端工作術 demo版
Dostoevskiy bratiy karamazovi
England in 50
Muradyan - HR Congress - RU
Crown Long Ppt
индия и европейцы
Особенности и развитие Латинской Америки
What, beyond the curriculum, does your school teach? Administrators Conferen...

Viewers also liked (16)

DOC
Krylov
PPTX
Communicating student learning, november 2015
DOC
Rileev
PDF
Cloudian meets CloudStack
PDF
クラウドストレージとは? (Cloudian Summit 2012)
DOC
PDF
Apresentacao sessoes mz
PPTX
Four Tips to improve & analyse your Sales Performance Scorecard Effectively
PPT
8 Things You Need To Know About the Shanghai World Expo, 2010
DOCX
Prodovolstvie
DOC
Solgenicin
PDF
How to become cloud backup provider
PDF
Eleven tips to build your succesfull body language
DOCX
Iskusstvo feod
DOC
Nabokov
DOC
Dostoevskiy unizennie
Krylov
Communicating student learning, november 2015
Rileev
Cloudian meets CloudStack
クラウドストレージとは? (Cloudian Summit 2012)
Apresentacao sessoes mz
Four Tips to improve & analyse your Sales Performance Scorecard Effectively
8 Things You Need To Know About the Shanghai World Expo, 2010
Prodovolstvie
Solgenicin
How to become cloud backup provider
Eleven tips to build your succesfull body language
Iskusstvo feod
Nabokov
Dostoevskiy unizennie
Ad

Protobuf