1. Introduction to Protocol Buffer
Protocol buffer (hereinafter referred to as protocol buffer) is a mixed language data standard within Google. It is a lightweight and efficient structured data storage format, which can be used for structured data serialization. It is very suitable for data storage or RPC data exchange format.
Protobuf is a pure presentation layer protocol, which can be used with various transport layer protocols. Protobuf's documents are also very perfect. google provides implementations in many languages: java, c#, C + +, go and python. Each implementation includes the compiler and library files of the corresponding language.
Protobuf supports relatively few data types and does not support constant types. Because its design concept is pure presentation layer protocol, there is no RPC framework that specifically supports protobuf.
2. Protobuf usage process
2.1 data preparation
- Composite type: structure / class
-
Data to serialize
- Foundation type
-
// Data to serialize struct Persion { int id; string name; int age; }; int id;
2.2. Create proto file, syntax format
// protobuf version // proto2 syntax = "proto3"; // Import another proto file import "filename.proto"; // Organizational personality structure // Syntax format message keyword(Equivalent to the name of the class created) { // Member variable Data type variable name = Number of variable; // The number starts from 1 and cannot be repeated }
- repeated qualifier (using array)
- Use import to import another proto file
- Adding namespaces using package
- Examples
// Persion.proto syntax = "proto3"; // Import another proto file import "Info.proto"; // Add namespace package namespace; // The person class belongs to the namespace enum TypeValue { value1 = 0; // The first enumeration value in protbuf must be 0 value2 = 6; value3 = 9; } message Persion { int32 id = 1; // Number starts from 1 repeated bytes name = 2; //repeated modified name can create multiple in the program and be used as a dynamic array in the program int32 age = 3; TypeValue value = 4; Info info = 5; // Info object, class in imported proto file }
// Info.proto syntax = "proto3"; // Add namespace package namespace; // The person class belongs to the namespace message Info { bytes address = 1; int32 number = 2; }
- . proto data type
. proto type | C + + type | remarks |
---|---|---|
double | double | 64 bit floating point number |
float | float | 32-bit floating point number |
int32 | int32 | 32-bit integer |
int64 | int64 | 64 bit integer |
uint32 | uint32 | 32-bit unsigned integer |
uint64 | uint64 | 64 bit unsigned integer |
sint32 | sint32 | 32-bit integer, processing negative numbers is more efficient than int32 |
sint64 | sint64 | 64 bit integer, processing negative numbers is more efficient than int64 |
fixed32 | uint32 | Always 4 bytes. If the value is always greater than 228, this type will be more efficient than uint32. |
fixed64 | uint64 | Always 8 bytes. If the value is always greater than 256, this type will be more efficient than uint64. |
sfixed32 | int32 | Always 4 bytes |
sfixed64 | int64 | Always 8 bytes |
bool | bool | Boolean type |
string | string | A string must be UTF-8 encoded or 7-bit ASCII encoded text |
bytes | string | Handle multi byte language characters, such as Chinese |
enum | enum | enumeration |
message | object of class | Custom message type |
2.2. Use protobuf compiler to generate C + + classes
# protobuf compiler, compile the source code to get the protoc exe # grammar # --cpp_out the location of the generated c + + class protoc.exe xxx.proto --cpp_out=catalogue
2.3. Use C++ API to read and write messages
-
Read: variable name ()
-
Write: set_ Variable name (arg1, arg2,...)
- Some C + + interface functions
Person p; //Serialize output as an outgoing parameter p.SerializeToString(std::string* output); //analysis Person pp; pp.ParseFromString(output);
2.4 use in VS
- Add the generated C + + classes and header files to the project
- Reference in header file in project file
- Note: for VS configuration, the macro "PROTOBUF_USE_DLLS" needs to be added in C/C + + - "preprocessor -"