-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPacket.cpp
More file actions
122 lines (99 loc) · 3.98 KB
/
Packet.cpp
File metadata and controls
122 lines (99 loc) · 3.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one at
http://mozilla.org/MPL/2.0/.
The Original Code is the Parlo library.
The Initial Developer of the Original Code is
Mats 'Afr0' Vederhus. All Rights Reserved.
Contributor(s): ______________________________________.
*/
#include "pch.h"
#include "Parlo.h"
#include <memory>
namespace Parlo
{
class Packet::Impl
{
public:
Impl(uint8_t id, const std::vector<uint8_t>& serializedData, bool isPacketCompressed = false)
: id(id), isCompressed(isPacketCompressed ? 1 : 0), isReliable(0),
length(static_cast<uint16_t>(PacketHeaders::STANDARD + serializedData.size())),
data(serializedData), isUDP(false)
{
if (serializedData.empty())
throw std::invalid_argument("Packet: SerializedData cannot be null!");
}
Impl(uint8_t id, const std::vector<uint8_t>& serializedData, bool isPacketCompressed, bool isPacketReliable)
: id(id), isCompressed(isPacketCompressed ? 1 : 0), isReliable(isPacketReliable ? 1 : 0),
length(static_cast<uint16_t>(PacketHeaders::UDP + serializedData.size())),
data(serializedData), isUDP(true)
{
if (serializedData.empty())
throw std::invalid_argument("Packet: SerializedData cannot be null!");
}
~Impl() {}
uint8_t getID() const { return id; }
uint8_t getIsCompressed() const { return isCompressed; }
uint16_t getLength() const { return length; }
const std::vector<uint8_t>& getData() const { return data; }
/*Builds a packet ready for transmission by turning it into serialized data.*/
std::vector<uint8_t> buildPacket() const
{
std::vector<uint8_t> packetData;
packetData.push_back(id);
packetData.push_back(isCompressed);
if (isUDP)
packetData.push_back(isReliable);
packetData.push_back(static_cast<uint8_t>(length & 0xFF));
packetData.push_back(static_cast<uint8_t>((length >> 8) & 0xFF));
packetData.insert(packetData.end(), data.begin(), data.end());
return packetData;
}
private:
uint8_t id;
uint8_t isCompressed;
uint8_t isReliable;
uint16_t length;
std::vector<uint8_t> data;
bool isUDP;
friend class Packet;
};
/*Constructs a new Packet instance for TCP transmission.
@param id The ID of the packet.
@param serializedData The serialized data of the packet.
@param isPacketCompressed Is the packet compressed?*/
Packet::Packet(uint8_t id, const std::vector<uint8_t>& serializedData, bool isPacketCompressed)
: pImpl(std::make_unique<Impl>(id, serializedData, isPacketCompressed))
{
}
/*Constructs a new Packet instance for UDP transmission.
@param id The ID of the packet.
@param serializedData The serialized data of the packet.
@param isPacketCompressed Is the packet compressed?
@param isPacketReliable Is this packet supposed to be transferred reliably?*/
Packet::Packet(uint8_t id, const std::vector<uint8_t>& serializedData, bool isPacketCompressed, bool isPacketReliable)
: pImpl(std::make_unique<Impl>(id, serializedData, isPacketCompressed, isPacketReliable))
{
}
Packet::~Packet() = default;
/*Builds a packet ready for transmission by turning it into serialized data.*/
std::vector<uint8_t> Packet::buildPacket() const
{
return pImpl->buildPacket();
}
uint8_t Packet::getID() const
{
return pImpl->getID();
}
uint8_t Packet::getIsCompressed() const
{
return pImpl->getIsCompressed();
}
uint16_t Packet::getLength() const
{
return pImpl->getLength();
}
const std::vector<uint8_t>& Packet::getData() const
{
return pImpl->getData();
}
}