feat: started building integration point
This commit is contained in:
parent
f565d330db
commit
dc1aed487e
7 changed files with 193 additions and 51 deletions
11
modules/you_done_it/flatscreen_root.cpp
Normal file
11
modules/you_done_it/flatscreen_root.cpp
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
#include "flatscreen_root.h"
|
||||
|
||||
void FlatscreenRoot::_bind_methods() {}
|
||||
|
||||
void FlatscreenRoot::enter_tree() {
|
||||
ydi::server::open();
|
||||
}
|
||||
|
||||
void FlatscreenRoot::exit_tree() {
|
||||
ydi::server::close();
|
||||
}
|
||||
13
modules/you_done_it/flatscreen_root.h
Normal file
13
modules/you_done_it/flatscreen_root.h
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
#pragma once
|
||||
|
||||
#include "ydi_server.h"
|
||||
#include <scene/gui/container.h>
|
||||
|
||||
class FlatscreenRoot : public Container {
|
||||
GDCLASS(FlatscreenRoot, Container);
|
||||
static void _bind_methods();
|
||||
void enter_tree();
|
||||
void exit_tree();
|
||||
protected:
|
||||
void _notification(int what);
|
||||
};
|
||||
|
|
@ -1,9 +1,6 @@
|
|||
#ifndef you_done_it_REGISTER_TYPES_H
|
||||
#define you_done_it_REGISTER_TYPES_H
|
||||
#pragma once
|
||||
|
||||
#include "modules/register_module_types.h"
|
||||
|
||||
void initialize_you_done_it_module(ModuleInitializationLevel p_level);
|
||||
void uninitialize_you_done_it_module(ModuleInitializationLevel p_level);
|
||||
|
||||
#endif // !you_done_it_REGISTER_TYPES_H
|
||||
|
|
|
|||
70
modules/you_done_it/ydi_networking.cpp
Normal file
70
modules/you_done_it/ydi_networking.cpp
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
#include "ydi_networking.h"
|
||||
#include <zmq.hpp>
|
||||
#include <zmq_addon.hpp>
|
||||
|
||||
namespace ydi {
|
||||
MessageType to_message_type(std::string const &msg) {
|
||||
int as_int{ std::stoi(msg) };
|
||||
if (as_int >= 0 && as_int < MESSAGE_TYPE_MAX) {
|
||||
return (MessageType)as_int;
|
||||
} else {
|
||||
return NONE;
|
||||
}
|
||||
}
|
||||
|
||||
MessageType message_type(zmq::multipart_t const &msg, size_t *type_idx) {
|
||||
MessageType type{ to_message_type(msg[0].to_string()) };
|
||||
size_t idx{ 0 };
|
||||
if (!type) {
|
||||
++idx;
|
||||
type = to_message_type(msg[0].to_string());
|
||||
}
|
||||
if (!type) {
|
||||
idx = std::numeric_limits<size_t>::max();
|
||||
}
|
||||
if (type_idx) {
|
||||
*type_idx = idx;
|
||||
}
|
||||
return type;
|
||||
|
||||
}
|
||||
|
||||
bool decompose_connect(zmq::multipart_t &msg, int *code) {
|
||||
size_t start{};
|
||||
MessageType actual_type{ message_type(msg, &start) };
|
||||
if (actual_type) {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
void extend_multipart(zmq::multipart_t &mpart, MessageType type) {
|
||||
mpart.addstr(std::to_string(type));
|
||||
}
|
||||
|
||||
void extend_multipart(zmq::multipart_t &mpart, ClueID id) {
|
||||
mpart.addstr(std::to_string(id));
|
||||
}
|
||||
|
||||
void extend_multipart(zmq::multipart_t &mpart, std::string const &string) {
|
||||
mpart.addstr(string);
|
||||
}
|
||||
|
||||
void extend_multipart(zmq::multipart_t &mpart, std::string_view const &strv) {
|
||||
mpart.addmem(strv.data(), strv.size());
|
||||
}
|
||||
|
||||
void extend_multipart(zmq::multipart_t &mpart, char const *cstr) {
|
||||
mpart.addstr(cstr);
|
||||
}
|
||||
|
||||
void extend_multipart(zmq::multipart_t &mpart, int const &arg) {
|
||||
std::string as_string{ std::to_string(arg) };
|
||||
mpart.addstr(as_string);
|
||||
}
|
||||
|
||||
void extend_multipart(zmq::multipart_t &mpart, zmq::multipart_t const &right) {
|
||||
mpart.append(right.clone());
|
||||
}
|
||||
|
||||
void extend_multipart(zmq::multipart_t &mpart) {}
|
||||
}
|
||||
|
|
@ -1,16 +1,58 @@
|
|||
#ifndef YDI_NETWORKING_H
|
||||
#define YDI_NETWORKING_H
|
||||
#pragma once
|
||||
|
||||
#include <zmq.h>
|
||||
#include <zmq.hpp>
|
||||
#include <zmq_addon.hpp>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
|
||||
namespace ydi {
|
||||
enum MessageType {
|
||||
NONE = 0u,
|
||||
CONNECTION_REQUEST = 1u,
|
||||
CONNECTION_RESPONSE = 2u,
|
||||
CONNECT = 1u,
|
||||
OK,
|
||||
NOK,
|
||||
MESSAGE_TYPE_MAX
|
||||
};
|
||||
|
||||
enum ClueID {
|
||||
FIRST_CLUE,
|
||||
};
|
||||
|
||||
MessageType to_message_type(std::string const &msg);
|
||||
MessageType message_type(zmq::multipart_t const &msg, size_t *type_idx = nullptr);
|
||||
|
||||
bool decompose_connect(zmq::multipart_t &msg, int *code);
|
||||
bool decompose_nok(zmq::multipart_t &msg, MessageType &failed);
|
||||
bool decompose_reveal_clue(zmq::multipart_t &msg, ClueID &id);
|
||||
|
||||
void extend_multipart(zmq::multipart_t &mpart, MessageType type);
|
||||
void extend_multipart(zmq::multipart_t &mpart, ClueID type);
|
||||
|
||||
void extend_multipart(zmq::multipart_t &mpart, std::string const &string);
|
||||
void extend_multipart(zmq::multipart_t &mpart, std::string_view const &strv);
|
||||
void extend_multipart(zmq::multipart_t &mpart, char const *cstr);
|
||||
void extend_multipart(zmq::multipart_t &mpart, int const &arg);
|
||||
void extend_multipart(zmq::multipart_t &mpart, zmq::multipart_t const &right);
|
||||
void extend_multipart(zmq::multipart_t &mpart);
|
||||
|
||||
template <typename TArg, typename... TArgs>
|
||||
void extend_multipart(zmq::multipart_t &mpart, TArg const &arg, TArgs const &... args) {
|
||||
extend_multipart(mpart, arg);
|
||||
extend_multipart(mpart, args...);
|
||||
}
|
||||
|
||||
#endif // !YDI_NETWORKING_H
|
||||
template <typename TArg>
|
||||
zmq::multipart_t multipart(TArg const &arg) {
|
||||
zmq::multipart_t mpart{};
|
||||
extend_multipart(mpart, arg);
|
||||
return mpart;
|
||||
}
|
||||
|
||||
template <typename TArg, typename... TArgs>
|
||||
zmq::multipart_t multipart(TArg const &arg, TArgs const &... args) {
|
||||
zmq::multipart_t mpart{ multipart(arg) };
|
||||
extend_multipart(mpart, args...);
|
||||
return mpart;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,4 +1,6 @@
|
|||
#include "ydi_server.h"
|
||||
#include "ydi_networking.h"
|
||||
#include <core/os/time.h>
|
||||
#include <core/templates/vector.h>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
|
|
@ -15,13 +17,46 @@ struct Service {
|
|||
std::optional<zmq::socket_t> socket{ std::nullopt };
|
||||
std::unordered_set<ClueID> revealedClues{};
|
||||
std::recursive_mutex mtx{};
|
||||
std::atomic<bool> stop_threads{ false };
|
||||
std::optional<std::string> client{ std::nullopt };
|
||||
double lastHeart{ 0.0 };
|
||||
double lastBeat{ 0.0 };
|
||||
|
||||
std::atomic<bool> stop_threads{ false };
|
||||
};
|
||||
|
||||
std::optional<Service> service{ std::nullopt };
|
||||
thread_local std::optional<std::thread> receiveThread{ std::nullopt };
|
||||
thread_local std::optional<std::thread> pingThread{ std::nullopt };
|
||||
std::optional<std::thread> receiveThread{ std::nullopt };
|
||||
std::optional<std::thread> pingThread{ std::nullopt };
|
||||
|
||||
void handle_reveal_clue(zmq::multipart_t const &message) {
|
||||
}
|
||||
|
||||
void handle_authorised_message(std::string_view const &sender, std::string_view const &type, zmq::multipart_t &message) {
|
||||
if (type == "BEAT") {
|
||||
service->lastBeat = Time::get_singleton()->get_unix_time_from_system();
|
||||
} else if(type == "REVEAL") {
|
||||
handle_reveal_clue(message);
|
||||
} else {
|
||||
multipart(sender, "NOK", "UNKOWN_COMMAND", message).send(*service->socket);
|
||||
}
|
||||
}
|
||||
|
||||
void handle_message(zmq::multipart_t &message) {
|
||||
std::string_view const sender{ message.at(0).to_string_view() };
|
||||
std::string_view const type{ message.at(1).to_string_view() };
|
||||
std::scoped_lock lock{ service->mtx };
|
||||
if (service->client) {
|
||||
if (sender != service->client) {
|
||||
multipart(sender,"NOK", "UNAUTHORIZED_REQUEST").send(*service->socket);
|
||||
}
|
||||
handle_authorised_message(sender, type, message);
|
||||
} else if (type == "CONNECT") {
|
||||
service->client.emplace(sender);
|
||||
multipart(sender, "OK", message).send(*service->socket);
|
||||
} else {
|
||||
multipart(sender,"NOK", "UNAUTHORIZED_REQUEST", message).send(*service->socket);
|
||||
}
|
||||
}
|
||||
|
||||
void receive_thread_entry() {
|
||||
using namespace std::chrono_literals;
|
||||
|
|
@ -30,6 +65,7 @@ void receive_thread_entry() {
|
|||
std::this_thread::sleep_for(20ms);
|
||||
std::scoped_lock lock{ service->mtx };
|
||||
if (incoming.recv(*service->socket)) {
|
||||
handle_message(incoming);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -41,11 +77,12 @@ void ping_thread_entry() {
|
|||
return;
|
||||
}
|
||||
}
|
||||
static zmq::multipart_t ping{ make_multipart(*service->client, std::string("PING")) };
|
||||
static zmq::multipart_t ping{ multipart(*service->client, "HEART") };
|
||||
while (!service->stop_threads) {
|
||||
std::this_thread::sleep_for(1s);
|
||||
std::scoped_lock lock{ service->mtx };
|
||||
ping.send(*service->socket);
|
||||
service->lastHeart = Time::get_singleton()->get_unix_time_from_system();
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -80,10 +117,14 @@ void open() {
|
|||
void close() {
|
||||
if (service) {
|
||||
std::scoped_lock lock{ service->mtx };
|
||||
service->socket->close();
|
||||
service->socket.reset();
|
||||
service->context->close();
|
||||
service->context.reset();
|
||||
if (service->socket) {
|
||||
service->socket->close();
|
||||
service->socket.reset();
|
||||
}
|
||||
if (service->context) {
|
||||
service->context->close();
|
||||
service->context.reset();
|
||||
}
|
||||
service.reset();
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,12 +1,10 @@
|
|||
#ifndef YDI_SERVER_H
|
||||
#define YDI_SERVER_H
|
||||
#pragma once
|
||||
|
||||
#include "ydi_networking.h"
|
||||
#include <cassert>
|
||||
#include <core/io/image.h>
|
||||
#include <core/string/ustring.h>
|
||||
#include <core/templates/vector.h>
|
||||
#include <zmq_addon.hpp>
|
||||
#include "ydi_networking.h"
|
||||
|
||||
namespace ydi::server {
|
||||
void open();
|
||||
|
|
@ -19,33 +17,3 @@ namespace send {
|
|||
void announceConclusion(ClueID method, ClueID motive, ClueID murderer);
|
||||
}
|
||||
}
|
||||
|
||||
template <typename TArg>
|
||||
void extend_multipart(zmq::multipart_t &, TArg const &arg);
|
||||
|
||||
template <>
|
||||
void extend_multipart(zmq::multipart_t &mpart, std::string const &string) {
|
||||
mpart.addstr(string);
|
||||
}
|
||||
|
||||
template <typename TArg>
|
||||
void extend_multipart(zmq::multipart_t &mpart, TArg const &arg) {
|
||||
std::string as_string{ std::to_string(arg) };
|
||||
mpart.addstr(as_string);
|
||||
}
|
||||
|
||||
template <typename TArg, typename... TArgs>
|
||||
void extend_multipart(zmq::multipart_t &mpart, TArg const &arg, TArgs const &... args) {
|
||||
extend_multipart(mpart, arg);
|
||||
extend_multipart(mpart, arg, args...);
|
||||
}
|
||||
|
||||
template <typename TArg, typename... TArgs>
|
||||
zmq::multipart_t make_multipart(std::string const &target, TArg const &arg, TArgs const &... args) {
|
||||
zmq::multipart_t mpart{};
|
||||
mpart.addstr(target);
|
||||
extend_multipart(mpart, arg, args...);
|
||||
return mpart;
|
||||
}
|
||||
|
||||
#endif // !YDI_SERVER_H
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue