Encoding method and decoding method of a message

By mapping messages into classes and using custom attribute parameters and reflection mechanisms, the problem of frequent message encoding/decoding code adjustments is solved, an efficient encoding/decoding process is implemented, maintenance costs are reduced, and code quality and scalability are improved.

CN116506527BActive Publication Date: 2025-10-14713TH RES INST OF CHINA STATE SHIPBUILDING CORP LTD

Patent Information

Application Number
CN202310306688.5
Authority / Receiving Office
CN · China
Patent Type
Patents(China)
Current Assignee / Owner
Filing Date
2023-03-27
Publication Date
2025-10-14
Estimated Expiration
2043-03-27

AI Technical Summary

Technical Problem

The encoding/decoding code operations between different messages or different fields of the same message are similar but not identical, which requires significant code adjustments when network messages change, increasing maintenance costs and difficulty in quality assurance.

Method used

Map the message into a class, record the position of member variables in the message through custom attribute parameters, use the reflection mechanism to trigger the encoding/decoding function, and implement the message encoding/decoding process through class inheritance and aggregation technology.

Benefits of technology

It lowers the maintenance threshold of message encoding/decoding, ensures the encoding/decoding quality, is applicable to messages of varying lengths, and improves the maintainability and scalability of the code.

✦ Generated by Eureka AI based on patent content.

Smart Images

  • Figure CN116506527B_ABST
    Figure CN116506527B_ABST
Patent Text Reader

Abstract

The application relates to a message encoding method and decoding method, and belongs to the technical field of software encoding and decoding. According to the application, a message is mapped into a class, a field in the message is mapped into a class member variable, corresponding encoding and decoding functions are defined according to the encoding and decoding requirements of the field in the message, the positions of the class member variables in the message and the corresponding encoding and decoding functions are recorded through self-defined attribute parameters, when encoding or decoding operation is performed, the corresponding encoding and decoding functions of the message fields are called according to the encoding or decoding requirements, the corresponding encoding or decoding work is performed on the fields, the positions and names of the fields in the message are obtained according to the self-defined attribute parameters, the encoding and decoding results of the fields are automatically spliced or assigned, and finally the encoding and decoding work of the whole message is completed. The application solves the problem that when the encoding / decoding of different messages or different fields of the same message is performed, the code is greatly adjusted in the encoding and decoding process due to the change of the message content.
Need to check novelty before this filing date? Find Prior Art

Description

Technical Field

[0001] The present invention relates to a message encoding method and a decoding method, belonging to the technical field of software encoding and decoding. Background Art

[0002] When sending and receiving network messages, the software needs to use high-level computer languages ​​to encode and decode data messages. The encoding process of network messages is: read the fields defined in the corresponding message in sequence according to the network message protocol, convert the data according to the data type defined in the field, generate a binary byte stream corresponding to the field, and splice the binary byte stream in sequence to generate a binary byte stream for the entire message; the decoding process of network messages is: according to the position of the field defined in the message by the network message protocol, read the binary byte stream at the specific position, convert the data according to the data definition of the specific field, generate the required numerical value, and finally map the relevant numerical value to the corresponding protocol definition field.

[0003] However, the encoding / decoding code operations between different messages or different fields of the same message are similar but not identical. When the network message changes, it will lead to a substantial adjustment of the previous code assets, which will ultimately lead to high maintenance costs and difficulty in quality assurance of the generated encoding / decoding code. Summary of the Invention

[0004] The purpose of the present invention is to provide a message encoding method and decoding method to solve the problem that when different messages or different fields of the same message are encoded / decoded, the message content changes and the code is significantly adjusted.

[0005] To achieve the above object, the solution of the present invention includes:

[0006] A message encoding method of the present invention comprises the following steps:

[0007] a) Map the message into a class, map the fields in the message into class member variables, define the corresponding encoding function according to the encoding requirements of the class member variables, and record the location of the member variables in the message through custom attribute parameters;

[0008] b) Read the message model class instance to be encoded;

[0009] c) Obtain the position of the class member in the message byte stream by reading the custom attribute parameter of the message model class definition member in the message;

[0010] d) Call the corresponding encoding function according to the class member variable name in the message model to generate a byte stream corresponding to the class member. According to the position of the class member obtained in step c) in the message byte stream, assemble the byte stream generated corresponding to the field to complete the encoding of the message model.

[0011] Beneficial effects: the message encoding method of the application first needs to map the message into the definition of the class, map the fields in the message into the class member variables, record the positions of the class member variables in the message through the self-defined attribute parameters, determine the encoding function by the encoding requirements of the message member variables, read the message model class instance to be encoded when encoding is needed, read the self-defined attribute parameters of the message model, obtain the positions of the fields in the message, call the corresponding encoding function according to the names of the fields, generate the byte stream corresponding to the class members, and assemble the byte stream according to the obtained field positions to complete the encoding process of the message. When the content of the message is changed, the encoding threshold is reduced by modifying the self-defined attribute parameters of the message model, adjusting the message member variables and the definition of the corresponding encoding function of the message field, the code is convenient to maintain, and the encoding quality of the message is ensured. At the same time, through the object-oriented design techniques such as class inheritance and aggregation, the previous code assets can be effectively inherited.

[0012] Further, the encoding function in the encoding process is triggered by the reflection mechanism, and the values of the corresponding message fields read by the encoding function are obtained from the corresponding class member variables in a reflection manner.

[0013] Further, the self-defined attribute parameters record the positions of the member variables in the message, including the starting position of the message member and the length of the message member.

[0014] Beneficial effects: according to the self-defined attribute parameters, the positions of the class member variables are recorded, including the starting position of the class member and the length of the class member, which can regularly record the positions of the fields, avoid defining the field positions incorrectly, and thus cause the encoding error of the message.

[0015] Further, when only a part of the variable-length part of the message exists in the last part of the message, the variable-length part of the message is processed by the method of loop nesting, and the variable-length part of the message is encoded by the pre-defined encoding function.

[0016] Beneficial effects: when the message has a variable-length part and the variable-length part exists in the last part of the message, the variable-length part of the message can be processed by the method of loop nesting, which breaks the limitation of only processing fixed-length messages. The method can also process variable-length messages, and the application range of the method is improved.

[0017] A message decoding method in the application comprises the following steps:

[0018] A) mapping the message into a class, mapping the fields in the message into class member variables, defining corresponding decoding functions according to the decoding requirements of the class member variables, and recording the positions of the class member variables in the message through self-defined attribute parameters;

[0019] B) reading a message model to be decoded;

[0020] C) obtaining the positions of each member variable in the message in the byte stream of the message according to the custom attribute parameters of the member positions in the read message model;

[0021] D) according to the positions of each member variable in the message model in the byte stream, the byte stream to be decoded is segmented and classified according to the corresponding field;

[0022] E) according to the name of the class member in the message model, the byte stream segmented in step D) is called as input to generate the corresponding decoding result and assign the result to the corresponding class member variable in the message model, and the decoding process is completed.

[0023] Beneficial effects: the message decoding method of the application first needs to map the message into the definition of the class, map the field in the message into the class member variable, record the position of each class member variable in the message through the custom attribute parameter, and determine the decoding function by the message member decoding requirement. When decoding is needed, the message to be decoded and the custom attribute parameter in the message are read, the positions of each field in the byte stream in the message are obtained, the byte stream in the message is segmented and classified, the corresponding decoding function is determined according to the name of each field, the segmented byte stream is decoded through the corresponding decoding function to generate a numerical value and assign it to the corresponding message field, and the decoding process is completed. The method can automatically decode each field of the message and complete the assignment of each field through the custom attribute parameter of the message model during decoding, reduce the decoding threshold, maintain the code conveniently, and ensure the decoding quality of the message. At the same time, through the object-oriented design technology such as inheritance and aggregation of the class, the previous code assets can be effectively inherited.

[0024] Further, the custom attribute parameter records the positions of the class member variables in the message, including the starting position and length of the class member variable in the byte stream of the message.

[0025] Beneficial effects: the positions of the message member variables are recorded according to the custom attribute parameters, including the starting position and length of the message member, which makes the message member variables have strong regularity, facilitates code location definition, avoids placing the field position incorrectly, and thus causes the message decoding to fail.

[0026] Further, the starting position and length of the message member are provided through the custom attribute parameter or the configuration file.

[0027] Further, the decoding function, the decoding process, and the operation of assigning the decoding result to the corresponding class member variable of the message field adopt a reflection mechanism to trigger.

[0028] Beneficial effect: the reflection mechanism is used in the decoding process to trigger the decoding function, the decoding function required by decoding is dynamically obtained through the reflection mechanism, and the decoding result is dynamically assigned, so that the code programming scale is reduced. BRIEF DESCRIPTION OF DRAWINGS

[0029] Figure 1 is a mapping diagram between the self-defined attribute parameters and the message definition in the message class model in the embodiment of the application;

[0030] Figure 2 is a mapping diagram between the self-defined attribute parameters and the message encoding and decoding definition in the message model in the embodiment of the application;

[0031] Figure 3 is a message structure diagram in the embodiment of the application;

[0032] Figure 4 is a message dynamic structure diagram in the embodiment of the application;

[0033] Figure 5 is a part of message encoding and decoding definition diagram in the embodiment of the application;

[0034] Figure 6 is a message encoding and decoding definition diagram in the embodiment of the application;

[0035] Figure 7 is a message decoding algorithm flow diagram in the embodiment of the application;

[0036] Figure 8 is a message encoding algorithm flow diagram in the embodiment of the application;

[0037] Figure 9 is a part of message unit test example diagram in the embodiment of the application. DETAILED DESCRIPTION

[0038] The application will be further described in detail below with reference to the accompanying drawings.

[0039] Message encoding method embodiment:

[0040] As Figure 8The message encoding method shown first needs to map the message into the definition of a class, map the fields in the message into class member variables, record the positions of the class member variables in the message through self-defined attribute parameters, and determine the encoding function by the encoding requirement of the message member variables. When encoding is needed, the message model class instance to be encoded is read first, the self-defined attribute parameters of the message model are read, the positions of the fields in the message are obtained, and the encoding function needed is determined. The corresponding encoding function is called according to the name of each field, the byte stream corresponding to the class member is generated, the byte stream is spliced according to the obtained field positions, and the encoding process of the message is completed. The specific implementation steps are as follows:

[0041] a) Map the message into a class, map the fields in the message into class member variables, determine the corresponding encoding function according to the class member variable name, and record the positions of the class member variables in the message through self-defined attribute parameters.

[0042] As shown in Figure 1 , a message is mapped into a class definition, the fields in the message are mapped into class member variables, the positions of the fields are defined by the self-defined attribute parameters of the corresponding class members, and the corresponding encoding function is obtained according to the corresponding field name. As shown in Figure 2 , the encoding function of each field corresponding encoding function is triggered by calling a unified encoding function interface, and the encoding function of the entire message is automatically completed.

[0043] b) Read the message model class instance that needs to be encoded.

[0044] c) According to the “field position” self-defined attribute parameter of the read message model class member, obtain the position (including the starting position and the field length) of each member variable in the message byte stream.

[0045] d) According to the name of the class member variable in the message, call the corresponding encoding function to generate the byte stream corresponding to the class member, and splice the byte stream corresponding to the field according to the position of the class member in the message byte stream obtained in step c), to complete the encoding of the message model.

[0046] The encoding process mainly calls the InvokeMember method in the Type type in the.NET reflection mechanism to obtain the value of the corresponding field. Then the Invoke method of MethodInfo is used to trigger the encoding function by taking the field value as input, to generate the encoding result (binary byte stream) of the field, and then the value of the “SEGMENT” self-defined attribute in MemberInfo is read to obtain the position of the field in the message. Then the byte streams of the fields are spliced according to the indicated positions, and the encoding of the message is finally completed. As shown in Figure 5 ,Figure 6 As shown, when the encoding function is processed, it is modified by the "SegmentParseMethod" custom attribute, which indicates the information field served by the currently modified function (the field information is mapped as follows Figure 3 、 Figure 4 The message class member variable name is clearly shown), and the encoding function of the function is indicated by the enumeration variable "NETMethodProperty.Setor (encoding function)".

[0047] If the variable-length portion of a message is only part of the message and exists at the end, in practice, a nested loop approach can be used to process the variable-length portion, enabling encoding and decoding of multiple variable-length portions within a single message. Secondly, by implementing polymorphic function functionality through inheritance and function overloading, additional messages can be quickly expanded based on existing messages, effectively inheriting previous code assets.

[0048] Message decoding method embodiment:

[0049] like Figure 7 The decoding process shown first requires mapping the message into a class definition, mapping the fields in the message into class member variables, and recording the positions of various member variables in the message through custom attribute parameters. The decoding function is determined by the encoding requirements of the message member variables. When decoding is required, the message to be decoded and the custom attribute parameters in the message are read to obtain the position of each field in the byte stream. The byte stream in the message is segmented and classified, and the required decoding function is determined. The corresponding encoding function is called according to the name type of each field, and the segmented byte stream is decoded to generate a numerical value and assigned to the corresponding message field to complete the decoding process. The specific implementation steps are as follows:

[0050] A) Map the message into a class, map the fields in the message into class member variables, determine the corresponding decoding function based on the class member variables, and record the location of each member variable in the message through custom attribute parameters.

[0051] like Figure 1 As shown, a message is mapped into a class definition, and the fields in the message are mapped into class member variables. The position of the field is defined by the variable in the custom attribute of the corresponding class member. The corresponding decoding function is obtained according to the corresponding field name defined by the custom attribute parameter, as shown in Figure 2 As shown, by calling a unified decoding function interface, the decoding function corresponding to each field can be triggered, thereby automatically completing the decoding function of the entire message.

[0052] B) Read the message model to be decoded.

[0053] C) According to the custom attribute parameter of the class member position in the read message model, the position of each class member variable in the byte stream of the message model is obtained.

[0054] D) According to the obtained position of each class member variable in the byte stream of the message model, the byte stream to be decoded is segmented and classified according to the belonging field.

[0055] E) According to the name of the class member in the message model, the segmented byte stream of step D) is called as input to generate the corresponding decoding result and assign the result to the corresponding field, completing the decoding process.

[0056] Before decoding, the program reads the custom attribute parameters in the message and decoding through the reflection mechanism of.NET, and arranges the message structure and the corresponding decoding function through the member variable value of the custom attribute itself and the modified variable and function, for use in the decoding process. The decoding function is triggered to perform decoding operation by calling the Invoke method of MethodInfo in the reflection mechanism of.NET, and the return value of the decoding function is assigned to the corresponding field by using the InvokeMember method in the Type type. As shown in Figure 5 、 Figure 6 When the decoding function is processed, it is modified by the "SegmentParseMethod" custom attribute, which indicates the information field served by the current modified function (the field information is explicitly indicated by mapping the message class member variable name as shown in Figure 3 、 Figure 4 ) and indicates the decoding function of the function, that is, it is indicated by the enumeration variable "NETMethodProperty.Getor" (decoding function).

[0057] Among them, Figure 6 is the definition of the encoding and decoding method of the internal element of the variable length part of the message, which is shown in Figure 5 for the variable length member variable List <dynamicsegmentitem>The "T.ImportMessage(buf)" statement in the predefined encoding function 'get_dynamic_segment_item_contain' and the "o.ExportMessage()" statement in the predefined decoding function'set_dynamic_segment_item_contain' are triggered to complete the encoding and decoding of each element in the variable-length container.

[0058] In the embodiment of the encoding method of the message and the embodiment of the decoding method of the message, the message is taken as an example shown in Table 1, the starting position of the message is 0, and there is no idle byte between the fields.

[0059] Table 1

[0060]

[0061] The class definition description is as follows by using the message in Table 1 (described in C# language):

[0062] public class class name: encoding and decoding interface basic class definition

[0063]

[0064]

[0065] The field position attribute (including the starting position and the length of the field) can be used to obtain the position of the field to be encoded, and the data type of each field in the message is mapped into the data type of the program language, including C++, JAVA and python, and the program language mainly used in the embodiment of the application is C#. According to the information field shown in Table 1, the one-to-one corresponding class member variable is obtained by using the custom attribute decoration, such as Figure 3 , Figure 4 is Figure 3 the definition of the corresponding element in the variable-length set in the message. Figure 3 , Figure 4 The class member variable in the message structure is decorated with the "SEGMENT" custom attribute, so that the position of the class member in the entire message is defined (described by the starting position and the length). As shown in Figure 3 , Figure 4 In the class shown in

[0066] According to the class definition, the definition of the corresponding message encoding and message decoding is obtained, and the message encoding or decoding class definition code is as follows:

[0067] public class Class Name: Data Encoding and Decoding Base Class Definition

[0068]

[0069] Wherein, the start position is a number, the field length is a number, the data type includes short / ushort, int / uint, byte / float, string, uint64 / int64, DateTime, class definition, the code form of the class definition is: class< string >, the function name and field identifier are strings, the string includes characters (A, B, C…Y, Z, a, b, c…y, z), numbers (0-9) and strings, the encoding and decoding identifier is an enumeration variable, and the determination function is an encoding function or a decoding function.

[0070] As shown in Figure 3 , Figure 4 , "AbstraceNETMessage" is an interface definition of a message encoding and decoding function. In the class, the basic interface form of message encoding and interface is defined in the form of a virtual function as shown in Table 2.

[0071] Table 2

[0072]

[0073] Among them, bool ImportMessage (byte[] Message) is a decoding function of the message model class. After inputting the current received byte stream, the function decodes and outputs a boolean result. If the decoding is successful, TRUE is output, and if the decoding fails, FALSE is output. The function is a virtual function, which can be overloaded and specialized according to the actual situation. byte[] ExportMessage() is an encoding function of the message model class, which generates a byte stream according to the current given message class instance. The function is a virtual function, which can be overloaded and specialized according to the actual situation.

[0074] The encoding and decoding functions of the generated message can be directly initialized for encoding and decoding unit testing. The testing steps are shown in Figure 9 . The diagram is also an actual use example of message encoding and decoding. First, a tested message instance is generated. Then, the encoding operation is performed to generate a binary byte stream. Finally, the binary byte stream is input into a new tested message instance to confirm whether the field values after encoding are consistent with the values initialized in the early stage, which can effectively improve the code quality.< / dynamicsegmentitem>

Claims

1. A message encoding method, characterized in that: The following steps are involved: a) Map the message into a class, map the fields in the message into class member variables, define the corresponding encoding function according to the encoding requirements of the class member variables, and record the location of the member variables in the message through custom attribute parameters; b) Read the message model class instance to be encoded; c) Obtain the position of the class member in the message byte stream by reading the custom attribute parameter of the message model class definition member in the message; d) Calling the corresponding encoding function according to the class member variable name in the message model, that is, using the field name as the basis to determine the encoding function corresponding to the field, and then using the field value as input to trigger the encoding function to perform the encoding operation to generate the byte stream corresponding to the class member; according to the position of the class member obtained in step c) in the message byte stream, assembling the byte stream generated corresponding to the field to complete the encoding of the message model; the custom attributes of the modified encoding function include the information field served by the currently modified encoding function, that is, the field name.

2. The message encoding method according to claim 1, characterized in that: In the encoding process, a reflection mechanism is used to trigger an encoding function, and the value of a corresponding message field that the encoding function needs to read is obtained from a corresponding class member variable in a reflection manner.

3. The message encoding method according to claim 1, characterized in that: The custom attribute parameter records the position of the member variables in the message, including the starting position of the message member and the length of the message member.

4. The message encoding method according to claim 3, characterized in that: When the variable-length portion of the message is only a part and exists in the last part of the message, the variable-length portion of the message is processed by a loop nesting method and encoded by a predefined encoding function.

5. A message decoding method, characterized in that: The following steps are involved: A) Mapping messages into classes, mapping fields in the messages into class member variables, defining corresponding decoding functions based on the decoding requirements of the class member variables, and recording the locations of various member variables in the message through custom attribute parameters; B) reading the message model to be decoded; C) obtaining the position of each member variable in the message in the message byte stream according to the custom attribute parameter of the member position in the message model; D) According to the position of each member variable in the obtained message model in the byte stream, the byte stream to be decoded is segmented and classified according to the fields to which it belongs; E) Based on the name of the class member in the message model, the byte stream segmented in step D) is used as input to call the decoding function, generate a corresponding decoding result and assign the result to the class member variable in the corresponding message model to complete the decoding process; the custom attribute of the modified decoding function includes the information field served by the currently modified decoding function, i.e., the field name.

6. The message decoding method according to claim 5, characterized in that: The custom attribute parameter records the position of the class member variable in the message, including the starting position and length of the class member variable in the message byte stream.

7. The message decoding method according to claim 6, characterized in that: The starting position and length of the message member are provided through custom attribute parameters or configuration files.

8. The message decoding method according to claim 5, characterized in that: The decoding function and the decoding process as well as the operation of assigning the decoding result to the class member variable corresponding to the message field are triggered by a reflection mechanism.

Citation Information

Patent Citations

  • Processing method and device of protocol interpreter

    CN103095726A

Cited By

  • Byte stream automatic coding splicing and segmentation decoding method based on Qt element object

    CN121418485A