From 0b516d3dc36c9075223095ad96083175455415bd Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 14 Aug 2021 23:37:22 +0300 Subject: [PATCH 1/2] Readded possibility to change comp-server connstring --- r5dev/include/CCompanion.h | 4 ++-- r5dev/src/CCompanion.cpp | 21 +++++++++++++++------ 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/r5dev/include/CCompanion.h b/r5dev/include/CCompanion.h index 395f6c4a..817ab31b 100644 --- a/r5dev/include/CCompanion.h +++ b/r5dev/include/CCompanion.h @@ -13,7 +13,7 @@ private: bool ThemeSet = false; public: CCompanion(); - + ~CCompanion(); //////////////////// // Enums // @@ -35,7 +35,7 @@ public: // Server Browser // //////////////////// - R5Net::Client r5net; + R5Net::Client* r5net; std::vector ServerList; ImGuiTextFilter ServerBrowserFilter; diff --git a/r5dev/src/CCompanion.cpp b/r5dev/src/CCompanion.cpp index fc579cc0..4576be09 100644 --- a/r5dev/src/CCompanion.cpp +++ b/r5dev/src/CCompanion.cpp @@ -15,7 +15,7 @@ CCompanion* g_ServerBrowser = nullptr; * _ccompanion.cpp *-----------------------------------------------------------------------------*/ -CCompanion::CCompanion() : MatchmakingServerStringBuffer("r5a-comp-sv.herokuapp.com"), r5net(R5Net::Client("r5a-comp-sv.herokuapp.com")) +CCompanion::CCompanion() : MatchmakingServerStringBuffer("r5a-comp-sv.herokuapp.com"), r5net(new R5Net::Client("r5a-comp-sv.herokuapp.com")) { memset(ServerConnStringBuffer, 0, sizeof(ServerConnStringBuffer)); @@ -44,6 +44,11 @@ CCompanion::CCompanion() : MatchmakingServerStringBuffer("r5a-comp-sv.herokuapp. HostingServerRequestThread.detach(); } +CCompanion::~CCompanion() +{ + delete r5net; +} + void CCompanion::UpdateHostingStatus() { if (!GameGlobals::HostState || !GameGlobals::Cvar) // Is HostState and Cvar valid? @@ -86,7 +91,7 @@ void CCompanion::RefreshServerList() std::cout << " [+CCompanion+] Refreshing server list with string " << MatchmakingServerStringBuffer << "\n"; #endif bThreadLocked = true; - ServerList = r5net.GetServersList(); + ServerList = r5net->GetServersList(); bThreadLocked = false; }); @@ -97,7 +102,7 @@ void CCompanion::RefreshServerList() void CCompanion::SendHostingPostRequest() { HostToken = ""; - bool result = r5net.PostServerHost(HostRequestMessage, HostToken, ServerListing{ MyServer.name, std::string(GameGlobals::HostState->m_levelName), "", GameGlobals::Cvar->FindVar("hostport")->m_pzsCurrentValue, MyServer.password}); + bool result = r5net->PostServerHost(HostRequestMessage, HostToken, ServerListing{ MyServer.name, std::string(GameGlobals::HostState->m_levelName), "", GameGlobals::Cvar->FindVar("hostport")->m_pzsCurrentValue, MyServer.password}); if (result) { HostRequestMessageColor = ImVec4(0.00f, 1.00f, 0.00f, 1.00f); @@ -307,7 +312,7 @@ void CCompanion::ServerBrowserSection() { PrivateServerRequestMessage = ""; ServerListing server; - bool result = r5net.GetServerByToken(server, PrivateServerRequestMessage, PrivateServerToken, PrivateServerPassword); // Send token connect request. + bool result = r5net->GetServerByToken(server, PrivateServerRequestMessage, PrivateServerToken, PrivateServerPassword); // Send token connect request. if (!server.name.empty()) { ConnectToServer(server.ip, server.port); // Connect to the server @@ -420,8 +425,12 @@ void CCompanion::HostServerSection() void CCompanion::SettingsSection() { - ImGui::Text("In renovation"); - //ImGui::InputText("Matchmaking Server String", MatchmakingServerStringBuffer, IM_ARRAYSIZE(MatchmakingServerStringBuffer), 0); + ImGui::InputTextWithHint("##MatchmakingServerString", "Matchmaking Server String", & MatchmakingServerStringBuffer); + if (ImGui::Button("Update Settings")) + { + if (r5net) delete r5net; + r5net = new R5Net::Client(MatchmakingServerStringBuffer); + } } void CCompanion::Draw(const char* title) From 0fde6763f358997279310a79df04b80bddcdae44 Mon Sep 17 00:00:00 2001 From: Alex Date: Sat, 14 Aug 2021 23:55:33 +0300 Subject: [PATCH 2/2] Fixed some crashes when server could not be reached --- r5net/src/r5net.cpp | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/r5net/src/r5net.cpp b/r5net/src/r5net.cpp index c7d3d916..0adf7ae2 100644 --- a/r5net/src/r5net.cpp +++ b/r5net/src/r5net.cpp @@ -10,7 +10,7 @@ std::vector R5Net::Client::GetServersList() auto res = m_HttpClient.Get("/servers"); - if (res) + if (!res) return std::vector(); { nlohmann::json root = nlohmann::json::parse(res->body); for (auto obj : root["servers"]) @@ -35,9 +35,15 @@ bool R5Net::Client::PostServerHost(std::string& outMessage, std::string& outToke auto res = m_HttpClient.Post("/servers/add", reqBodyStr.c_str(), reqBodyStr.length(), "application/json"); - + if (!res) + { + outMessage = "Failed to reach comp-server"; + outToken = ""; + return false; + } + nlohmann::json resBody = nlohmann::json::parse(res->body); - if (res && resBody["success"].is_boolean() && resBody["success"]) + if (resBody["success"].is_boolean() && resBody["success"]) { outMessage = "Broadcasting!"; @@ -67,7 +73,13 @@ bool R5Net::Client::GetServerByToken(ServerListing& outServer, std::string& outE httplib::Result res = m_HttpClient.Post("/server/byToken", reqBody.dump().c_str(), reqBody.dump().length(), "application/json"); - std::cout << "YEEEEEEEEEEEEEE" << res->body << "\n"; + if (!res) + { + outError = "Failed to reach comp-server"; + outServer = ServerListing{}; + return false; + } + nlohmann::json resBody = nlohmann::json::parse(res->body); if (res && resBody["success"].is_boolean() && resBody["success"])