Ali P7 mobile Internet architect, Advanced Video (Daily update) for free learning, please click: https://space.bilibili.com/474380680
This article will start with google protobuf to introduce network traffic and data storage optimization:
Brief introduction
protobuf also known as protocol buffer is a format for data exchange google, it is language-independent, platform-independent. google provides multiple-language realization: java, c #, c ++, go and python, every implementation contains a compiler and library files for the appropriate language. Because it is a binary format than XML, JSON exchange data much faster. It can be used for data exchange in the data communication between heterogeneous environments or distributed applications. As an excellent efficiency and compatibility are binary data transmission format may be used in many fields such as a network transmission, profile, data storage. protobuf in the realization of various rpc occupy an important role.
advantage
good performance / high efficiency
code generation mechanisms
support "backward compatibility" and "forward compatible"
support for multiple programming languages
Shortcoming
application is not wide enough (compared to xml and json)
binary format results in poor readability
lack of self-description
installation
github Address Source Codes: https://github.com/google/protobuf source packet src / README.md, detailed installation instructions, the installation process is as follows: 1, extracting archive: unzip protobuf-master.zip 2, the files are decompressed file folder: cd protobuf-master 3, tools required for installation: sudo apt-get install autoconf automake libtool curl make g ++ unzip 4, generated automatically configure profile: ./ autogen.sh 5, configure the environment: ./configure 6, compiling source code (long time): make 7, installation: sudo make install 8, refreshing dynamic library: sudo ldconfig
Compile .proto file
protoc: protobuf own compilation tools, .proto file will generate the specified class
-cpp_out: the generated C ++ code files into the directory specified by the equal sign, which also specify the current directory
Tools Compiler through protoc .proto file, the compiler generates code for the selected language, the code may be operable .proto message types defined in the file, including acquiring, setting the field value of the message sequence to an output stream, and parses a message from the input stream. For C ++, the compiler generates a file and a .h file for each .proto .cc files, file .proto each message has a corresponding class.
Compiled code
backquote ( `): backticks role is to linux command in backtick execution
pkg-config is to obtain all the necessary information repository through a .pc document library, including version information, compiling and linking needs the parameters.
pkg-config -cflags protobuf: lists and pre-compile flags specify the shared library
pkg-config -libs protobuf: Lists the specified shared library link flags
Message format defined in the file .proto
Formed by at least one message field combination, the structure is similar to the C language, each field has a certain format:
Field Name Data Type ID = unique tag values;
type of data
proto prototype file is a message protocol definition file in the file we can use descriptive language, to a well-defined data formats we need to use the program. By looking at the file header, you can be found in the following several functions substantially generated for each field are to name an example. As can be seen, for each field will generate a clear clear function (clear_name), set function (set_name), get function (name and mutable_name).
Serialization and de-serialization of an array C
C ++ String serialization and de-serialization
File descriptor serialization and de-serialization
C ++ stream serialization and deserialization
defining repeated modifier
repeated representative may be repeated, we can be understood as an array.
For field generated field as a function of modifier repeated, the number of different slightly, such as people field, the compiler will generate the following code for:
example
enumerate
·example
package
.proto file to add an optional package declarator, to prevent different message types have naming conflicts. Packet specifier will affect the generated code in accordance with different use of language. For C ++, the class will be generated in the namespace packed in C ++.
example
Import Definitions
Original link https://cloud.tencent.com/developer/article/1056432
Ali P7 mobile Internet architect, Advanced Video (Daily update) for free learning, please click: https://space.bilibili.com/474380680