From 0979c8b1e47885a898003c1573a03eb4a262f7b6 Mon Sep 17 00:00:00 2001 From: Lion Kortlepel Date: Thu, 28 Apr 2022 14:04:54 +0200 Subject: [PATCH] HTTPServer: Attempt to catch more errors --- include/Client.h | 2 +- src/Client.cpp | 1 + src/Http.cpp | 12 ++++++++++-- src/main.cpp | 9 ++++++++- 4 files changed, 20 insertions(+), 4 deletions(-) diff --git a/include/Client.h b/include/Client.h index 93882f2..d513659 100644 --- a/include/Client.h +++ b/include/Client.h @@ -92,7 +92,7 @@ private: std::queue mPacketsSync; std::unordered_map mIdentifiers; bool mIsGuest = false; - std::mutex mVehicleDataMutex; + mutable std::mutex mVehicleDataMutex; TSetOfVehicleData mVehicleData; std::string mName = "Unknown Client"; SOCKET mSocket[2] { SOCKET(0), SOCKET(0) }; diff --git a/src/Client.cpp b/src/Client.cpp index d5736fe..4edbbc7 100644 --- a/src/Client.cpp +++ b/src/Client.cpp @@ -27,6 +27,7 @@ void TClient::ClearCars() { int TClient::GetOpenCarID() const { int OpenID = 0; bool found; + std::unique_lock lock(mVehicleDataMutex); do { found = true; for (auto& v : mVehicleData) { diff --git a/src/Http.cpp b/src/Http.cpp index 467fd3c..9a3b9b6 100644 --- a/src/Http.cpp +++ b/src/Http.cpp @@ -293,7 +293,7 @@ Http::Server::THttpServerInstance::THttpServerInstance() { mThread.detach(); } -void Http::Server::THttpServerInstance::operator()() { +void Http::Server::THttpServerInstance::operator()() try { beammp_info("HTTP(S) Server started on port " + std::to_string(Application::Settings.HTTPServerPort)); std::unique_ptr HttpLibServerInstance; if (Application::Settings.HTTPServerUseSSL) { @@ -370,6 +370,14 @@ void Http::Server::THttpServerInstance::operator()() { HttpLibServerInstance->Get({ 0x2f, 0x6b, 0x69, 0x74, 0x74, 0x79 }, [](const httplib::Request&, httplib::Response& res) { res.set_content(std::string(Magic), "text/plain"); }); + HttpLibServerInstance->set_logger([](const httplib::Request& Req, const httplib::Response& Res) { + beammp_debug("Http Server: " + Req.method + " " + Req.target + " -> " + std::to_string(Res.status)); + }); Application::SetSubsystemStatus("HTTPServer", Application::Status::Good); - HttpLibServerInstance->listen("0.0.0.0", Application::Settings.HTTPServerPort); + auto ret = HttpLibServerInstance->listen("0.0.0.0", Application::Settings.HTTPServerPort); + if (!ret) { + beammp_error("Failed to start http server (failed to listen). Please ensure the http server is configured properly in the ServerConfig.toml, or turn it off if you don't need it."); + } +} catch (const std::exception& e) { + beammp_error("Failed to start http server. Please ensure the http server is configured properly in the ServerConfig.toml, or turn it off if you don't need it. Error: " + std::string(e.what())); } diff --git a/src/main.cpp b/src/main.cpp index 905b994..5e5a96d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -119,7 +119,7 @@ int BeamMPServerMain(MainArguments Arguments) { } Application::SetSubsystemStatus("Main", Application::Status::Starting); - + Application::Console().StartLoggingToFile(); SetupSignalHandlers(); @@ -170,6 +170,10 @@ int BeamMPServerMain(MainArguments Arguments) { Application::SetSubsystemStatus("Main", Application::Status::Good); RegisterThread("Main(Waiting)"); + std::set IgnoreSubsystems { + "UpdateCheck" // Ignore as not to confuse users (non-vital system) + }; + bool FullyStarted = false; while (!Shutdown) { if (!FullyStarted) { @@ -178,6 +182,9 @@ int BeamMPServerMain(MainArguments Arguments) { std::string SystemsBadList {}; auto Statuses = Application::GetSubsystemStatuses(); for (const auto& NameStatusPair : Statuses) { + if (IgnoreSubsystems.count(NameStatusPair.first) > 0) { + continue; // ignore + } if (NameStatusPair.second == Application::Status::Starting) { FullyStarted = false; } else if (NameStatusPair.second == Application::Status::Bad) {