Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions GhostServer/networkmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,9 @@ static void file_log(std::string str) {
#ifdef GHOST_GUI
# include <QVector>
# define GHOST_LOG(x) (file_log(x), emit this->OnNewEvent(QString::fromStdString(x)))
# define UI_EVENT(x) (emit this->UIEvent(x))
#else
# include <stdio.h>
# 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
Expand Down Expand Up @@ -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<std::tuple<sf::Packet, sf::IpAddress, unsigned short>>& buffer)
Expand Down Expand Up @@ -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<void()> callback) {
uiEventCallbacks.insert({uiEventCallbackId++, {type, callback}});
return uiEventCallbackId - 1;
}
18 changes: 12 additions & 6 deletions GhostServer/networkmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

#include <SFML/Network.hpp>

#include <vector>
#include <set>
#include <mutex>
#include <atomic>
#include <thread>
#include <cstdint>
#include <functional>
#include <map>
#include <mutex>
#include <set>
#include <thread>
#include <vector>

#ifdef GHOST_GUI
#include <QObject>
Expand Down Expand Up @@ -104,6 +105,9 @@ class NetworkManager

sf::Clock clock;

std::map<int, std::tuple<std::string, std::function<void()>>> uiEventCallbacks;
int uiEventCallbackId = 1;

void DoHeartbeats();

public:
Expand Down Expand Up @@ -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<void()> callback);
void UnregisterEventCallback(int id) { uiEventCallbacks.erase(id); }

#ifdef GHOST_GUI
signals:
void OnNewEvent(QString log);
void UIEvent(std::string event);
#endif


};