add '--dbg-server' command-line argument & add debugging server draft

This commit is contained in:
MihailRis 2025-10-05 23:54:29 +03:00
parent 2895cff5f2
commit 80cd187c6f
7 changed files with 142 additions and 5 deletions

View File

@ -0,0 +1,77 @@
#include "DebuggingServer.hpp"
#include "engine/Engine.hpp"
#include "network/Network.hpp"
#include "debug/Logger.hpp"
using namespace devtools;
static debug::Logger logger("debug-server");
static network::Server& create_tcp_server(
DebuggingServer& dbgServer, Engine& engine, int port
) {
auto& network = engine.getNetwork();
u64id_t serverId = network.openTcpServer(
port,
[&network, &dbgServer](u64id_t sid, u64id_t id) {
auto& connection = *network.getConnection(id, true);
connection.setPrivate(true);
logger.info() << "connected client " << id << ": "
<< connection.getAddress() << ":"
<< connection.getPort();
dbgServer.setClient(connection);
}
);
auto& server = *network.getServer(serverId, true);
server.setPrivate(true);
auto& tcpServer = dynamic_cast<network::TcpServer&>(server);
tcpServer.setMaxClientsConnected(1);
logger.info() << "tcp debugging server open at port " << server.getPort();
return tcpServer;
}
static network::Server& create_server(
DebuggingServer& dbgServer, Engine& engine, const std::string& serverString
) {
logger.info() << "starting debugging server";
size_t sepPos = serverString.find(':');
if (sepPos == std::string::npos) {
throw std::runtime_error("invalid debugging server configuration string");
}
auto transport = serverString.substr(0, sepPos);
if (transport == "tcp") {
int port;
try {
port = std::stoi(serverString.substr(sepPos + 1));
} catch (const std::exception& err) {
throw std::runtime_error("invalid tcp port");
}
return create_tcp_server(dbgServer, engine, port);
} else {
throw std::runtime_error(
"unsupported debugging server transport '" + transport + "'"
);
}
}
DebuggingServer::DebuggingServer(
Engine& engine, const std::string& serverString
)
: engine(engine),
server(create_server(*this, engine, serverString)),
client(nullptr) {
}
DebuggingServer::~DebuggingServer() {
logger.info() << "stopping debugging server";
}
bool DebuggingServer::update() {
return false;
}

View File

@ -0,0 +1,29 @@
#pragma once
#include <memory>
#include <string>
namespace network {
class Server;
class Connection;
}
class Engine;
namespace devtools {
class DebuggingServer {
public:
DebuggingServer(Engine& engine, const std::string& serverString);
~DebuggingServer();
bool update();
void setClient(network::Connection& client) {
this->client = &client;
}
private:
Engine& engine;
network::Server& server;
network::Connection* client;
};
}

View File

@ -14,6 +14,7 @@
#include "coders/commons.hpp"
#include "devtools/Editor.hpp"
#include "devtools/Project.hpp"
#include "devtools/DebuggingServer.hpp"
#include "content/ContentControl.hpp"
#include "core_defs.hpp"
#include "io/io.hpp"
@ -115,6 +116,9 @@ void Engine::initializeClient() {
if (ENGINE_DEBUG_BUILD) {
title += " [debug]";
}
if (debuggingServer) {
title = "[debugging] " + title;
}
auto [window, input] = Window::initialize(&settings.display, title);
if (!window || !input){
throw initialize_error("could not initialize window");
@ -173,6 +177,18 @@ void Engine::initialize(CoreParameters coreParameters) {
cmd = std::make_unique<cmd::CommandsInterpreter>();
network = network::Network::create(settings.network);
if (!params.debugServerString.empty()) {
try {
debuggingServer = std::make_unique<devtools::DebuggingServer>(
*this, params.debugServerString
);
} catch (const std::runtime_error& err) {
throw initialize_error(
"debugging server error: " + std::string(err.what())
);
}
}
if (!params.scriptFile.empty()) {
paths.setScriptFolder(params.scriptFile.parent_path());
}
@ -299,7 +315,11 @@ void Engine::saveSettings() {
io::write_string(EnginePaths::SETTINGS_FILE, toml::stringify(*settingsHandler));
if (!params.headless) {
logger.info() << "saving bindings";
io::write_string(EnginePaths::CONTROLS_FILE, input->getBindings().write());
if (input) {
io::write_string(
EnginePaths::CONTROLS_FILE, input->getBindings().write()
);
}
}
}
@ -318,6 +338,7 @@ void Engine::close() {
logger.info() << "gui finished";
}
audio::close();
debuggingServer.reset();
network.reset();
clearKeepedObjects();
project.reset();

View File

@ -36,6 +36,7 @@ namespace network {
namespace devtools {
class Editor;
class DebuggingServer;
}
class initialize_error : public std::runtime_error {
@ -50,6 +51,7 @@ struct CoreParameters {
std::filesystem::path userFolder = ".";
std::filesystem::path scriptFile;
std::filesystem::path projectFolder;
std::string debugServerString;
int tps = 20;
};
@ -72,6 +74,7 @@ class Engine : public util::ObjectsKeeper {
std::unique_ptr<Input> input;
std::unique_ptr<gui::GUI> gui;
std::unique_ptr<devtools::Editor> editor;
std::unique_ptr<devtools::DebuggingServer> debuggingServer;
PostRunnables postRunnables;
Time time;
OnWorldOpen levelConsumer;

View File

@ -617,7 +617,9 @@ namespace lua {
void remove_environment(lua::State*, int id);
inline void close(lua::State* L) {
lua_close(L);
if (L) {
lua_close(L);
}
}
inline void addfunc(

View File

@ -25,7 +25,7 @@ int main(int argc, char** argv) {
}
std::signal(SIGTERM, sigterm_handler);
debug::Logger::init(coreParameters.userFolder.string()+"/latest.log");
debug::Logger::init(coreParameters.userFolder.string() + "/latest.log");
platform::configure_encoding();
auto& engine = Engine::getInstance();
@ -33,7 +33,8 @@ int main(int argc, char** argv) {
engine.initialize(std::move(coreParameters));
engine.run();
} catch (const initialize_error& err) {
logger.error() << "could not to initialize engine\n" << err.what();
logger.error() << err.what();
logger.error() << "could not to initialize engine";
}
#if defined(NDEBUG) and defined(_WIN32)
catch (const std::exception& err) {

View File

@ -70,11 +70,15 @@ static bool perform_keyword(
std::cout << ENGINE_VERSION_STRING << std::endl;
return false;
}, "", "display the engine version."),
ArgC("--dbg-server", [&params, &reader]() -> bool {
params.debugServerString = reader.next();
return true;
}, "<serv>", "open debugging server where <serv> is {transport}:{port}"),
ArgC("--help", []() -> bool {
std::cout << "VoxelCore v" << ENGINE_VERSION_STRING << "\n\n";
std::cout << "Command-line arguments:\n";
for (auto& a : argumentsCommandline) {
std::cout << std::setw(20) << std::left << (a.keyword + " " + a.args);
std::cout << std::setw(24) << std::left << (a.keyword + " " + a.args);
std::cout << "- " << a.help << std::endl;
}
std::cout << std::endl;