test debugger commands

This commit is contained in:
MihailRis
2025-10-06 01:54:35 +03:00
parent 44bf4a2f9e
commit 3fef284a02
4 changed files with 133 additions and 9 deletions
+98 -4
View File
@@ -3,11 +3,62 @@
#include "engine/Engine.hpp" #include "engine/Engine.hpp"
#include "network/Network.hpp" #include "network/Network.hpp"
#include "debug/Logger.hpp" #include "debug/Logger.hpp"
#include "coders/json.hpp"
using namespace devtools; using namespace devtools;
static debug::Logger logger("debug-server"); static debug::Logger logger("debug-server");
ClientConnection::~ClientConnection() {
if (auto connection = dynamic_cast<network::ReadableConnection*>(
network.getConnection(this->connection, true)
)) {
connection->close();
}
}
std::string ClientConnection::read() {
auto connection = dynamic_cast<network::ReadableConnection*>(
network.getConnection(this->connection, true)
);
if (connection == nullptr) {
return "";
}
if (messageLength == 0) {
if (connection->available() >= sizeof(int32_t)) {
int32_t length = 0;
connection->recv(reinterpret_cast<char*>(&length), sizeof(int32_t));
if (length <= 0) {
logger.error() << "invalid message length " << length;
} else {
messageLength = length;
}
}
} else if (connection->available() >= messageLength) {
std::string string(messageLength, 0);
connection->recv(string.data(), messageLength);
return string;
}
return "";
}
void ClientConnection::send(const dv::value& object) {
auto connection = dynamic_cast<network::ReadableConnection*>(
network.getConnection(this->connection, true)
);
if (connection == nullptr) {
return;
}
auto message = json::stringify(object, false);
int32_t length = message.length();
connection->send(reinterpret_cast<char*>(&length), sizeof(int32_t));
connection->send(message.data(), length);
}
void ClientConnection::sendResponse(const std::string& type) {
send(dv::object({{"type", type}}));
}
static network::Server& create_tcp_server( static network::Server& create_tcp_server(
DebuggingServer& dbgServer, Engine& engine, int port DebuggingServer& dbgServer, Engine& engine, int port
) { ) {
@@ -15,12 +66,14 @@ static network::Server& create_tcp_server(
u64id_t serverId = network.openTcpServer( u64id_t serverId = network.openTcpServer(
port, port,
[&network, &dbgServer](u64id_t sid, u64id_t id) { [&network, &dbgServer](u64id_t sid, u64id_t id) {
auto& connection = *network.getConnection(id, true); auto& connection = dynamic_cast<network::ReadableConnection&>(
*network.getConnection(id, true)
);
connection.setPrivate(true); connection.setPrivate(true);
logger.info() << "connected client " << id << ": " logger.info() << "connected client " << id << ": "
<< connection.getAddress() << ":" << connection.getAddress() << ":"
<< connection.getPort(); << connection.getPort();
dbgServer.setClient(connection); dbgServer.setClient(id);
} }
); );
auto& server = *network.getServer(serverId, true); auto& server = *network.getServer(serverId, true);
@@ -64,14 +117,55 @@ DebuggingServer::DebuggingServer(
) )
: engine(engine), : engine(engine),
server(create_server(*this, engine, serverString)), server(create_server(*this, engine, serverString)),
client(nullptr) { connection(nullptr) {
} }
DebuggingServer::~DebuggingServer() { DebuggingServer::~DebuggingServer() {
logger.info() << "stopping debugging server"; logger.info() << "stopping debugging server";
server.close();
} }
bool DebuggingServer::update() { bool DebuggingServer::update() {
return false; if (connection == nullptr) {
return true;
}
std::string message = connection->read();
if (message.empty()) {
return true;
}
logger.debug() << "received: " << message;
try {
auto obj = json::parse(message);
if (!obj.has("type")) {
logger.error() << "missing message type";
return true;
}
const auto& type = obj["type"].asString();
return performCommand(type, obj);
} catch (const std::runtime_error& err) {
logger.error() << "could not to parse message: " << err.what();
}
return true;
}
bool DebuggingServer::performCommand(
const std::string& type, const dv::value& map
) {
if (type == "terminate") {
engine.quit();
connection->sendResponse("success");
} else if (type == "detach") {
connection->sendResponse("success");
connection.reset();
return false;
} else {
logger.error() << "unsupported command '" << type << "'";
}
return true;
}
void DebuggingServer::setClient(u64id_t client) {
this->connection =
std::make_unique<ClientConnection>(engine.getNetwork(), client);
} }
+28 -4
View File
@@ -3,14 +3,38 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include "typedefs.hpp"
namespace network { namespace network {
class Server; class Server;
class Connection; class Connection;
class ReadableConnection;
class Network;
}
namespace dv {
class value;
} }
class Engine; class Engine;
namespace devtools { namespace devtools {
class ClientConnection {
public:
ClientConnection(network::Network& network, u64id_t connection)
: network(network), connection(connection) {
}
~ClientConnection();
std::string read();
void send(const dv::value& message);
void sendResponse(const std::string& type);
private:
network::Network& network;
size_t messageLength = 0;
u64id_t connection;
};
class DebuggingServer { class DebuggingServer {
public: public:
DebuggingServer(Engine& engine, const std::string& serverString); DebuggingServer(Engine& engine, const std::string& serverString);
@@ -18,12 +42,12 @@ namespace devtools {
bool update(); bool update();
void setClient(network::Connection& client) { void setClient(u64id_t client);
this->client = &client;
}
private: private:
Engine& engine; Engine& engine;
network::Server& server; network::Server& server;
network::Connection* client; std::unique_ptr<ClientConnection> connection;
bool performCommand(const std::string& type, const dv::value& map);
}; };
} }
+6
View File
@@ -282,6 +282,12 @@ void Engine::postUpdate() {
network->update(); network->update();
postRunnables.run(); postRunnables.run();
scripting::process_post_runnables(); scripting::process_post_runnables();
if (debuggingServer) {
if (!debuggingServer->update()) {
debuggingServer.reset();
}
}
} }
void Engine::updateFrontend() { void Engine::updateFrontend() {
+1 -1
View File
@@ -51,7 +51,7 @@ struct CoreParameters {
std::filesystem::path userFolder = "."; std::filesystem::path userFolder = ".";
std::filesystem::path scriptFile; std::filesystem::path scriptFile;
std::filesystem::path projectFolder; std::filesystem::path projectFolder;
std::string debugServerString; std::string debugServerString = "tcp:9030";
int tps = 20; int tps = 20;
}; };