Method and system for processing sticky packets and broken packets of multiple protocols
Through singleton threading and packet header detection, the problem of poor packet break processing complexity and poor scalability of multiple protocols is solved, packet integrity detection and splicing is realized, and system stability and observability are improved.
Patent Information
- Application Number
- CN202411210389.2
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- Filing Date
- 2024-08-30
- Publication Date
- 2024-12-10
AI Technical Summary
The prior art is difficult to effectively deal with the problem of sticking and packet breakage in multiple communication protocols, resulting in complex writing, prone to logical errors and poor scalability, affecting system stability.
The data is received through a singleton thread and the packet header is used to recursively detect the packet integrity, and the data of the broken packet sticky packet is stored in the cache to realize the integrity detection and splicing of the data packet.
It simplifies the process of sticky packet breaking of multiple protocols, improves code obsession and system stability, avoids problems such as thread jamming and memory overflow, and enhances scalability.
Smart Images

Figure CN119109828A_ABST
Abstract
Description
Technical Field
[0001] The present invention relates to the technical field of data processing, and in particular to a method and system for processing packet sticking and packet breaking of multiple protocols. Background Art
[0002] The current communication protocols are complicated because each enterprise has its own customized communication protocol, and the customized logic of each communication protocol is not exactly the same. Each program with similar overall logic but different specific details is written for each communication protocol. The repeated writing steps are tedious and boring. In addition, when writing the logic for processing packet sticking and packet breaking, it is easy to have logical problems that cause thread jams or logical error data accumulation that causes memory overflow, and even abnormal flashbacks and errors. Overly complex writing can easily lead to logical confusion when looking for problems later, and the code is not observable. However, if only one protocol is handled for packet sticking and packet breaking, the scalability of the problem is poor, and because a serial port or a communication port has multiple protocols, the logic will be affected, resulting in logical errors.
[0003] Therefore, a method is currently needed that can handle packet sticking and packet breaking for multiple protocols and is simple and efficient. Summary of the invention
[0004] To solve the above problems, the present invention provides a method and system for processing packet sticking and packet disconnection of multiple protocols, which recursively detects whether a single sent data packet is complete by matching the data packet header, thereby storing the data of the broken and stuck packets into a cache and detecting them, thereby solving the problem that the prior art can only process the broken and stuck packets of a single protocol and the process is complicated.
[0005] To achieve the above object, the present invention provides the following technical solutions:
[0006] A method for processing multiple protocols of packet sticking and packet disconnection, comprising the following steps:
[0007] S1. Receive data through a single thread and store each transmitted data in a data cache according to the queue;
[0008] S2. Using the packet header to detect whether there is a complete packet in the data cache according to the packet protocol rules, if so, then take out the complete packet into the parsing process, and leave the remaining data in the data cache. If not, then keep the data in the data cache;
[0009] S3. Repeat S1-S2 and continue to receive data through the single thread until the data transmission is completed.
[0010] Further, in step S2, the detecting whether there is a complete data packet in the data cache according to the data packet protocol rule by using the data packet header also includes: detecting whether there is an invalid byte in the data cache, and if so, deleting the invalid byte.
[0011] Furthermore, the method of using the data packet header to detect whether there is a complete data packet in the data cache according to the data packet protocol rules includes: constructing a data detection function, passing the data in the data cache into the data detection function, the data detection function detects the data, and generates a return value based on the detection result.
[0012] Furthermore, the data detection function detects the data, and its specific implementation method includes: detecting whether there is a data packet header in the data, and detecting the integrity of the data packet according to the packet length of the data packet corresponding to the data packet header until the data transmission is completed. When the length of the data packet header and its subsequent data bytes meets the packet length, the data packet is complete. If not, there is a broken packet or a stuck packet.
[0013] Furthermore, the detection of whether there is a data packet header in the data is specifically implemented as follows: performing a format search on the data starting from the data header, and sequentially searching whether there are data segments in the data. If the data segment format satisfies any data packet header format, then the data packet header exists in the data.
[0014] Furthermore, the taking out of the complete data packet and putting it into the parsing process is specifically implemented in the following manner: starting from the data packet header in the data, according to the packet length of the data packet corresponding to the data packet header, intercepting the data of the corresponding byte length, the data of the corresponding byte length is a complete data packet, taking out the complete data packet and putting it into the parsing process.
[0015] Furthermore, in step S1, the data is received through a single thread, specifically, data is received through TCP or UDP protocol.
[0016] Furthermore, it also includes: customizing the data packet structure, the data packet includes a data packet header, and detecting each received data according to the customized data packet structure to determine whether the data received this time includes the complete customized data packet.
[0017] Through the above technical scheme, the present invention has the following beneficial effects: using the data packet header format and the data in the data packet header to determine whether each segment of transmitted data is complete, determine whether the packet is stuck or broken, and store and splice incomplete data by queue through caching, thereby restoring the incomplete data. BRIEF DESCRIPTION OF THE DRAWINGS
[0018] Figure 1 The present invention is a schematic diagram of the overall flow of a method for processing packet sticking and packet disconnection of multiple protocols.
[0019] Figure 2 The present invention is a schematic diagram of the structure of a packet sticking and breaking system for processing multiple protocols in an embodiment of the present invention. DETAILED DESCRIPTION
[0020] The following will be combined with the drawings in the embodiments of the present invention to clearly and completely describe the technical solutions in the embodiments of the present invention. Obviously, the described embodiments are only part of the embodiments of the present invention, not all of the embodiments. Based on the embodiments of the present invention, all other embodiments obtained by ordinary technicians in this field without creative work are within the scope of protection of the present invention.
[0021] In order to make the above-mentioned objects, features and advantages of the present invention more obvious and easy to understand, the present invention is further described in detail below with reference to the accompanying drawings and specific embodiments.
[0022] Example 1
[0023] See also Figure 1 , a method for processing multiple protocols of packet sticking and packet breaking, comprising the following steps:
[0024] S1. Receive data through a single thread and store each transmitted data in a data cache according to the queue;
[0025] S2. Using the packet header to detect whether there is a complete packet in the data cache according to the packet protocol rules, if so, then take out the complete packet into the parsing process, and leave the remaining data in the data cache. If not, then keep the data in the data cache;
[0026] S3. Repeat S1-S2 and continue to receive data through the single thread until the data transmission is completed.
[0027] The present invention detects broken and stuck packets through the unified commonality of all data packets, processes data with a singleton thread, obtains data packets from streaming data methods such as TCP / UDP / serial port through an independent singleton thread, and adds them to a data cache in a queue manner, which can effectively control the order of cache addition and prevent the data from being discarded as invalid bytes due to confusion at the head and tail of the data. The singleton thread processing logic is less likely to cause blockage during logic processing, and the processing logic is clearer and more concise through the singleton thread, and the overall operation process is more concise.
[0028] In an optional embodiment, in step S2, the detecting whether there is a complete data packet in the data cache according to the data packet protocol rule by using the data packet header further includes: detecting whether there are invalid bytes in the data cache, and if so, deleting the invalid bytes.
[0029] The data cache is searched to see whether there are discrete bytes, thereby eliminating invalid bytes.
[0030] In an optional embodiment, the method of using the data packet header to detect whether there is a complete data packet in the data cache according to the data packet protocol rules includes: constructing a data detection function, passing the data in the data cache into the data detection function, the data detection function detects the data, and generates a return value based on the detection result.
[0031] Construct a data detection function, set up an interface return value for the data detection function, and determine whether the current data has broken or stuck packets based on the interface return value. If the current packet is incomplete, the return value is 0, and it will be added to the cache, waiting for the next packet of data to be fed back, and then added to the cache for detection, so as to rejoin the broken data. If the data packet is complete, the total length of the packet is returned, and the fertility data is parsed after intercepting this data packet to determine whether there is a complete data packet in the remaining data.
[0032] In an optional embodiment, the data detection function detects the data, and its specific implementation method includes: detecting whether there is a data packet header in the data, and detecting the integrity of the data packet according to the packet length of the data packet corresponding to the data packet header until the data transmission is completed. When the length of the data packet header and its subsequent data bytes meets the packet length, the data packet is complete. If not, there is a broken packet or a stuck packet.
[0033] The Ethernet protocol stipulates that the minimum link layer data packet is 64 bytes, of which the data packet header + tail totals 18 bytes. The data packet header often has a fixed structure and format, and includes data such as version number, data packet header length, service type, and total packet length. Therefore, we can get the total packet length of the data packet from the data packet header, and can also search the data packet header according to the fixed structure to determine whether the data packet in this segment is complete.
[0034] In an optional embodiment, the detection of whether there is a data packet header in the data is specifically implemented as follows: performing a format search on the data starting from the data header, and searching in turn whether there are data segments in the data. If the data segment format satisfies any data packet header format, then the data packet header exists in the data.
[0035] Because the length of the data packet header is in units of 32 bits, the length of the data packet header must be an integer multiple of 32 bits. Therefore, the IP protocol will fill in several zeros to reach an integer multiple of 32 bits, and the data packet can be retrieved based on this structure and the composition of the data packet header.
[0036] In an optional embodiment, the taking out of the complete data packet and putting it into the parsing process is specifically implemented by: starting from the data packet header in the data and according to the packet length of the data packet corresponding to the data packet header, intercepting the data of the corresponding byte length, the data of the corresponding byte length is a complete data packet, taking out the complete data packet and putting it into the parsing process.
[0037] In an optional embodiment, in step S1, the data is received through a single thread, specifically, the data is received through a TCP or UDP protocol.
[0038] In an optional embodiment, it also includes: customizing the data packet structure, the data packet includes a data packet header, and detecting each received data according to the customized data packet structure to determine whether the data received this time includes the complete customized data packet.
[0039] Users can customize data packets according to their needs and preset specific formats such as data packet headers, so that user-defined data packets can be identified through preset formats during use, achieving highly customized broken packet and sticky packet identification, and processing broken packet and sticky packet to ensure the integrity of communication.
[0040] Example 2
[0041] See also Figure 2 , a packet sticking and breaking system for processing multiple protocols, comprising:
[0042] The data receiving module is used to receive data through a single thread and store each transmitted data into a data cache according to a queue;
[0043] A cache detection module is used to detect whether there is a complete data packet in the data cache according to the data packet protocol rules using the data packet header. If so, the complete data packet is taken out and put into the parsing process, and the remaining data is left in the data cache. If not, the data is retained in the data cache;
[0044] The process loop module continues to receive data through a single thread until the data transmission is completed.
[0045] The embodiment disclosed in this specification is only an example of a unilateral feature of the present invention, and the protection scope of the present invention is not limited to this embodiment, and any other functionally equivalent embodiments fall within the protection scope of the present invention. For those skilled in the art, various other corresponding changes and deformations can be made according to the technical solutions and concepts described above, and all of these changes and deformations should fall within the protection scope of the claims of the present invention.
Claims
1. A method for processing multiple protocols of packet sticking and packet breaking. It is characterized in that The following steps are involved: S1. Receive data through a single thread and store each transmitted data in a data cache according to the queue; S2. Using the packet header to detect whether there is a complete packet in the data cache according to the packet protocol rules, if so, then take out the complete packet into the parsing process, and leave the remaining data in the data cache. If not, then keep the data in the data cache; S3. Repeat S1-S2 and continue to receive data through the single thread until the data transmission is completed.
2. A method for processing multiple protocols by sticking and disconnecting packets according to claim 1, It is characterized in that In step S2, the detecting whether there is a complete data packet in the data cache according to the data packet protocol rule by using the data packet header further includes: detecting whether there are invalid bytes in the data cache, and if so, deleting the invalid bytes.
3. A method for processing multiple protocols by sticking and disconnecting packets according to claim 1, It is characterized in that The method of using the data packet header to detect whether there is a complete data packet in the data cache according to the data packet protocol rules includes: constructing a data detection function, passing the data in the data cache into the data detection function, the data detection function detects the data, and generates a return value based on the detection result.
4. A method for processing multiple protocols by sticking and disconnecting packets according to claim 3, It is characterized in that The data detection function detects the data, and its specific implementation method includes: detecting whether there is a data packet header in the data, and detecting the integrity of the data packet according to the packet length of the data packet corresponding to the data packet header until the data transmission is completed. When the length of the data packet header and its subsequent data bytes meets the packet length, the data packet is complete. If not, there is a broken packet or a stuck packet.
5. A method for processing multiple protocols by sticking and disconnecting packets according to claim 4, It is characterized in that The specific implementation method of detecting whether there is a data packet header in the data is as follows: performing a format search on the data starting from the data header, and searching in sequence whether there are data segments in the data. If the data segment format satisfies any data packet header format, then the data packet header exists in the data.
6. A method for processing multiple protocols by sticking and disconnecting packets according to claim 5, It is characterized in that The method of taking out the complete data packet and putting it into the parsing process includes: starting from the data packet header in the data and according to the packet length of the data packet corresponding to the data packet header, intercepting data of corresponding byte length, the data of corresponding byte length is a complete data packet, taking out the complete data packet and putting it into the parsing process.
7. A method for processing multiple protocols by sticking and disconnecting packets according to claim 1, It is characterized in that In step S1, data is received through a single thread, specifically, data is received through TCP or UDP protocol.
8. A method for processing multiple protocols by sticking and disconnecting packets according to claim 1, It is characterized in that Also includes: The data packet structure is customized, the customized data packet includes a data packet header, and each received data is detected according to the customized data packet structure to determine whether the data received this time includes the complete customized data packet.
9. A packet sticking and breaking system for processing multiple protocols. It is characterized in that include: The data receiving module is used to receive data through a single thread and store each transmitted data into a data cache according to a queue; A cache detection module is used to detect whether there is a complete data packet in the data cache according to the data packet protocol rules using the data packet header. If so, the complete data packet is taken out and put into the parsing process, and the remaining data is left in the data cache. If not, the data is retained in the data cache; The process loop module continues to receive data through a single thread until the data transmission is completed.
