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

View File

@ -3,11 +3,62 @@
#include "engine/Engine.hpp"
#include "network/Network.hpp"
#include "debug/Logger.hpp"
#include "coders/json.hpp"
using namespace devtools;
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(
DebuggingServer& dbgServer, Engine& engine, int port
) {
@ -15,12 +66,14 @@ static network::Server& create_tcp_server(
u64id_t serverId = network.openTcpServer(
port,
[&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);
logger.info() << "connected client " << id << ": "
<< connection.getAddress() << ":"
<< connection.getPort();
dbgServer.setClient(connection);
dbgServer.setClient(id);
}
);
auto& server = *network.getServer(serverId, true);
@ -64,14 +117,55 @@ DebuggingServer::DebuggingServer(
)
: engine(engine),
server(create_server(*this, engine, serverString)),
client(nullptr) {
connection(nullptr) {
}
DebuggingServer::~DebuggingServer() {
logger.info() << "stopping debugging server";
server.close();
}
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);
}

View File

@ -3,14 +3,38 @@
#include <memory>
#include <string>
#include "typedefs.hpp"
namespace network {
class Server;
class Connection;
class ReadableConnection;
class Network;
}
namespace dv {
class value;
}
class Engine;
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 {
public:
DebuggingServer(Engine& engine, const std::string& serverString);
@ -18,12 +42,12 @@ namespace devtools {
bool update();
void setClient(network::Connection& client) {
this->client = &client;
}
void setClient(u64id_t client);
private:
Engine& engine;
network::Server& server;
network::Connection* client;
std::unique_ptr<ClientConnection> connection;
bool performCommand(const std::string& type, const dv::value& map);
};
}

View File

@ -282,6 +282,12 @@ void Engine::postUpdate() {
network->update();
postRunnables.run();
scripting::process_post_runnables();
if (debuggingServer) {
if (!debuggingServer->update()) {
debuggingServer.reset();
}
}
}
void Engine::updateFrontend() {

View File

@ -51,7 +51,7 @@ struct CoreParameters {
std::filesystem::path userFolder = ".";
std::filesystem::path scriptFile;
std::filesystem::path projectFolder;
std::string debugServerString;
std::string debugServerString = "tcp:9030";
int tps = 20;
};