Overte C++ Documentation
PacketQueue.h
1 //
2 // PacketQueue.h
3 // libraries/networking/src/udt
4 //
5 // Created by Clement on 9/16/15.
6 // Copyright 2015 High Fidelity, Inc.
7 //
8 // Distributed under the Apache License, Version 2.0.
9 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
10 //
11 
12 #ifndef hifi_PacketQueue_h
13 #define hifi_PacketQueue_h
14 
15 #include <list>
16 #include <vector>
17 #include <memory>
18 #include <mutex>
19 
20 #include "Packet.h"
21 
22 namespace udt {
23 
24 class PacketList;
25 
26 using MessageNumber = uint32_t;
27 
28 class PacketQueue {
29  using Mutex = std::recursive_mutex;
30  using LockGuard = std::lock_guard<Mutex>;
31  using PacketPointer = std::unique_ptr<Packet>;
32  using PacketListPointer = std::unique_ptr<PacketList>;
33  using RawChannel = std::list<PacketPointer>;
34  using Channel = std::unique_ptr<RawChannel>;
35  using Channels = std::list<Channel>;
36 
37 public:
38  PacketQueue(MessageNumber messageNumber = 0);
39  void queuePacket(PacketPointer packet);
40  void queuePacketList(PacketListPointer packetList);
41 
42  bool isEmpty() const;
43  PacketPointer takePacket();
44 
45  Mutex& getLock() { return _packetsLock; }
46 
47  MessageNumber getCurrentMessageNumber() const { return _currentMessageNumber; }
48 
49 private:
50  MessageNumber getNextMessageNumber();
51 
52  MessageNumber _currentMessageNumber { 0 };
53 
54  mutable Mutex _packetsLock; // Protects the packets to be sent.
55  Channels _channels; // One channel per packet list + Main channel
56 
57  Channels::iterator _currentChannel;
58  unsigned int _channelsVisitedCount { 0 };
59 };
60 
61 }
62 
63 
64 #endif // hifi_PacketQueue_h