Dynamic analysis method based on protobuf protocol and related device
By dynamically parsing the message structure, only the message data stream and file name are sent, and the receiving end dynamically loads and generates message classes, which solves the problem of large data volume in protobuf protocol transmission and improves the transmission speed.
Patent Information
- Application Number
- CN202411096731.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-12
- Publication Date
- 2024-09-10
- Estimated Expiration
- 2044-08-12
AI Technical Summary
When using the protobuf protocol to transmit data, the sender needs to send a description file of the message content and message structure, resulting in a large amount of data and affecting the transmission speed.
By dynamically parsing the message structure, the sender only sends the message data stream and the file name and message name of the protobuf file. The receiver dynamically loads the description file to generate a descriptor object, constructs a dynamic message class for parsing, and reduces the data traffic of the message structure description file.
The data transmission speed is improved, the data traffic of the message structure description file is reduced, and more efficient data processing is achieved.
Smart Images

Figure CN118631918A
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of computer data processing, and in particular to a dynamic parsing method based on protobuf protocol and related devices. Background Art
[0002] In data processing business, we often encounter the following business situation: the upstream business data collection module collects data in a certain format and transmits it to the downstream data processing module. At this time, the downstream data processing module must be able to recognize the data format transmitted from the upstream in order to further process, convert, calculate and other operations on the data.
[0003] Protobuf is an open source serialization framework provided by Google. It is a lightweight and efficient binary structured data storage format that can be used for structured data serialization, or serialization. It is very suitable for data storage or remote procedure call (RPC) data exchange format. It can be used in communication protocols, data storage and other fields. It is a language-independent, platform-independent, and extensible serialized structured data format. With the recent widespread use of the protobuf format, another solution has emerged: first, the upstream application and the downstream application agree to use the protobuf format and share a structure description file (.proto file), and then use the tool to generate .cc and .h files. In these two files, a description class is automatically generated according to the structure description file. Next, the description class can be conveniently used in the upstream data collection module code and the downstream data processing module code to perform set or get operations. This method is actually still operated at the code level.
[0004] In the current scheme of using protobuf protocol transmission, the data sent by the sender to the receiver includes the message content and the content of the description file of the message structure. The amount of data transmitted in this way is relatively large. Summary of the invention
[0005] The present invention aims to provide a dynamic parsing method and related device based on protobuf protocol. It can realize dynamic parsing of message bus content, real-time analysis of the operation interaction of each module, monitor the data flow of the entire message bus, and provide a solution for dynamic parsing messages based on protobuf protocol. It can reduce the data flow of message structure description files and improve the transmission speed.
[0006] In view of this, the first aspect of the present application provides a dynamic parsing method based on the protobuf protocol, characterized in that the method includes: configuring the protobuf file according to business needs, creating a description file of the message structure, and converting the description file into a class function callable by C++ code; the sending end generates a message data stream by calling the class structure of the protobuf file; the sending end saves the message data stream, the file name of the protobuf file, and the message name in the message object; after receiving the message object, the receiving end dynamically loads the description file of the message structure through the file name and message name of the protobuf file, and then generates a descriptor object based on the description file, and constructs a corresponding dynamic message class based on the descriptor object, and the descriptor object is a class structure; the receiving end completes the conversion of dynamically parsing protobuf.
[0007] In combination with the first aspect, in a possible implementation method, the sending end saves the message data stream, the file name of the protobuf file, and the message name in the message object, specifically including: the sending end calls the class generated by the protobuf conversion for processing, and converts the message class into a msg data stream, and sends it to the message bus.
[0008] In combination with the first aspect, in a possible implementation method, after receiving the message object, the receiving end dynamically loads the description file of the message structure through the file name and message name of the protobuf file, specifically including: the receiving end specifies the file directory of the protobuf and creates an import class; creates a protobuf file description object according to the file name under the file directory; generates a message content description class according to the message content name; and identifies the message according to the message content description class and the descriptor object.
[0009] In combination with the first aspect, in a possible implementation method, identifying the message based on the message content description class and the descriptor object specifically includes: if the msg data stream is equal to null, determining that the parsing has failed; if the msg data stream is not null, the automatic parsing is successful, and the msg data stream is the parsed message.
[0010] The second aspect of the present application provides a dynamic parsing system based on the protobuf protocol, characterized in that the system includes: a configuration module, a sending end and a receiving end, the configuration module is used to configure the protobuf file according to business needs, create a description file of the message structure, and convert the description file into a class function callable by C++ code; the sending end is used to generate a message data stream by calling the class structure of the protobuf file; the sending end is also used to save the message data stream, the file name of the protobuf file, and the message name in the message object; the receiving end is used to dynamically load the description file of the message structure through the file name and message name of the protobuf file after receiving the message object, and then generate a descriptor object based on the description file, and construct a corresponding dynamic message class based on the descriptor object, and the descriptor object is a class structure; the receiving end is used to complete the conversion of dynamic parsing protobuf.
[0011] In combination with the second aspect, in a possible implementation, the sending end is specifically used to call the class generated by the protobuf conversion for processing, and convert the message class into a msg data stream, and send it to the message bus.
[0012] In combination with the second aspect, in a possible implementation method, the receiving end is specifically used to: specify a protobuf file directory and create an import class; create a protobuf file description object according to the file name under the file directory; generate a message content description class according to the message content name; and identify the message according to the message content description class and the descriptor object.
[0013] In combination with the second aspect, in a possible implementation, the receiving end is specifically used to: if the msg data stream is equal to null, determine that the parsing fails; if the msg data stream is not null, the automatic parsing is successful, and the msg data stream is the parsed message.
[0014] The third aspect of the present application provides a computer device, characterized in that computer-readable instructions are stored in the computer device. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps in the dynamic parsing method based on the protobuf protocol as described in any possible implementation method of the first aspect to the first aspect.
[0015] The fourth aspect of the present application provides a computer-readable storage medium, characterized in that the computer-readable storage medium stores computer-readable instructions, and when the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps in the dynamic parsing method based on the protobuf protocol as described in any one of the first aspect to the first aspect.
[0016] The present application provides a dynamic parsing method and related device based on the protobuf protocol, through which the data sent from the sending end to the receiving end can only include the message data stream, the protobuf file name, and the message name, and the receiving end can dynamically parse the message content after receiving the message data stream, the protobuf file name, and the message name. This solution can reduce the data flow of the message structure description file, thereby increasing the transmission speed. BRIEF DESCRIPTION OF THE DRAWINGS
[0017] The present invention will be further described below with reference to the accompanying drawings and embodiments, in which: Figure 1 A flowchart of a dynamic parsing method based on the protobuf protocol provided in this application; Figure 2 A flowchart of a dynamic parsing method based on the protobuf protocol provided in this application; Figure 3 A schematic diagram of a dynamic parsing system based on the protobuf protocol provided for this application. DETAILED DESCRIPTION
[0018] The technical solution in the embodiment of the present application will be described below in conjunction with the drawings in the embodiment of the present application. In the present application, "at least one" means one or more, and "multiple" means two or more. "And / or" describes the association relationship of the associated objects, indicating that there may be three relationships. For example, A and / or B can represent: A exists alone, A and B exist at the same time, and B exists alone, where A and B can be singular or plural. The character " / " generally indicates that the associated objects before and after are in an "or" relationship. "At least one of the following" or its similar expressions refers to any combination of these items, including any combination of single or plural items. For example, at least one of a, b or c can represent: a, b, c, a and b, a and c, b and c or a and b and c, where a, b and c can be single or multiple. It is worth noting that "at least one" can also be interpreted as "one or more".
[0019] It should be noted that, in this application, words such as "exemplary" or "for example" are used to indicate examples, illustrations or descriptions. Any embodiment or design described as "exemplary" or "for example" in this application should not be interpreted as being more preferred or more advantageous than other embodiments or designs. Specifically, the use of words such as "exemplary" or "for example" is intended to present related concepts in a specific way.
[0020] The first, second, etc. descriptions appearing in the embodiments of the present application are only used for illustration and distinction of the description objects. There is no order, nor do they indicate any special limitation on the number of devices in the embodiments of the present application, and cannot constitute any limitation on the embodiments of the present application.
[0021] Protobuf is an open source serialization framework provided by Google. It is a lightweight and efficient binary structured data storage format that can be used for structured data serialization, or serialization. It is very suitable for data storage or remote procedure call (RPC) data exchange format. It can be used in communication protocols, data storage and other fields. It is a language-independent, platform-independent, and extensible serialized structured data format. With the recent widespread use of the protobuf format, another solution has emerged: first, the upstream application and the downstream application agree to use the protobuf format and share a structure description file (.proto file), and then use the tool to generate .cc and .h files. In these two files, a description class is automatically generated according to the structure description file. Next, the description class can be conveniently used in the upstream data collection module code and the downstream data processing module code to perform set or get operations. This method is actually still operated at the code level.
[0022] In the current scheme of using protobuf protocol transmission, the data sent by the sender to the receiver includes the message content and the content of the description file of the message structure. The amount of data transmitted in this way is relatively large.
[0023] Therefore, this application provides a dynamic parsing method based on the protobuf protocol, see Figure 1 , the method comprising: Step S1: Configure the protobuf file according to business requirements, create a description file of the message structure, and convert the description file into a class function that can be called by C++ code.
[0024] Configure the protobuf file according to business requirements and create a description file of the message structure in .proto format.
[0025] And convert the description file into a class function that can be called by C++ code.
[0026] Step S2: The sending end generates a message data stream by calling the class structure of the protobuf file.
[0027] The sender uses C++ code to generate a message data stream by calling the class structure of the protobuf file.
[0028] Step S3: The sender saves the message data stream, the file name of the protobuf file, and the message name in the message object.
[0029] The sender saves the message data stream, the file name of the protobuf file, and the message name in the message object.
[0030] Step S4: After receiving the message object, the receiving end dynamically loads the description file of the message structure through the file name and message name of the protobuf file, generates a descriptor object based on the description file, and constructs a corresponding dynamic message class based on the descriptor object.
[0031] After receiving the message object, the receiving end dynamically loads the description file of the message structure through the file name and message name of the protobuf file, generates a descriptor object based on the description file, and constructs a corresponding dynamic message class based on the descriptor object, which is a class structure.
[0032] Step S5: The receiving end completes the conversion of the dynamic parsing protobuf.
[0033] The receiving end completes the conversion of dynamically parsed protobuf.
[0034] It should be noted that the above step S2 specifically includes: The sending end calls the class generated by protobuf conversion for processing, and converts the message class into a msg data stream, and sends it to the message bus. It should be noted that msg is a binary stream, which is a binary data stream transmitted inside protobuf.
[0035] For example, the front-end service collects the device data through the protocol and sends it to the bus. After receiving the data, the data processing service generates an alarm message to the alarm service and generates change data to be sent to the storage service for storage.
[0036] It should be noted that in step S4, after receiving the message object, the receiving end dynamically loads the description file of the message structure through the file name and message name of the protobuf file, specifically including: Step S41: The receiving end specifies the file directory of protobuf and creates an import class.
[0037] In a specific case, the receiving end specifies the protobuf file directory and creates an import class specifically including: google::protobuf::compiler::DiskSourceTree sourceTree; sourceTree.MapPath("","E:\\RelyEZ\\ems_new\\product\\common\\sysconfig\\potobuf"); google::protobuf::compiler::Importer importer(&sourceTree, NULL); Step S42: Create a protobuf file description object according to the file name in the file directory.
[0038] The receiving end creates a protobuf file description object based on the file name in the file directory. Specifically, it may include: const google::protobuf::FileDescriptor *file = importer.Import(fileName.toStdString()); Step S43: Generate a message content description class according to the message content name.
[0039] The receiving end may generate a message content description class based on the message name, which may include: const Descriptor* descptor = file->FindMessageTypeByName(msgStr.toStdString()); Step S44: Identify the message according to the message content description class and descriptor object.
[0040] The receiving end identifies the message based on the message content description class and descriptor object.
[0041] Specifically, the receiving end can determine that if the msg data stream is equal to null, then it is determined that the parsing has failed and it is not this message; if the msg data stream is not null, then the parsing is automatically successful, and the msg data stream is the parsed message.
[0042] Furthermore, the receiving end may identify the message based on the message content description class and structure format, and may specifically include: google::protobuf::DynamicMessageFactory message_factory; const Message* msg = message_factory.GetPrototype(descptor); The present application provides a dynamic parsing method and related device based on the protobuf protocol, through which the data sent from the sending end to the receiving end can only include the message data stream, the protobuf file name, and the message name, and the receiving end can dynamically parse the message content after receiving the message data stream, the protobuf file name, and the message name. This solution can reduce the data flow of the message structure description file, thereby increasing the transmission speed.
[0043] Referring to the above-mentioned dynamic parsing method based on the protobuf protocol, the present application also provides a dynamic parsing system based on the protobuf protocol. The system includes: a configuration module, a sending end and a receiving end. Wherein, the configuration module is used to configure the protobuf file according to business needs, create a description file of the message structure, and convert the description file into a class function callable by C++ code; the sending end is used to generate a message data stream by calling the class structure of the protobuf file; the sending end is also used to save the message data stream, the file name of the protobuf file, and the message name in the message object; the receiving end is used to dynamically load the description file of the message structure through the file name and message name of the protobuf file after receiving the message object, and then generate a descriptor object based on the description file, and construct a corresponding dynamic message class based on the descriptor object, and the descriptor object is a class structure; the receiving end is used to complete the conversion of the dynamic parsing protobuf.
[0044] The sending end is specifically used to call the class generated by protobuf conversion for processing, and convert the message class into a msg data stream and send it to the message bus.
[0045] The receiving end is specifically used to: specify the file directory of protobuf and create an import class; create a protobuf file description object according to the file name under the file directory; generate a message content description class according to the message content name; and identify the message according to the message content description class and the descriptor object.
[0046] The receiving end is specifically used to: determine if the msg data stream is equal to null, then determine that the parsing fails; if the msg data stream is not null, then the automatic parsing is successful, and the msg data stream is the parsed message.
[0047] The present application also provides a computer device having computer-readable instructions stored therein. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps in the dynamic parsing method based on the protobuf protocol as described above in the present application.
[0048] The present application also provides a computer-readable storage medium, in which computer-readable instructions are stored. When the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps in the dynamic parsing method based on the protobuf protocol as described above in the present application.
[0049] The technical means disclosed in the present application are not limited to the technical means disclosed in the above-mentioned implementation methods, but also include technical solutions composed of any combination of the above technical features. It should be pointed out that for ordinary technicians in this technical field, several improvements and modifications can be made without departing from the principles of this application, and these improvements and modifications are also regarded as the protection scope of this application.
[0050] The above is a detailed introduction to a dynamic parsing method based on the protobuf protocol and related devices provided in the embodiments of the present application. Specific examples are used in this article to illustrate the principles and implementation methods of the present application. The description of the above embodiments is only used to help understand the method of the present application and its core idea; at the same time, for those of ordinary skill in the art, according to the idea of the present application, there will be changes in the specific implementation methods and application scope. In summary, the content of this specification should not be understood as a limitation on the present application. Although the present application is described in detail with reference to the aforementioned embodiments, those of ordinary skill in the art should understand that they can still modify the technical solutions recorded in the aforementioned embodiments, or replace some of the technical features therein by equivalents; and these modifications or replacements do not deviate the essence of the corresponding technical solutions from the spirit and scope of the technical solutions of the embodiments of the present application.
Claims
1. A dynamic parsing method based on protobuf protocol, characterized in that: The method comprises: Configure the protobuf file according to business requirements, create a description file of the message structure, and convert the description file into a class function that can be called by C++ code; The sender generates a message data stream by calling the class structure of the protobuf file; The sending end saves the message data stream, the file name of the protobuf file, and the message name in the message object; After receiving the message object, the receiving end dynamically loads the description file of the message structure through the file name and message name of the protobuf file, generates a descriptor object based on the description file, and constructs a corresponding dynamic message class based on the descriptor object, wherein the descriptor object is a class structure; The receiving end completes the conversion of dynamically parsed protobuf.
2. The dynamic parsing method based on protobuf protocol according to claim 1, characterized in that: The sending end saves the message data stream, the file name of the protobuf file, and the message name in the message object, specifically including: The sending end calls the class generated by protobuf conversion for processing, converts the message class into a msg data stream, and sends it to the message bus.
3. The dynamic parsing method based on protobuf protocol according to claim 1, characterized in that: After receiving the message object, the receiving end dynamically loads the description file of the message structure through the file name and message name of the protobuf file, specifically including: The receiving end specifies the file directory of protobuf and creates an import class; Create a protobuf file description object according to the file name in the file directory; Generate a message content description class based on the message content name; The message is identified according to the message content description class and the descriptor object.
4. The dynamic parsing method based on protobuf protocol according to claim 3, characterized in that: The identifying the message according to the message content description class and the descriptor object specifically includes: If the msg data stream is equal to null, the parsing is determined to have failed; If the msg data stream is not null, the automatic parsing is successful and the msg data stream is the parsed message.
5. A dynamic parsing system based on protobuf protocol, characterized in that: The system comprises: a configuration module, a sending end and a receiving end, The configuration module is used to configure the protobuf file according to business requirements, create a description file of the message structure, and convert the description file into a class function that can be called by C++ code; The sending end is used to generate a message data stream by calling the class structure of the protobuf file; The sending end is also used to save the message data stream, the file name of the protobuf file, and the message name in the message object; The receiving end is used to dynamically load the description file of the message structure through the file name and message name of the protobuf file after receiving the message object, then generate a descriptor object based on the description file, and construct a corresponding dynamic message class based on the descriptor object, wherein the descriptor object is a class structure; The receiving end is used to complete the conversion of dynamically parsing protobuf.
6. The dynamic parsing system based on protobuf protocol according to claim 5, characterized in that: The sending end is specifically used to call the class generated by protobuf conversion for processing, and convert the message class into a msg data stream and send it to the message bus.
7. The dynamic parsing system based on protobuf protocol according to claim 5, characterized in that: The receiving end is specifically used for: Specify the protobuf file directory and create an import class; Create a protobuf file description object according to the file name in the file directory; Generate a message content description class based on the message content name; The message is identified according to the message content description class and the descriptor object.
8. The dynamic parsing system based on protobuf protocol according to claim 5, characterized in that: The receiving end is specifically used for: If the msg data stream is equal to null, the parsing is determined to have failed; If the msg data stream is not null, the automatic parsing is successful and the msg data stream is the parsed message.
9. A computer device, characterized in that: The computer device stores computer-readable instructions, and when the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps in the dynamic parsing method based on the protobuf protocol as described in any one of claims 1 to 4.
10. A computer-readable storage medium, characterized in that: The computer-readable storage medium stores computer-readable instructions, and when the computer-readable instructions are executed by one or more processors, the one or more processors execute the steps in the dynamic parsing method based on the protobuf protocol as described in any one of claims 1 to 4.
Citation Information
Patent Citations
Data creation method and device based on Protobuf protocol dynamic analysis
CN113742231A
Message sending method based on Protobuf protocol dynamic analysis
CN113746842A
IROS message conversion method and device based on Protobuf and medium
CN117369828A
Cited By
Method for analyzing reported data of equipment based on Protocol Buffer
CN120455564A
