forked from sixonionpotatoes/commons
add basic serialization functionallity
This commit is contained in:
@@ -1,111 +1,94 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <boost/asio.hpp>
|
||||
#include <cstddef>
|
||||
|
||||
#include <cstring>
|
||||
#include <glm/vec2.hpp>
|
||||
#include <glm/vec3.hpp>
|
||||
#include <map>
|
||||
#include <memory>
|
||||
|
||||
namespace commons::protocol
|
||||
{
|
||||
#include "spdlog/spdlog.h"
|
||||
|
||||
enum class Type
|
||||
{
|
||||
namespace commons::protocol {
|
||||
|
||||
enum class Type {
|
||||
DRAW_RECTANGLE = 0,
|
||||
DRAW_PIXEL,
|
||||
COMPOSED_MESSAGE,
|
||||
};
|
||||
|
||||
class generic_message_base
|
||||
: std::enable_shared_from_this<generic_message_base> {
|
||||
public:
|
||||
virtual ~generic_message_base() { spdlog::debug("base dtor"); }
|
||||
virtual constexpr Type get_type() const = 0;
|
||||
virtual uint32_t get_size() const = 0;
|
||||
|
||||
struct header
|
||||
{
|
||||
Type type;
|
||||
uint32_t message_length;
|
||||
auto get_shared_ptr() { return shared_from_this(); }
|
||||
|
||||
auto serialize() const {
|
||||
const auto* void_ptr = reinterpret_cast<const void*>(this);
|
||||
return boost::asio::buffer(void_ptr, get_size());
|
||||
}
|
||||
|
||||
virtual void operator()() {
|
||||
if (is_handler) {
|
||||
handle_message();
|
||||
return;
|
||||
}
|
||||
|
||||
create_message();
|
||||
}
|
||||
|
||||
virtual void handle_message() = 0;
|
||||
virtual void create_message() {
|
||||
spdlog::debug("create_message is not implemented yet");
|
||||
}
|
||||
|
||||
protected:
|
||||
bool is_handler = true;
|
||||
};
|
||||
|
||||
struct generic_message_base
|
||||
{
|
||||
virtual constexpr Type get_type() const = 0;
|
||||
virtual uint32_t get_size() const = 0;
|
||||
class draw_rectangle;
|
||||
|
||||
//virtual void operator()()
|
||||
//{
|
||||
// if(is_handler)
|
||||
// {
|
||||
// handle_message();
|
||||
// return;
|
||||
// }
|
||||
template <Type TypeValue>
|
||||
class generic_message : public generic_message_base {
|
||||
public:
|
||||
virtual ~generic_message() = default;
|
||||
virtual constexpr Type get_type() const override { return type_; }
|
||||
|
||||
// create_message();
|
||||
//}
|
||||
|
||||
//virtual void handle_message() = 0;
|
||||
//virtual void create_message() = 0;
|
||||
|
||||
bool is_handler = false;
|
||||
protected:
|
||||
static constexpr Type type_ = TypeValue;
|
||||
};
|
||||
|
||||
template<Type TypeValue>
|
||||
struct generic_message : public generic_message_base
|
||||
{
|
||||
virtual ~generic_message() = default;
|
||||
inline std::unique_ptr<generic_message_base> deserialize(
|
||||
char* buffer, std::size_t object_size) {
|
||||
void* base_ptr = std::malloc(object_size);
|
||||
std::memcpy(base_ptr, buffer, object_size);
|
||||
|
||||
virtual constexpr Type get_type() const override
|
||||
{
|
||||
return type;
|
||||
}
|
||||
return std::unique_ptr<generic_message_base>(
|
||||
reinterpret_cast<generic_message_base*>(base_ptr));
|
||||
}
|
||||
|
||||
virtual header get_header() const
|
||||
{
|
||||
return header{get_type(), get_size()};
|
||||
}
|
||||
|
||||
static constexpr Type type = TypeValue;
|
||||
struct vec2 {
|
||||
int x, y;
|
||||
};
|
||||
|
||||
struct message
|
||||
{
|
||||
//header head;
|
||||
//generic_message_base* msg;
|
||||
|
||||
//bool init_header()
|
||||
//{
|
||||
// if(!msg)
|
||||
// {
|
||||
// return false;
|
||||
// }
|
||||
|
||||
// head = msg->get_header();
|
||||
// return true;
|
||||
//}
|
||||
|
||||
//uint32_t get_size() const
|
||||
//{
|
||||
//}
|
||||
struct vec3 {
|
||||
int x, y, z;
|
||||
};
|
||||
|
||||
struct composed_message : public generic_message<Type::COMPOSED_MESSAGE>
|
||||
{
|
||||
message** messages;
|
||||
};
|
||||
class draw_rectangle : public generic_message<Type::DRAW_RECTANGLE> {
|
||||
public:
|
||||
virtual ~draw_rectangle() { spdlog::debug("draw_rect dtor"); }
|
||||
|
||||
struct vec2
|
||||
{
|
||||
int x,y;
|
||||
};
|
||||
virtual uint32_t get_size() const override { return sizeof(*this); }
|
||||
|
||||
struct vec3
|
||||
{
|
||||
int x,y,z;
|
||||
};
|
||||
|
||||
struct draw_rectangle : public generic_message<Type::DRAW_RECTANGLE>
|
||||
{
|
||||
virtual ~draw_rectangle() = default;
|
||||
|
||||
virtual uint32_t get_size() const override
|
||||
{
|
||||
return sizeof(*this);
|
||||
virtual void handle_message() override {
|
||||
spdlog::debug("draw_rectangle::handle_message()");
|
||||
spdlog::debug("Position: X = {}, Y = {}", position.x, position.y);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
vec2 position;
|
||||
@@ -113,78 +96,22 @@ struct draw_rectangle : public generic_message<Type::DRAW_RECTANGLE>
|
||||
vec3 color;
|
||||
};
|
||||
|
||||
struct draw_pixel : public generic_message<Type::DRAW_PIXEL>
|
||||
{
|
||||
virtual ~draw_pixel() = default;
|
||||
class draw_pixel : public generic_message<Type::DRAW_PIXEL> {
|
||||
public:
|
||||
virtual ~draw_pixel() { spdlog::debug("draw_pixel dtor"); }
|
||||
|
||||
virtual uint32_t get_size() const override
|
||||
{
|
||||
return sizeof(*this);
|
||||
virtual uint32_t get_size() const override { return sizeof(*this); }
|
||||
|
||||
virtual void handle_message() override {
|
||||
spdlog::debug("draw_pixel::handle_message()");
|
||||
spdlog::debug("Color: ({}, {}, {})", color.x, color.y, color.z);
|
||||
std::this_thread::sleep_for(std::chrono::seconds(1));
|
||||
}
|
||||
|
||||
vec2 position;
|
||||
vec3 color;
|
||||
};
|
||||
|
||||
//uint32_t get_size(Type type)
|
||||
//{
|
||||
// static std::map<Type, uint32_t> type_map{};
|
||||
//
|
||||
// //initalizes map on first run
|
||||
// static bool run_once = [&](){
|
||||
// type_map.emplace(Type::DRAW_PIXEL, sizeof(draw_pixel));
|
||||
// type_map.emplace(Type::DRAW_RECTANGLE, sizeof(draw_rectangle));
|
||||
// type_map.emplace(Type::COMPOSED_MESSAGE, sizeof(composed_message));
|
||||
// return true;
|
||||
// }();
|
||||
//
|
||||
// return type_map[type];
|
||||
//}
|
||||
class Serializer {};
|
||||
|
||||
|
||||
//generic_message_base* get_object(Type type, generic_message_base* msg)
|
||||
//{
|
||||
// switch(type)
|
||||
// {
|
||||
// case Type::COMPOSED_MESSAGE:
|
||||
// {
|
||||
// return reinterpret_cast<composed_message*>(msg);
|
||||
// }
|
||||
// case Type::DRAW_RECTANGLE:
|
||||
// {
|
||||
// return reinterpret_cast<draw_rectangle*>(msg);
|
||||
// }
|
||||
// case Type::DRAW_PIXEL:
|
||||
// {
|
||||
// return reinterpret_cast<draw_pixel*>(msg);
|
||||
// }
|
||||
// default:
|
||||
// return nullptr;
|
||||
// }
|
||||
//}
|
||||
|
||||
//template<Type T>
|
||||
//auto* get_object(generic_message_base* msg)
|
||||
//{
|
||||
// return nullptr;
|
||||
//}
|
||||
//
|
||||
//template<>
|
||||
//auto* get_object<Type::DRAW_PIXEL>(generic_message_base* msg)
|
||||
//{
|
||||
// return reinterpret_cast<draw_pixel*>(msg);
|
||||
//}
|
||||
//
|
||||
//template<>
|
||||
//auto* get_object<Type::DRAW_RECTANGLE>(generic_message_base* msg)
|
||||
//{
|
||||
// return reinterpret_cast<draw_rectangle*>(msg);
|
||||
//}
|
||||
//
|
||||
//template<>
|
||||
//auto* get_object<Type::COMPOSED_MESSAGE>(generic_message_base* msg)
|
||||
//{
|
||||
// return reinterpret_cast<composed_message*>(msg);
|
||||
//}
|
||||
|
||||
}
|
||||
} // namespace commons::protocol
|
||||
|
||||
Reference in New Issue
Block a user