mirror of
https://github.com/Kelsidavis/WoWee.git
synced 2026-03-26 16:50:15 +00:00
Add Packet::hasRemaining(size_t) and remove free function from game_handler.cpp. Replaces 8 call sites with method calls.
55 lines
1.6 KiB
C++
55 lines
1.6 KiB
C++
#pragma once
|
|
|
|
#include <vector>
|
|
#include <cstdint>
|
|
#include <string>
|
|
|
|
namespace wowee {
|
|
namespace network {
|
|
|
|
class Packet {
|
|
public:
|
|
Packet() = default;
|
|
explicit Packet(uint16_t opcode);
|
|
Packet(uint16_t opcode, const std::vector<uint8_t>& data);
|
|
Packet(uint16_t opcode, std::vector<uint8_t>&& data);
|
|
|
|
void writeUInt8(uint8_t value);
|
|
void writeUInt16(uint16_t value);
|
|
void writeUInt32(uint32_t value);
|
|
void writeUInt64(uint64_t value);
|
|
void writeFloat(float value);
|
|
void writeString(const std::string& value);
|
|
void writeBytes(const uint8_t* data, size_t length);
|
|
|
|
uint8_t readUInt8();
|
|
uint16_t readUInt16();
|
|
uint32_t readUInt32();
|
|
uint64_t readUInt64();
|
|
float readFloat();
|
|
std::string readString();
|
|
|
|
uint16_t getOpcode() const { return opcode; }
|
|
const std::vector<uint8_t>& getData() const { return data; }
|
|
size_t getReadPos() const { return readPos; }
|
|
size_t getSize() const { return data.size(); }
|
|
size_t getRemainingSize() const { return data.size() - readPos; }
|
|
bool hasRemaining(size_t need) const { return readPos <= data.size() && need <= (data.size() - readPos); }
|
|
bool hasFullPackedGuid() const {
|
|
if (readPos >= data.size()) return false;
|
|
uint8_t mask = data[readPos];
|
|
size_t guidBytes = 1;
|
|
for (int bit = 0; bit < 8; ++bit)
|
|
if (mask & (1u << bit)) ++guidBytes;
|
|
return getRemainingSize() >= guidBytes;
|
|
}
|
|
void setReadPos(size_t pos) { readPos = pos; }
|
|
|
|
private:
|
|
uint16_t opcode = 0;
|
|
std::vector<uint8_t> data;
|
|
size_t readPos = 0;
|
|
};
|
|
|
|
} // namespace network
|
|
} // namespace wowee
|