2018-04-02 00:24:32 +00:00
|
|
|
#pragma once
|
|
|
|
#include "../util/Serializable.h"
|
|
|
|
#include <cstdint>
|
|
|
|
#include <vector>
|
|
|
|
#include <sstream>
|
|
|
|
#include <type_traits>
|
|
|
|
|
|
|
|
namespace ki
|
|
|
|
{
|
|
|
|
namespace protocol
|
|
|
|
{
|
2018-04-03 16:02:30 +00:00
|
|
|
class Packet final : public util::Serializable
|
2018-04-02 00:24:32 +00:00
|
|
|
{
|
|
|
|
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);
|
2018-04-03 15:43:00 +00:00
|
|
|
std::string data_string = oss.str();
|
|
|
|
m_payload.assign(data_string.begin(), data_string.end());
|
2018-04-02 00:24:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
template <typename DataT>
|
|
|
|
DataT *get_payload_data() const
|
|
|
|
{
|
|
|
|
static_assert(std::is_base_of<Serializable, DataT>::value,
|
|
|
|
"DataT must derive from Serializable.");
|
|
|
|
|
2018-04-03 20:36:49 +00:00
|
|
|
std::istringstream iss(std::string(m_payload.data(), m_payload.size()));
|
2018-04-02 00:24:32 +00:00
|
|
|
DataT *data = new DataT();
|
|
|
|
data->read_from(iss);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
2018-04-03 16:02:30 +00:00
|
|
|
void write_to(std::ostream &ostream) const override final;
|
|
|
|
void read_from(std::istream &istream) override final;
|
|
|
|
size_t get_size() const override final;
|
2018-04-02 00:24:32 +00:00
|
|
|
private:
|
|
|
|
bool m_control;
|
|
|
|
uint8_t m_opcode;
|
|
|
|
std::vector<char> m_payload;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|