forked from sixonionpotatoes/commons
prototyping protocol stuff
This commit is contained in:
180
include/protocol.hpp
Normal file
180
include/protocol.hpp
Normal file
@@ -0,0 +1,180 @@
|
||||
#pragma once
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
#include <glm/vec2.hpp>
|
||||
#include <glm/vec3.hpp>
|
||||
#include <map>
|
||||
|
||||
namespace commons::protocol
|
||||
{
|
||||
|
||||
enum class Type
|
||||
{
|
||||
DRAW_RECTANGLE = 0,
|
||||
DRAW_PIXEL,
|
||||
COMPOSED_MESSAGE,
|
||||
};
|
||||
|
||||
|
||||
struct header
|
||||
{
|
||||
Type type;
|
||||
uint32_t message_length;
|
||||
};
|
||||
|
||||
struct generic_message_base
|
||||
{
|
||||
virtual constexpr Type get_type() const = 0;
|
||||
virtual uint32_t get_size() const = 0;
|
||||
|
||||
//virtual void operator()()
|
||||
//{
|
||||
// if(is_handler)
|
||||
// {
|
||||
// handle_message();
|
||||
// return;
|
||||
// }
|
||||
|
||||
// create_message();
|
||||
//}
|
||||
|
||||
//virtual void handle_message() = 0;
|
||||
//virtual void create_message() = 0;
|
||||
|
||||
bool is_handler = false;
|
||||
};
|
||||
|
||||
template<Type TypeValue>
|
||||
struct generic_message : public generic_message_base
|
||||
{
|
||||
virtual ~generic_message() = default;
|
||||
|
||||
virtual constexpr Type get_type() const override
|
||||
{
|
||||
return type;
|
||||
}
|
||||
|
||||
virtual header get_header() const
|
||||
{
|
||||
return header{get_type(), get_size()};
|
||||
}
|
||||
|
||||
static constexpr Type type = TypeValue;
|
||||
};
|
||||
|
||||
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 composed_message : public generic_message<Type::COMPOSED_MESSAGE>
|
||||
{
|
||||
message** messages;
|
||||
};
|
||||
|
||||
struct draw_rectangle : public generic_message<Type::DRAW_RECTANGLE>
|
||||
{
|
||||
virtual ~draw_rectangle() = default;
|
||||
|
||||
virtual uint32_t get_size() const override
|
||||
{
|
||||
return sizeof(*this);
|
||||
}
|
||||
|
||||
glm::vec2 position;
|
||||
glm::vec2 size;
|
||||
glm::vec3 color;
|
||||
};
|
||||
|
||||
struct draw_pixel : public generic_message<Type::DRAW_PIXEL>
|
||||
{
|
||||
virtual ~draw_pixel() = default;
|
||||
|
||||
virtual uint32_t get_size() const override
|
||||
{
|
||||
return sizeof(*this);
|
||||
}
|
||||
|
||||
glm::vec2 position;
|
||||
glm::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];
|
||||
}
|
||||
|
||||
|
||||
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);
|
||||
//}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user