Clean termination

This commit is contained in:
Anonymous275
2022-01-20 02:07:57 +02:00
parent 45099eba29
commit a6cb175eb4
4 changed files with 42 additions and 10 deletions
+4 -1
View File
@@ -25,6 +25,9 @@ public: //constructors
public: //available functions public: //available functions
static void StaticAbort(Launcher* Instance = nullptr); static void StaticAbort(Launcher* Instance = nullptr);
std::string Login(const std::string& fields); std::string Login(const std::string& fields);
static bool Terminated() noexcept;
static void setExit(bool exit);
static bool getExit();
void RunDiscordRPC(); void RunDiscordRPC();
void QueryRegistry(); void QueryRegistry();
void WaitForGame(); void WaitForGame();
@@ -62,7 +65,7 @@ private: //variables
std::string DiscordMessage{}; std::string DiscordMessage{};
std::string Version{"3.0"}; std::string Version{"3.0"};
std::string TargetBuild{"default"}; std::string TargetBuild{"default"};
std::atomic<bool> Shutdown{false}; static std::atomic<bool> Shutdown, Exit;
std::string FullVersion{Version + ".0"}; std::string FullVersion{Version + ".0"};
VersionParser SupportedVersion{"0.24.1.1"}; VersionParser SupportedVersion{"0.24.1.1"};
}; };
+23 -3
View File
@@ -7,6 +7,7 @@
#include "Launcher.h" #include "Launcher.h"
#include "Logger.h" #include "Logger.h"
#include "BeamNG.h" #include "BeamNG.h"
#include "Http.h"
#include <csignal> #include <csignal>
#include <windows.h> #include <windows.h>
#include <shellapi.h> #include <shellapi.h>
@@ -16,6 +17,7 @@ LONG WINAPI CrashHandler(EXCEPTION_POINTERS* p) {
return EXCEPTION_EXECUTE_HANDLER; return EXCEPTION_EXECUTE_HANDLER;
} }
std::atomic<bool> Launcher::Shutdown{false}, Launcher::Exit{false};
Launcher::Launcher(int argc, char* argv[]) : CurrentPath(std::filesystem::path(argv[0])), DiscordMessage("Just launched") { Launcher::Launcher(int argc, char* argv[]) : CurrentPath(std::filesystem::path(argv[0])), DiscordMessage("Just launched") {
Launcher::StaticAbort(this); Launcher::StaticAbort(this);
Log::Init(); Log::Init();
@@ -41,13 +43,20 @@ void Launcher::Abort() {
} }
Launcher::~Launcher() { Launcher::~Launcher() {
Abort(); if(!Shutdown.load()) {
Abort();
}
} }
void ShutdownHandler(int sig) { void ShutdownHandler(int sig) {
LOG(INFO) << "Got signal (" << sig << ") Launcher shutting down";
Launcher::StaticAbort(); 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) { void Launcher::StaticAbort(Launcher* Instance) {
@@ -180,3 +189,14 @@ const std::string& Launcher::getUserRole() {
return UserRole; return UserRole;
} }
bool Launcher::Terminated() noexcept {
return Shutdown.load();
}
bool Launcher::getExit() {
return Exit.load();
}
void Launcher::setExit(bool exit) {
Exit.store(exit);
}
+14 -6
View File
@@ -2,10 +2,9 @@
/// Created by Anonymous275 on 1/17/22 /// Created by Anonymous275 on 1/17/22
/// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info. /// Copyright (c) 2021-present Anonymous275 read the LICENSE file for more info.
/// ///
#define CPPHTTPLIB_OPENSSL_SUPPORT #define CPPHTTPLIB_OPENSSL_SUPPORT
#include <cpp-httplib/httplib.h> #include <cpp-httplib/httplib.h>
#include "Launcher.h"
#include <iostream> #include <iostream>
#include "Logger.h" #include "Logger.h"
#include <fstream> #include <fstream>
@@ -14,6 +13,7 @@
#include <cmath> #include <cmath>
bool HTTP::isDownload = false; bool HTTP::isDownload = false;
std::atomic<httplib::Client*> CliRef = nullptr;
std::string HTTP::Get(const std::string &IP) { std::string HTTP::Get(const std::string &IP) {
static std::mutex Lock; static std::mutex Lock;
std::scoped_lock Guard(Lock); std::scoped_lock Guard(Lock);
@@ -21,7 +21,8 @@ std::string HTTP::Get(const std::string &IP) {
auto pos = IP.find('/',10); auto pos = IP.find('/',10);
httplib::Client cli(IP.substr(0, pos)); 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); auto res = cli.Get(IP.substr(pos).c_str(), ProgressBar);
std::string Ret; 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()); LOG(ERROR) << "HTTP Get failed on " << httplib::to_string(res.error());
} }
CliRef.store(nullptr);
return Ret; return Ret;
} }
@@ -47,7 +48,8 @@ std::string HTTP::Post(const std::string& IP, const std::string& Fields) {
auto pos = IP.find('/',10); auto pos = IP.find('/',10);
httplib::Client cli(IP.substr(0, pos)); 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; std::string Ret;
if(!Fields.empty()) { 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()); LOG(ERROR) << "HTTP Post failed on " << httplib::to_string(res.error());
} }
} }
CliRef.store(nullptr);
if(Ret.empty())return "-1"; if(Ret.empty())return "-1";
else return Ret; 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 << "."; for (i = 0; i < 25 - progress_bar_adv; i++)std::cout << ".";
std::cout << "]"; std::cout << "]";
} }
if(Launcher::Terminated()) {
CliRef.load()->stop();
std::cout << '\n';
isDownload = false;
throw ShutdownException("Interrupted");
}
return true; return true;
} }
+1
View File
@@ -24,5 +24,6 @@ int main(int argc, char* argv[]) {
LOG(FATAL) << e.what(); LOG(FATAL) << e.what();
} }
std::this_thread::sleep_for(std::chrono::seconds(5)); std::this_thread::sleep_for(std::chrono::seconds(5));
Launcher::setExit(true);
return 0; return 0;
} }