Skip to content

Commit 67587fa

Browse files
authored
Merge pull request #109 from edunad/fixes/packetWriteLength
Packet::read/writeLength: optimize bytes written to stream to reduce bandwidth
2 parents 3c3e614 + 75805a9 commit 67587fa

File tree

2 files changed

+31
-28
lines changed

2 files changed

+31
-28
lines changed

network/include/rawrbox/network/packet.hpp

Lines changed: 31 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,10 @@ namespace rawrbox {
2525
};
2626
// ----------------
2727

28-
enum class LengthType {
29-
UInt8,
30-
UInt16,
31-
UInt32,
32-
UInt64
33-
};
34-
3528
class Packet {
3629
protected:
3730
std::vector<uint8_t> buffer = {};
3831
size_t pos = 0;
39-
LengthType lengthFormat = LengthType::UInt16;
4032

4133
public:
4234
Packet() = default;
@@ -124,15 +116,23 @@ namespace rawrbox {
124116
}
125117
}
126118

127-
template <class T>
119+
template <class T = size_t>
128120
T readLength() {
129-
switch (this->lengthFormat) {
130-
case LengthType::UInt8: return static_cast<T>(this->read<uint8_t>());
131-
case LengthType::UInt16: return static_cast<T>(this->read<uint16_t>());
132-
case LengthType::UInt32: return static_cast<T>(this->read<uint32_t>());
133-
case LengthType::UInt64: return static_cast<T>(this->read<uint64_t>());
134-
default: throw std::runtime_error("[RawrBox-Packet] Unknown lengthFormat");
121+
constexpr uint64_t maskNum = 0x7F;
122+
constexpr uint64_t maskFlag = 0x80;
123+
124+
uint64_t ret = 0;
125+
uint64_t bitsReceived = 0;
126+
while (true) {
127+
uint64_t byte = read<uint8_t>();
128+
129+
ret = ret | ((byte & maskNum) << bitsReceived);
130+
bitsReceived += 7;
131+
132+
if ((byte & maskFlag) == 0) break;
135133
}
134+
135+
return static_cast<T>(ret);
136136
}
137137

138138
virtual void read(std::string& ret);
@@ -208,21 +208,28 @@ namespace rawrbox {
208208

209209
void write(const std::string& obj, bool shouldWriteLength = true);
210210

211-
template <class T>
211+
template <class T = size_t>
212212
void writeLength(T size) {
213-
switch (this->lengthFormat) {
214-
case LengthType::UInt8: this->write(static_cast<uint8_t>(size)); break;
215-
case LengthType::UInt16: this->write(static_cast<uint16_t>(size)); break;
216-
case LengthType::UInt32: this->write(static_cast<uint32_t>(size)); break;
217-
case LengthType::UInt64: this->write(static_cast<uint64_t>(size)); break;
218-
default: throw std::runtime_error("[RawrBox-Packet] Unknown lengthFormat");
213+
if (size < 0) throw std::runtime_error("[RawrBox-Packet] invalid length");
214+
if (size == 0) {
215+
write<uint8_t>(0);
216+
return;
217+
}
218+
219+
auto remaining = static_cast<size_t>(size);
220+
constexpr uint64_t maskNum = 0x7F;
221+
constexpr uint64_t maskFlag = 0x80;
222+
223+
while (remaining > 0) {
224+
bool hasMore = (remaining >> 7) > 0;
225+
write(static_cast<uint8_t>((remaining & maskNum) | (hasMore ? maskFlag : 0x00ULL)));
226+
227+
remaining >>= 7;
219228
}
220229
}
221230
// -----------------
222231

223232
// UTILS -----
224-
void setlengthFormat(LengthType format);
225-
226233
bool seek(size_t offset);
227234
bool seek(std::vector<uint8_t>::iterator offset);
228235

network/src/packet.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,6 @@ namespace rawrbox {
5050
// --------
5151

5252
// UTILS -----
53-
void Packet::setlengthFormat(LengthType format) {
54-
this->lengthFormat = format;
55-
}
56-
5753
bool Packet::seek(size_t offset) {
5854
if (offset > size()) return false;
5955
this->pos = offset;

0 commit comments

Comments
 (0)