sources/network/packetqueue.h

Sat, 23 Jul 2016 12:28:52 +0300

author
Teemu Piippo <teemu@compsta2.com>
date
Sat, 23 Jul 2016 12:28:52 +0300
branch
protocol5
changeset 167
0150f86e68f0
permissions
-rw-r--r--

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.

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 #pragma once
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 <map>
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 #include <set>
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 #include "../main.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
35 #include "../list.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
36 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
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 /*!
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 * \brief The PacketQueue class stores 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
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 * The UDP protocol makes no guarantees of packet delivery. Packets may wind up being dropped, or delivered in the wrong
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 * order. This class mitigates that issue by storing the packets into a queue, and popping packets in order.
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 */
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 class 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
45 {
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 public:
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 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
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 bool 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
50 std::set<unsigned int> 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
51 std::set<int> 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
52 int 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
53 bool 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
54 bool 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
55 bool 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
56
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 private:
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 std::map<unsigned int, ByteArray> 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
59 unsigned int 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
60 };
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 END_ZFC_NAMESPACE

mercurial