feat: added singleton logic to Client-/Server-Node
This commit is contained in:
parent
8086924141
commit
0703b30ed9
4 changed files with 23 additions and 0 deletions
|
|
@ -1,6 +1,7 @@
|
|||
#include "client_node.h"
|
||||
#include "ydi_client.h"
|
||||
|
||||
ClientNode *ClientNode::singleton_instance{ nullptr };
|
||||
String const ClientNode::sig_connection_changed{ "connection_changed" };
|
||||
|
||||
void ClientNode::_bind_methods() {
|
||||
|
|
@ -8,6 +9,13 @@ void ClientNode::_bind_methods() {
|
|||
ADD_SIGNAL(MethodInfo(sig_connection_changed, PropertyInfo(Variant::INT, "connected", PROPERTY_HINT_ENUM, "Disconnected,Connected,Authenticated")));
|
||||
}
|
||||
|
||||
void ClientNode::enter_tree() {
|
||||
if (singleton_instance) {
|
||||
print_error("Attempt to create duplicate ClientNode, aborting");
|
||||
abort();
|
||||
}
|
||||
}
|
||||
|
||||
void ClientNode::process() {
|
||||
NetworkData::ConnectionStatus const new_status{ ydi::client::status() };
|
||||
if (new_status != this->state) {
|
||||
|
|
@ -18,6 +26,9 @@ void ClientNode::process() {
|
|||
|
||||
void ClientNode::exit_tree() {
|
||||
ydi::client::disconnect();
|
||||
if (singleton_instance == this) {
|
||||
singleton_instance = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void ClientNode::_notification(int what) {
|
||||
|
|
|
|||
|
|
@ -5,7 +5,9 @@
|
|||
|
||||
class ClientNode : Node {
|
||||
GDCLASS(ClientNode, Node);
|
||||
static ClientNode *singleton_instance;
|
||||
static void _bind_methods();
|
||||
void enter_tree();
|
||||
void process();
|
||||
void exit_tree();
|
||||
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@
|
|||
#include "ydi_server.h"
|
||||
#include <core/config/engine.h>
|
||||
|
||||
ServerNode *ServerNode::singleton_instance{ nullptr };
|
||||
String const ServerNode::sig_clue_revealed{ "clue_revealed" };
|
||||
String const ServerNode::sig_connection_established{ "connection_established" };
|
||||
String const ServerNode::sig_connection_lost{ "connection_lost" };
|
||||
|
|
@ -13,6 +14,11 @@ void ServerNode::_bind_methods() {
|
|||
}
|
||||
|
||||
void ServerNode::enter_tree() {
|
||||
if (singleton_instance) {
|
||||
print_error("Attempt to create duplicate ServerNode, aborting");
|
||||
abort();
|
||||
}
|
||||
singleton_instance = this;
|
||||
ydi::server::open();
|
||||
}
|
||||
|
||||
|
|
@ -32,6 +38,9 @@ void ServerNode::process(double delta) {
|
|||
|
||||
void ServerNode::exit_tree() {
|
||||
ydi::server::close();
|
||||
if (singleton_instance == this) {
|
||||
singleton_instance = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
void ServerNode::_notification(int what) {
|
||||
|
|
|
|||
|
|
@ -4,6 +4,7 @@
|
|||
|
||||
class ServerNode : public Node {
|
||||
GDCLASS(ServerNode, Node);
|
||||
static ServerNode *singleton_instance;
|
||||
static void _bind_methods();
|
||||
void enter_tree();
|
||||
void process(double delta);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue