From 6ce16849df368776b7398862bc97b7083fce6aff Mon Sep 17 00:00:00 2001 From: Yousef Khadadeh Date: Mon, 27 Oct 2025 20:19:47 +0300 Subject: [PATCH] p2p.json can now be changed from config folder --- data/config/{p2p.json => default_p2p.json} | 0 include/Helpers/Networking/NetLibrary.cpp | 79 +++++++++++++++++----- include/Helpers/Networking/NetLibrary.hpp | 11 ++- src/MainProgram.cpp | 6 ++ src/MainProgram.hpp | 2 + src/World.cpp | 4 +- 6 files changed, 83 insertions(+), 19 deletions(-) rename data/config/{p2p.json => default_p2p.json} (100%) diff --git a/data/config/p2p.json b/data/config/default_p2p.json similarity index 100% rename from data/config/p2p.json rename to data/config/default_p2p.json diff --git a/include/Helpers/Networking/NetLibrary.cpp b/include/Helpers/Networking/NetLibrary.cpp index 6279659..39c3150 100644 --- a/include/Helpers/Networking/NetLibrary.cpp +++ b/include/Helpers/Networking/NetLibrary.cpp @@ -24,23 +24,15 @@ std::vector> NetLibrary::clients; std::vector> NetLibrary::servers; std::promise NetLibrary::wsPromise; -void NetLibrary::init() { - if(alreadyInitialized) - return; +NetLibrary::LoadP2PSettingsInPathResult NetLibrary::load_p2p_settings_in_path(const std::filesystem::path& p2pConfigPath) { + config.iceServers.clear(); + signalingAddr.clear(); - using json = nlohmann::json; - std::ifstream f("data/config/p2p.json"); - - std::vector stunServerList; - std::vector turnServerList; - - rtc::InitLogger(rtc::LogLevel::Info, [](rtc::LogLevel level, std::string message) { - Logger::get().log("INFO", "[LibDataChannel Log Level " + std::to_string(static_cast(level)) + "] " + message); - }); + std::ifstream f(p2pConfigPath); if(f.is_open()) { try { - json j; + nlohmann::json j; f >> j; j.at("signalingServer").get_to(signalingAddr); @@ -56,14 +48,43 @@ void NetLibrary::init() { config.iceServers.emplace_back(turnServer["url"].get(), turnServer["port"].get(), turnServer["username"].get(), turnServer["credential"].get()); } catch(...) { - throw std::runtime_error("[NetLibrary::init] Invalid config/p2p.json"); + return LoadP2PSettingsInPathResult::FAILED_TO_READ; } } else - throw std::runtime_error("[NetLibrary::init] Could not open config/p2p.json"); + return LoadP2PSettingsInPathResult::FAILED_TO_OPEN; f.close(); + return LoadP2PSettingsInPathResult::SUCCESS; +} + +void NetLibrary::init(const std::filesystem::path& p2pConfigPath) { + if(alreadyInitialized) + return; + + rtc::InitLogger(rtc::LogLevel::Info, [](rtc::LogLevel level, std::string message) { + Logger::get().log("INFO", "[LibDataChannel Log Level " + std::to_string(static_cast(level)) + "] " + message); + }); + + switch(load_p2p_settings_in_path(p2pConfigPath)) { + case LoadP2PSettingsInPathResult::SUCCESS: + Logger::get().log("INFO", "Successfully loaded local P2P configuration"); + break; + case LoadP2PSettingsInPathResult::FAILED_TO_READ: + Logger::get().log("USERINFO", "Invalid custom P2P config, using defaults"); + case LoadP2PSettingsInPathResult::FAILED_TO_OPEN: + switch(load_p2p_settings_in_path("data/config/default_p2p.json")) { + case LoadP2PSettingsInPathResult::SUCCESS: + Logger::get().log("INFO", "Successfully loaded default P2P configuration"); + break; + case LoadP2PSettingsInPathResult::FAILED_TO_READ: + throw std::runtime_error("[NetLibrary::init] Failed to read default_p2p.json"); + case LoadP2PSettingsInPathResult::FAILED_TO_OPEN: + throw std::runtime_error("[NetLibrary::init] Failed to open default_p2p.json"); + } + break; + } // Not sure why, but TLS verification fails on mac // Should be fixed later, but for now this is fine, as the signaling server doesn't have @@ -97,7 +118,7 @@ void NetLibrary::init() { if(!std::holds_alternative(data)) return; - json message = json::parse(std::get(data)); + nlohmann::json message = nlohmann::json::parse(std::get(data)); auto itID = message.find("id"); if(itID == message.end()) @@ -151,6 +172,32 @@ void NetLibrary::init() { alreadyInitialized = true; } +void NetLibrary::copy_default_p2p_config_to_path(const std::filesystem::path& newP2PConfigPath) { + std::ifstream checkIfFileAlreadyExists(newP2PConfigPath); + if(checkIfFileAlreadyExists.is_open()) { // Don't copy if P2P config exists already + Logger::get().log("INFO", "Local P2P configuration already exists"); + checkIfFileAlreadyExists.close(); + return; + } + std::ifstream defaultP2PConfig("data/config/default_p2p.json"); + if(defaultP2PConfig.is_open()) { + nlohmann::json j; + defaultP2PConfig >> j; + defaultP2PConfig.close(); + + std::ofstream newFile(newP2PConfigPath); + if(newFile.is_open()) { + newFile << std::setw(4) << j; + newFile.close(); + Logger::get().log("INFO", "New P2P configuration created"); + } + else + Logger::get().log("INFO", "[NetLibrary::copy_default_p2p_config_to_path] Could not open new p2p.json to write to"); + } + else + Logger::get().log("INFO", "[NetLibrary::copy_default_p2p_config_to_path] Could not open default_p2p.json"); +} + std::string NetLibrary::get_random_server_local_id() { return Random::get().alphanumeric_str(LOCALID_LEN); } diff --git a/include/Helpers/Networking/NetLibrary.hpp b/include/Helpers/Networking/NetLibrary.hpp index 127248e..54ef784 100644 --- a/include/Helpers/Networking/NetLibrary.hpp +++ b/include/Helpers/Networking/NetLibrary.hpp @@ -3,6 +3,7 @@ #include #include #include +#include typedef uint64_t MessageOrder; class NetClient; @@ -19,7 +20,8 @@ typedef uint8_t MessageCommandType; class NetLibrary { public: - static void init(); + static void init(const std::filesystem::path& p2pConfigPath); + static void copy_default_p2p_config_to_path(const std::filesystem::path& newP2PConfigPath); static void update(); static std::string get_random_server_local_id(); static const std::string& get_global_id(); @@ -43,6 +45,13 @@ class NetLibrary { static constexpr size_t MAX_BUFFERED_DATA_PER_CHANNEL = 64000; static constexpr std::chrono::seconds TIMEOUT_DURATION = std::chrono::seconds(30); + enum class LoadP2PSettingsInPathResult { + SUCCESS, + FAILED_TO_OPEN, + FAILED_TO_READ + }; + static LoadP2PSettingsInPathResult load_p2p_settings_in_path(const std::filesystem::path& p2pConfigPath); + static bool is_ordered_channel(std::string_view channelName); static MessageOrder calc_order_for_queued_message(std::string_view channelName, MessageOrder& mOrder); diff --git a/src/MainProgram.cpp b/src/MainProgram.cpp index 9672af5..3018045 100755 --- a/src/MainProgram.cpp +++ b/src/MainProgram.cpp @@ -158,6 +158,10 @@ void MainProgram::set_tab_to_close(size_t tabToClose) { setTabToClose = tabToClose; } +void MainProgram::init_net_library() { + NetLibrary::init(configPath / "p2p.json"); +} + void MainProgram::save_config() { std::ofstream f(configPath / "config.json"); if(f.is_open()) { @@ -227,6 +231,8 @@ void MainProgram::load_config() { toolbar.load_palettes(); toolbar.load_theme(); toolbar.load_licenses(); + + NetLibrary::copy_default_p2p_config_to_path(configPath / "p2p.json"); } void MainProgram::draw(SkCanvas* canvas) { diff --git a/src/MainProgram.hpp b/src/MainProgram.hpp index b627e0b..0cca3e8 100644 --- a/src/MainProgram.hpp +++ b/src/MainProgram.hpp @@ -90,6 +90,8 @@ class MainProgram { void save_config(); void load_config(); + void init_net_library(); + std::filesystem::path homePath; std::filesystem::path configPath; diff --git a/src/World.cpp b/src/World.cpp index e67a969..2d4f93b 100644 --- a/src/World.cpp +++ b/src/World.cpp @@ -36,7 +36,7 @@ World::World(MainProgram& initMain, OpenWorldInfo& worldInfo): netSource = worldInfo.netSource; if(worldInfo.conType == CONNECTIONTYPE_CLIENT || worldInfo.conType == CONNECTIONTYPE_SERVER) - NetLibrary::init(); + main.init_net_library(); drawData.rMan = &rMan; drawData.main = &main; @@ -254,7 +254,7 @@ void World::set_name(const std::string& n) { void World::start_hosting(const std::string& initNetSource, const std::string& serverLocalID) { if(conType != CONNECTIONTYPE_LOCAL) return; - NetLibrary::init(); + main.init_net_library(); con.init_local_p2p(*this, serverLocalID); drawProg.init_client_callbacks(); rMan.init_client_callbacks();