mirror of
https://github.com/BeamMP/BeamMP-Launcher.git
synced 2026-04-04 14:56:24 +00:00
Clean termination
This commit is contained in:
@@ -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<bool> Shutdown{false};
|
||||
static std::atomic<bool> Shutdown, Exit;
|
||||
std::string FullVersion{Version + ".0"};
|
||||
VersionParser SupportedVersion{"0.24.1.1"};
|
||||
};
|
||||
|
||||
@@ -7,6 +7,7 @@
|
||||
#include "Launcher.h"
|
||||
#include "Logger.h"
|
||||
#include "BeamNG.h"
|
||||
#include "Http.h"
|
||||
#include <csignal>
|
||||
#include <windows.h>
|
||||
#include <shellapi.h>
|
||||
@@ -16,6 +17,7 @@ LONG WINAPI CrashHandler(EXCEPTION_POINTERS* p) {
|
||||
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::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);
|
||||
}
|
||||
|
||||
@@ -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 <cpp-httplib/httplib.h>
|
||||
#include "Launcher.h"
|
||||
#include <iostream>
|
||||
#include "Logger.h"
|
||||
#include <fstream>
|
||||
@@ -14,6 +13,7 @@
|
||||
#include <cmath>
|
||||
|
||||
bool HTTP::isDownload = false;
|
||||
std::atomic<httplib::Client*> 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;
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user