From a6cb175eb4ae7afbd688d3d957b67245eedf595f Mon Sep 17 00:00:00 2001 From: Anonymous275 <36374260+Anonymous-275@users.noreply.github.com> Date: Thu, 20 Jan 2022 02:07:57 +0200 Subject: [PATCH] Clean termination --- include/Launcher.h | 5 ++++- src/Launcher.cpp | 26 +++++++++++++++++++++++--- src/Network/Http.cpp | 20 ++++++++++++++------ src/main.cpp | 1 + 4 files changed, 42 insertions(+), 10 deletions(-) diff --git a/include/Launcher.h b/include/Launcher.h index cb70259..56540fa 100644 --- a/include/Launcher.h +++ b/include/Launcher.h @@ -25,6 +25,9 @@ public: //constructors public: //available functions static void StaticAbort(Launcher* Instance = nullptr); std::string Login(const std::string& fields); + static bool Terminated() noexcept; + static void setExit(bool exit); + static bool getExit(); void RunDiscordRPC(); void QueryRegistry(); void WaitForGame(); @@ -62,7 +65,7 @@ private: //variables std::string DiscordMessage{}; std::string Version{"3.0"}; std::string TargetBuild{"default"}; - std::atomic Shutdown{false}; + static std::atomic Shutdown, Exit; std::string FullVersion{Version + ".0"}; VersionParser SupportedVersion{"0.24.1.1"}; }; diff --git a/src/Launcher.cpp b/src/Launcher.cpp index 4753f41..01c6710 100644 --- a/src/Launcher.cpp +++ b/src/Launcher.cpp @@ -7,6 +7,7 @@ #include "Launcher.h" #include "Logger.h" #include "BeamNG.h" +#include "Http.h" #include #include #include @@ -16,6 +17,7 @@ LONG WINAPI CrashHandler(EXCEPTION_POINTERS* p) { return EXCEPTION_EXECUTE_HANDLER; } +std::atomic Launcher::Shutdown{false}, Launcher::Exit{false}; Launcher::Launcher(int argc, char* argv[]) : CurrentPath(std::filesystem::path(argv[0])), DiscordMessage("Just launched") { Launcher::StaticAbort(this); Log::Init(); @@ -41,13 +43,20 @@ void Launcher::Abort() { } Launcher::~Launcher() { - Abort(); + if(!Shutdown.load()) { + Abort(); + } } void ShutdownHandler(int sig) { - LOG(INFO) << "Got signal (" << sig << ") Launcher shutting down"; Launcher::StaticAbort(); - exit(sig); + while(HTTP::isDownload) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + } + LOG(INFO) << "Got termination signal (" << sig << ")"; + while(!Launcher::getExit()) { + std::this_thread::sleep_for(std::chrono::seconds(1)); + } } void Launcher::StaticAbort(Launcher* Instance) { @@ -180,3 +189,14 @@ const std::string& Launcher::getUserRole() { return UserRole; } +bool Launcher::Terminated() noexcept { + return Shutdown.load(); +} + +bool Launcher::getExit() { + return Exit.load(); +} + +void Launcher::setExit(bool exit) { + Exit.store(exit); +} diff --git a/src/Network/Http.cpp b/src/Network/Http.cpp index 84ce49d..5684967 100644 --- a/src/Network/Http.cpp +++ b/src/Network/Http.cpp @@ -2,10 +2,9 @@ /// Created by Anonymous275 on 1/17/22 /// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info. /// - #define CPPHTTPLIB_OPENSSL_SUPPORT - #include +#include "Launcher.h" #include #include "Logger.h" #include @@ -14,6 +13,7 @@ #include bool HTTP::isDownload = false; +std::atomic CliRef = nullptr; std::string HTTP::Get(const std::string &IP) { static std::mutex Lock; std::scoped_lock Guard(Lock); @@ -21,7 +21,8 @@ std::string HTTP::Get(const std::string &IP) { auto pos = IP.find('/',10); httplib::Client cli(IP.substr(0, pos)); - cli.set_connection_timeout(std::chrono::seconds(10)); + CliRef.store(&cli); + cli.set_connection_timeout(std::chrono::seconds(5)); auto res = cli.Get(IP.substr(pos).c_str(), ProgressBar); std::string Ret; @@ -36,7 +37,7 @@ std::string HTTP::Get(const std::string &IP) { } LOG(ERROR) << "HTTP Get failed on " << httplib::to_string(res.error()); } - + CliRef.store(nullptr); return Ret; } @@ -47,7 +48,8 @@ std::string HTTP::Post(const std::string& IP, const std::string& Fields) { auto pos = IP.find('/',10); httplib::Client cli(IP.substr(0, pos)); - cli.set_connection_timeout(std::chrono::seconds(10)); + CliRef.store(&cli); + cli.set_connection_timeout(std::chrono::seconds(5)); std::string Ret; if(!Fields.empty()) { @@ -72,7 +74,7 @@ std::string HTTP::Post(const std::string& IP, const std::string& Fields) { LOG(ERROR) << "HTTP Post failed on " << httplib::to_string(res.error()); } } - + CliRef.store(nullptr); if(Ret.empty())return "-1"; else return Ret; } @@ -90,6 +92,12 @@ bool HTTP::ProgressBar(size_t c, size_t t){ for (i = 0; i < 25 - progress_bar_adv; i++)std::cout << "."; std::cout << "]"; } + if(Launcher::Terminated()) { + CliRef.load()->stop(); + std::cout << '\n'; + isDownload = false; + throw ShutdownException("Interrupted"); + } return true; } diff --git a/src/main.cpp b/src/main.cpp index b902aa7..ec0536f 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -24,5 +24,6 @@ int main(int argc, char* argv[]) { LOG(FATAL) << e.what(); } std::this_thread::sleep_for(std::chrono::seconds(5)); + Launcher::setExit(true); return 0; }