【ProtoBuf】1. Getting to know ProtoBuf

v2-51ee98b05ce2ada206b744b6ff86cbd1_r
———————————— Every day without dancing is a disappointment to life.

1. The concept of serialization

In daily life, the voice messages received on the mobile phone cannot be transmitted directly in the network, but through a series of signals, such as the conversion of binary sequences in the network, the voice messages are converted into binary sequences for network transmission when they are sent out. When the message is received, the binary sequence is converted into a voice message. This process is actually serialization.

image-20230520151823201

The same is true for the code we write, and the objects transmitted in the network must not be directly created objects, but transmitted through the binary sequence of object conversion. The process of converting an object into a binary sequence is called the serialization process, and the process of restoring the binary sequence to an object is called the deserialization process.

image-20230520153531906

It can be seen that all the above mentioned are used in network transmission. On the local side, structured data in memory, that is, objects, can only be put into files after serialization, and this process also requires serialization. In addition, incoming and outgoing data such as databases and caches also involve serialization.

  • So what is serialization and deserialization?

Serialization: The process of converting an object into a sequence of bytes is called serialization of an object.

Deserialization: The process of restoring a sequence of bytes to an object? It is called deserialization of an object.

  • How to achieve serialization?

Serialization can be achieved through Json, XML, and ProtoBuf. (This article is about ProtoBuf)

二. ProtoBuf

1. What is ProtoBuf

ProtoBuf concept: a way to serialize structured data.

2. The characteristics of ProtoBuf

Simply put, ProtoBuf (full name is Protocol Buffer) is a method to serialize structured data.

1. Features:

  • Language-independent and platform-independent: that is, ProtoBuf supports multiple languages ​​such as Java, C++, and Python, and supports multiple platforms.
  • Efficient: Smaller, faster, and simpler than XML and Json.
  • Good scalability and compatibility: you can update the data structure without affecting or destroying the original old program.

2. Features of use: ProtoBuf needs to rely onHeader and source files generated by compilationto use. (for C++)

When defining a class, three things need to happen:

  1. Defines a series of attribute fields.
  2. Methods of handling fields: such as get, set.
  3. Methods of processing classes: serialization and deserialization (there are other methods not listed one by one)

For developers, defining attribute fields is relatively simple; writing the other two methods is thankless and time-consuming)

3. ProtoBuf serialization and deserialization process

In ProtoBuf, the class (class) is called a message (message)

image-20230520155519101

Relying on the header files and source files generated by compilation is actually to generate class XXX through message XXX, and class XXX will be broken up during generation to generate source files and header files.


Specific logic:

image-20230520161935348

  1. The purpose of writing a .proto file is to define the structural object (message) and attribute content.
  2. Use the photoc compiler to compile the .proto file to generate a series of interface codes, which can be placed in the newly generated header files and source files.
  3. Rely on the generated interface, include the header file generated by compilation into our code, realize setting and obtaining the fields defined in the .proto file, and serialize and deserialize the message object.

In general: ProtoBuf needs to rely on the header files and source files generated by compilation. With this code generation mechanism, developers no longer have to write codes for protocol parsing (this kind of work is typically thankless).

3. Summary of this chapter

image-20230520161505538

Guess you like

Origin blog.csdn.net/NEFUT/article/details/130782885