Fix string create crash when file does not exist
This commit is contained in:
parent
bf9567a71c
commit
fd43f030bb
@ -8,66 +8,69 @@ 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;
|
||||||
app_ptr_ = (void*)&app;
|
{
|
||||||
|
|
||||||
CROW_WEBSOCKET_ROUTE(app, "/ws")
|
|
||||||
.onopen([&](crow::websocket::connection& conn) {
|
|
||||||
CROW_LOG_INFO << "new websocket connection from " << conn.get_remote_ip();
|
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(mtx_);
|
|
||||||
conn.set_nodelay();
|
|
||||||
|
|
||||||
WSConnId conn_id = utils::AllocNum(id2conn_, last_id_);
|
|
||||||
|
|
||||||
// register connection
|
|
||||||
id2conn_[conn_id] = &conn;
|
|
||||||
conn.userdata(reinterpret_cast<void*>(static_cast<uintptr_t>(conn_id)));
|
|
||||||
// push connection event
|
|
||||||
events_.emplace_back(WSE_CONNECTED, conn_id);
|
|
||||||
})
|
|
||||||
.onclose([&](crow::websocket::connection& conn, const std::string& reason, uint16_t) {
|
|
||||||
CROW_LOG_INFO << "websocket connection closed: " << reason;
|
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(mtx_);
|
std::lock_guard<std::mutex> lock(mtx_);
|
||||||
|
|
||||||
WSConnId conn_id = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(conn.userdata()));
|
app_ptr_ = (void*)&app;
|
||||||
|
|
||||||
// push disonnected event
|
CROW_WEBSOCKET_ROUTE(app, "/ws")
|
||||||
events_.emplace_back(WSE_DISCONNECTED, conn_id);
|
.onopen([&](crow::websocket::connection& conn) {
|
||||||
|
CROW_LOG_INFO << "new websocket connection from " << conn.get_remote_ip();
|
||||||
|
|
||||||
id2conn_.erase(conn_id);
|
std::lock_guard<std::mutex> lock(mtx_);
|
||||||
})
|
conn.set_nodelay();
|
||||||
.onmessage([&](crow::websocket::connection& conn, const std::string& data, bool is_binary) {
|
|
||||||
if (!is_binary)
|
|
||||||
return; // only accept binary messages here
|
|
||||||
|
|
||||||
std::lock_guard<std::mutex> lock(mtx_);
|
WSConnId conn_id = utils::AllocNum(id2conn_, last_id_);
|
||||||
|
|
||||||
WSConnId conn_id = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(conn.userdata()));
|
// register connection
|
||||||
events_.emplace_back(WSE_MESSAGE, conn_id, data);
|
id2conn_[conn_id] = &conn;
|
||||||
|
conn.userdata(reinterpret_cast<void*>(static_cast<uintptr_t>(conn_id)));
|
||||||
|
// push connection event
|
||||||
|
events_.emplace_back(WSE_CONNECTED, conn_id);
|
||||||
|
})
|
||||||
|
.onclose([&](crow::websocket::connection& conn, const std::string& reason, uint16_t) {
|
||||||
|
CROW_LOG_INFO << "websocket connection closed: " << reason;
|
||||||
|
|
||||||
});
|
std::lock_guard<std::mutex> lock(mtx_);
|
||||||
|
|
||||||
// CROW_ROUTE(app, "/")
|
WSConnId conn_id = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(conn.userdata()));
|
||||||
// ([] {
|
|
||||||
// crow::mustache::context x;
|
// push disonnected event
|
||||||
// x["servername"] = "127.0.0.1";
|
events_.emplace_back(WSE_DISCONNECTED, conn_id);
|
||||||
// auto page = crow::mustache::load("ws.html");
|
|
||||||
// return page.render(x);
|
id2conn_.erase(conn_id);
|
||||||
// });
|
})
|
||||||
|
.onmessage([&](crow::websocket::connection& conn, const std::string& data, bool is_binary) {
|
||||||
|
if (!is_binary)
|
||||||
|
return; // only accept binary messages here
|
||||||
|
|
||||||
|
std::lock_guard<std::mutex> lock(mtx_);
|
||||||
|
|
||||||
|
WSConnId conn_id = static_cast<uint32_t>(reinterpret_cast<uintptr_t>(conn.userdata()));
|
||||||
|
events_.emplace_back(WSE_MESSAGE, conn_id, data);
|
||||||
|
});
|
||||||
|
|
||||||
|
// CROW_ROUTE(app, "/")
|
||||||
|
// ([] {
|
||||||
|
// crow::mustache::context x;
|
||||||
|
// x["servername"] = "127.0.0.1";
|
||||||
|
// auto page = crow::mustache::load("ws.html");
|
||||||
|
// 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;
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
bool sv::WSServer::PollEvent(WSEvent &out_event)
|
bool sv::WSServer::PollEvent(WSEvent& out_event)
|
||||||
{
|
{
|
||||||
std::lock_guard<std::mutex> lock(mtx_);
|
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);
|
auto it = id2conn_.find(conn_id);
|
||||||
if (it == id2conn_.end())
|
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;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
(*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