From 0f96730a78bb8afcd15f075a7ac8adf24f06763c Mon Sep 17 00:00:00 2001 From: wutno Date: Sun, 12 Feb 2023 16:37:30 -0500 Subject: [PATCH] Check the status of HTTP when launching --- Includes/WebIo.cpp | 7 ++++++- Includes/WebIo.h | 3 ++- main.cpp | 5 +++-- 3 files changed, 11 insertions(+), 4 deletions(-) diff --git a/Includes/WebIo.cpp b/Includes/WebIo.cpp index 5b31bd4..2473512 100644 --- a/Includes/WebIo.cpp +++ b/Includes/WebIo.cpp @@ -34,9 +34,12 @@ WebIo::~WebIo() m_svr.stop(); } -void WebIo::Spawn() +bool WebIo::Spawn() { std::thread(&WebIo::StartServer, this).detach(); + // Need to wait before checking the status otherwise we'll crash + std::this_thread::sleep_for(std::chrono::milliseconds(10)); + return m_svr.is_running(); } void WebIo::StartServer() @@ -90,6 +93,8 @@ void WebIo::Router(const httplib::Request &req, httplib::Response &res) break; case Routes::stop: m_svr.stop(); + // g_running controls the main loop, if we kill it before http is flushed then we'll crash + std::this_thread::sleep_for(std::chrono::milliseconds(10)); *g_running = false; break; default: diff --git a/Includes/WebIo.h b/Includes/WebIo.h index 23588b3..7f5db9f 100644 --- a/Includes/WebIo.h +++ b/Includes/WebIo.h @@ -28,6 +28,7 @@ #include #include #include +#include #include "CardIo.h" #include "base64.h" @@ -42,7 +43,7 @@ class WebIo ~WebIo(); void StartServer(); - void Spawn(); + bool Spawn(); int m_port = 0; CardIo::Settings *m_card = nullptr; diff --git a/main.cpp b/main.cpp index a6d34c3..a2b5344 100755 --- a/main.cpp +++ b/main.cpp @@ -143,9 +143,10 @@ int main() return 1; } - // TODO: Verify service is actually running std::unique_ptr webHandler = std::make_unique(&globalSettings.card, globalSettings.webPort, &running); - webHandler->Spawn(); + if (!webHandler->Spawn()) { + return 1; + } // TODO: These don't need to be here, put them in their respective classes SerIo::Status serialStatus = SerIo::Status::Okay;