Fix string create crash when file does not exist
This commit is contained in:
parent
bf9567a71c
commit
fd43f030bb
@ -8,6 +8,9 @@ sv::WSServer::WSServer(uint16_t port)
|
|||||||
{
|
{
|
||||||
ws_thread_ = std::make_unique<std::thread>([this, port]() {
|
ws_thread_ = std::make_unique<std::thread>([this, port]() {
|
||||||
crow::SimpleApp app;
|
crow::SimpleApp app;
|
||||||
|
{
|
||||||
|
std::lock_guard<std::mutex> lock(mtx_);
|
||||||
|
|
||||||
app_ptr_ = (void*)&app;
|
app_ptr_ = (void*)&app;
|
||||||
|
|
||||||
CROW_WEBSOCKET_ROUTE(app, "/ws")
|
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()));
|
WSConnId conn_id = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(conn.userdata()));
|
||||||
events_.emplace_back(WSE_MESSAGE, conn_id, data);
|
events_.emplace_back(WSE_MESSAGE, conn_id, data);
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// CROW_ROUTE(app, "/")
|
// CROW_ROUTE(app, "/")
|
||||||
@ -55,15 +57,16 @@ sv::WSServer::WSServer(uint16_t port)
|
|||||||
// auto page = crow::mustache::load("ws.html");
|
// auto page = crow::mustache::load("ws.html");
|
||||||
// return page.render(x);
|
// return page.render(x);
|
||||||
// });
|
// });
|
||||||
|
}
|
||||||
|
|
||||||
app.port(port).run();
|
app.port(port).run();
|
||||||
|
|
||||||
// push exit event
|
|
||||||
std::lock_guard<std::mutex> lock(mtx_);
|
std::lock_guard<std::mutex> lock(mtx_);
|
||||||
|
|
||||||
|
// push exit event
|
||||||
events_.emplace_back(WSE_EXIT);
|
events_.emplace_back(WSE_EXIT);
|
||||||
|
|
||||||
app_ptr_ = nullptr;
|
app_ptr_ = nullptr;
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -91,7 +94,6 @@ void sv::WSServer::Send(WSConnId conn_id, std::string data)
|
|||||||
}
|
}
|
||||||
|
|
||||||
(*it->second).send_binary(std::move(data));
|
(*it->second).send_binary(std::move(data));
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void sv::WSServer::Exit()
|
void sv::WSServer::Exit()
|
||||||
@ -103,6 +105,7 @@ void sv::WSServer::Exit()
|
|||||||
|
|
||||||
sv::WSServer::~WSServer()
|
sv::WSServer::~WSServer()
|
||||||
{
|
{
|
||||||
|
Exit();
|
||||||
if (ws_thread_ && ws_thread_->joinable())
|
if (ws_thread_ && ws_thread_->joinable())
|
||||||
ws_thread_->join();
|
ws_thread_->join();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,10 +1,15 @@
|
|||||||
#include "files.hpp"
|
#include "files.hpp"
|
||||||
|
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
|
#include <format>
|
||||||
|
|
||||||
std::string fs::ReadFileAsString(const std::string& path)
|
std::string fs::ReadFileAsString(const std::string& path)
|
||||||
{
|
{
|
||||||
std::ifstream t(path, std::ios::binary);
|
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);
|
t.seekg(0, std::ios::end);
|
||||||
size_t size = t.tellg();
|
size_t size = t.tellg();
|
||||||
std::string buffer(size, ' ');
|
std::string buffer(size, ' ');
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user