sources/network/packetqueue.cpp

Wed, 27 Jan 2021 12:34:56 +0200

author
Teemu Piippo <teemu@hecknology.net>
date
Wed, 27 Jan 2021 12:34:56 +0200
branch
protocol5
changeset 172
0b0bc8045d28
parent 167
0150f86e68f0
permissions
-rw-r--r--

added packet queue as a new head

167
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
1 /*
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
2 Copyright 2016 Teemu Piippo
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
3 All rights reserved.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
4
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
5 Redistribution and use in source and binary forms, with or without
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
6 modification, are permitted provided that the following conditions
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
7 are met:
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
8
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
9 1. Redistributions of source code must retain the above copyright
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
10 notice, this list of conditions and the following disclaimer.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
11 2. Redistributions in binary form must reproduce the above copyright
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
12 notice, this list of conditions and the following disclaimer in the
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
13 documentation and/or other materials provided with the distribution.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
14 3. Neither the name of the copyright holder nor the names of its
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
15 contributors may be used to endorse or promote products derived from
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
16 this software without specific prior written permission.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
17
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
19 "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
20 TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
21 PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
22 OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
23 EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
25 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
26 LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
27 NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
28 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
29 */
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
30
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
31 #include <algorithm>
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
32 #include "packetqueue.h"
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
33 BEGIN_ZFC_NAMESPACE
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
34
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
35 /*!
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
36 * \brief Constructs an empty packet queue.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
37 */
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
38 PacketQueue::PacketQueue() :
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
39 m_currentSequenceNumber(0) {}
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
40
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
41 /*!
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
42 * \brief Inserts the packet into the queue, unless the packet is the next packet to be processed.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
43 * \param sequenceNumber Sequence number of the packet.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
44 * \param data Payload of the packet.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
45 * \return True, if the packet was stored, false if the packet should be processed immediately.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
46 */
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
47 bool PacketQueue::addPacket(unsigned int sequenceNumber, const ByteArray& data)
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
48 {
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
49 // Check whether this packet is the one we're supposed to process next.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
50 if (sequenceNumber != m_currentSequenceNumber)
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
51 {
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
52 // It is not, therefore store it for later.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
53 m_queue[sequenceNumber] = data;
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
54 return true;
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
55 }
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
56 else
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
57 {
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
58 // It is, therefore the caller processes it, and we can advance to the next packet right away.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
59 m_currentSequenceNumber = getNextSequenceNumber();
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
60 return false;
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
61 }
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
62 }
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
63
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
64 /*!
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
65 * \returns whether there are packets in queue that cannot be processed due to missing in-between packets. If true, the
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
66 * \returns caller should initiate packet recovery protocol.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
67 */
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
68 bool PacketQueue::isStuck() const
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
69 {
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
70 return m_queue.size() > 0 and m_queue.find(m_currentSequenceNumber) == m_queue.end();
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
71 }
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
72
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
73 /*!
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
74 * \returns whether or not there are packets awaiting processing.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
75 */
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
76 bool PacketQueue::hasPacketsToPop() const
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
77 {
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
78 return m_queue.size() > 0 and m_queue.find(m_currentSequenceNumber) != m_queue.end();
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
79 }
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
80
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
81 /*!
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
82 * \brief Retrieves the next packet to be processed, and removes it from the queue.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
83 * \param packet Reference to a byte array to store the packet payload into.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
84 * \returns whether the next packet was successfully popped from the queue, or not.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
85 */
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
86 bool PacketQueue::popNextPacket(ByteArray& packet)
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
87 {
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
88 // Find the packet matching our current sequence number.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
89 auto iterator = m_queue.find(m_currentSequenceNumber);
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
90
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
91 if (iterator != m_queue.end())
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
92 {
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
93 // We found the packet we were looking for. Pass it to the caller.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
94 packet = iterator->second;
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
95 // Remove the packet from the queue.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
96 m_queue.erase(iterator);
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
97 // We can now advance to the next packet.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
98 m_currentSequenceNumber = getNextSequenceNumber();
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
99 return true;
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
100 }
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
101 else
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
102 {
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
103 // We did not find the next packet.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
104 return false;
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
105 }
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
106 }
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
107
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
108 /*!
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
109 * \returns the sequence number for the next packet.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
110 */
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
111 int PacketQueue::getNextSequenceNumber() const
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
112 {
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
113 return (m_currentSequenceNumber + 1) % 1024;
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
114 }
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
115
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
116 /*!
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
117 * \returns a list of packets that have to be requested from the server.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
118 */
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
119 std::set<int> PacketQueue::getLostPackets() const
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
120 {
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
121 std::set<int> packetsNeeded;
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
122 std::set<int> packetsInQueue;
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
123
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
124 // Build the set of packet numbers we currently have.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
125 for (auto pair : m_queue)
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
126 packetsInQueue.insert(pair.first);
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
127
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
128 // Build the set of packets we wish to process. To do this we need the smallest and largest numbers in
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
129 // packetsInQueue.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
130 Range<int> packetRange(min(packetsInQueue), max(packetsInQueue));
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
131
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
132 for (int i : packetRange)
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
133 packetsNeeded.insert(i);
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
134
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
135 // The set of lost packets is now the set of packets we want, minus the packets we have.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
136 std::set<int> packetsLost;
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
137 std::set_difference(packetsNeeded.begin(), packetsNeeded.end(),
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
138 packetsInQueue.begin(), packetsInQueue.end(),
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
139 std::inserter(packetsLost, packetsLost.begin()));
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
140 return packetsLost;
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
141 }
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
142
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
143 std::set<unsigned int> PacketQueue::getWaitingPackets() const
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
144 {
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
145 std::set<unsigned int> packetsInQueue;
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
146
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
147 // Build the set of packet numbers we currently have.
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
148 for (auto pair : m_queue)
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
149 packetsInQueue.insert(pair.first);
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
150
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
151 return packetsInQueue;
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
152 }
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
153
0150f86e68f0 Added the PacketQueue class to make use of the sequence numbering to process packets in the correct order, and the ZFC9000 end of packet recovery protocol.
Teemu Piippo <teemu@compsta2.com>
parents:
diff changeset
154 END_ZFC_NAMESPACE

mercurial