HTTPServer: Attempt to catch more errors

This commit is contained in:
Lion Kortlepel 2022-04-28 14:04:54 +02:00
parent 0761036c8c
commit 0979c8b1e4
No known key found for this signature in database
GPG Key ID: 4322FF2B4C71259B
4 changed files with 20 additions and 4 deletions

View File

@ -92,7 +92,7 @@ private:
std::queue<std::string> mPacketsSync; std::queue<std::string> mPacketsSync;
std::unordered_map<std::string, std::string> mIdentifiers; std::unordered_map<std::string, std::string> mIdentifiers;
bool mIsGuest = false; bool mIsGuest = false;
std::mutex mVehicleDataMutex; mutable std::mutex mVehicleDataMutex;
TSetOfVehicleData mVehicleData; TSetOfVehicleData mVehicleData;
std::string mName = "Unknown Client"; std::string mName = "Unknown Client";
SOCKET mSocket[2] { SOCKET(0), SOCKET(0) }; SOCKET mSocket[2] { SOCKET(0), SOCKET(0) };

View File

@ -27,6 +27,7 @@ void TClient::ClearCars() {
int TClient::GetOpenCarID() const { int TClient::GetOpenCarID() const {
int OpenID = 0; int OpenID = 0;
bool found; bool found;
std::unique_lock lock(mVehicleDataMutex);
do { do {
found = true; found = true;
for (auto& v : mVehicleData) { for (auto& v : mVehicleData) {

View File

@ -293,7 +293,7 @@ Http::Server::THttpServerInstance::THttpServerInstance() {
mThread.detach(); 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)); beammp_info("HTTP(S) Server started on port " + std::to_string(Application::Settings.HTTPServerPort));
std::unique_ptr<httplib::Server> HttpLibServerInstance; std::unique_ptr<httplib::Server> HttpLibServerInstance;
if (Application::Settings.HTTPServerUseSSL) { 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) { HttpLibServerInstance->Get({ 0x2f, 0x6b, 0x69, 0x74, 0x74, 0x79 }, [](const httplib::Request&, httplib::Response& res) {
res.set_content(std::string(Magic), "text/plain"); 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); 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()));
} }

View File

@ -119,7 +119,7 @@ int BeamMPServerMain(MainArguments Arguments) {
} }
Application::SetSubsystemStatus("Main", Application::Status::Starting); Application::SetSubsystemStatus("Main", Application::Status::Starting);
Application::Console().StartLoggingToFile(); Application::Console().StartLoggingToFile();
SetupSignalHandlers(); SetupSignalHandlers();
@ -170,6 +170,10 @@ int BeamMPServerMain(MainArguments Arguments) {
Application::SetSubsystemStatus("Main", Application::Status::Good); Application::SetSubsystemStatus("Main", Application::Status::Good);
RegisterThread("Main(Waiting)"); RegisterThread("Main(Waiting)");
std::set<std::string> IgnoreSubsystems {
"UpdateCheck" // Ignore as not to confuse users (non-vital system)
};
bool FullyStarted = false; bool FullyStarted = false;
while (!Shutdown) { while (!Shutdown) {
if (!FullyStarted) { if (!FullyStarted) {
@ -178,6 +182,9 @@ int BeamMPServerMain(MainArguments Arguments) {
std::string SystemsBadList {}; std::string SystemsBadList {};
auto Statuses = Application::GetSubsystemStatuses(); auto Statuses = Application::GetSubsystemStatuses();
for (const auto& NameStatusPair : Statuses) { for (const auto& NameStatusPair : Statuses) {
if (IgnoreSubsystems.count(NameStatusPair.first) > 0) {
continue; // ignore
}
if (NameStatusPair.second == Application::Status::Starting) { if (NameStatusPair.second == Application::Status::Starting) {
FullyStarted = false; FullyStarted = false;
} else if (NameStatusPair.second == Application::Status::Bad) { } else if (NameStatusPair.second == Application::Status::Bad) {