From e6f4d6c66f9a7f3632afca5e95c9040f1e97e4d9 Mon Sep 17 00:00:00 2001 From: Joshua Scott Date: Mon, 2 Apr 2018 01:24:32 +0100 Subject: [PATCH] protocol: Packet class definition --- include/ki/protocol/Packet.h | 56 ++++++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 include/ki/protocol/Packet.h diff --git a/include/ki/protocol/Packet.h b/include/ki/protocol/Packet.h new file mode 100644 index 0000000..79e2bc2 --- /dev/null +++ b/include/ki/protocol/Packet.h @@ -0,0 +1,56 @@ +#pragma once +#include "../util/Serializable.h" +#include +#include +#include +#include + +namespace ki +{ +namespace protocol +{ + class Packet : 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 + void set_payload_data(const DataT &data) + { + static_assert(std::is_base_of::value, + "DataT must derive from Serializable."); + + std::ostringstream oss; + data.write_to(oss); + m_payload = std::vector(oss.str().c_str(), data.get_size()); + } + + template + DataT *get_payload_data() const + { + static_assert(std::is_base_of::value, + "DataT must derive from Serializable."); + + std::istringstream iss(m_payload.data()); + DataT *data = new DataT(); + data->read_from(iss); + return data; + } + + virtual void write_to(std::ostream &ostream) const final; + virtual void read_from(std::istream &istream) final; + virtual size_t get_size() const final; + private: + bool m_control; + uint8_t m_opcode; + std::vector m_payload; + }; +} +}