Packet assembly and depacketization method based on TCP custom data protocol
By customizing the packetization and unpacking method defined by the variable-length message body and ordered fields, the problem of low data flow control efficiency in the existing TCP protocol is solved, and the network transmission traffic is reduced and the packetization and unpacking performance is improved. Data security is protected at the same time.
Patent Information
- Application Number
- CN202111548359.9
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2021-12-16
- Publication Date
- 2024-08-23
- Estimated Expiration
- 2041-12-16
AI Technical Summary
The existing TCP network communication protocol is based on fixed-length or full-string data packets, resulting in low efficiency in data flow control, packetization and unpacking, and large resource consumption.
Customized variable-length message bodies are used, and packetization and unpacking are performed through ordered and defined basic data type fields to reduce network transmission traffic and improve performance efficiency.
It reduces network transmission traffic, improves packet and unpacking performance, protects data security, and avoids data leakage under unknown protocol definitions.
Smart Images

Figure CN114222008B
Abstract
Description
Technical Field
[0001] The invention belongs to the technical field of data protocols and relates to a packet assembly and depacketization method based on a TCP user-defined data protocol. Background Art
[0002] At present, many TCP (Transmission Control Protocol) network communication protocols are based on fixed-length custom messages or full-string data messages (XML, JSON). Although there are certain convenience advantages, there are obvious deficiencies in data flow control and data packet unpacking efficiency. Because XML and JSON are based on string documents, for example, a four-byte integer number 1000000000, binary data only occupies 4 bytes, while a text string may occupy 10 bytes. It is conceivable that expressing all protocol fields in text documents and expressing all protocol fields in binary can save at least half of the memory space. This reduces communication traffic and reduces server bandwidth pressure.
[0003] XML and JSON protocols first convert the contents of the buffer into text strings, and then parse them into data packets, so they consume more system resources. When assembling a package, the data packet must first be converted into an XML or JSON document. Then the document is written into the buffer, which has an extra conversion step, so it also consumes more system resources. Summary of the invention
[0004] To solve the above problems, the present invention adopts a customized variable-length message body, the data field is composed of basic data types, and the orderly field definition significantly improves the efficiency of packet assembly and unpacking. When unpacking, the buffer area is read in order once, and the data packet is parsed.
[0005] To achieve the above object, the technical solution of the present invention is a packet assembly method based on TCP custom data protocol, comprising the following steps:
[0006] S11, the application processing layer sends a message;
[0007] S12, write the message into the packet buffer area;
[0008] S13, write the packet buffer data into the TCP stream;
[0009] The message includes a message header and a message body, and the message body is a user-defined variable-length message body.
[0010] Preferably, the S12 specifically includes the following steps:
[0011] S121, creating a data buffer area for group packages;
[0012] S122, write the message header into the data buffer in order according to the definition;
[0013] S123: Write the message body into the data buffer area in sequence.
[0014] Preferably, the fields, types and descriptions of the message header include:
[0015] Protocol version identifier, byte, generally a fixed value;
[0016] Message sequence number, int, unique number of the message entity;
[0017] Business transaction number, short, different business processing corresponds to different numbers;
[0018] Message type number, short, number to distinguish different message types;
[0019] Extension flag, byte, 8 bits, each bit indicates whether a certain function is enabled;
[0020] Message body size, int, message body size.
[0021] Preferably, the message body defines fields in order, and the field types and descriptions include:
[0022] bool, single-byte Boolean;
[0023] byte, single-byte and signed integer;
[0024] short, two-byte signed integer;
[0025] int, four-byte signed integer;
[0026] long, eight-byte signed integer;
[0027] float, four-byte signed floating point number;
[0028] double, eight-byte signed floating point number;
[0029] string, a string with length, the encoding convention is UTF8, and the four bytes before the field content are the length;
[0030] byte[], data array with length, the four bytes before the field content are the length.
[0031] Preferably, before writing the content into the field string byte[] with length in the message body, the length needs to be written first, and then the specific field content is written.
[0032] Corresponding to the above-mentioned packet assembly method, the present invention also provides a method for unpacking based on a TCP custom data protocol, comprising the following steps:
[0033] S21, data obtained from the TCP stream is written into the depacketization buffer area;
[0034] S22, parsing the message from the depacketization buffer area;
[0035] S23, push the parsed message to the application processing layer.
[0036] Preferably, before S21, the step further includes creating an unpacked data buffer area.
[0037] Preferably, the S22 specifically includes the following steps:
[0038] S221, parsing the message header;
[0039] S222, determining whether there is a complete message in the depacketization buffer;
[0040] S223: If yes, parse the message body according to the message type in the message header.
[0041] Preferably, the determining whether there is a complete message in the depacketization buffer is specifically to determine whether the entire message can be parsed according to the message header and the actual buffer data.
[0042] Preferably, the message header has a fixed space and a fixed field definition.
[0043] The present invention has at least the following beneficial effects:
[0044] (1) Customized variable-length message body reduces the message size, thereby reducing network transmission traffic, saving an average of half the network traffic;
[0045] (2) Packet assembly and unpacking are done in sequence, and only the cache needs to be read once, which greatly improves the efficiency of packet assembly and unpacking performance;
[0046] (3) Protect data security and prevent third-party users from easily viewing it: The message body uses a custom binary protocol, and the data packet fields are ordered and of custom types. Each field has a strict boundary. It cannot be unpacked without knowing the protocol definition, and the user sees a piece of binary code. In the prior art, in the XML JSON protocol format, a third-party user intercepts a piece of cached data and converts it all into a document. If the document is not encrypted, the content is clear at a glance. BRIEF DESCRIPTION OF THE DRAWINGS
[0047] Figure 1 The present invention is a flowchart of the steps of the packet assembly and unpacking method based on the TCP custom data protocol according to an embodiment of the present invention;
[0048] Figure 2 It is a flow chart of step S12 of the packet assembly method based on the TCP custom data protocol according to a specific embodiment of the present invention;
[0049] Figure 3 The present invention is a flowchart of the steps of a method for unpacking a packet based on a TCP custom data protocol according to a specific embodiment of the present invention. DETAILED DESCRIPTION
[0050] In order to make the purpose, technical solution and advantages of the present invention more clearly understood, the present invention is further described in detail below in conjunction with the accompanying drawings and embodiments. It should be understood that the specific embodiments described herein are only used to explain the present invention and are not intended to limit the present invention.
[0051] On the contrary, the present invention covers any substitution, modification, equivalent method and scheme made on the essence and scope of the present invention as defined by the claims. Further, in order to make the public have a better understanding of the present invention, some specific details are described in detail in the detailed description of the present invention below. Those skilled in the art can fully understand the present invention without the description of these details.
[0052] See also Figure 1 , which is a flowchart of the steps of the technical solution of the present invention in an embodiment of the present invention, a method for assembling and unpacking a packet based on a TCP custom data protocol, wherein the method for assembling a packet comprises the following steps:
[0053] S11, the application processing layer sends a message;
[0054] S12, write the message into the packet buffer area;
[0055] S13, write the packet buffer data into the TCP stream;
[0056] The message includes a message header and a message body, and the message body is a user-defined variable-length message body.
[0057] See also Figure 2 , S12 specifically includes the following steps:
[0058] S121, creating a data buffer area for group packages;
[0059] S122, write the message header into the data buffer in order according to the definition;
[0060] S123: Write the message body into the data buffer area in sequence.
[0061] The fields, types, and descriptions of the message header include:
[0062] Protocol version identifier, byte, generally a fixed value;
[0063] Message sequence number, int, unique number of the message entity;
[0064] Business transaction number, short, different business processing corresponds to different numbers;
[0065] Message type number, short, number to distinguish different message types;
[0066] Extension flag, byte, 8 bits, each bit indicates whether a certain function is enabled;
[0067] Message body size, int, message body size.
[0068] The message body defines the fields in order. The field types and descriptions include:
[0069] bool, single-byte Boolean;
[0070] byte, single-byte and signed integer;
[0071] short, two-byte signed integer;
[0072] int, four-byte signed integer;
[0073] long, eight-byte signed integer;
[0074] float, four-byte signed floating point number;
[0075] double, eight-byte signed floating point number;
[0076] string, a string with length, the encoding convention is UTF8, and the four bytes before the field content are the length;
[0077] byte[], data array with length, the four bytes before the field content are the length.
[0078] Before writing the content of the string byte[] field with length in the message body, you need to write the length first, and then write the specific field content.
[0079] In a specific embodiment, the content of the login message is as shown in Table 1, where sequence numbers 1-6 are message headers, and sequence numbers 7 and 8 are message bodies.
[0080] Table 1 Login message
[0081]
[0082] Corresponding to the above-mentioned packet assembly method, the present invention also provides a method for unpacking based on a TCP custom data protocol, comprising the following steps:
[0083] S21, data obtained from the TCP stream is written into the depacketization buffer area;
[0084] S22, parsing the message from the depacketization buffer area;
[0085] S23, push the parsed message to the application processing layer.
[0086] Before S21, it also includes creating an unpacked data buffer area.
[0087] See also Figure 3 , S22 specifically includes the following steps:
[0088] S221, parsing the message header;
[0089] S222, determining whether there is a complete message in the depacketization buffer;
[0090] S223: If yes, parse the message body according to the message type in the message header and perform polling and unpacking.
[0091] Determine whether there is a complete message in the depacketization buffer, specifically, determine whether the entire message can be parsed based on the message header and the actual buffer data.
[0092] The present invention is better described by taking JAVA's Netty as the underlying communication framework as a specific embodiment. Netty is a java open source framework that provides an asynchronous, event-driven network application framework and tools for quickly developing high-performance, high-reliability network servers and client programs. It has been verified by hundreds of commercial / commercial projects, such as Hadoop (a distributed system infrastructure) RPC (remote procedure call) framework Avro (a data serialization system), RocketMQ (a queue model message middleware based on publish-subscribe), etc. In other words, Netty is a client-server programming framework based on NIO. Using Netty, a network application can be quickly and simply developed, such as a client-server application that implements a certain protocol, which simplifies and streamlines the programming and development process of network applications. Based on the Netty network application framework and tools to build a custom TCP protocol parser middleware, the parsing work of data of different custom TCP protocols can be dynamically applied, that is, the data is encoded and decoded. In addition, using the asynchronous event-driven characteristics of Netty, concurrent operations of 100,000, 1 million, or even higher device levels can be completed.
[0093] Packet assembly method: S121, after creating the data buffer area for packet assembly; S122, write the message header into the data buffer area in sequence according to the definition; S123, after the message header is written, continue to write the message body into the data buffer area in sequence, wherein the length of the field with length (string byte[]) is written before the content is written, and then the specific field content is written.
[0094] The implementation of packet encoding is to use Netty's MessageToByteEncoder as the base parent class to implement custom packet encoding. The whole process is to write the custom data message class into Netty's ByteBuf class. Netty's ByteBuf class manages the data buffer.
[0095] Unpacking method: after creating the unpacked data buffer area; S21, read the data from the TCP stream to the unpacking buffer area, S221 try to parse the message header from the unpacking buffer area, the message header format is fixed, after effectively getting the message header; S222, determine whether the unpacking buffer area has a complete message, if the buffer area has a complete message, S223, according to the message type number in the message header to specifically parse the message body. And polling unpacking processing.
[0096] The implementation of unpacking and decoding is to implement custom unpacking based on Netty's ByteToMessageDecoder as the basic parent class. The whole process is to try to parse the fixed message header first, and judge whether the entire message can be parsed based on the message header and the actual buffer data; if the condition for continuing parsing is met, the data in the data buffer is parsed into a message. The buffer data is provided by Netty's ByteBuf.
[0097] The above description is only a preferred embodiment of the present invention and is not intended to limit the present invention. Any modifications, equivalent substitutions and improvements made within the spirit and principles of the present invention should be included in the protection scope of the present invention.
Claims
1. A packet assembly method based on TCP custom data protocol, characterized in that: The following steps are involved: S11, the application processing layer sends a message; S12, write the message into the packet buffer area; S13, write the packet buffer data into the TCP stream; The message includes a message header and a message body, and the message body is a custom variable-length message body; the specific composition of the message includes: 0x02 indicates the protocol version identifier; 0x01 indicates the message sequence number; 0x01 indicates the business transaction number; 0x01 indicates the message body type number; 0x00 indicates the extension mark; 0x11 indicates the message body size; the first field of the message body indicates the user name, which occupies 7 bytes, and the 4-byte prefix indicates the length; the second field of the message body indicates the password, which occupies 10 bytes, and the 4-byte prefix indicates the length; The S12 specifically includes the following steps: S121, creating a data buffer area for group packages; S122, write the message header into the data buffer in order according to the definition; S123: Write the message body into the data buffer in order; The fields, types and descriptions of the message header include: Protocol version identifier, byte, generally a fixed value; Message sequence number, int, unique number of the message entity; Business transaction number, short, different business processing corresponds to different numbers; Message type number, short, number to distinguish different message types; Extension flag, byte, 8 bits, each bit indicates whether a certain function is enabled; Message body size, int, message body size; The message body defines fields in order, and the field types and descriptions include: bool, single-byte Boolean; byte, single-byte signed integer; short, two-byte signed integer; int, four-byte signed integer; long, eight-byte signed integer; float, four-byte signed floating point number; double, eight-byte signed floating point number; string, a string with length, the encoding convention is UTF8, and the four bytes before the field content are the length; byte[], data array with length, the four bytes before the field content are the length; Before writing the content of the field string byte[] with length in the message body, the length must be written first, and then the specific field content is written; The implementation of packet encoding is to implement custom packet encoding based on Netty's MessageToByteEncoder as the basic parent class. The whole process is to write the custom data message class into Netty's ByteBuf class, and the data buffer area is managed by Netty's ByteBuf class.
2. A method for unpacking based on a TCP custom data protocol corresponding to claim 1, characterized in that: The following steps are involved: S21, data obtained from the TCP stream is written into the depacketization buffer area; S22, parsing the message from the depacketization buffer area; S23, push the parsed message to the application processing layer; The step S21 also includes creating an unpacked data buffer area; The S22 specifically includes the following steps: S221, parsing the message header; S222, determining whether there is a complete message in the depacketization buffer; S223: if yes, parse the message body according to the message type in the message header; The determining whether there is a complete message in the depacketization buffer is specifically to determine whether the entire message can be parsed according to the message header and the actual buffer data; the space of the message header is fixed and the field definition is fixed; The implementation of unpacking and decoding is to implement custom unpacking based on Netty's ByteToMessageDecoder as the basic parent class; the whole process is to try to parse the fixed message header first, and judge whether the entire message can be parsed based on the message header and the actual buffer data; if the condition for continuing parsing is met, the data in the data buffer is parsed into a message, and the buffer data is provided by Netty's ByteBuf.
