Overte C++ Documentation
Packet.h
1 //
2 // Packet.h
3 // libraries/networking/src/udt
4 //
5 // Created by Clement on 7/2/15.
6 // Copyright 2015 High Fidelity, Inc.
7 // Copyright 2021 Vircadia contributors.
8 //
9 // Distributed under the Apache License, Version 2.0.
10 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
11 //
12 
13 #ifndef hifi_Packet_h
14 #define hifi_Packet_h
15 
16 #include <memory>
17 
18 #include <QtCore/QIODevice>
19 
20 #include "BasePacket.h"
21 #include "PacketHeaders.h"
22 #include "SequenceNumber.h"
23 
24 namespace udt {
25 
26 class Packet : public BasePacket {
27  Q_OBJECT
28 public:
29  // Packet Header Format
30  //
31  // 0 1 2 3
32  // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
33  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34  // |C|R|M| O | Sequence Number |
35  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
36  // | P | Message Number | Optional (only if M = 1)
37  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
38  // | Message Part Number | Optional (only if M = 1)
39  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
40  //
41  // C: Control bit
42  // R: Reliable bit
43  // M: Message bit
44  // O: Obfuscation level
45  // P: Position bits
46 
47 
48  // NOTE: The SequenceNumber is only actually 29 bits to leave room for a bit field
49  using SequenceNumberAndBitField = uint32_t;
50 
51  // NOTE: The MessageNumber is only actually 30 bits to leave room for a bit field
52  using MessageNumber = uint32_t;
53  using MessageNumberAndBitField = uint32_t;
54  using MessagePartNumber = uint32_t;
55 
56  // Use same size as MessageNumberAndBitField so we can use the enum with bitwise operations
57  enum PacketPosition : MessageNumberAndBitField {
58  ONLY = 0x0, // 00
59  FIRST = 0x2, // 10
60  MIDDLE = 0x3, // 11
61  LAST = 0x1 // 01
62  };
63 
64  // Use same size as SequenceNumberAndBitField so we can use the enum with bitwise operations
65  enum ObfuscationLevel : SequenceNumberAndBitField {
66  NoObfuscation = 0x0, // 00
67  ObfuscationL1 = 0x1, // 01
68  ObfuscationL2 = 0x2, // 10
69  ObfuscationL3 = 0x3, // 11
70  };
71 
72  static std::unique_ptr<Packet> create(qint64 size = -1, bool isReliable = false, bool isPartOfMessage = false);
73  static std::unique_ptr<Packet> fromReceivedPacket(std::unique_ptr<char[]> data, qint64 size, const SockAddr& senderSockAddr);
74 
75  // Provided for convenience, try to limit use
76  static std::unique_ptr<Packet> createCopy(const Packet& other);
77 
78  // Current level's header size
79  static int localHeaderSize(bool isPartOfMessage = false);
80  // Cumulated size of all the headers
81  static int totalHeaderSize(bool isPartOfMessage = false);
82  // The maximum payload size this packet can use to fit in MTU
83  static int maxPayloadSize(bool isPartOfMessage = false);
84 
85  bool isPartOfMessage() const { return _isPartOfMessage; }
86  bool isReliable() const { return _isReliable; }
87  void setReliable(bool reliable) { _isReliable = reliable; }
88 
89  ObfuscationLevel getObfuscationLevel() const { return _obfuscationLevel; }
90  SequenceNumber getSequenceNumber() const { return _sequenceNumber; }
91  MessageNumber getMessageNumber() const { return _messageNumber; }
92  PacketPosition getPacketPosition() const { return _packetPosition; }
93  MessagePartNumber getMessagePartNumber() const { return _messagePartNumber; }
94 
95  void writeMessageNumber(MessageNumber messageNumber, PacketPosition position, MessagePartNumber messagePartNumber);
96  void writeSequenceNumber(SequenceNumber sequenceNumber) const;
97  void obfuscate(ObfuscationLevel level);
98 
99 protected:
100  Packet(qint64 size, bool isReliable = false, bool isPartOfMessage = false);
101  Packet(std::unique_ptr<char[]> data, qint64 size, const SockAddr& senderSockAddr);
102 
103  Packet(const Packet& other);
104  Packet(Packet&& other);
105 
106  Packet& operator=(const Packet& other);
107  Packet& operator=(Packet&& other);
108 
109 private:
110  void copyMembers(const Packet& other);
111 
112  // Header readers - these read data to member variables after pulling packet off wire
113  void readHeader() const;
114  void writeHeader() const;
115 
116  // Simple holders to prevent multiple reading and bitwise ops
117  mutable bool _isReliable { false };
118  mutable bool _isPartOfMessage { false };
119  mutable ObfuscationLevel _obfuscationLevel { NoObfuscation };
120  mutable SequenceNumber _sequenceNumber { 0 };
121  mutable MessageNumber _messageNumber { 0 };
122  mutable PacketPosition _packetPosition { PacketPosition::ONLY };
123  mutable MessagePartNumber _messagePartNumber { 0 };
124 };
125 
126 } // namespace udt
127 
128 Q_DECLARE_METATYPE(udt::Packet*);
129 
130 #endif // hifi_Packet_h