Protocol encoding method, protocol decoding method, computer program product and device
The data types of communication protocol frames are obtained and used for encoding and decoding through reflection mechanisms, which solves the problems of code reusability and low development efficiency during access to multiple communication protocols, and realizes an efficient and flexible protocol encoding and decoding process.
Patent Information
- Application Number
- CN202411871092.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-12-17
- Publication Date
- 2025-07-01
AI Technical Summary
In the face of the need to access multiple communication protocols, the prior art is difficult to improve code reusability and development efficiency, resulting in high development costs and low efficiency.
The data type of the target protocol field in the structure corresponding to the communication protocol frame is obtained through the reflection mechanism, and the field value is encoded or decoded using the encoding or decoding method corresponding to the data type to generate or parse the byte stream.
It realizes the high reusability of code and the improvement of development efficiency, reduces development costs, and improves the flexibility and dynamics of the system.
Smart Images

Figure CN120238581A_ABST
Abstract
Description
Technical Field
[0001] The present application relates to the field of communication technologies, and particularly to a protocol encoding method, a protocol decoding method, a computer program product, and a device. Background Art
[0002] In a platform capable of accessing multiple devices, the protocol encoding and decoding module, as a core component, is responsible for encoding and decoding device communication protocols and providing a translation function for communication protocol messages. Currently, when encoding and decoding communication protocols, it is usually necessary to design corresponding encoding and decoding logics for each message type respectively. When docking different communication protocols, a large amount of time and resources are often required for development and testing. Moreover, different protocol encoding and decoding methods are difficult to reuse, resulting in the need to start development from scratch every time a new protocol is docked, greatly reducing the development efficiency. There are significant differences in efficiency and quality among different developers when writing encoding and decoding logics, further affecting the overall development efficiency and stability of the project.
[0003] In summary, in the face of the need to access multiple communication protocols, how to improve code reusability and development efficiency and reduce development costs are problems that need to be solved currently. Summary of the Invention
[0004] In view of this, the purpose of the present application is to provide a protocol encoding method, a protocol decoding method, a computer program product, and a device, which can improve code reusability and development efficiency, thereby reducing development costs. The specific solutions are as follows:
[0005] In the first aspect, the present application discloses a protocol encoding method, including:
[0006] Obtain a first structure corresponding to a communication protocol frame; wherein, the first structure is a structure defined based on the communication protocol frame, the first structure includes protocol fields of the communication protocol frame and data types of the protocol fields, and the protocol fields include field values;
[0007] Obtain the data type of a target protocol field based on the reflection mechanism, where the target protocol field is a protocol field to be encoded among the protocol fields;
[0008] Encode the field value of the target protocol field using the encoding method corresponding to the data type to obtain a byte stream.
[0009] Optionally, the first structure further includes tag information of the protocol field, and the method further includes:
[0010] Judge whether the protocol field is the target protocol field based on the tag information.
[0011] Optionally, encoding the field value of the target protocol field by using the encoding method corresponding to the data type includes:
[0012] When the data type is a target type, obtain the target information corresponding to the data type in the tag information;
[0013] Based on the target information, and encode the field value of the target protocol field by using the encoding method corresponding to the data type.
[0014] Optionally, obtaining the target information corresponding to the data type in the tag information includes:
[0015] When the data type is a string type, obtain the string length information from the tag information to obtain the target information;
[0016] Correspondingly, encoding the field value of the target protocol field based on the target information and using the encoding method corresponding to the data type includes:
[0017] If the string length information is the string length, then encode the field value of the target protocol field according to the string length and using the encoding method corresponding to the string type;
[0018] If the string length information is the field name of the protocol field storing the string length, then obtain the string length based on the field name, and encode the field value of the target protocol field according to the string length and using the encoding method corresponding to the string type.
[0019] Optionally, obtaining the string length based on the field name includes:
[0020] Search for the field value corresponding to the field name in the protocol fields cached in the context object to obtain the string length.
[0021] Optionally, encoding the field value of the target protocol field by using the encoding method corresponding to the data type includes:
[0022] When the data type is an integer, perform integer encoding on the field value of the target protocol field based on a preset byte order.
[0023] Optionally, the communication protocol frame is a protocol frame of a charging protocol.
[0024] In a second aspect, the present application discloses a protocol decoding method, including:
[0025] Obtain a second structure corresponding to the communication protocol frame and the target byte stream; wherein, the second structure is a structure defined based on the communication protocol frame, the second structure includes the protocol fields of the communication protocol frame and the data types of the protocol fields, and the target byte stream is a byte stream composed of byte streams corresponding to each target protocol field, and the target protocol field is a field to be decoded in the protocol fields;
[0026] Obtain the data type of the target protocol field based on the reflection mechanism;
[0027] Decode the byte stream corresponding to the target protocol field by using the decoding method corresponding to the data type to obtain the field value of the target protocol field.
[0028] In a third aspect, the present application discloses a protocol encoding device, including:
[0029] A structure obtaining module, configured to obtain a first structure corresponding to a communication protocol frame; wherein, the first structure is a structure defined based on the communication protocol frame, the first structure includes the protocol fields of the communication protocol frame and the data types of the protocol fields, and the protocol fields include field values;
[0030] A data type obtaining module, configured to obtain the data type of a target protocol field based on the reflection mechanism, wherein the target protocol field is a field to be encoded in the protocol fields;
[0031] A field encoding module, configured to encode the field value of the target protocol field by using the encoding method corresponding to the data type to obtain a byte stream.
[0032] In a fourth aspect, the present application discloses a computer program product, which when executed implements the foregoing protocol encoding method and / or protocol decoding method.
[0033] In a fifth aspect, the present application discloses an electronic device, including a memory and a processor, wherein:
[0034] The memory is used to store a computer program;
[0035] The processor is configured to execute the computer program to implement the foregoing protocol encoding method and / or the foregoing protocol decoding method.
[0036] As can be seen from the above solution, the present invention provides a protocol encoding method, including: obtaining a first structure corresponding to a communication protocol frame; wherein, the first structure is a structure defined based on the communication protocol frame, and the first structure includes protocol fields of the communication protocol frame and data types of the protocol fields, and the protocol fields include field values; obtaining the data type of a target protocol field based on the reflection mechanism, wherein the target protocol field is a field to be encoded in the protocol fields; encoding the field value of the target protocol field by using the encoding method corresponding to the data type to obtain a byte stream.
[0037] It can be seen that the beneficial effects of the present application are as follows: Based on the reflection mechanism, automatically obtain the data type of the target protocol field in the structure corresponding to the communication protocol frame, and use the encoding method corresponding to the data type to encode the field value of the target protocol field. In this way, developers only need to define the structure corresponding to the communication protocol frame, and the reflection mechanism can automatically identify the data type corresponding to the field to be encoded, and then automatically match the corresponding encoding method for encoding. The corresponding code can be reused for different protocol formats, simplifying the development process, improving code reusability and development efficiency, and thus reducing development costs.
[0038] Correspondingly, a protocol decoding method, a computer program product, and a device provided by the present application also have the above technical effects. Description of the Drawings
[0039] In order to more clearly illustrate the technical solutions in the embodiments of the present application or the prior art, the following will briefly introduce the drawings required for use in the description of the embodiments or the prior art. Obviously, the drawings in the following description are only the embodiments of the present application. For those of ordinary skill in the art, other drawings can be obtained according to the provided drawings without creative efforts.
[0040] Figure 1 It is a flowchart of a protocol encoding method provided by an embodiment of the present application;
[0041] Figure 2 It is a schematic diagram of a frame type in a charging protocol provided by an embodiment of the present application;
[0042] Figure 3 It is a flowchart of a protocol decoding method provided by an embodiment of the present application;
[0043] Figure 4 It is a schematic diagram of a protocol encoding provided by an embodiment of the present application;
[0044] Figure 5 It is a schematic diagram of a protocol decoding provided by an embodiment of the present application;
[0045] Figure 6 Schematic structural diagram of a protocol encoding device provided by an embodiment of the present application;
[0046] Figure 7 Structural diagram of an electronic device provided by an embodiment of the present application. Specific implementation manners
[0047] Next, the technical solutions in the embodiments of the present application will be clearly and completely described in conjunction with the accompanying drawings in the embodiments of the present application. Obviously, the described embodiments are only a part of the embodiments of the present application, rather than all the embodiments. All other embodiments obtained by those of ordinary skill in the art based on the embodiments of the present application without creative efforts shall fall within the protection scope of the present application.
[0048] Currently, in a platform that can access multiple devices, in the case of facing the need to access multiple communication protocols, how to improve code reusability and development efficiency and reduce development costs are the problems that need to be solved currently.
[0049] For example, when the device is a charging device and in the scenario where a third-party charging device accesses the charging network platform, the protocol encoding and decoding module, as a core component, plays a crucial role and is used to be responsible for the encoding and decoding of the communication protocol of the third-party charging device and provide the translation function of the communication protocol message. However, due to its inherent limitations, the traditional charging protocol faces many technical challenges. The traditional charging protocol generally uses a binary protocol for data transmission. Although this method has certain advantages in data transmission efficiency, it has the following disadvantages: Poor readability: The structure of the binary protocol is complex and lacks intuitive readability, making protocol debugging and maintenance difficult. High development cost: Each message type (i.e., the frame type in the communication protocol) requires corresponding encoding and decoding logics. When docking different third-party communication protocols, it often takes a lot of time and resources for development and testing. Low reusability: Different protocol encoding and decoding methods are difficult to reuse, resulting in the need to develop from scratch every time a new protocol is docked, greatly reducing the development efficiency. Inconsistent efficiency: When different developers write encoding and decoding logics, there are significant differences in efficiency and quality, further affecting the overall development efficiency and stability of the project.
[0050] Therefore, the present application provides a protocol encoding method, a protocol decoding method, a computer program product, and a device, which can improve code reusability and development efficiency, thereby reducing development costs.
[0051] Next, the terms involved in the present application will be explained first:
[0052] Serialization: Converting a data structure or object into a format that can be stored or transmitted. Encoding is to serialize the structure fields (variables) into a byte stream.
[0053] Deserialization: Restoring the stored or transmitted data format to a data structure or object. Decoding is to deserialize the byte stream into the respective fields in the structure.
[0054] Context object: Used to store and manage the information and status required during the serialization / deserialization process.
[0055] tag (tag information): A marker string used to specify some additional information in the structure.
[0056] Cache: A storage mechanism used to quickly access data that has been calculated or obtained, avoiding repeated calculation or acquisition.
[0057] Expression: In code, it usually refers to a calculation process that returns a value based on given conditions or inputs.
[0058] See Figure 1 As shown, an embodiment of the present application discloses a protocol encoding method, including:
[0059] Step S11: Obtain a first structure corresponding to the communication protocol frame; wherein, the first structure is a structure defined based on the communication protocol frame, and the first structure includes the protocol fields of the communication protocol frame and the data types of the protocol fields, and the protocol fields contain field values.
[0060] In this embodiment, the communication protocol may include, but is not limited to, a charging protocol, that is, the communication protocol frame includes, but is not limited to, the protocol frame of the charging protocol. The communication protocol may include multiple frame types, and each frame type contains several fields and corresponding data types. In this embodiment, each frame type may correspond to a structure in a programming language, and each field of the frame type corresponds to a variable of the structure, that is, a field of the structure. See Figure 2 As shown, Figure 2 is a schematic diagram of a frame type in the charging protocol provided by an embodiment of the present application. The frame type is a login authentication response, which is used to reply to the charging pile login result. Figure 2Among them, there are two fields to be encoded in the 0x02 frame, corresponding to different data types respectively. When mapped to the code level, each protocol frame type corresponds to a structure. The protocol fields and data types correspond to variables and variable types in the structure, and the protocol field value is the variable value. For example, if the data type defined in the protocol is a single-byte BIN code, the corresponding variable definition in the program is byte, and byte is a basic type provided by the programming language. The programming language may include but is not limited to the Go language.
[0061] Step S12: Obtain the data type of the target protocol field based on the reflection mechanism, where the target protocol field is the field to be encoded in the protocol field.
[0062] Among them, the reflection mechanism refers to an ability of a program to access, detect, and modify the state or behavior of the program itself, allowing the program to inspect or modify its own structure (such as classes, interfaces, fields, and methods, etc.) and behavior at runtime, so as to implement a series of dynamic functions.
[0063] In this embodiment, the data type of the field to be encoded in the first structure can be obtained based on the reflection mechanism. In an optional implementation manner, the first structure further includes the tag information of the protocol field, and the method further includes: determining whether the protocol field is the target protocol field based on the tag information. That is, in this embodiment, according to the communication protocol frame, by adding tag information (i.e., tag), the protocol field that needs to be encoded can be specified.
[0064] In this embodiment, the tag information of the protocol field can be parsed and the parsed tag information can be stored in the context object. In this embodiment, the tag information can be parsed based on the reflection mechanism to obtain the custom tag information. Further, based on the tag information, it is detected in turn whether the fields in the first structure are fields to be encoded. Specifically, it can be checked whether the fields in the first structure are enabled based on the enable tag (i.e., the enable label) in the tag information. If the enable tag exists and is not empty, the expression in the enable tag is parsed, and it is determined whether the field is enabled based on the expression. If the expression is valid, the field is enabled, otherwise the field is not enabled. If the enable tag is empty, the field is default enabled, and if the field is enabled, it is the target protocol field.
[0065] Step S13: Encode the field value of the target protocol field using the encoding method corresponding to the data type to obtain a byte stream.
[0066] In this embodiment, when the data type is the target type, the target information corresponding to the data type in the tag information is obtained; based on the target information, the field value of the target protocol field is encoded by using the encoding method corresponding to the data type. The target type includes, but is not limited to, string type, floating-point type, etc. That is to say, in this embodiment, corresponding target information can be created in the tag information for different data types, and the target information is the information required for encoding or decoding the corresponding data type.
[0067] In an alternative embodiment, when the data type is the string type, the string length information is obtained from the tag information to obtain the target information; correspondingly, the encoding of the field value of the target protocol field based on the target information and using the encoding method corresponding to the data type includes: if the string length information is the string length, then according to the string length, the field value of the target protocol field is encoded by using the encoding method corresponding to the string type; if the string length information is the field name of the protocol field storing the string length, then the string length is obtained based on the field name, and according to the string length, the field value of the target protocol field is encoded by using the encoding method corresponding to the string type.
[0068] It can be understood that the string length information is the information used to determine the string length, which can be the string length itself or the field name storing the string length. The tag information can be stored in the form of key-value pairs, where the key is the information identifier and the value is the information value. For example, for the information identifier (long) corresponding to the string length, the information value is the string length, and the string length is an integer. In this embodiment, it can first be checked whether the corresponding value of the string length found in the tag information is an integer. If it is an integer, then based on this integer, the field value of the target protocol field is encoded by using the encoding method corresponding to the string type. If it is not an integer, indicating that the value is the field name, then the field value corresponding to this value is searched in the first structure. If it is an integer, then based on this integer, the field value of the target protocol field is encoded by using the encoding method corresponding to the string type, otherwise an error is returned.
[0069] In an alternative embodiment, the field value corresponding to the field name can be searched from the protocol fields cached in the context object to obtain the string length. That is to say, in this embodiment, the protocol fields can be cached through the context object. After obtaining the data type of the target protocol field based on the reflection mechanism, the protocol field and the data type of the field can be cached in the context object.
[0070] In this embodiment, when the data type is a floating-point number, the precision is obtained from the tag information, and based on the endianness and the precision, the field value of the target protocol field is encoded using the encoding method corresponding to the floating-point number. It can be understood that for other basic data types, the information required in the corresponding encoding process can also be defined in the tag information, and the corresponding information can be obtained according to the data type for encoding. If the data type is a custom type, a custom encoding method can be called to encode the field value of the target protocol field.
[0071] In this embodiment, when the data type is an integer, the field value of the target protocol field is encoded as an integer based on a preset endianness. The preset endianness can be the endianness set based on the communication protocol frame. The endianness can be understood as the arrangement order of each byte when storing a multi-byte data type in computer memory, including big-endian and little-endian, etc., which are defined in the communication protocol frame.
[0072] It can be understood that each field defining the frame type of the protocol will contain some information, and this information is mapped to the program through tags. For example, the byte length, data precision, field enabling time, etc. of each field in the protocol. When the program defines the structure according to the communication protocol, tag information is added, and it is obtained through reflection during runtime. The field lengths of some field types in the program are uncertain. For example, for the string type, it may occupy n bytes. The length needs to be defined in the tag according to the protocol, and this length can be obtained from the tag during serialization and deserialization to proceed normally. For example, protocol frame A has two fields, a and b. Field a is fixed at 2 bytes and represents a numerical value, and the length of field b is determined by the value represented by field a. Corresponding to the program, the tag information cannot be a fixed value but should be the value of variable a.
[0073] In an alternative embodiment, this embodiment can also allocate memory space. When the byte stream corresponding to any of the target protocol fields is obtained, it is stored in the memory space. When the encoding of all the fields to be encoded corresponding to the communication protocol frame is completed and a complete byte stream is obtained, the complete byte stream is transmitted to the corresponding device, such as a charging device.
[0074] It should be noted that by pre-allocating memory space, when the byte stream corresponding to any target protocol field is obtained, it is stored in the pre-allocated memory space, without the need to allocate memory space each time a byte stream is obtained, thereby improving the encoding efficiency of the communication protocol frame.
[0075] In an alternative embodiment, the context object of this embodiment can store the reflection information and byte order of variables, and can also include a pre-allocated memory space. Among them, the reflection information includes the data type of the protocol field, the field value, and the label information. The pre-allocated memory space serves as a cache required during the intermediate process of encoding and decoding when the program is running. After obtaining the first structure, a context object can be created, the memory space can be allocated, the byte order can be determined, the label information can be parsed, and then stored in the context object.
[0076] Furthermore, in this embodiment, the first structure may further include a hook method, and the corresponding hook method can be executed. For example, the hook method can be a method for generating a check code.
[0077] It can be seen that the embodiment of this application, based on the reflection mechanism, automatically obtains the data type of the target protocol field in the structure corresponding to the communication protocol frame, and encodes the field value of the target protocol field using the encoding method corresponding to the data type. In this way, developers only need to define the structure corresponding to the communication protocol frame. Through the reflection mechanism, the data type corresponding to the field to be encoded can be automatically recognized, and then the corresponding encoding method can be automatically matched for encoding. The corresponding code can be reused for different protocol formats, simplifying the development process, improving the code reusability and development efficiency, and thus reducing the development cost.
[0078] In this way, based on the reflection mechanism, dynamic parsing and generation of protocol messages are allowed, making the protocol structure more intuitive and easy to understand, with high readability; through the reflection mechanism, the encoding and decoding logic can be automatically performed, greatly improving the code reusability and development efficiency, with high reusability; the standardized encoding and decoding process ensures the consistency of the code quality and efficiency written by different developers, reducing the development quality fluctuations caused by personnel differences. Using the reflection mechanism to automatically encode and decode not only improves the code reusability but also enhances the flexibility and dynamics of the system, allowing the system to more efficiently adapt to constantly changing protocol formats.
[0079] Furthermore, referring to Figure 3 as shown, this embodiment discloses a protocol decoding method, including:
[0080] Step S21: Obtain the second structure corresponding to the communication protocol frame and the target byte stream; among them, the second structure is a structure defined based on the communication protocol frame, the second structure includes the protocol fields of the communication protocol frame and the data types of the protocol fields, the target byte stream is a byte stream composed of the byte streams corresponding to each target protocol field respectively, and the target protocol field is the field to be decoded in the protocol fields.
[0081] In an alternative embodiment, the communication protocol frame can be a protocol frame of a charging protocol.
[0082] Among them, the second structure further includes the tag information of the protocol field, and in this embodiment, it can be determined whether the protocol field is the target protocol field based on the tag information.
[0083] Step S22: Obtain the data type of the target protocol field based on the reflection mechanism.
[0084] Step S23: Decode the byte stream corresponding to the target protocol field by using the decoding method corresponding to the data type to obtain the field value of the target protocol field.
[0085] In this embodiment, when the data type is the target type, the target information corresponding to the data type in the tag information is obtained; based on the target information, the field value of the target protocol field is decoded by using the decoding method corresponding to the data type.
[0086] Among them, when the data type is a string type, the string length information is obtained from the tag information to obtain the target information; correspondingly, if the string length information is the string length, then according to the string length, the field value of the target protocol field is decoded by using the decoding method corresponding to the string type; if the string length information is the field name of the protocol field storing the string length, then the string length is obtained based on the field name, and according to the string length, the field value of the target protocol field is decoded by using the decoding method corresponding to the string type. Specifically, the field value corresponding to the field name can be found from the protocol fields cached in the context object to obtain the string length. When the data type is an integer, the integer decoding is performed on the field value of the target protocol field based on the preset byte order.
[0087] It can be seen that in the embodiment of the present application, based on the reflection mechanism, the data type of the target protocol field in the structure corresponding to the communication protocol frame is automatically obtained, and the field value of the target protocol field is decoded by using the decoding method corresponding to the data type. In this way, the developer only needs to define the structure corresponding to the communication protocol frame, and the data type corresponding to the field to be decoded can be automatically identified through the reflection mechanism, and then the corresponding decoding method is automatically matched for decoding. The corresponding code can be reused for different protocol formats, which simplifies the development process, improves the code reusability and development efficiency, and thus reduces the development cost.
[0088] Taking the charging network platform as an example, the encoding and decoding schemes provided by this application will be further elaborated. The charging network platform can access charging devices that adopt multiple communication protocols. The platform and the devices interact through messages (i.e., message data), and each message is transmitted in the form of a byte sequence, while the communication protocol specifies how the byte sequence should be organized. According to the frame type of the communication protocol, a structure and several fields (variables) and tags within the structure are defined. When the platform sends a message to the device, each field of the structure corresponding to the frame type is converted into a byte sequence according to the definition, and finally combined into a complete byte sequence, that is, a byte stream, and sent to the device. When the device sends a message to the platform, the data form is a byte sequence, and the message adopts one of the frame types in the protocol. The platform obtains the byte sequence and, based on the structure within the program corresponding to the frame type, parses out the values from the byte sequence and fills them into each field of the structure. An encoding tool and a decoding tool, or an encoding / decoding tool, can be integrated in the platform, that is, encoding and decoding are performed through one tool. A general encoding / decoding tool can be implemented using a Go language program, which can be applicable to charging devices of any manufacturer, without the need to separately write encoding and decoding programs according to the differences in device communication protocols, identify the data types corresponding to the protocol fields in the structure, match the corresponding encoding and decoding logics, and then run and output a byte stream or obtain field values.
[0089] See Figure 4 as shown Figure 4A schematic diagram of protocol encoding provided by this embodiment. In the tool, the input structure A is used to create a context, that is, to initialize the context (NewContext), and a new context object is created. Memory space is allocated, the byte order is determined, the tag in A is parsed through reflection to obtain custom information, and the tag string in the structure is parsed. If the tag string is not empty, it is cached in the context object; otherwise, the next step is continued. Further, the fields in the structure A are input in sequence, and it is checked whether the fields are available according to the tag. The enable tag (enable label) of the structure field can be checked. If the enable tag exists and is not empty, the expression in the enable tag is parsed and it is judged whether the field is enabled. If the expression is valid, the field is enabled; otherwise, the field is not enabled. If the enable tag is empty, the field is enabled by default. It should be noted that some communication protocol fields need to be serialized under certain conditions. For example, when the field a in a certain frame is not 1, the field b is valid. The expression can handle this scenario. If a certain field does not meet the condition, the field is skipped during serialization. Further, the data type of the variable is obtained through reflection, and the encoding method is selected. After obtaining the data type of the variable, the variable and its data type can also be stored in the context. The context stores the reflection information (type, value, tag) of the variable, the pre-allocated memory space (which is required for caching during the encoding and decoding intermediate process during program operation), and the byte order (big endian / little endian, defined in the protocol). The encoding method can include integers, strings, floating-point numbers, and other basic types, as well as custom encoding methods. When the data type is a string, the integer value corresponding to the key can be first tried to be found from the tag. If found, it is returned. If not found, the corresponding field value in the structure is searched. If the field value is an integer, the value is returned; otherwise, an error is returned. In this embodiment, after obtaining the byte stream of any field, it is written into the memory, that is, the memory space is pre-allocated. After obtaining the byte streams of all fields to be encoded, if there is a hook method, the custom hook method is executed.
[0090] See Figure 5 as shown in Figure 5 A schematic diagram of protocol decoding provided by this embodiment. What is input into the tool is the byte stream and the structure A. After obtaining the data type, decoding is performed according to the corresponding decoding method, and the byte stream is decoded into field values.
[0091] In this embodiment, encoding is the serialization process. The input is the variables (fields) in the structure, and the output is a byte stream. Decoding is the deserialization process. The input is a byte sequence and a structure with no values, and the output is a structure with values filled in. During serialization, the Dumps function can be called to serialize the object, and the DumpsValue function processes the specific serialization logic. If the value is a non-null pointer, the object is serialized; otherwise, an error is returned. During deserialization, the Loads function can be called to deserialize the data, and the LoadsValue function processes the specific deserialization logic. If the data is valid, deserialization is performed; otherwise, an error is returned.
[0092] In this way, different variable types in the program correspond to different encoding and decoding methods. Throwing the entire structure into the processing program, this program can automatically identify the variable types of each field, thereby automatically matching the encoding and decoding methods. This simplifies the development and maintenance of the encoding and decoding logic. There is no need to write specific encoding and decoding methods for each protocol. These logics can be automatically generated through the reflection mechanism, thus greatly simplifying the development and maintenance process. Developers only need to define the protocol data model, and the rest of the encoding and decoding work is automatically processed by the reflection mechanism, reducing human errors and cumbersome work in the encoding and decoding processes. It improves the flexibility and dynamics of protocol parsing. By introducing the reflection mechanism, the protocol structure can be dynamically parsed without pre-defining all possible protocol formats. It can more flexibly adapt to various different protocol formats, especially when facing frequently changing third-party device protocols. It enhances the code reusability and development efficiency. Since the reflection mechanism can automatically parse the protocol data at runtime, it reduces the need to write new encoding and decoding logics every time a new protocol is introduced. This method improves the code reusability, shortens the development cycle, and thus enhances the overall development efficiency.
[0093] See Figure 6 As shown, this embodiment discloses a protocol encoding device, including:
[0094] A structure acquisition module 11, configured to acquire a first structure corresponding to a communication protocol frame; wherein, the first structure is a structure defined based on the communication protocol frame, and the first structure includes protocol fields of the communication protocol frame and data types of the protocol fields, and the protocol fields include field values;
[0095] A data type acquisition module 12, configured to acquire the data type of a target protocol field based on the reflection mechanism, where the target protocol field is a field to be encoded in the protocol fields;
[0096] A field encoding module 13, configured to encode the field value of the target protocol field by using the encoding method corresponding to the data type to obtain a byte stream.
[0097] Wherein, the device further includes a target protocol field determination module, configured to determine whether the protocol field is the target protocol field based on the tag information when the first structure further includes the tag information of the protocol field.
[0098] Further, the field encoding module 13 may specifically include:
[0099] A target information acquisition sub-module, configured to acquire the target information corresponding to the data type in the tag information when the data type is the target type;
[0100] An encoding sub-module, configured to encode the field value of the target protocol field based on the target information and using the encoding method corresponding to the data type.
[0101] Wherein, the target information acquisition sub-module is specifically configured to, when the data type is the string type, acquire the string length information from the tag information to obtain the target information;
[0102] Correspondingly, the encoding sub-module is specifically configured to:
[0103] If the string length information is the string length, then encode the field value of the target protocol field according to the string length and using the encoding method corresponding to the string type;
[0104] If the string length information is the field name of the protocol field storing the string length, then obtain the string length based on the field name, and encode the field value of the target protocol field according to the string length and using the encoding method corresponding to the string type.
[0105] Wherein, the encoding sub-module may specifically be configured to: find the field value corresponding to the field name from the protocol fields cached in the context object to obtain the string length.
[0106] The field encoding module 13 may also be configured to: when the data type is an integer, perform integer encoding on the field value of the target protocol field based on a preset byte order.
[0107] Wherein, the communication protocol frame may be a protocol frame of a charging protocol.
[0108] It can be seen that based on the reflection mechanism, the embodiment of the present application automatically obtains the data type of the target protocol field in the structure corresponding to the communication protocol frame, and encodes the field value of the target protocol field using the encoding method corresponding to the data type. In this way, developers only need to define the structure corresponding to the communication protocol frame. Through the reflection mechanism, the data type corresponding to the field to be encoded can be automatically identified, and then the corresponding encoding method can be automatically matched for encoding. The corresponding code can be reused for different protocol formats, simplifying the development process, improving code reusability and development efficiency, and thus reducing the development cost.
[0109] Further, this embodiment discloses a protocol decoding device, including:
[0110] A structure and byte stream acquisition module, configured to acquire a second structure corresponding to a communication protocol frame and a target byte stream; wherein, the second structure is a structure defined based on the communication protocol frame, the second structure includes the protocol fields of the communication protocol frame and the data types of the protocol fields, the target byte stream is a byte stream composed of byte streams corresponding to each target protocol field respectively, and the target protocol field is the field to be decoded in the protocol fields;
[0111] A data type acquisition module, configured to acquire the data type of the target protocol field based on the reflection mechanism;
[0112] A decoding module, configured to decode the byte stream corresponding to the target protocol field using the decoding method corresponding to the data type to obtain the field value of the target protocol field.
[0113] It can be seen that based on the reflection mechanism, the embodiment of the present application automatically obtains the data type of the target protocol field in the structure corresponding to the communication protocol frame, and decodes the field value of the target protocol field using the decoding method corresponding to the data type. In this way, developers only need to define the structure corresponding to the communication protocol frame. Through the reflection mechanism, the data type corresponding to the field to be decoded can be automatically identified, and then the corresponding decoding method can be automatically matched for decoding. The corresponding code can be reused for different protocol formats, simplifying the development process, improving code reusability and development efficiency, and thus reducing the development cost.
[0114] Further, the embodiment of the present application discloses a computer program product, which when executed implements the protocol encoding method and / or protocol decoding method disclosed in the foregoing embodiments.
[0115] For the specific processes of the foregoing protocol encoding method and / or protocol decoding method, reference can be made to the corresponding content disclosed in the foregoing embodiments, and details will not be elaborated herein.
[0116] See Figure 7As shown in the figure, an embodiment of the present application discloses an electronic device 20, including a processor 21 and a memory 22; wherein, the memory 22 is used to store a computer program; the processor 21 is used to execute the computer program, that is, the protocol encoding method and / or protocol decoding method disclosed in the foregoing embodiment.
[0117] For the specific process of the above protocol encoding method and / or protocol decoding method, reference can be made to the corresponding content disclosed in the foregoing embodiment, and details will not be elaborated herein.
[0118] Moreover, as a carrier for resource storage, the memory 22 can be a read-only memory, a random access memory, a magnetic disk, an optical disk, etc., and the storage method can be temporary storage or permanent storage.
[0119] In addition, the electronic device 20 further includes a power supply 23, a communication interface 24, an input / output interface 25, and a communication bus 26; wherein, the power supply 23 is used to provide operating voltage for each hardware device on the electronic device 20; the communication interface 24 can create a data transmission channel between the electronic device 20 and external devices, and the communication protocol it follows can be any communication protocol applicable to the technical solution of the present application, and specific limitations are not imposed herein; the input / output interface 25 is used to obtain external input data or output data to the outside, and its specific interface type can be selected according to specific application requirements, and specific limitations are not imposed herein.
[0120] Furthermore, an embodiment of the present application also discloses a computer-readable storage medium for storing a computer program, wherein the computer program, when executed by a processor, implements the protocol encoding method and / or protocol decoding method disclosed in the foregoing embodiment.
[0121] For the specific process of the above protocol encoding method and / or protocol decoding method, reference can be made to the corresponding content disclosed in the foregoing embodiment, and details will not be elaborated herein.
[0122] In this specification, each embodiment is described in a progressive manner. The key point of each embodiment is the difference from other embodiments. The same or similar parts among the embodiments can be referred to each other. For the device disclosed in the embodiment, since it corresponds to the method disclosed in the embodiment, the description is relatively simple, and the relevant parts can be referred to the description of the method part.
[0123] The steps of the methods or algorithms described in connection with the embodiments disclosed herein may be implemented directly in hardware, in software modules executed by a processor, or in a combination thereof. The software modules may be located in random access memory (RAM), memory, read-only memory (ROM), electrically programmable ROM, electrically erasable programmable ROM, registers, hard disk, removable disk, CD-ROM, or any other form of storage medium well known in the art.
[0124] The above has introduced in detail the protocol encoding method, protocol decoding method, computer program product and device provided by the present application. Specific examples are used herein to elaborate on the principles and implementation manners of the present application. The description of the above embodiments is only used to help understand the method and its core idea of the present application; 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 manners and application scopes. In summary, the content of this specification should not be construed as a limitation to the present application.
Claims
1. A protocol encoding method, characterized in that: include: Obtain a first structure corresponding to a communication protocol frame; wherein the first structure is a structure defined based on the communication protocol frame, the first structure includes a protocol field of the communication protocol frame and a data type of the protocol field, and the protocol field includes a field value; Acquire a data type of a target protocol field based on a reflection mechanism, wherein the target protocol field is a field to be encoded in the protocol field; The field value of the target protocol field is encoded using the encoding method corresponding to the data type to obtain a byte stream.
2. The protocol encoding method according to claim 1, characterized in that: The first structure also includes label information of the protocol field, and the method further includes: It is determined whether the protocol field is the target protocol field based on the tag information.
3. The protocol encoding method according to claim 2, characterized in that: The encoding of the field value of the target protocol field by using the encoding method corresponding to the data type includes: When the data type is a target type, obtaining target information corresponding to the data type in the tag information; Based on the target information, the field value of the target protocol field is encoded using an encoding method corresponding to the data type.
4. The protocol encoding method according to claim 3, characterized in that: The acquiring target information corresponding to the data type in the tag information includes: When the data type is a string type, the string length information is obtained from the tag information to obtain the target information; Correspondingly, encoding the field value of the target protocol field based on the target information and using the encoding method corresponding to the data type includes: If the character string length information is a character string length, encoding the field value of the target protocol field according to the character string length and using an encoding method corresponding to the character string type; If the string length information is the field name of the protocol field storing the string length, the string length is obtained based on the field name, and the field value of the target protocol field is encoded according to the string length and using the encoding method corresponding to the string type.
5. The protocol encoding method according to claim 4, characterized in that: The obtaining the string length based on the field name includes: The field value corresponding to the field name is searched from the protocol field cached in the context object to obtain the string length.
6. The protocol encoding method according to claim 1, characterized in that: The encoding of the field value of the target protocol field by using the encoding method corresponding to the data type includes: When the data type is an integer, the field value of the target protocol field is integer-encoded based on a preset byte order.
7. The protocol coding method according to any one of claims 1 to 6, characterized in that: The communication protocol frame is a protocol frame of a charging protocol.
8. A protocol decoding method, characterized in that: include: Obtain a second structure and a target byte stream corresponding to a communication protocol frame; wherein the second structure is a structure defined based on the communication protocol frame, the second structure includes a protocol field of the communication protocol frame and a data type of the protocol field, the target byte stream is a byte stream composed of byte streams corresponding to each target protocol field, and the target protocol field is a field to be decoded in the protocol field; Acquire the data type of the target protocol field based on a reflection mechanism; The byte stream corresponding to the target protocol field is decoded using a decoding method corresponding to the data type to obtain a field value of the target protocol field.
9. A computer program product, which, when executed, implements the protocol encoding method according to any one of claims 1 to 7 and / or the protocol decoding method according to claim 8.
10. An electronic device, characterized in that: comprising a memory and a processor, wherein: The memory is used to store the computer program; The processor is used to execute the computer program to implement the protocol encoding method according to any one of claims 1 to 7 and / or the protocol decoding method according to claim 8.
Citation Information
Cited By
Message data serialization packaging and analysis method supporting interface protocol verification
CN121691508A