CN-122027715-A - General protocol packet unpacking method
Abstract
The application discloses a general protocol packet unpacking method, and belongs to the technical field of network transmission. The method comprises the steps of firstly constructing a command ID definition header file, further constructing a package unpacking type definition header file and a source file, defining a package unpacking type in the package unpacking type definition header file and the source file, constructing a protocol type definition header file and the source file, defining a protocol base type in the protocol type definition header file and the source file, responding to protocol addition, adding a protocol ID in an enumeration variable of the command ID definition header file, defining a protocol extension type in the protocol type definition header file and the source file, inheriting the protocol base type by the protocol extension type, and then executing a package process or unpacking process. The method can realize the rapid completion of protocol encapsulation or analysis in the application layer code by only supplementing or modifying the member variables and member functions of the protocol extension class, does not need to additionally modify protocol analysis logic, does not generate additional protocol test work, and does not have any influence on other modules which do not use the protocol.
Inventors
- YANG JINCHAO
- LENG CHAOJIE
- QU SHAOJIE
- ZOU CHUNXIA
- WANG JINGJING
Assignees
- 烟台北方星空自控科技有限公司
Dates
- Publication Date
- 20260512
- Application Date
- 20260210
Claims (10)
- 1. A method for unpacking a universal protocol packet, the method comprising: Constructing a command ID definition header file, wherein the command ID definition header file comprises a protocol header, a protocol maximum length and a protocol ID; Constructing a packet unpacking class definition header file and a source file, and defining a packet unpacking class in the packet unpacking class definition header file and the source file; constructing a protocol class definition header file and a source file, and defining a protocol base class in the protocol class definition header file and the source file; In response to the protocol addition, adding a protocol ID in an enumeration variable of the command ID definition header file, defining a protocol extension class in the protocol class definition header file and the source file, the protocol extension class inherits a protocol base class, and then executing a packaging process or an unpacking process.
- 2. The method of claim 1, wherein defining a package unpacking class in a package unpacking class definition header file and a source file comprises defining a protocol ID member variable, a protocol buffer member variable and a buffer effective data length member variable in the package unpacking class, defining and reloading a protocol buffer write function and a protocol buffer read function in the package unpacking class, wherein parameters of the protocol buffer write function and the protocol buffer read function are variables representing protocol contents, defining a package function and an unpacking function in the package unpacking class, wherein package function parameters are used for storing post-package protocol data, unpacking function parameters are used for storing data to be analyzed, defining an acquisition protocol ID function and a setting protocol ID function in the package unpacking class, acquiring protocol ID function no function parameters, and setting protocol ID function parameters to be variables representing protocol IDs.
- 3. The method of claim 2, wherein defining the protocol base class in the protocol class definition header and source file includes defining a protocol ID member variable in the protocol base class, defining a protocol base class constructor in the protocol base class, the protocol base class constructor parameter being a variable representing a protocol ID, defining a protocol ID write interface function and a protocol ID compare function in the protocol base class, the protocol ID write interface function parameter being a variable representing a protocol ID, the protocol ID compare function parameter being a variable of a packet unpacking class type.
- 4. A method of unpacking a universal protocol packet as recited in claim 3, wherein adding a protocol ID to an enumeration variable of a command ID definition header file in response to a protocol addition comprises: adding an enumeration value into the defined protocol ID enumeration variable; The method comprises the steps of defining a protocol extension class in a protocol class definition header file and a source file, wherein the protocol extension class inherits a protocol base class, and comprises the steps of defining a protocol extension class constructing function in the protocol extension class, defining a protocol writing interface function and a protocol reading interface function in the protocol extension class, wherein the parameter types of the protocol writing interface function and the protocol reading interface function are all variables of a packet unpacking class type, defining a protocol content member variable in the protocol extension class, and inheriting the protocol base class by the protocol extension class definition.
- 5. A method for unpacking a general protocol packet according to claim 3, wherein the packing process comprises defining a packet unpacking class variable and a protocol expansion class variable in an application layer code, assigning values to protocol content member variables of the protocol expansion class variable, calling a protocol write interface function of the protocol expansion class, and calling a packing function of the packet unpacking class; The unpacking process includes defining the unpacking type variable in the application layer code, calling the unpacking Bao Hanshu of the unpacking type, calling the ID function of the unpacking type, defining the protocol extension type variable, and calling the protocol reading interface function of the protocol extension type.
- 6. The method of claim 5, wherein defining a packet unpacking class variable and a protocol extension class variable in the application layer code for the packet includes defining a packet unpacking class type variable using the packet unpacking class and defining a protocol extension class type variable using the protocol extension class; Assigning values to protocol content member variables of protocol extension class variables includes assigning values to protocol content member variables of defined protocol extension class type variables; The protocol writing interface function of calling protocol extension class includes calling the protocol writing interface function of protocol extension class by using defined protocol extension class variable, and transmitting defined packet unpacking class variable into the protocol writing interface function parameter of protocol extension class; Calling the package function of the package unpacking class includes calling the package function of the package unpacking class by using the defined package unpacking class variable, and transmitting the data buffer area variable used for storing the packaged data in the application layer code into the package function parameter of the package unpacking class.
- 7. The method of claim 6, wherein the processing of the protocol buffer write function of the packet unpacking class includes adding the parameter content of the protocol buffer write function to the protocol buffer and updating the effective data length member variable of the buffer in response to the parameter type of the protocol buffer write function occupying less than or equal to the remaining memory of the protocol buffer; The processing procedure of the packet unpacking class for setting the protocol ID function comprises the steps of assigning the parameter content of the protocol ID function to the protocol ID member variable; the processing procedure of writing the protocol ID of the protocol base class into the interface function comprises the steps of calling a protocol ID setting function of a packet unpacking class; The protocol ID writing interface function of the protocol basic class is called, the protocol cache writing function of the packet unpacking class is called in turn according to the number and the type of the member variables, and the member variables are sequentially used as parameters to be transmitted into the protocol cache writing function of the packet unpacking class; And in response to the parameter of the packet function not being null, sequentially assigning the protocol header, the data length, the protocol ID, the protocol buffer data and the checksum to the parameter variable of the packet function, and calculating and outputting the total length of the packet data.
- 8. The method of claim 5, wherein, for the unpacking, Defining a packet unpacking class variable in an application layer code, wherein the packet unpacking class variable comprises the step of defining a packet unpacking class type variable by using a packet unpacking class; Calling a unpacking function of a package unpacking class by using a defined package unpacking class variable, and transmitting a data buffer area variable to be analyzed in an application layer code into unpacking member function parameters of the package unpacking class variable; Calling an acquisition protocol ID function of a packet unpacking class, wherein the acquisition protocol ID member function is called by using a defined packet unpacking class variable; Defining protocol extension class variables, namely defining the protocol extension class variables corresponding to the return values of the packet unpacking class variable acquisition protocol ID member functions in an application layer code; the calling of the protocol reading interface function of the protocol extension class comprises the steps of calling the protocol reading interface member function by using the defined protocol extension class variable, and transmitting the defined packet unpacking class variable into the protocol reading interface member function parameter of the protocol extension class variable.
- 9. The method of claim 8, wherein the processing procedure of the protocol buffer read function of the packet unpacking class includes responding to the fact that the number of bytes occupied by the parameter type of the protocol buffer read function is smaller than or equal to the remaining data length of the protocol buffer, judging the length of the data to be read according to the content of the parameter, reading the content of the data of the length from the protocol buffer and assigning the parameter of the protocol buffer read function, deleting the read data from the protocol buffer and updating the member variable of the effective data length of the buffer; the processing procedure of obtaining protocol ID function of packet unpacking class includes outputting protocol ID member variable; the protocol ID comparison function of the protocol base class is used for calling the protocol ID acquisition function of the packet unpacking class by using the protocol ID comparison function parameter, comparing the protocol ID member variable in the protocol base class with the acquired protocol ID, and outputting a comparison result; Calling a protocol ID comparison function of a protocol base class, transmitting protocol read interface function parameters of the protocol extension class into the protocol ID comparison function parameters of the protocol base class, sequentially calling a protocol cache reading function of a packet unpacking class by using the protocol read interface function parameters of the protocol extension class according to the number and the type of the member variables, and sequentially transmitting the member variables into the protocol cache reading function parameters of the packet unpacking class; And in response to the parameters of the unpacking function not being null and the total length of the parameters being greater than or equal to the sum of the number of bytes occupied by the protocol header, the number of bytes occupied by the data length, the number of bytes occupied by the protocol ID and the number of bytes occupied by the checksum, sequentially analyzing the protocol header, the data length and the protocol ID, assigning the protocol ID to the protocol ID member variable, analyzing the protocol content and assigning the protocol content to the protocol buffer member variable, calculating the effective data length of the protocol buffer and assigning the effective data length member variable, and outputting the read byte number.
- 10. The method of claim 5, wherein the protocol header and the maximum length of the protocol are defined by macros, the protocol ID is obtained by enumerating types, and when the protocol extension class variables are defined, the protocol ID corresponding to the protocol extension class is assigned to the protocol ID member variables of the protocol base class by automatically calling the protocol extension class constructor and the protocol base class constructor.
Description
General protocol packet unpacking method Technical Field The application relates to the technical field of network transmission, in particular to a general protocol packet unpacking method. Background Along with development of science and technology, software system scale is larger and larger, software module division is also more and more, interaction content among modules is complex and changeable in the design and development process, and protocol debugging work is also more and more complex. In the related technology, an internal interaction protocol is determined when software is designed in detail to form a protocol document, a software module developer develops according to the protocol, however, the protocol content is often required to be modified or supplemented due to detail problems in the development process, large additional workload is often increased due to various reasons such as inconsistent understanding or incorrect analysis of the protocol in the debugging process, and if the interaction boundary content is not sufficiently tested, the reliability is also greatly reduced, so that a breakdown problem occurs. Therefore, there is a need to develop a method for unpacking a universal protocol packet to solve the above-mentioned problems. Disclosure of Invention In view of this, the present application provides a general protocol packet unpacking method, which can implement rapid protocol packing or analysis in an application layer code by only supplementing or modifying a protocol extension class member variable and a member function, without additionally modifying a protocol analysis logic, without generating additional protocol test work, and without any influence on other modules that do not use the protocol. Specifically, the method comprises the following technical scheme: The application provides a general protocol packet unpacking method, which comprises the following steps: Constructing a command ID definition header file, wherein the command ID definition header file comprises a protocol header, a protocol maximum length and a protocol ID; Constructing a packet unpacking class definition header file and a source file, and defining a packet unpacking class in the packet unpacking class definition header file and the source file; constructing a protocol class definition header file and a source file, and defining a protocol base class in the protocol class definition header file and the source file; In response to the protocol addition, adding a protocol ID in an enumeration variable of the command ID definition header file, defining a protocol extension class in the protocol class definition header file and the source file, the protocol extension class inherits a protocol base class, and then executing a packaging process or an unpacking process. In some embodiments, defining the package unpacking class in the package unpacking class definition header file and the source file comprises defining a protocol ID member variable, a protocol buffer member variable and a buffer effective data length member variable in the package unpacking class, defining and reloading a protocol buffer write function and a protocol buffer read function in the package unpacking class, wherein parameters of the protocol buffer write function and the protocol buffer read function are variables representing protocol contents, defining a package function and a package unpacking function in the package unpacking class, wherein package function parameters are used for storing packaged protocol data, unpacking function parameters are used for storing data to be analyzed, defining an acquisition protocol ID function and a protocol ID function setting function in the package unpacking class, acquiring a protocol ID function no-function parameter, and setting the protocol ID function parameter as a variable representing protocol ID. In some embodiments, defining the protocol base class in the protocol class definition header file and the source file includes defining a protocol ID member variable in the protocol base class, defining a protocol base class constructor in the protocol base class, the protocol base class constructor parameter being a variable representing a protocol ID, defining a protocol ID write interface function and a protocol ID compare function in the protocol base class, the protocol ID write interface function parameter being a variable representing a protocol ID, the protocol ID compare function parameter being a variable of a packet unpacking class type. In some embodiments, adding the protocol ID in the enumeration variable of the command ID definition header file in response to the protocol addition includes: adding an enumeration value into the defined protocol ID enumeration variable; The method comprises the steps of defining a protocol extension class in a protocol class definition header file and a source file, wherein the protocol extension class inherits a protocol base class, and com