prototyping protocol stuff

This commit is contained in:
2022-04-23 03:04:41 +02:00
parent cc029ba804
commit a3901029a4
5 changed files with 221 additions and 7 deletions

View File

@@ -11,7 +11,11 @@ add_definitions("-std=c++2a -g")
include(${CMAKE_BINARY_DIR}/conanbuildinfo.cmake)
conan_basic_setup()
add_library(commons_lib include/commons.cpp)
add_library(commons_lib
include/commons.cpp
src/protocol.cpp
)
target_include_directories (commons_lib
PUBLIC
${CONAN_INCLUDE_DIRS}
@@ -23,6 +27,22 @@ target_link_libraries(commons_lib
${CONAN_LIBS}
)
add_executable(commons
src/main.cpp
)
target_include_directories(commons
PUBLIC
${CONAN_INCLUDE_DIRS}
include
commons_lib
)
target_link_directories(commons
PUBLIC
commons_lib
)
#enable_testing()
#add_subdirectory(tests)
set_target_properties(commons_lib PROPERTIES LINKER_LANGUAGE CXX)

View File

@@ -1,4 +1,5 @@
[requires]
glm/0.9.9.8
boost/1.78.0
spdlog/1.10.0
gtest/cci.20210126

180
include/protocol.hpp Normal file
View 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);
//}
}

View File

@@ -1,15 +1,27 @@
#include <iostream>
#include <boost/asio.hpp>
#include "crow.h"
#include "protocol.hpp"
int main()
{
crow::SimpleApp app;
using namespace commons::protocol;
auto draw_msg = draw_rectangle{};
CROW_ROUTE(app, "/")([]() {
return "Hello World";
});
for(int i = 0; i < 3; ++i)
{
std::cout << "Size of " << i << " is " << get_size(static_cast<Type>(i)) << "\n";
}
auto rectangle = std::make_unique<draw_pixel>();
void* void_ptr = reinterpret_cast<void*>(rectangle.get());
generic_message_base* rectangle_ptr = reinterpret_cast<generic_message_base*>(void_ptr);
std::cout << "Type: " << static_cast<int>(rectangle_ptr->get_type()) << "\n";
std::cout << "Size: " << static_cast<int>(rectangle_ptr->get_size()) << "\n";
auto* final_msg = get_object(rectangle_ptr->get_type(), rectangle_ptr);
app.port(18080).run();
return 0;
}

1
src/protocol.cpp Normal file
View File

@@ -0,0 +1 @@
#include "protocol.hpp"