Skip to content

Commit

Permalink
DebugServer: fix errors with websocket server
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitalita committed Sep 1, 2023
1 parent 48e1e03 commit 0ce2ead
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 25 deletions.
2 changes: 2 additions & 0 deletions src/DarkId.Papyrus.DebugServer/DebugServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ namespace DarkId::Papyrus::DebugServer
#elif FALLOUT
int port = 2077;
#endif
m_server.stop();

auto onClientConnected =
[&](const std::shared_ptr<dap::ReaderWriter>& connection) {
std::shared_ptr<dap::Session> sess;
Expand Down
47 changes: 25 additions & 22 deletions src/DarkId.Papyrus.DebugServer/Protocol/websocket_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,19 @@ namespace dap
namespace net
{

WebsocketServer::WebsocketServer()
WebsocketServer::WebsocketServer() : m_server(nullptr), stopped(true), rw(nullptr)
{
}

bool WebsocketServer::start(int p_port,
const OnConnect& onConnect,
const OnError& onError)
{
if (m_server) {
stop();
}
m_server = std::make_unique<server>();
port = p_port;
std::unique_lock<std::mutex> lock(mutex);
stopWithLock();
connectCallback = onConnect;
errorHandler = onError;

Expand All @@ -28,22 +30,22 @@ namespace dap

void WebsocketServer::HandleOpen(websocketpp::connection_hdl hdl)
{
std::shared_ptr<connection> con = m_server.get_con_from_hdl(hdl);
if (!m_connectionHandle.expired() && con != m_server.get_con_from_hdl(m_connectionHandle))
std::shared_ptr<connection> con = m_server->get_con_from_hdl(hdl);
if (!m_connectionHandle.expired() && con != m_server->get_con_from_hdl(m_connectionHandle))
{
m_server.close(m_connectionHandle, websocketpp::close::status::normal, "Connection closed by new session.");
if (rw) {
m_server->close(m_connectionHandle, websocketpp::close::status::normal, "Connection closed by new session.");
}
if (rw) {
rw->close();
}
}
m_connectionHandle = hdl;
rw = std::shared_ptr<WebsocketReaderWriter>( new dap::WebsocketReaderWriter(con) );
rw = std::make_shared<WebsocketReaderWriter>(con);
connectCallback(rw);
}

void WebsocketServer::HandleClose(websocketpp::connection_hdl hdl)
{
if (m_connectionHandle.expired() || m_server.get_con_from_hdl(hdl) != m_server.get_con_from_hdl(m_connectionHandle))
if (m_connectionHandle.expired() || m_server->get_con_from_hdl(hdl) != m_server->get_con_from_hdl(m_connectionHandle))
{
return;
}
Expand All @@ -57,19 +59,19 @@ namespace dap
{
try
{
m_server.init_asio();
m_server->init_asio();

m_server.set_open_handler(bind(&WebsocketServer::HandleOpen, this, ::_1));
m_server.set_close_handler(bind(&WebsocketServer::HandleClose, this, ::_1));
m_server.set_max_message_size(1024 * 1024 * 10);
m_server.set_max_http_body_size(1024 * 1024 * 10);
m_server->set_open_handler(bind(&WebsocketServer::HandleOpen, this, ::_1));
m_server->set_close_handler(bind(&WebsocketServer::HandleClose, this, ::_1));
m_server->set_max_message_size(1024 * 1024 * 10);
m_server->set_max_http_body_size(1024 * 1024 * 10);

m_server.listen(port);
m_server->listen(port);
// Start the server accept loop
m_server.start_accept();
m_server->start_accept();

// Start the ASIO io_service run loop
m_server.run();
m_server->run();
}
catch (websocketpp::exception const & e)
{
Expand All @@ -92,13 +94,14 @@ namespace dap
}

void WebsocketServer::stopWithLock() {
if (!stopped.exchange(true)) {
if (!stopped) {
stopped.exchange(true);
// this will trigger HandleClose for us
if (m_server.is_listening()) {
if (m_server && m_server->is_listening()) {
try
{
m_server.close(m_connectionHandle, websocketpp::close::status::normal, "Connection closed by debugger.");
m_server.stop();
m_server->close(m_connectionHandle, websocketpp::close::status::normal, "Connection closed by debugger.");
m_server->stop();
}
catch (websocketpp::exception const& e)
{
Expand Down
2 changes: 1 addition & 1 deletion src/DarkId.Papyrus.DebugServer/Protocol/websocket_server.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace net {

OnConnect connectCallback;
OnError errorHandler;
server m_server;
std::unique_ptr <server> m_server;
websocketpp::connection_hdl m_connectionHandle;
public:
using OnError = std::function<void(const char*)>;
Expand Down
8 changes: 6 additions & 2 deletions src/DarkId.Papyrus.DebugServer/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,11 @@ bool InitializeLog()

auto log = std::make_shared<spdlog::logger>("global log"s, std::move(sink));
log->set_level(spdlog::level::debug);
#if _DEBUG
log->flush_on(spdlog::level::debug);
#else
log->flush_on(spdlog::level::err);
#endif
spdlog::set_default_logger(std::move(log));
spdlog::set_pattern("%H:%M:%S,%e %l %@: %v"s);
logger::info("Papyrus Debug Server v{}"sv, DIDPDS_VERSION_SEMVER);
Expand Down Expand Up @@ -129,7 +133,8 @@ extern "C"
#elif FALLOUT
DLLEXPORT bool F4SEPlugin_Load(const XSE::LoadInterface* a_xse)
#endif


//#define _PAUSE_ON_START 1
{
if (!log_initialized) {
InitializeLog();
Expand Down Expand Up @@ -167,7 +172,6 @@ extern "C"
auto messaging = XSE::GetMessagingInterface();
messaging->RegisterListener(MessageHandler);
#endif
g_debugServer->Listen();
return true;
}
};

0 comments on commit 0ce2ead

Please sign in to comment.