52 lines
1.3 KiB
C++
52 lines
1.3 KiB
C++
#pragma once
|
|
#include <vector>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <stdexcept>
|
|
#include <arpa/inet.h>
|
|
|
|
namespace network
|
|
{
|
|
class Buffer
|
|
{
|
|
public:
|
|
explicit Buffer(std::vector<uint8_t> &buf) : buffer(buf), pos(0) {}
|
|
explicit Buffer(size_t reserve = 0) : buffer(reserve), pos(0) {}
|
|
|
|
// --- Writing ---
|
|
void writeBool(bool v);
|
|
void writeUint8(uint8_t v);
|
|
void writeUint32(uint32_t v);
|
|
void writeUint16(uint16_t v);
|
|
void writeUint64(uint64_t v);
|
|
void writeInt8(int8_t v);
|
|
void writeInt32(int32_t v);
|
|
void writeInt16(int16_t v);
|
|
void writeInt64(int64_t v);
|
|
void writeFloat(float f);
|
|
void writeBytes(const uint8_t *data, size_t len);
|
|
void writeString(const std::string &s);
|
|
// --- Reading ---
|
|
bool readBool();
|
|
uint8_t readUint8();
|
|
uint16_t readUint16();
|
|
uint32_t readUint32();
|
|
uint64_t readUint64();
|
|
int8_t readInt8();
|
|
int16_t readInt16();
|
|
int32_t readInt32();
|
|
int64_t readInt64();
|
|
float readFloat();
|
|
std::string readString();
|
|
|
|
size_t remaining() const;
|
|
std::vector<uint8_t> &data();
|
|
|
|
private:
|
|
void checkRemaining(size_t n);
|
|
|
|
std::vector<uint8_t> buffer;
|
|
size_t pos;
|
|
};
|
|
} // namespace network
|