To communicate, there must be a protocol, otherwise the two sides cannot understand each other's code stream. In Protobuf, a protocol is made up of a series of messages. Therefore, the most important thing is to define the message format to use when communicating.
PROTOBUF message definition
Messages are grouped by at least one field, similar to the structure in C. Each field has a certain format.
Field format: Qualifier modifier ①| Data type ②| Field name ③| = | Field encoded value ④| [Field default value ⑤]
①. Qualifier modifier contains required\optional\repeated
Required: Represents a required field and must be set to the value of the field relative to the sender before sending the message, and must be able to recognize the meaning of the field for the receiver. No required field or unrecognized required field before sending will cause a codec exception, causing the message to be discarded.
Optional: Represents an optional field, optionally for the sender, when sending a message, you can have a selective setting or do not set the value of the field. For the receiver, if the optional field is recognized for processing, if unrecognized, the field is ignored, and the other fields in the message are processed correctly. ---Because of the characteristics of the optional field, many interfaces in the upgraded version of the subsequent addition of the field are set to the optional field, so that the old version of the program can not upgrade the normal communication with the new software, but the new field is not recognized just, Because not every node needs new functionality, you can upgrade and smooth transitions on demand.
Repeated: Indicates that the field can contain 0~n elements. It has the same characteristics as optional, but can contain multiple values each time. Can be thought of as passing the value of an array.
②. Data type
PROTOBUF defines a set of basic data types. Almost all can be mapped to the underlying data types in languages such as C++\java.
Protobuf data type |
Describe |
Packaged |
C + + language mapping |
bool |
Boolean type |
1 bytes |
bool |
Double |
64-bit floating-point numbers |
N |
Double |
Float |
32 for floating-point numbers |
N |
Float |
Int32 |
32-bit integers, |
N |
Int |
Uin32 |
Unsigned 32-bit integer |
N |
unsigned int |
Int64 |
64-bit integer |
N |
__int64 |
UInt64 |
64 is unsigned integer |
N |
unsigned __int64 |
Sint32 |
32-bit integer that handles negative numbers more efficiently |
N |
Int32 |
Sing64 |
64-bit integer handling negative numbers more efficient |
N |
__int64 |
Fixed32 |
32-bit unsigned integer |
4 |
unsigned int32 |
Fixed64 |
64-bit unsigned integer |
8 |
unsigned __int64 |
Sfixed32 |
32-bit integer, capable of handling negative numbers at a higher efficiency |
4 |
unsigned int32 |
Sfixed64 |
64 is an integer |
8 |
unsigned __int64 |
String |
Only ASCII characters can be processed |
N |
std::string |
bytes |
Used to handle multibyte language characters, such as Chinese |
N |
std::string |
Enum |
Can contain a user-defined enumeration type UInt32 |
N (UInt32) |
Enum |
Message |
Can contain a user-defined message type |
N |
Object of Class |
N indicates that the packaged byte is not fixed. But depending on the size or length of the data.
For example, Int32, if the number is small, when 0~127, use a byte package.
The enumeration is packaged in the same way as UInt32.
With regard to message, a structure similar to the C language contains another structure as a data member.
About the difference between Fixed32 and Int32. FIXED32 packing efficiency is higher than Int32, but the space used is more than Int32. Therefore one belongs to the time efficiency high, one belongs to the space efficiency high. According to the actual situation of the project, the general choice of FIXED32, if encountered on the transmission of data requirements more stringent environment, you can choose Int32.
③. Field name
The naming of field names is almost identical to the way variables are named in languages such as C, C + +, Java, and so on.
Protobuf suggested that the name of the field should be the hump with the following underline. such as first_name rather than FirstName.
④. Field encoded value
With this value, both sides of the communication can recognize each other's fields. Of course, the same encoding value must have the same qualification modifier and data type.
The value range of the encoded value is 1~2^32 (4294967296).
The 1~15 encoding time and space efficiency are the highest, the larger the encoding value, the lower the time and space efficiency of the encoding (relative to 1-15), of course, the average adjacent 2-value coding efficiency is the same, unless 2 values are exactly 4 bytes, 12 bytes, 20 bytes, and so the critical section. Like 15 and 16.
The 1900~2000 encoding value is the internal retention value of the Google Protobuf system and is not recommended for use in your own project.
Protobuf also recommends that the values that are frequently passed are set to the value of the field encoding 1-15.
The encoding value of a field in a message does not need to be contiguous, as long as it is legitimate, and a field cannot contain the same encoded value in the same message.
Recommendation: Item