当前位置:网站首页>Protobuf binary file learning and parsing
Protobuf binary file learning and parsing
2022-06-29 07:56:00 【Hermit_ Rabbit】
0. brief introduction
protobuf Also called protocol buffer yes google A data exchange format of , It is independent of language , Platform independent .google Provides the implementation of multiple languages :java、c#、c++、go and python, Each implementation contains the compiler and library files of the corresponding language .
Because it's a binary format , Than using xml 、json Data exchange is much faster . It can be used for data communication between distributed applications or data exchange in heterogeneous environment . As a binary data transmission format with excellent efficiency and compatibility , Can be used for such as network transmission 、 The configuration file 、 Data storage and many other fields .
For more details, please see :https://developers.google.com/protocol-buffers/docs/overview
1. Basic grammar
1 Field restrictions
required: Characters that must be assigned optional: Optional fields , have access to [default = xxx] Configure defaults repeated: Repeatable variable length field , It's like an array
2 tag
Each field has a unique tag
tag 1-15 Is byte encoding ,16-2047 Use 2 Byte encoding , therefore 1-15 For frequently used fields
3 type

System default :
string The default is an empty string ;
bool The default is false;
The default value is 0;
enum The default is the first element
4 Parsing and serialization
Every message Both contain the following methods , For parsing and serializing , Note that the target is in byte form , Non text .bool SerializeToString(string* output) const: take message Serialization into binary is stored in output in , Note that it is binary , It's not text ; just string As a container .bool ParseFromString(const string& data): From the given binary value, it can be resolved to messagebool SerializeToOstream(ostream* output) const: Serialize to ostream in bool ParseFromIstream(istream* input): from istream Resolve in message
3. Explanation examples
establish .proto file
syntax = "proto3";// Specify version information , No error will be reported
message Person //message Keyword , The function is to define a message type
{
string name = 1; // full name
int32 id = 2; //id
string email = 3; // mail
}
message AddressBook
{
repeated Person people = 1;
}
Field API
Generally, we often use protoc To automatically generate :
protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/addressbook.proto
among protoc Tools to address :protoc
The field modifier is repeated Function generated by the field of , It's a little different , Such as people Field , Then the compiler will generate the following code :
int people_size() const;
void clear_people();
const ::Person& people(int index) const;
::Person* mutable_people(int index);
::Person* add_people();
::google::protobuf::RepeatedPtrField< ::Person >* mutable_people();
const ::google::protobuf::RepeatedPtrField< ::Person >& people() const;
The test program
void set_addressbook()
{
AddressBook obj;
Person *p1 = obj.add_people(); // Add a new Person
p1->set_name("mike");
p1->set_id(1);
p1->set_email("[email protected]");
Person *p2 = obj.add_people(); // Add a new Person
p2->set_name("jiang");
p2->set_id(2);
p2->set_email("[email protected]");
Person *p3 = obj.add_people(); // Add a new Person
p3->set_name("abc");
p3->set_id(3);
p3->set_email("[email protected]");
fstream output("pb.xxx", ios::out | ios::trunc | ios::binary);
bool flag = obj.SerializeToOstream(&output);// serialize
if (!flag)
{
cerr << "Failed to write file." << endl;
return;
}
output.close();// Close file
}
void get_addressbook()
{
AddressBook obj;
fstream input("./pb.xxx", ios::in | ios::binary);
obj.ParseFromIstream(&input); // Deserialization
input.close(); // Close file
for (int i = 0; i < obj.people_size(); i++)
{
const Person& person = obj.people(i);// Take the first place i individual people
cout << " The first " << i + 1 << " Messages \n";
cout << "name = " << person.name() << endl;
cout << "id = " << person.id() << endl;
cout << "email = " << person.email() << endl << endl;
}
}
Running results :

… Please refer to Ancient Moon House
边栏推荐
- postman预处理/前置条件Pre-request
- Vulnhub's dc9 target
- Automated test - uiautomator2 framework application - automatic clock in
- 【工控老马】西门子PLC s7-300SCL编程详解
- C compiler - implicit function declaration
- 多态中的向上和向下转型
- Django - installing mysqlclient error: mysqlclient 1.4.0 or newer is required; you have 0.9.3
- Embedded product anti-theft version
- 358. K distance interval rearrange string sorting
- 小白大战指针 (上)
猜你喜欢

【深度之眼吴恩达机器学习作业班第四期】Linear Regression with One Variable,单变量线性回归

Cartographer中的线程池操作

Dump (cl\alv\tree\base================================cp|set\items\for\column) when expanding node or clicking toolbar button

VSLAM特征之线特征&面特征

Viewing application and installation of Hana database license

Postman pre request

ROS2中的行为树 BehaviorTree

基于Sophus的Ceres优化

Reasons why the ext.dic file configured in ES does not take effect

code::blocks代码格式化快捷键
随机推荐
[FreeRTOS] interrupt mechanism
【深度之眼吴恩达第四期作业班】多元线性回归Linear Regression with multiple variables总结
【深度之眼吴恩达机器学习作业班第四期】Logistic Regression 逻辑回归总结
101. 对称二叉树(递归与迭代方法)
【深度之眼吴恩达机器学习作业班第四期】Regularization正则化总结
面试官:为什么数据库连接很消耗资源,资源都消耗在哪里?
打包时提示: Property ‘sqlSessionFactory‘ or ‘sqlSessionTemplate‘
程序调试 - Debug/Release 版本
C # import CSV into MySQL database
在iframe标签中操作外层dom
【修复收藏功能、更新登录接口】知识付费小程序、博客小程序、完整版开源源码、资源变现小程序,带299整站资源数据
ROS2中的行为树 BehaviorTree
Summary of array knowledge points
JS XOR obfuscation code
Postman pre request
C实战——高配版贪吃蛇游戏设计
4 years of working experience, and you can't tell the five communication modes between multithreads. Can you believe it?
软件测试鸾音鹤信
SAP ui5 Beginner (I) Introduction
Appium 环境搭建