feat: improved ServerNode scriptability

This commit is contained in:
Sara Gerretsen 2025-10-29 17:19:18 +01:00
parent cfb901ec46
commit 6add0813d1
4 changed files with 42 additions and 18 deletions

View file

@ -13,6 +13,9 @@ void ServerNode::_bind_methods() {
ADD_SIGNAL(MethodInfo(sig_connection_lost)); ADD_SIGNAL(MethodInfo(sig_connection_lost));
ClassDB::bind_method(D_METHOD("open"), &self_type::open); ClassDB::bind_method(D_METHOD("open"), &self_type::open);
ClassDB::bind_method(D_METHOD("close"), &self_type::close);
ClassDB::bind_method(D_METHOD("is_open"), &self_type::is_open);
ClassDB::bind_static_method(self_type::get_class_static(), "get_singleton", &self_type::get_singleton);
} }
void ServerNode::enter_tree() { void ServerNode::enter_tree() {
@ -25,17 +28,20 @@ void ServerNode::enter_tree() {
} }
void ServerNode::process(double delta) { void ServerNode::process(double delta) {
bool new_is_connected{ ydi::server::has_client() };
if (this->is_connected != new_is_connected) {
this->is_connected = new_is_connected;
emit_signal(this->is_connected ? sig_connection_established : sig_connection_lost);
}
if (!ydi::server::has_client()) {
return;
}
Vector<NetworkData::ClueID> new_clues{}; Vector<NetworkData::ClueID> new_clues{};
if (ydi::server::receive::new_clues(new_clues)) { if (ydi::server::receive::new_clues(new_clues)) {
for (NetworkData::ClueID clue : new_clues) { for (NetworkData::ClueID clue : new_clues) {
emit_signal(sig_clue_revealed, clue); emit_signal(sig_clue_revealed, clue);
} }
} }
bool new_is_connected{ ydi::server::has_client() };
if (this->is_connected != new_is_connected) {
this->is_connected = new_is_connected;
emit_signal(this->is_connected ? sig_connection_established : sig_connection_lost);
}
} }
void ServerNode::exit_tree() { void ServerNode::exit_tree() {
@ -69,6 +75,14 @@ ServerNode *ServerNode::get_singleton() {
return singleton_instance; return singleton_instance;
} }
void ServerNode::open() { bool ServerNode::open() {
ydi::server::open(); return ydi::server::open();
}
void ServerNode::close() {
ydi::server::close();
}
bool ServerNode::is_open() const {
return ydi::server::is_running();
} }

View file

@ -15,7 +15,9 @@ protected:
public: public:
static ServerNode *get_singleton(); static ServerNode *get_singleton();
void open(); bool open();
void close();
bool is_open() const;
public: public:
static String const sig_clue_revealed; static String const sig_clue_revealed;

View file

@ -99,10 +99,10 @@ void receive_thread_entry() {
} }
} }
void open() { bool open() {
if (service) { if (service) {
print_error("Server: Detected attempt to open duplicate Server, exiting without action."); print_error("Server: Detected attempt to open duplicate Server, exiting without action.");
return; return false;
} }
print_line("Server: Starting"); print_line("Server: Starting");
service.emplace(); service.emplace();
@ -111,7 +111,7 @@ void open() {
} catch (...) { } catch (...) {
service.reset(); service.reset();
print_line("Server: Failed to create context"); print_line("Server: Failed to create context");
return; return false;
} }
print_line("Server: Created zmq context"); print_line("Server: Created zmq context");
try { try {
@ -121,7 +121,7 @@ void open() {
service->context.reset(); service->context.reset();
service.reset(); service.reset();
print_line("Server: Failed to create socket"); print_line("Server: Failed to create socket");
return; return false;
} }
print_line("Server: Created socket"); print_line("Server: Created socket");
try { try {
@ -133,12 +133,13 @@ void open() {
service->context.reset(); service->context.reset();
service.reset(); service.reset();
print_line("Server: Failed to bind socket"); print_line("Server: Failed to bind socket");
return; return false;
} }
print_line("Server: Bound socket"); print_line("Server: Bound socket");
receive_thread.emplace(receive_thread_entry); receive_thread.emplace(receive_thread_entry);
assert(receive_thread->joinable()); assert(receive_thread->joinable());
print_line("Server: Startup complete!"); print_line("Server: Startup complete!");
return true;
} }
void close() { void close() {
@ -170,16 +171,22 @@ void close() {
} }
bool has_client() { bool has_client() {
if (service) { if (!service) {
std::scoped_lock lock{ service->mtx };
return service->client.has_value();
} else {
return false; return false;
} }
std::scoped_lock lock{ service->mtx };
return service->client.has_value();
}
bool is_running() {
return service.has_value();
} }
namespace receive { namespace receive {
bool new_clues(Vector<NetworkData::ClueID> &out) { bool new_clues(Vector<NetworkData::ClueID> &out) {
if (!service) {
return false;
}
std::scoped_lock lock{ service->mtx }; std::scoped_lock lock{ service->mtx };
bool const has_new{ !service->new_clues.is_empty() }; bool const has_new{ !service->new_clues.is_empty() };
if (has_new) { if (has_new) {

View file

@ -5,9 +5,10 @@
#include <cassert> #include <cassert>
namespace ydi::server { namespace ydi::server {
void open(); bool open();
void close(); void close();
bool has_client(); bool has_client();
bool is_running();
namespace receive { namespace receive {
bool new_clues(Vector<NetworkData::ClueID> &out); bool new_clues(Vector<NetworkData::ClueID> &out);