diff --git a/include/ki/protocol/Packet.h b/include/ki/protocol/Packet.h deleted file mode 100644 index ef56c05..0000000 --- a/include/ki/protocol/Packet.h +++ /dev/null @@ -1,66 +0,0 @@ -#pragma once -#include "../util/Serializable.h" -#include "exception.h" -#include -#include -#include -#include - -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 - 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); - std::string data_string = oss.str(); - m_payload.assign(data_string.begin(), data_string.end()); - } - - template - DataT *get_payload_data() const - { - static_assert(std::is_base_of::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 m_payload; - }; -} -} diff --git a/include/ki/protocol/net/PacketHeader.h b/include/ki/protocol/net/PacketHeader.h new file mode 100644 index 0000000..702cb3b --- /dev/null +++ b/include/ki/protocol/net/PacketHeader.h @@ -0,0 +1,32 @@ +#pragma once +#include "../../util/Serializable.h" +#include + +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; + }; +} +} +} \ No newline at end of file diff --git a/src/protocol/Packet.cpp b/src/protocol/Packet.cpp deleted file mode 100644 index 1223793..0000000 --- a/src/protocol/Packet.cpp +++ /dev/null @@ -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(); - } - - 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(); - } -} -} diff --git a/src/protocol/net/PacketHeader.cpp b/src/protocol/net/PacketHeader.cpp new file mode 100644 index 0000000..db64ae0 --- /dev/null +++ b/src/protocol/net/PacketHeader.cpp @@ -0,0 +1,64 @@ +#include "ki/protocol/net/PacketHeader.h" +#include "ki/protocol/exception.h" +#include + +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; + } +} +} +}