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
|
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */
#ifndef UNPACK_PACKET_H
#define UNPACK_PACKET_H
#include "RawPacket.h"
#include <algorithm>
#include <string>
#include <vector>
#include <cstring>
#include <stdexcept>
#include <memory>
namespace netcode
{
class UnpackPacketException : public std::runtime_error {
public:
UnpackPacketException(const std::string& what) : std::runtime_error(what) {}
};
class UnpackPacket
{
public:
UnpackPacket(std::shared_ptr<const RawPacket>, size_t skipBytes = 0);
template <typename T>
void operator>>(T& t)
{
if ((pos + sizeof(T)) > pckt->length)
throw UnpackPacketException("Unpack failure (type)");
std::memcpy(reinterpret_cast<void*>(&t), pckt->data + pos, sizeof(T));
pos += sizeof(T);
}
template <typename T>
void operator>>(std::vector<T>& vec)
{
const size_t size = vec.size() * sizeof(T);
if ((pckt->length - pos) < size)
throw UnpackPacketException("Unpack failure (vector)");
std::memcpy(reinterpret_cast<void*>(&vec[0]), pckt->data + pos, size);
pos += size;
}
void operator>>(std::string& text)
{
const auto beg = pckt->data + pos;
const auto end = pckt->data + pckt->length;
const auto it = std::find(beg, end, 0);
// for strings, require a null-term to have been added during packing
if (it == end)
throw UnpackPacketException("Unpack failure (string)");
text.clear();
text.assign(reinterpret_cast<const char*>(beg), it - beg);
pos += (text.size() + 1);
}
private:
std::shared_ptr<const RawPacket> pckt;
size_t pos;
};
} // namespace netcode
#endif // UNPACK_PACKET_H
|