diff --git a/CMakeLists.txt b/CMakeLists.txt index b4aa0e3..44d56ff 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -62,7 +62,6 @@ message(STATUS "Adding local source dependencies") include_directories("asio/asio/include") include_directories("rapidjson/include") include_directories("websocketpp") -add_subdirectory("socket.io-client-cpp") add_subdirectory("include/commandline") @@ -104,7 +103,6 @@ target_include_directories(BeamMP-Server PUBLIC ${Boost_INCLUDE_DIRS} ${LUA_INCLUDE_DIR} ${CURL_INCLUDE_DIRS} - "socket.io-client-cpp/src" "include/tomlplusplus" "include/sentry-native/include" "include/curl/include") @@ -142,4 +140,4 @@ elseif (WIN32) commandline sioclient_tls sentry) -endif () \ No newline at end of file +endif () diff --git a/include/Common.h b/include/Common.h index cc5fb94..bdcd7ab 100644 --- a/include/Common.h +++ b/include/Common.h @@ -91,7 +91,7 @@ private: static inline std::mutex mShutdownHandlersMutex {}; static inline std::deque mShutdownHandlers {}; - static inline Version mVersion { 2, 3, 0 }; + static inline Version mVersion { 2, 4, 0 }; }; std::string ThreadName(bool DebugModeOverride = false); diff --git a/include/SocketIO.h b/include/SocketIO.h deleted file mode 100644 index 47c090e..0000000 --- a/include/SocketIO.h +++ /dev/null @@ -1,69 +0,0 @@ -#pragma once - -#include -#include -#include -#include -#include -#include - -/* - * We send relevant server events over socket.io to the backend. - * - * We send all events to `backend.beammp.com`, to the room `/key` - * where `key` is the currently active auth-key. - */ - -enum class SocketIOEvent { - ConsoleOut, - CPUUsage, - MemoryUsage, - NetworkUsage, - PlayerList, -}; - -enum class SocketIORoom { - None, - Stats, - Player, - Info, - Console, -}; - -class SocketIO final { -private: - struct Event; - -public: - enum class EventType { - }; - - // Singleton pattern - static SocketIO& Get(); - - void Emit(SocketIOEvent Event, const std::string& Data); - - ~SocketIO(); - - void SetAuthenticated(bool auth) { mAuthenticated = auth; } - -private: - SocketIO() noexcept; - - void ThreadMain(); - - struct Event { - std::string Name; - std::string Data; - }; - - bool mAuthenticated { false }; - sio::client mClient; - std::thread mThread; - std::atomic_bool mCloseThread { false }; - std::mutex mQueueMutex; - std::deque mQueue; - - friend std::unique_ptr std::make_unique(); -}; - diff --git a/src/SocketIO.cpp b/src/SocketIO.cpp deleted file mode 100644 index 2c10f6f..0000000 --- a/src/SocketIO.cpp +++ /dev/null @@ -1,98 +0,0 @@ -#include "SocketIO.h" -#include "Common.h" -#include - - -//TODO Default disabled with config option -static std::unique_ptr SocketIOInstance = std::make_unique(); - -SocketIO& SocketIO::Get() { - return *SocketIOInstance; -} - -SocketIO::SocketIO() noexcept - : mThread([this] { ThreadMain(); }) { - - mClient.socket()->on("network", [&](sio::event&e) { - if(e.get_message()->get_string() == "Welcome"){ - info("SocketIO Authenticated!"); - mAuthenticated = true; - } - }); - - mClient.socket()->on("welcome", [&](sio::event&) { - info("Got welcome from backend! Authenticating SocketIO..."); - mClient.socket()->emit("onInitConnection", Application::Settings.Key); - }); - - mClient.set_logs_quiet(); - mClient.set_reconnect_delay(10000); - mClient.connect(Application::GetBackendUrlForSocketIO()); -} - -SocketIO::~SocketIO() { - mCloseThread.store(true); - mThread.join(); -} - - -static constexpr auto EventNameFromEnum(SocketIOEvent Event) { - switch (Event) { - case SocketIOEvent::CPUUsage: - return "cpu usage"; - case SocketIOEvent::MemoryUsage: - return "memory usage"; - case SocketIOEvent::ConsoleOut: - return "console out"; - case SocketIOEvent::NetworkUsage: - return "network usage"; - case SocketIOEvent::PlayerList: - return "player list"; - default: - error("unreachable code reached (developer error)"); - abort(); - } -} - -void SocketIO::Emit(SocketIOEvent Event, const std::string& Data) { - if (!mAuthenticated) { - debug("trying to emit a socket.io event when not yet authenticated"); - return; - } - std::string EventName = EventNameFromEnum(Event); - debug("emitting event \"" + EventName + "\" with data: \"" + Data); - std::unique_lock Lock(mQueueMutex); - mQueue.push_back({EventName, Data }); - debug("queue now has " + std::to_string(mQueue.size()) + " events"); -} - -void SocketIO::ThreadMain() { - while (!mCloseThread.load()) { - bool empty; - { // queue lock scope - std::unique_lock Lock(mQueueMutex); - empty = mQueue.empty(); - } // end queue lock scope - if (empty || !mClient.opened()) { - std::this_thread::sleep_for(std::chrono::milliseconds(100)); - continue; - } else { - Event TheEvent; - { // queue lock scope - std::unique_lock Lock(mQueueMutex); - TheEvent = mQueue.front(); - mQueue.pop_front(); - } // end queue lock scope - debug("sending \"" + TheEvent.Name + "\" event"); - mClient.socket()->emit(TheEvent.Name, TheEvent.Data); - debug("sent \"" + TheEvent.Name + "\" event"); - } - } - // using std::cout as this happens during static destruction and the logger might be dead already - std::cout << "closing " + std::string(__func__) << std::endl; - - mClient.sync_close(); - mClient.clear_con_listeners(); - - std::cout << "closed" << std::endl; -}