-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathpacket.cpp
More file actions
162 lines (128 loc) · 3.71 KB
/
Copy pathpacket.cpp
File metadata and controls
162 lines (128 loc) · 3.71 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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
////////////////////////////////////////////////////////////////////////////////
// Distributed under the Boost Software License, Version 1.0. //
// (See accompanying file LICENSE or copy at //
// https://www.boost.org/LICENSE_1_0.txt) //
////////////////////////////////////////////////////////////////////////////////
#include "networking/packet.h"
#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
#include <ostream>
#include "core/data_buffer.h"
#include "core/error_handling.h"
#include "networking/channel/channel_type.h"
#include "networking/packet_type.h"
namespace iris
{
Packet::Packet()
: Packet(PacketType::INVAlID, ChannelType::INVAlID, {})
{
}
Packet::Packet(PacketType type, ChannelType channel, const DataBuffer &body)
: header_(type, channel)
, body_()
, size_(body.size())
{
expect(body.size() <= sizeof(body_), "body too large");
std::memcpy(body_, body.data(), body.size());
}
Packet::Packet(const DataBuffer &raw_packet)
: Packet()
{
const auto size_to_copy = std::min<std::size_t>(128ul, raw_packet.size());
std::memcpy(this, raw_packet.data(), size_to_copy);
size_ = size_to_copy - sizeof(Header);
}
const std::byte *Packet::data() const
{
return reinterpret_cast<const std::byte *>(this);
}
std::byte *Packet::data()
{
return reinterpret_cast<std::byte *>(this);
}
const std::byte *Packet::body() const
{
return body_;
}
std::byte *Packet::body()
{
return body_;
}
DataBuffer Packet::body_buffer() const
{
return DataBuffer(body(), body() + size_);
}
std::size_t Packet::packet_size() const
{
// sanity check the Packet class is the expected size
static_assert(sizeof(Packet) == 128 + sizeof(size_), "packet has invalid size");
return sizeof(Header) + body_size();
}
std::size_t Packet::body_size() const
{
return size_;
}
PacketType Packet::type() const
{
return header_.type;
}
ChannelType Packet::channel() const
{
return header_.channel;
}
bool Packet::is_valid() const
{
return header_.type != PacketType::INVAlID;
}
std::uint16_t Packet::sequence() const
{
return header_.sequence;
}
void Packet::set_sequence(std::uint16_t sequence)
{
header_.sequence = sequence;
}
bool Packet::operator==(const Packet &other) const
{
return ((packet_size() == other.packet_size()) && (std::memcmp(data(), other.data(), packet_size()) == 0));
}
bool Packet::operator!=(const Packet &other) const
{
return !(*this == other);
}
std::ostream &operator<<(std::ostream &out, const Packet &packet)
{
switch (packet.header_.type)
{
case PacketType::INVAlID: out << "INVALID"; break;
case PacketType::HELLO: out << "HELLO"; break;
case PacketType::CONNECTED: out << "CONNECTED"; break;
case PacketType::DATA: out << "DATA"; break;
case PacketType::ACK: out << "ACK"; break;
default: out << "UNKNOWN"; break;
}
out << ", ";
switch (packet.header_.channel)
{
case ChannelType::INVAlID: out << "INVALID"; break;
case ChannelType::UNRELIABLE_UNORDERED: out << "UNRELIABLE_UNORDERED"; break;
case ChannelType::UNRELIABLE_SEQUENCED: out << "UNRELIABLE_SEQUENCED"; break;
case ChannelType::RELIABLE_ORDERED: out << "RELIABLE_ORDERED"; break;
default: out << "UNKNOWN"; break;
}
out << ", ";
out << "[" << packet.header_.sequence << "]";
out << " ";
out << packet.size_;
out << " | ";
out << std::hex;
for (auto i = 0u; i < std::min(static_cast<std::uint32_t>(packet.size_), 8u); ++i)
{
out << static_cast<int>(packet.body_[i]) << " ";
}
out << std::dec << std::endl;
return out;
}
}