谷歌protobuf(Protocol buffers)的使用
生活随笔
收集整理的這篇文章主要介紹了
谷歌protobuf(Protocol buffers)的使用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
谷歌protobuf的使用
- 一、概述
- 二、安裝
- 三、protobuf中的限定符
- 四、protobuf支持的數據類型
- 五、編譯
- 1. 將proto文件編譯成 C++ 文件
- 2. 將編譯好的文件與代碼一起編譯執行
- 六、應用
- 1. proto 文件的編寫(文件后綴名:proto)
- 2. 基礎應用
- 3. 嵌套應用(即message內嵌套message)
- 4. libevent 和 protobuf 協作
一、概述
二、安裝
# 如果在解壓或安裝時出現問題,執行 apt-get install autoconf automake libtool curl make g++ unzip # 1. 從github上克隆protobuf git clone https://github.com/protocolbuffers/protobuf # 2. 解壓下載好的文件 unzip protobuf-master.zip # 3. 進入 protobuf-master 目錄 cd protobuf-master/ # 4. 執行如下命令 ./autogen.sh ./configure make make check make install ldconfig # 5. 在 shell 下輸入protoc ,如果出現 Usage: protoc [OPTION] PROTO_FILES 則成功。三、protobuf中的限定符
| required | 必填字段 |
| optional | 可選字段 |
| repeated | 可重復字段 |
四、protobuf支持的數據類型
| float | float | 無 |
| double | double | 無 |
| int32 | __int32 | 使用變長編碼,對于負值的效率很低,如果你的域有可能有負值,請使用sint64替代 |
| uint32 | unsigned __int32 | 使用變長編碼 |
| int64 | __int64 | 使用變長編碼 |
| uint64 | unsigned __int64 | 使用變長編碼 |
| sint32 | __int32 | 使用變長編碼,這些編碼在負值時比int32高效的多 |
| sint64 | __int64 | 使用變長編碼,有符號的整型值。編碼時比通常的int64高效。 |
| fixed32 | unsigned __int32 | 總是4個字節,如果數值總是比228大的話,這個類型會比uint32高效。 |
| fixed64 | unsigned __int64 | 總是8個字節,如果數值總是比總是比256大的話,這個類型會比uint64高效。 |
| sfixed32 | __int32 | 總是4個字節 |
| sfixed64 | __int64 | 總是8個字節 |
| bool | bool | |
| string | std::string | 一個字符串必須是UTF-8編碼或者7-bit ASCII編碼的文本。 |
| bytes | std::string | 可能包含任意順序的字節數據。 |
| enum | enum | 枚舉類型。如果要將兩個枚舉變量的值設為相等,那么需要添加如下代碼,否則會報錯:option allow_alias = true; |
五、編譯
1. 將proto文件編譯成 C++ 文件
# SRC_DIR: proto文件(example.proto)所在目錄 # cpp_out(DST_DIR): 制定了生成代碼的路徑 # example.proto: 指proto文件名 protoc -I=$SRC_DIR --cpp_out=$DST_DIR example.proto# 示例: protoc -I=./ --cpp_out=./ example.proto # 說明:在當前目錄中查找 example.proto 并將生成的文件放在當前目錄下2. 將編譯好的文件與代碼一起編譯執行
g++ -std=c++11 example.cc example.pb.cc -lprotobuf六、應用
1. proto 文件的編寫(文件后綴名:proto)
syntax = "proto2";package example;// 使用手機號登錄獲取驗證碼(請求端) message get_code_requset {required string mobile = 1; }// 使用手機號登錄獲取驗證碼(響應端) message get_code_response {required int32 code = 1; // 響應代號required int32 icode = 2; // 驗證碼optional string reason = 3; // 失敗原因 }// 使用手機號登錄(請求端) message login_request {required string mobile = 1; // 手機號碼required int32 icode = 2; // 驗證碼 }// 使用手機號登錄(響應端) message login_response {required int32 code = 1; // 響應代號optional string reason = 2; // 失敗原因 }// 查詢登錄記錄(請求端) message login_record_request {required string mobile = 1; // 手機號碼 }// 查詢登錄記錄(響應端) message login_record_response {required int32 code = 1; // 響應代號optional string reason = 2; // 失敗原因message login_record {required int32 timestamp = 1; // 時間戳required string device_name = 2; // 設備名}repeated login_record records = 3; }// 將以上代碼編譯成 C++ 文件 // protoc -I=./ --cpp_out=./ example.proto2. 基礎應用
#include <string> #include <iostream> #include "example.pb.h"int main(int argc, char *argv[]) {std::string data; // 存儲序列化之后的數據// 客戶端發送請求{example::get_code_requset getCodeRequest;getCodeRequest.set_mobile("19912341234");getCodeRequest.SerializeToString(&data);std::cout << "serial[" << data.length() << "]:" << data << std::endl;}// 服務器端解析數據{example::get_code_requset parse;parse.ParseFromString(data);std::cout << "mobile: " << parse.mobile() << std::endl;}return 0; }/********************************************************************************************** 使用如下語句在shell下編譯:g++ -std=c++11 example.cc example.pb.cc -o example.exe -lprotobuf **********************************************************************************************/3. 嵌套應用(即message內嵌套message)
#include <time.h> #include <string> #include <iostream> #include "example.pb.h"int main(int argc, char *argv[]) {std::string data; // 存儲序列化之后的數據// 客戶端發送請求{example::login_record_response response;response.set_code(200);response.set_reason("OK");// 插入登錄數據time_t now = time(NULL);for (int i = 0; i < 5; ++i) {example::login_record_response_login_record* login = response.add_records();login->set_timestamp(now + i);login->set_device_name(std::string("phone-") + std::to_string(i));}std::cout << "record size: " << response.records_size() << std::endl;// 序列化response.SerializeToString(&data);}// 服務器端解析數據{example::login_record_response response;response.ParseFromString(data);std::cout << "code: " << response.code()<< " reason:" << response.reason()<< " parse size : " << response.records_size() << std::endl;for (int i = 0; i < response.records_size(); ++i) {const example::login_record_response_login_record& login = response.records(i);std::cout << "timestamp: " << login.timestamp()<< " device_name: " << login.device_name() << std::endl;}}return 0; }/********************************************************************************************** 使用如下語句在shell下編譯:g++ -std=c++11 example.cc example.pb.cc -o example.exe -lprotobuf **********************************************************************************************/4. libevent 和 protobuf 協作
總結
以上是生活随笔為你收集整理的谷歌protobuf(Protocol buffers)的使用的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 基于51单片机的LCD1602显示温湿度
- 下一篇: CANoe自动化测试系统 简介