Disclosure of Invention
The invention aims to provide a message encoding method and a message decoding method, which are used for solving the problem that codes are greatly adjusted due to the fact that message contents are changed when different messages or different fields of the same message are encoded/decoded.
In order to achieve the above object, the present invention provides a method comprising:
the invention relates to a message coding method, which comprises the following steps:
a) Mapping the message into a class, mapping the field in the message into class member variables, defining corresponding coding functions according to the coding requirements of the class member variables, and recording the positions of the member variables in the message through custom attribute parameters;
b) Reading a message model instance to be coded;
c) The method comprises the steps of obtaining the position of a class member in message text throttling by reading user-defined attribute parameters of the position of the message model class definition member in a message;
d) Calling a corresponding coding function according to the variable names of the class members in the message model, generating byte streams corresponding to the class members, and assembling the byte streams corresponding to the fields according to the positions of the class members in the message throttling, which are obtained in the step c), so as to finish the coding of the message model.
The message coding method has the beneficial effects that firstly, a message is required to be mapped into class definition, fields in the message are mapped into class member variables, the positions of various member variables in the message are recorded through custom attribute parameters, coding functions are determined by the coding requirements of the message member variables, when coding is required, a message model class example to be coded is read, custom attribute parameters of a message model are read, the positions of the fields in the message are obtained, the corresponding coding functions are called according to the names of the fields, byte streams corresponding to the members are generated, and the byte streams are spliced according to the obtained positions of the fields to complete the coding process of the message. When the message content is changed, the method can be completed by modifying the custom attribute parameters of the message model, adjusting the message member variables and the definition of the coding function corresponding to the corresponding message segment, thereby reducing the coding threshold, facilitating the maintenance of codes and ensuring the coding quality of the message. Meanwhile, the early code assets can be effectively inherited through object-oriented design technologies such as inheritance and aggregation of classes.
Further, a reflection mechanism is adopted in the encoding process to trigger an encoding function, and the value of a corresponding message field to be read by the encoding function is obtained from a corresponding class member variable in a reflection mode.
Further, the position of the member variable in the custom attribute parameter record message includes the starting position of the message member and the length of the message member.
The method has the advantages that the positions of the class member variables are recorded according to the custom attribute parameters, wherein the positions comprise the starting positions of the class members and the lengths of the class members, the positions of the fields can be recorded regularly, and the errors of field position definition are avoided, so that the message coding errors are caused.
Further, when the message variable length part only has a part and exists in the last part of the message, the message variable length part content is processed by a cyclic nesting method, and the message variable length part is encoded by a predefined encoding function.
The method has the beneficial effects that when the message has a variable length part and exists in the last part of the message, the variable length part content of the message can be processed by a cyclic nesting method, the limitation that only fixed-length messages can be processed is eliminated, the message with variable length can be processed, and the application range of the method is improved.
The invention discloses a message decoding method, which comprises the following steps:
A) Mapping the message into 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 various member variables in the message through custom attribute parameters;
B) Reading a message model to be decoded;
C) Acquiring the position of each member variable in the message in message throttling according to the user-defined attribute parameters of the member positions in the read message model;
d) Dividing the byte stream to be decoded according to the position of each member variable in the byte stream in the acquired message model, and classifying according to the field;
E) And D) calling a decoding function by taking the byte stream segmented in the step D) as input according to the names of class members in the message model, generating a corresponding decoding result, and giving the result to the class member variables in the corresponding message model to finish the decoding process.
The method has the beneficial effects that firstly, the message is required to be mapped into class definition, the fields in the message are mapped into class member variables, the positions of various member variables in the message are recorded through the custom attribute parameters, the decoding function is determined by the decoding requirements of the message members, when the message to be decoded is required to be decoded, the message to be decoded and the custom attribute parameters in the message are read, the positions of the fields in the message in byte streams are obtained, the byte streams in the message are segmented and classified, the corresponding decoding function is determined according to the names of the fields, and the segmented byte streams are decoded through the corresponding decoding function to generate numerical values to be assigned to the corresponding message fields, so that the decoding process is completed. The method can automatically decode each field of the message and complete assignment work of each field by self-defining attribute parameters of the message model during decoding, reduces the decoding threshold, has convenient code maintenance and ensures the decoding quality of the message. Meanwhile, the early code assets can be effectively inherited through object-oriented design technologies such as inheritance and aggregation of classes.
Further, the position of the class member variable in the custom attribute parameter record message comprises the starting position and the length of the class member variable in the message throttling.
The method has the beneficial effects that the position of the message member variable is recorded according to the self-defined attribute parameters, wherein the position comprises the message member starting position and the message member length, and the message member starting position and the message member length are recorded, so that the message member variable has stronger regularity, the code walking position definition is convenient, and the error placement of the field position is avoided, thereby leading to the failure of decoding the message.
Further, the starting position and length of the message member are provided by the custom attribute parameter or configuration file.
Further, the operations of the decoding function and the decoding process and giving the decoding result to the class member variable corresponding to the message field are triggered by adopting a reflection mechanism.
The method has the advantages that the reflection mechanism is adopted to trigger the decoding function in the decoding process, the decoding function required by decoding is dynamically obtained through the reflection mechanism, and the decoding result is dynamically assigned, so that the code compiling scale is reduced.
Detailed Description
The invention is described in further detail below with reference to the accompanying drawings.
The embodiment of the method for encoding the message comprises the following steps:
A message coding method as shown in figure 8 includes mapping message to class definition, mapping field in message to class member variable, recording position of each class member variable in message by custom attribute parameter, determining coding function by coding requirement of message member variable, reading message model class instance to be coded when coding is needed, reading custom attribute parameter of message model to obtain position of each field in message, determining required coding function, calling corresponding coding function according to name of each field to generate byte stream corresponding to class member, assembling byte stream according to field position to finish coding process of message. The specific implementation steps are as follows:
a) Mapping the message into class, mapping the field in the message into class member variables, determining corresponding coding functions according to the names of the class member variables, and recording the positions of the various member variables in the message through custom attribute parameters.
As shown in fig. 1, a message is mapped into a class definition, fields in the message are mapped into class member variables, the positions of the fields are modified and defined by user-defined attribute parameters corresponding to class members, corresponding coding functions are obtained according to corresponding field names, as shown in fig. 2, the coding functions of the coding functions corresponding to the fields are triggered by calling a unified coding function interface, and the coding functions of the whole message are automatically completed.
B) And reading the message model class instance needing to be encoded.
C) And acquiring the positions (including the starting positions and the field lengths) of the byte streams of the member variables in the message according to the user-defined attribute parameters of the read message model class members.
D) Calling a corresponding coding function according to the variable names of the class members in the message, generating byte streams corresponding to the class members, and assembling the byte streams corresponding to the fields according to the positions of the class members in the message throttling acquired in the step c) to finish the coding of the message model.
The coding process mainly obtains the value of the corresponding field by calling InvokeMember methods in the Type in the NET reflection mechanism. Then, by using the Invoke method of MethodInfo, the field value is used as an input to trigger a coding function, coding operation is performed to generate a coding result (binary byte stream) of the field, then the position of the field in the message is obtained by reading the value of the custom attribute of SEGMENT in MemberInfo, then the byte streams of each field are spliced according to the indicated position, and finally the coding of the message is completed. As shown in fig. 5 and 6, when the encoding function is processed, the attribute modification is customized through "SegmentParseMethod", and the attribute indicates an information field served by the currently modified function (the field information is explicitly obtained by mapping the names of the message class member variables shown in fig. 3 and 4), and indicates the function of encoding the function, that is, indicates by an enumeration variable "netmethod property.
If the message variable length part is only one part and exists in the last part of the message. In the actual operation process, the variable length part content can be processed by a cyclic nesting method, so that the encoding and decoding processes of a plurality of variable length parts in one message are realized. Secondly, through the multi-state characteristic of the function realized by the inheritance mode and the function reload, other messages can be rapidly expanded on the basis of the existing messages, and the early-stage code asset is effectively inherited.
The embodiment of the method for decoding the message comprises the following steps:
in the decoding process shown in fig. 7, firstly, a message is required to be mapped into a definition of a class, fields in the message are mapped into member variables of the class, positions of various member variables in the message are recorded through custom attribute parameters, a decoding function is determined by the coding requirements of the member variables of the message, when decoding is required, the message to be decoded and the custom attribute parameters in the message are read, the positions of the fields in the message in byte streams are obtained, the byte streams in the message are segmented and classified, the required decoding function is determined, the corresponding coding function is called according to the name type of each field, and the segmented byte streams are decoded to generate numerical values and assigned to the corresponding message fields, so that the decoding process is completed. The specific implementation steps are as follows:
A) Mapping the message into class, mapping the field in the message into class member variables, determining corresponding decoding functions according to the class member variables, and recording the positions of the member variables in the message through custom attribute parameters.
As shown in fig. 1, a message is mapped into a class definition, fields in the message are mapped into class member variables, the positions of the fields are defined by the variables in the custom attributes of the corresponding class members, and corresponding decoding functions are obtained according to the corresponding field names defined by the custom attribute parameters, as shown in fig. 2, only by calling a unified decoding function interface, the decoding functions of the decoding functions corresponding to the fields can be triggered, so that the decoding functions of the whole message can be automatically completed.
B) And reading the message model to be decoded.
C) And acquiring the positions of various member variables in the message model in message throttling according to the custom attribute parameters of the class member positions in the read message model.
D) And dividing the byte stream to be decoded according to the positions of various member variables in the byte stream in the acquired message model, and classifying according to the field.
E) And D) calling a decoding function by taking the byte stream cut in the step D) as input according to the names of class members in the message model, generating a corresponding decoding result, and giving the result to a corresponding field to finish the decoding process.
Before decoding, the program reads the message and the self-defined attribute parameters in decoding through the reflecting mechanism of NET, and sorts the message structure and the corresponding decoding function through the self-defined attribute member variable value and the modified variable and function for use in the decoding process. The Invoke method MethodInfod in the NET reflection mechanism triggers the decoding function to execute decoding operation, and the return value of the decoding function is assigned to the corresponding field by utilizing the InvokeMember method in the Type. As shown in fig. 5 and 6, when the decoding function is processed, the attribute modification is customized through "SegmentParseMethod", and the attribute indicates an information field served by the currently modified function (the field information is explicitly obtained by mapping the names of the message class member variables shown in fig. 3 and 4), and indicates the function of decoding the function, namely, indicates by enumerating the variable "netmethod property.
Wherein fig. 6 is a definition of a method of encoding and decoding internal elements of a variable length portion of a message, and the encoding and decoding of each element in a variable length container is completed by triggering a "t.importmessage (buf)" statement in a predefined encoding function 'get_dynamic_segment_item_container' and a "o.exportmessage ()" statement in a predefined decoding function 'set_dynamic_segment_item_container' shown in fig. 5 for a variable length member variable List < DYNAMICSEGMENTITEM >.
In the embodiment of the method for encoding the message and the embodiment of the method for decoding the message, the message is exemplified by the table 1, the initial position of the message starts from 0, and no idle bytes exist between the fields.
TABLE 1
The class definition by the messages of table 1 is described as follows (in c# language):
public class name, codec interface base class definition
The field position to be encoded can be obtained through a field position attribute (including a starting position and a field length), and each field data type in the message is mapped into a data type of a programming language, wherein the programming language comprises C++, JAVA, python and the like, and the embodiment of the invention mainly adopts the programming language of C#. According to the information fields shown in table 1, the class member variables with one-to-one correspondence are obtained by using custom attribute modification, as shown in fig. 3, where fig. 4 is a definition of a corresponding element in the variable length set in the message defined in fig. 3. The class member variables in the message structures of fig. 3 and fig. 4 are modified by using a "SEGMENT" custom attribute, so that the position of the class member in the attribute in the whole message is defined (described by the starting position and the length). As shown in fig. 3 and 4, a override function "InitializeMethodClassInstance" is provided, and by assigning a value to the override function, the encoding process and decoding process corresponding to each field are defined.
And carrying out corresponding definition of message coding and message decoding according to class definition, wherein the definition codes of the message coding or decoding class are as follows:
public class name data codec base class definition
The initial position is a number, the length of the word segment is a number, the data type comprises short/ushort, int/uint, byte/float, string, unt/int 64, dateTime and class definition, the code form of the class definition is class < character string >, the function name and the field mark are character strings, the character strings comprise characters (A, B, C. Y, Z, a, b, C.y, z), the numbers (0-9) and the character strings, the coding and decoding mark is an enumeration variable, and the function is determined to be a coding function or a decoding function.
As shown in fig. 3 and 4, "AbstraceNETMessage" is the interface definition of the message codec function, and in this class, the basic interface form of the message code and interface are defined in a virtual function manner as shown in table 2.
TABLE 2
Bool ImportMessage (byte [ ] Message) is a decoding function of a Message model class, the function inputs a currently received byte stream, then decodes the byte stream, outputs a boolean type result, outputs TRUE if the decoding is successful, outputs FALSE if the decoding is failed, the function is a virtual function, and the virtual function can be reloaded and specified according to actual conditions. byte ExportMessage () is an encoding function of a message model class, and generates a byte stream according to the current given instance of the message class, where the function is a virtual function, and the virtual function can be reloaded and specified according to the actual situation.
The encoding and decoding functions of the generated message can be directly initialized and then tested by the encoding and decoding units, the testing steps are shown in fig. 9, and the fig. is also an actual use example of the encoding and decoding of the message. Firstly, a measured message instance is generated, then, coding operation is carried out to generate a binary byte stream, finally, the binary byte stream is input into a new measured message instance, whether the value of a field after coding is consistent with the value initialized in the earlier stage is confirmed, and the code quality can be effectively improved.