diff --git a/GhostServer/networkmanager.cpp b/GhostServer/networkmanager.cpp index 9fc7e38..a1fd04c 100644 --- a/GhostServer/networkmanager.cpp +++ b/GhostServer/networkmanager.cpp @@ -23,11 +23,9 @@ static void file_log(std::string str) { #ifdef GHOST_GUI # include # define GHOST_LOG(x) (file_log(x), emit this->OnNewEvent(QString::fromStdString(x))) -# define UI_EVENT(x) (emit this->UIEvent(x)) #else # include # define GHOST_LOG(x) (file_log(x), printf("[LOG] %s\n", std::string(x).c_str())) -# define UI_EVENT(x) ((void)0) #endif #define HEARTBEAT_RATE 5000 @@ -372,10 +370,11 @@ void NetworkManager::CheckConnection() c.tcpSocket->send(packet_notify_all); } - UI_EVENT("client_change"); GHOST_LOG("Connection: " + client.name + " (" + (client.spectator ? "spectator" : "player") + ") @ " + client.IP.toString() + ":" + std::to_string(client.port)); this->clients.push_back(std::move(client)); + + UI_EVENT("client_change"); } void NetworkManager::ReceiveUDPUpdates(std::vector>& buffer) @@ -658,3 +657,19 @@ bool NetworkManager::IsOnWhitelist(std::string name, sf::IpAddress IP) { return index != whitelist.end(); } + + +void NetworkManager::UI_EVENT(std::string event) { + for (auto item : uiEventCallbacks) { + auto [type, callback] = item.second; + if (type == event) callback(); + } +#ifdef GHOST_GUI + emit this->UIEvent(event); +#endif +} + +int NetworkManager::RegisterEventCallback(std::string type, std::function callback) { + uiEventCallbacks.insert({uiEventCallbackId++, {type, callback}}); + return uiEventCallbackId - 1; +} diff --git a/GhostServer/networkmanager.h b/GhostServer/networkmanager.h index 1f90a33..0bece3f 100644 --- a/GhostServer/networkmanager.h +++ b/GhostServer/networkmanager.h @@ -2,13 +2,14 @@ #include -#include -#include -#include #include -#include #include #include +#include +#include +#include +#include +#include #ifdef GHOST_GUI #include @@ -104,6 +105,9 @@ class NetworkManager sf::Clock clock; + std::map>> uiEventCallbacks; + int uiEventCallbackId = 1; + void DoHeartbeats(); public: @@ -144,11 +148,13 @@ class NetworkManager bool IsOnWhitelist(std::string name, sf::IpAddress IP); + void UI_EVENT(std::string event); + int RegisterEventCallback(std::string type, std::function callback); + void UnregisterEventCallback(int id) { uiEventCallbacks.erase(id); } + #ifdef GHOST_GUI signals: void OnNewEvent(QString log); void UIEvent(std::string event); #endif - - };