feat: working on ydi server
This commit is contained in:
parent
ce2b4e7c7b
commit
019e326c6f
2 changed files with 104 additions and 5 deletions
|
|
@ -1,7 +1,11 @@
|
|||
#include "ydi_server.h"
|
||||
#include <core/templates/vector.h>
|
||||
#include <mutex>
|
||||
#include <optional>
|
||||
#include <stop_token>
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
#include <unordered_set>
|
||||
#include <zmq.hpp>
|
||||
#include <zmq_addon.hpp>
|
||||
|
||||
|
|
@ -9,12 +13,78 @@ namespace ydi::server {
|
|||
struct Service {
|
||||
std::optional<zmq::context_t> context{ std::nullopt };
|
||||
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 };
|
||||
};
|
||||
|
||||
std::optional<Service> service{ std::nullopt };
|
||||
thread_local std::optional<std::thread> receiveThread{ std::nullopt };
|
||||
thread_local std::optional<std::thread> pingThread{ std::nullopt };
|
||||
|
||||
void open(int port) {
|
||||
void receive_thread_entry() {
|
||||
using namespace std::chrono_literals;
|
||||
zmq::multipart_t incoming{};
|
||||
while (service->stop_threads) {
|
||||
std::this_thread::sleep_for(20ms);
|
||||
std::scoped_lock lock{ service->mtx };
|
||||
if (incoming.recv(*service->socket)) {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void ping_thread_entry() {
|
||||
using namespace std::chrono_literals;
|
||||
{ std::scoped_lock lock{ service->mtx };
|
||||
if (!service->client) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
static zmq::multipart_t ping{ make_multipart(*service->client, std::string("PING")) };
|
||||
while (!service->stop_threads) {
|
||||
std::this_thread::sleep_for(1s);
|
||||
std::scoped_lock lock{ service->mtx };
|
||||
ping.send(*service->socket);
|
||||
}
|
||||
}
|
||||
|
||||
void open() {
|
||||
service.emplace();
|
||||
try {
|
||||
service->context.emplace(1);
|
||||
} catch (...) {
|
||||
service.reset();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
service->socket.emplace(*service->context, zmq::socket_type::router);
|
||||
} catch (...) {
|
||||
service->context->close();
|
||||
service->context.reset();
|
||||
service.reset();
|
||||
return;
|
||||
}
|
||||
try {
|
||||
service->socket->connect("tcp://*:6667");
|
||||
} catch (...) {
|
||||
service->socket->close();
|
||||
service->socket.reset();
|
||||
service->context->close();
|
||||
service->context.reset();
|
||||
service.reset();
|
||||
}
|
||||
receiveThread.emplace(receive_thread_entry);
|
||||
}
|
||||
|
||||
void close() {
|
||||
if (service) {
|
||||
std::scoped_lock lock{ service->mtx };
|
||||
service->socket->close();
|
||||
service->socket.reset();
|
||||
service->context->close();
|
||||
service->context.reset();
|
||||
service.reset();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,14 +1,15 @@
|
|||
#ifndef YDI_SERVER_H
|
||||
#define YDI_SERVER_H
|
||||
|
||||
#include "ydi_networking.h"
|
||||
#include <cassert>
|
||||
#include <core/io/image.h>
|
||||
#include <string/ustring.h>
|
||||
#include <templates/vector.h>
|
||||
#include "ydi_networking.h"
|
||||
#include <core/string/ustring.h>
|
||||
#include <core/templates/vector.h>
|
||||
#include <zmq_addon.hpp>
|
||||
|
||||
namespace ydi::server {
|
||||
void open(int port);
|
||||
void open();
|
||||
void close();
|
||||
namespace receive {
|
||||
bool isRevealed(ClueID id);
|
||||
|
|
@ -19,4 +20,32 @@ 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