PS: This implementation is not necessarily the best implementation for some specific situations, you can use arrays instead of queues to implement
Refer to TCP/IP in detail the second volume, 24~29, discusses the implementation of TCP protocol, and summarizes how TCP can guarantee the correctness and reliability of packets to the application layer, that is, how TCP realizes the reorganization of data packets.
The first is to design two message queues, a message to store the normal arrival, and a message to store the incoming disorder.
For example, the last message data of the normal message queue is as follows:
The ordinal datagram length of the first byte of the message data segment
The next incoming message may have multiple scenarios, which are analyzed in turn as follows:
1) Normal message
SEQ2 = Seq1+len1
The SEQ of this message indicates that the message carries the data sequence 200~399, which is the expected subsequent message of the previous message, and appends this message to the normal message queue.
2) Complete Duplicate message
SEQ2 ==seq1 and Len2==len1.
This message carries the serial number 100~199, which is exactly the same as the serial number 100~199 the previous message, which is completely duplicated, so the message should be discarded.
3) Repeat sub-message
SEQ2 ==seq1 and Len2<len1.
This message carries the data sequence 100~149, indicating that this is part of the previous message, so this message should be discarded.
Note: The second to third of these two cases can be combined, namely SEQ2 ==seq1 and Len2<=len1, which are listed here only to illustrate the various situations.
4) Part of the repeated message situation one
Seq2>seq1 and Seq2<seq1+len1 and Seq2+len2<=seq1+len1.
That is, the message carries the ordinal 150~179, which is included in the previous segment (100~199),
So this message should be discarded.
5) Partial Repeat message case two
Seq2>seq1 and Seq2<seq1+len1 and Seq2+len2>seq1+len1.
That is, this message carries the serial number 150~249, the first part of the ordinal segment 150~199 is included in the previous message segment (100~199), the latter part of the 200~249 is the new data, this message should be treated as follows:
A. Calculating the number of duplicate bytes
(SEQ1+LEN1)-seq2= 100+100-150 = 50
That is, the first 50 bytes of this message segment are duplicates.
B. Interception of new data in the segment
Discard the first 50 bytes of this segment and intercept the new data behind it, that is, only the byte ordinal segment 200~249 is preserved.
C. Re-set the SEQ for this message segment
SEQ2 = seq2+50 = 150+50 = 200
D. Reset the data length of this message segment
Len2 = len2-50 =100-50=50
E. Re-set the following message segment
Now this message segment carries the data serial number 200~249, exactly is the last message the subsequent message, can now append it as normal message to the normal message queue.
6) Early Arrival messages
Seq2>seq1+len1
This message section carries the serial number 300~399 the data, namely is not the last message 100~199 the subsequent message, but is the early arrival message, at this time should put this message in the sequence message queue to store up, for the subsequent reorganization use.
This is done until TCP disconnects the socket (fin=1), merging the data from the normal message queue and the out-of-sequence message queue to complete the reorganization. The SEQ and Len of the last message in the normal message queue are taken out, and the subsequent packets belonging to it are found in the queue of out-of-sequence messages, and the message can be analyzed as the subsequent message processing process of the normal message queue with the preceding 1).
[Turn] TCP packet reassembly implementation analysis