Skip to content

Commit

Permalink
Check the status of HTTP when launching
Browse files Browse the repository at this point in the history
  • Loading branch information
GXTX committed Feb 13, 2023
1 parent c242f65 commit 0f96730
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 4 deletions.
7 changes: 6 additions & 1 deletion Includes/WebIo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion Includes/WebIo.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <utility>
#include <thread>
#include <atomic>
#include <thread>

#include "CardIo.h"
#include "base64.h"
Expand All @@ -42,7 +43,7 @@ class WebIo
~WebIo();

void StartServer();
void Spawn();
bool Spawn();

int m_port = 0;
CardIo::Settings *m_card = nullptr;
Expand Down
5 changes: 3 additions & 2 deletions main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,9 +143,10 @@ int main()
return 1;
}

// TODO: Verify service is actually running
std::unique_ptr<WebIo> webHandler = std::make_unique<WebIo>(&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;
Expand Down

0 comments on commit 0f96730

Please sign in to comment.