1、在.proto文件中定義消息格式
2、使用protobuf編譯器
3、使用c++ api來讀寫消息
0、為何使用protobuf?
1、原始內存數據結構,可以以二進制方式sent/saved.這種方式需要相同的內存布局和字節序。
2、以ad-hoc方式將數據項編碼成一個簡單字符串----比如,將4個int類型編碼成"12:3:-23:67"。這種方式簡靈活。適用於簡單數據。
3、將數據序列化為XML。這種方式很流行,因為xml可讀性好,編碼解碼方便,性能也好。僅僅XML dom樹比較復雜。
protobuf可以很好的解決上述問題。你編寫一個.proto文件來描述數據結構。protobuf編譯器使用它創建一個類,使用二進制方式自動編碼/解碼該數據結構。生成的類提供getter/setter方法。
最重要的是,protobuf支持在此基礎上進行格式擴展。
1、定義協議格式
package tutorial; message Person {
required string name = 1;
required int32 id = 2;
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;
}
message AddressBook {
repeated Personperson = 1;
}
該結構與c++或java很像.
.proto文件以包聲明開始,防止名字沖突。
簡單類型:bool, int32, float, double, string.
其它類型:如上述的Person, PhoneNumber
類型可以嵌套。
“=1”, “=2”標識唯一“tag”.tag數1-15需要至少一個字節。
required: 必須設置它的值
optional: 可以設置,也可以不設置它的值
repeated: 可以認為是動態分配的數組
google工程師認為使用required威害更大,他們更喜歡使用optional, repeated.
2、編譯你的協議
運行protoc 來生成c++文件:
protoc -I=$SRC_DIR --cpp_out=$DST_DIR$SRC_DIR/addressbook.proto
生成的文件為:
addressbook.pb.h,
addressbook.pb.cc
3、protobuf API
生成的文件中有如下方法:
// name
inline bool has_name() const;
inline voidclear_name();
inline const ::std::string& name() const;
inline void set_name(const ::std::string& value);
inline void set_name(const char* value);
inline ::std::string*mutable_name();
// id
inline bool has_id() const;
inline void clear_id();
inline int32_t id() const;
inline void set_id(int32_t value);
inline bool has_email() const;
inline voidclear_email();
inline const ::std::string& email() const;
inline void set_email(const ::std::string& value);
inline void set_email(const char* value);
inline ::std::string* mutable_email();
// phone
inline intphone_size() const;
inline voidclear_phone();
inline const ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >& phone() const;
inline ::google::protobuf::RepeatedPtrField< ::tutorial::Person_PhoneNumber >*mutable_phone();
inline const ::tutorial::Person_PhoneNumber& phone(int index) const;
inline ::tutorial::Person_PhoneNumber*mutable_phone(int index);
inline ::tutorial::Person_PhoneNumber* add_phone();
4、枚舉與嵌套類
生成的代碼包含一個PhoneType枚舉。Person::PhoneType, Person:MOBILE,Person::HOME, Person:WORK.
編譯器生成的嵌套類稱為Person::PhoneNumber. 實際生成類為Person_PhoneNumber.
5、標准方法
bool IsInitialized() const: 確認required字段是否被設置
string DebugString() const: 返回消息的可讀表示,用於調試
void CopyFrom(const Person& from): 使用給定消息值copy
void Clear(): 清除所有元素為空狀態
6、解析與序列化
bool SerializeToString(string* output) const: 序列化消息,將存儲字節的以string方式輸出。注意字節是二進制,而非文本;
bool ParseFromString(const string& data): 解析給定的string
bool SerializeToOstream(ostream* output) const: 寫消息給定的c++ ostream中
bool ParseFromIstream(istream* input): 從給定的c++ istream中解析出消息
7、protobuf和 oo設計
不要繼承生成類並在此基礎上添加相應的行為
8、寫消息
示例:它從一個文件中讀取AddressBook,基於io添加一個新的Person,並將新的AddressBook寫回文件。
#include <iostream>
#include <fstream>
#include <string>
#include"addressbook.pb.h"
using namespace std;
// Thisfunction fills in a Person message based on user input.
void PromptForAddress(tutorial::Person* person) {
cout << "Enter person ID number: ";
int id;
cin>> id;
person->set_id(id);
cin.ignore(256, '\n');
cout << "Enter name: ";
getline(cin, *person->mutable_name());
cout << "Enter email address (blank for none):";
string email;
getline(cin, email);
if (!email.empty()) {
person->set_email(email);
}
while (true) {
cout << "Enter a phone number (or leave blank tofinish): ";
string number;
getline(cin, number);
if (number.empty()) {
break;
}
tutorial::Person::PhoneNumber* phone_number = person->add_phone();
phone_number->set_number(number);
cout << "Is this a mobile, home, or work phone?";
string type;
getline(cin, type);
if (type == "mobile") {
phone_number->set_type(tutorial::Person::MOBILE);
} else if (type == "home") {
phone_number->set_type(tutorial::Person::HOME);
} else if (type == "work") {
phone_number->set_type(tutorial::Person::WORK);
} else {
cout << "Unknownphone type. Using default." << endl;
}
}
}
// Mainfunction: Reads the entire address book from a file,
// adds one person based on user input, then writes it back out to the same
// file.
int main(int argc, char* argv[]) {
// Verifythat the version of the library that we linked against is
//compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2) {
cerr << "Usage: " << argv[0] << "ADDRESS_BOOK_FILE" << endl;
return -1;
}
tutorial::AddressBook address_book;
{
// Read the existing address book.
fstream input(argv[1], ios::in | ios::binary);
if (!input) {
cout << argv[1] << ":File not found. Creating a new file." << endl;
} else if (!address_book.ParseFromIstream(&input)) {
cerr << "Failedto parse address book." << endl;
return -1;
}
}
// Add anaddress.
PromptForAddress(address_book.add_person());
{
// Write the new address book back to disk.
fstream output(argv[1], ios::out | ios::trunc | ios::binary);
if (!address_book.SerializeToOstream(&output)) {
cerr << "Failedto write address book." << endl;
return -1;
}
}
//Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
注意使用GOOGLE_PROTOBUF_VERIFY_VERSION宏。每一個.pb.cc文件在啟動時都將自動調用該宏。
注意在程序結尾處調用ShutdownProtobufLibrary()。
9、讀消息
#include <iostream>
#include <fstream>
#include <string>
#include"addressbook.pb.h"
using namespace std;
//Iterates though all people in the AddressBook and prints info about them.
void ListPeople(const tutorial::AddressBook& address_book) {
for (int i = 0; i < address_book.person_size(); i++) {
const tutorial::Person& person = address_book.person(i);
cout << "Person ID: " << person.id() << endl;
cout << " Name: " << person.name() << endl;
if (person.has_email()) {
cout << " E-mail address: " << person.email() << endl;
}
for (int j = 0; j < person.phone_size(); j++) {
const tutorial::Person::PhoneNumber& phone_number = person.phone(j);
switch (phone_number.type()) {
case tutorial::Person::MOBILE:
cout << " Mobile phone #: ";
break;
case tutorial::Person::HOME:
cout << " Home phone #: ";
break;
case tutorial::Person::WORK:
cout << " Work phone #: ";
break;
}
cout << phone_number.number() << endl;
}
}
}
// Mainfunction: Reads the entire address book from a file and prints all
// the information inside.
int main(int argc, char* argv[]) {
// Verifythat the version of the library that we linked against is
//compatible with the version of the headers we compiled against.
GOOGLE_PROTOBUF_VERIFY_VERSION;
if (argc != 2) {
cerr << "Usage: " << argv[0] << "ADDRESS_BOOK_FILE" << endl;
return -1;
}
tutorial::AddressBook address_book;
{
// Read the existing address book.
fstream input(argv[1], ios::in | ios::binary);
if (!address_book.ParseFromIstream(&input)) {
cerr << "Failedto parse address book." << endl;
return -1;
}
}
ListPeople(address_book);
//Optional: Delete all global objects allocated by libprotobuf.
google::protobuf::ShutdownProtobufLibrary();
return 0;
}
10、擴展protobuf
如果希望向后兼容,必須遵循:
a、不必更改tag數
b、不必添加或刪除任何required字段
c、可以刪除optional或repeated字段
d、可以添加新的optional或repeated字段,但你必須使用新的tag數。
11、優化
c++的protobuf庫,已經極大地優化了。合理使用可以改善性能。
a、如果可能,復用message對象。
b、關於多線程的內存分配器
12、高級用法
protobuf的消息類的一個關鍵特性是,反射(reflection)。可以使用xml或json來實現。參考。
================================================================
常見問題:
1、undefined reference to`pthread_once'
使用-lpthread:
2、error while loading shared libraries:libprotobuf.so.7: cannot open shared object file: No such file or directory
使用-Wl,-Bstatic -lprotobuf -Wl,-Bdynamic-lpthread