Overte C++ Documentation
NLPacket.h
1 //
2 // NLPacket.h
3 // libraries/networking/src
4 //
5 // Created by Clement on 7/6/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_NLPacket_h
14 #define hifi_NLPacket_h
15 
16 #include <UUID.h>
17 
18 #include "udt/Packet.h"
19 
20 class HMACAuth;
21 
22 class NLPacket : public udt::Packet {
23  Q_OBJECT
24 public:
25  //
26  // NLPacket format:
27  //
28  // | BYTE | BYTE | BYTE | BYTE |
29  // 0 1 2 3
30  // 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
31  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
32  // | Packet Type | Version | Local Node ID - sourced only |
33  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
34  // | |
35  // | MD5 Verification - 16 bytes |
36  // | (ONLY FOR VERIFIED PACKETS) |
37  // | |
38  // +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
39  //
40 
41  using LocalID = NetworkLocalID;
42  static const LocalID NULL_LOCAL_ID = 0;
43 
44  static const int NUM_BYTES_LOCALID = sizeof(LocalID);
45  // this is used by the Octree classes - must be known at compile time
46  static const int MAX_PACKET_HEADER_SIZE =
47  sizeof(udt::Packet::SequenceNumberAndBitField) + sizeof(udt::Packet::MessageNumberAndBitField) +
48  sizeof(PacketType) + sizeof(PacketVersion) + NUM_BYTES_LOCALID + NUM_BYTES_MD5_HASH;
49 
50  static std::unique_ptr<NLPacket> create(PacketType type, qint64 size = -1,
51  bool isReliable = false, bool isPartOfMessage = false, PacketVersion version = 0);
52 
53  static std::unique_ptr<NLPacket> fromReceivedPacket(std::unique_ptr<char[]> data, qint64 size,
54  const SockAddr& senderSockAddr);
55 
56  static std::unique_ptr<NLPacket> fromBase(std::unique_ptr<Packet> packet);
57 
58  // Provided for convenience, try to limit use
59  static std::unique_ptr<NLPacket> createCopy(const NLPacket& other);
60 
61  // Current level's header size
62  static int localHeaderSize(PacketType type);
63  // Cumulated size of all the headers
64  static int totalHeaderSize(PacketType type, bool isPartOfMessage = false);
65  // The maximum payload size this packet can use to fit in MTU
66  static int maxPayloadSize(PacketType type, bool isPartOfMessage = false);
67 
68  static PacketType typeInHeader(const udt::Packet& packet);
69  static PacketVersion versionInHeader(const udt::Packet& packet);
70 
71  static LocalID sourceIDInHeader(const udt::Packet& packet);
72  static QByteArray verificationHashInHeader(const udt::Packet& packet);
73  static QByteArray hashForPacketAndHMAC(const udt::Packet& packet, HMACAuth& hash);
74 
75  PacketType getType() const { return _type; }
76  void setType(PacketType type);
77 
78  PacketVersion getVersion() const { return _version; }
79  void setVersion(PacketVersion version);
80 
81  LocalID getSourceID() const { return _sourceID; }
82 
83  void writeSourceID(LocalID sourceID) const;
84  void writeVerificationHash(HMACAuth& hmacAuth) const;
85 
86 protected:
87 
88  NLPacket(PacketType type, qint64 size = -1, bool forceReliable = false, bool isPartOfMessage = false, PacketVersion version = 0);
89  NLPacket(std::unique_ptr<char[]> data, qint64 size, const SockAddr& senderSockAddr);
90 
91  NLPacket(const NLPacket& other);
92  NLPacket(NLPacket&& other);
93  NLPacket(Packet&& other);
94 
95  NLPacket& operator=(const NLPacket& other);
96  NLPacket& operator=(NLPacket&& other);
97 
98  // Header writers
99  void writeTypeAndVersion();
100 
101  // Header readers, used to set member variables after getting a packet from the network
102  void readType();
103  void readVersion();
104  void readSourceID();
105 
106  PacketType _type;
107  PacketVersion _version;
108  mutable LocalID _sourceID;
109 };
110 
111 #endif // hifi_NLPacket_h