forked from sixonionpotatoes/commons
128 lines
3.0 KiB
C++
128 lines
3.0 KiB
C++
#pragma once
|
|
|
|
#include <algorithm>
|
|
#include <boost/asio.hpp>
|
|
#include <cstddef>
|
|
#include <cstring>
|
|
#include <glm/vec2.hpp>
|
|
#include <glm/vec3.hpp>
|
|
#include <map>
|
|
#include <memory>
|
|
|
|
#include "spdlog/spdlog.h"
|
|
|
|
namespace commons::protocol {
|
|
|
|
enum class Type {
|
|
DRAW_RECTANGLE = 0,
|
|
DRAW_PIXEL,
|
|
};
|
|
|
|
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;
|
|
|
|
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");
|
|
}
|
|
|
|
virtual void handle_sent(const boost::system::error_code& er) {
|
|
if (!er) {
|
|
spdlog::debug("Message was sent");
|
|
} else {
|
|
spdlog::error("Error occured on client::send");
|
|
}
|
|
}
|
|
|
|
protected:
|
|
bool is_handler = true;
|
|
};
|
|
|
|
class draw_rectangle;
|
|
|
|
template <Type TypeValue>
|
|
class generic_message : public generic_message_base {
|
|
public:
|
|
virtual ~generic_message() = default;
|
|
virtual constexpr Type get_type() const override { return type_; }
|
|
|
|
protected:
|
|
static constexpr Type type_ = TypeValue;
|
|
};
|
|
|
|
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);
|
|
|
|
return std::unique_ptr<generic_message_base>(
|
|
reinterpret_cast<generic_message_base*>(base_ptr));
|
|
}
|
|
|
|
struct vec2 {
|
|
int x, y;
|
|
};
|
|
|
|
struct vec3 {
|
|
int x, y, z;
|
|
};
|
|
|
|
class draw_rectangle : public generic_message<Type::DRAW_RECTANGLE> {
|
|
public:
|
|
virtual ~draw_rectangle() { spdlog::debug("draw_rect dtor"); }
|
|
|
|
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);
|
|
spdlog::debug("handle_message sleeps for 1 sec to test concurrency");
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
}
|
|
|
|
vec2 position;
|
|
vec2 size;
|
|
vec3 color;
|
|
};
|
|
|
|
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 void handle_message() override {
|
|
spdlog::debug("draw_pixel::handle_message()");
|
|
spdlog::debug("Color: ({}, {}, {})", color.x, color.y, color.z);
|
|
spdlog::debug("handle_message sleeps for 1 sec to test concurrency");
|
|
std::this_thread::sleep_for(std::chrono::seconds(1));
|
|
}
|
|
|
|
vec2 position;
|
|
vec3 color;
|
|
};
|
|
|
|
class Serializer {};
|
|
|
|
} // namespace commons::protocol
|