Fix string create crash when file does not exist

This commit is contained in:
tovjemam 2026-01-08 18:30:20 +01:00
parent bf9567a71c
commit fd43f030bb
2 changed files with 57 additions and 49 deletions

View File

@ -8,6 +8,9 @@ sv::WSServer::WSServer(uint16_t port)
{
ws_thread_ = std::make_unique<std::thread>([this, port]() {
crow::SimpleApp app;
{
std::lock_guard<std::mutex> lock(mtx_);
app_ptr_ = (void*)&app;
CROW_WEBSOCKET_ROUTE(app, "/ws")
@ -45,7 +48,6 @@ sv::WSServer::WSServer(uint16_t port)
WSConnId conn_id = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(conn.userdata()));
events_.emplace_back(WSE_MESSAGE, conn_id, data);
});
// CROW_ROUTE(app, "/")
@ -55,19 +57,20 @@ sv::WSServer::WSServer(uint16_t port)
// auto page = crow::mustache::load("ws.html");
// return page.render(x);
// });
}
app.port(port).run();
// push exit event
std::lock_guard<std::mutex> lock(mtx_);
// push exit event
events_.emplace_back(WSE_EXIT);
app_ptr_ = nullptr;
});
}
bool sv::WSServer::PollEvent(WSEvent &out_event)
bool sv::WSServer::PollEvent(WSEvent& out_event)
{
std::lock_guard<std::mutex> lock(mtx_);
@ -86,12 +89,11 @@ void sv::WSServer::Send(WSConnId conn_id, std::string data)
auto it = id2conn_.find(conn_id);
if (it == id2conn_.end())
{
std::cerr << "attempted to send message to unknown conn ID " << conn_id <<std::endl;
std::cerr << "attempted to send message to unknown conn ID " << conn_id << std::endl;
return;
}
(*it->second).send_binary(std::move(data));
}
void sv::WSServer::Exit()
@ -103,6 +105,7 @@ void sv::WSServer::Exit()
sv::WSServer::~WSServer()
{
Exit();
if (ws_thread_ && ws_thread_->joinable())
ws_thread_->join();
}

View File

@ -1,10 +1,15 @@
#include "files.hpp"
#include <fstream>
#include <format>
std::string fs::ReadFileAsString(const std::string& path)
{
std::ifstream t(path, std::ios::binary);
if (!t)
throw std::runtime_error(std::format("File not found: {}", path));
t.seekg(0, std::ios::end);
size_t size = t.tellg();
std::string buffer(size, ' ');