sources/network/packetqueue.cpp

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

mercurial