protocol: Implement PacketHeader

This commit is contained in:
Joshua Scott 2018-04-09 21:51:02 +01:00
parent 611943e3ad
commit 57ef9b95f8
4 changed files with 96 additions and 129 deletions

View File

@ -1,66 +0,0 @@
#pragma once
#include "../util/Serializable.h"
#include "exception.h"
#include <cstdint>
#include <vector>
#include <sstream>
#include <type_traits>
namespace ki
{
namespace protocol
{
class Packet final : public util::Serializable
{
public:
Packet(bool control = false, uint8_t opcode = 0);
virtual ~Packet() = default;
bool is_control() const;
void set_control(bool control);
uint8_t get_opcode() const;
void set_opcode(uint8_t opcode);
template <typename DataT>
void set_payload_data(const DataT &data)
{
static_assert(std::is_base_of<Serializable, DataT>::value,
"DataT must derive from Serializable.");
std::ostringstream oss;
data.write_to(oss);
std::string data_string = oss.str();
m_payload.assign(data_string.begin(), data_string.end());
}
template <typename DataT>
DataT *get_payload_data() const
{
static_assert(std::is_base_of<Serializable, DataT>::value,
"DataT must derive from Serializable.");
std::istringstream iss(std::string(m_payload.data(), m_payload.size()));
DataT *data = new DataT();
try
{
data->read_from(iss);
}
catch (parse_error &e)
{
delete data;
throw;
}
return data;
}
void write_to(std::ostream &ostream) const override final;
void read_from(std::istream &istream) override final;
size_t get_size() const override final;
private:
bool m_control;
uint8_t m_opcode;
std::vector<char> m_payload;
};
}
}

View File

@ -0,0 +1,32 @@
#pragma once
#include "../../util/Serializable.h"
#include <cstdint>
namespace ki
{
namespace protocol
{
namespace net
{
class PacketHeader final : public util::Serializable
{
public:
PacketHeader(bool control = false, uint8_t opcode = 0);
virtual ~PacketHeader() = default;
bool is_control() const;
void set_control(bool control);
uint8_t get_opcode() const;
void set_opcode(uint8_t opcode);
void write_to(std::ostream &ostream) const override final;
void read_from(std::istream &istream) override final;
size_t get_size() const override final;
private:
bool m_control;
uint8_t m_opcode;
};
}
}
}

View File

@ -1,63 +0,0 @@
#include "ki/protocol/Packet.h"
namespace ki
{
namespace protocol
{
Packet::Packet(bool control, uint8_t opcode)
{
m_control = control;
m_opcode = opcode;
m_payload = std::vector<char>();
}
bool Packet::is_control() const
{
return m_control;
}
void Packet::set_control(bool control)
{
m_control = control;
}
uint8_t Packet::get_opcode() const
{
return m_opcode;
}
void Packet::set_opcode(uint8_t opcode)
{
m_opcode = opcode;
}
void Packet::write_to(std::ostream& ostream) const
{
ostream.put(m_control);
ostream.put(m_opcode);
ostream.put(0);
ostream.put(0);
ostream.write(m_payload.data(), m_payload.size());
}
void Packet::read_from(std::istream& istream)
{
m_control = istream.get() >= 1;
m_opcode = istream.get();
istream.ignore(2);
const std::ios::pos_type pos = istream.tellg();
istream.seekg(0, std::ios::end);
const size_t size = istream.tellg() - pos;
istream.seekg(pos, std::ios::beg);
m_payload.resize(size);
istream.read(m_payload.data(), size);
}
size_t Packet::get_size() const
{
return 4 + m_payload.size();
}
}
}

View File

@ -0,0 +1,64 @@
#include "ki/protocol/net/PacketHeader.h"
#include "ki/protocol/exception.h"
#include <sstream>
namespace ki
{
namespace protocol
{
namespace net
{
PacketHeader::PacketHeader(const bool control, const uint8_t opcode)
{
m_control = control;
m_opcode = opcode;
}
bool PacketHeader::is_control() const
{
return m_control;
}
void PacketHeader::set_control(const bool control)
{
m_control = control;
}
uint8_t PacketHeader::get_opcode() const
{
return m_opcode;
}
void PacketHeader::set_opcode(const uint8_t opcode)
{
m_opcode = opcode;
}
void PacketHeader::write_to(std::ostream& ostream) const
{
ostream.put(m_control);
ostream.put(m_opcode);
ostream.put(0);
ostream.put(0);
}
void PacketHeader::read_from(std::istream& istream)
{
m_control = istream.get() >= 1;
if (istream.fail())
throw parse_error("Not enough data was available to read packet header. (m_control)");
m_opcode = istream.get();
if (istream.fail())
throw parse_error("Not enough data was available to read packet header. (m_opcode)");
istream.ignore(2);
if (istream.fail())
throw parse_error("Not enough data was available to read packet header. (ignored bytes)");
}
size_t PacketHeader::get_size() const
{
return 4;
}
}
}
}