1
0
Files

87 lines
3.5 KiB
C++
Raw Permalink Normal View History

2026-01-08 19:23:19 +08:00
#include "cmd_client.hpp"
2026-01-06 19:50:45 +08:00
#include <stdexcept>
2026-01-08 19:23:19 +08:00
namespace basalt::presenter::cmd_client {
2026-01-06 19:50:45 +08:00
2026-01-08 19:37:25 +08:00
CmdClient::CmdClient() : m_PipeOperator(BSTEXT("ed0e3f1f-d214-4880-9562-640bce15e72e")), m_Status(CmdClientStatus::Ready) {}
2026-01-06 19:50:45 +08:00
2026-01-08 19:37:25 +08:00
CmdClient::~CmdClient() {}
2026-01-06 19:50:45 +08:00
2026-01-08 19:37:25 +08:00
HandshakePayload CmdClient::wait_handshake() {
if (m_Status != CmdClientStatus::Ready) {
2026-01-06 21:24:47 +08:00
throw std::runtime_error("unexcpected client status");
2026-01-06 19:50:45 +08:00
}
// Wait for handshake request from Trainer (code 0x61)
2026-01-06 21:24:47 +08:00
ProtocolCode request_code;
2026-01-08 19:37:25 +08:00
m_PipeOperator.read_pod(request_code);
2026-01-06 21:24:47 +08:00
if (request_code != ProtocolCode::HANDSHAKE_REQUEST) {
throw std::runtime_error("unexpcted handshake code");
2026-01-06 19:50:45 +08:00
}
2026-01-06 21:24:47 +08:00
// Accept payload
HandshakePayload handshake_payload;
2026-01-10 17:10:14 +08:00
std::uint8_t raw_headless;
m_PipeOperator.read_pod(raw_headless);
handshake_payload.headless = static_cast<bool>(raw_headless);
m_PipeOperator.read_pod(handshake_payload.pixel_kind);
m_PipeOperator.read_pod(handshake_payload.width);
m_PipeOperator.read_pod(handshake_payload.height);
m_PipeOperator.read_bsstring(handshake_payload.engine_name);
m_PipeOperator.read_pod(handshake_payload.engine_device);
2026-01-10 20:02:50 +08:00
m_PipeOperator.read_bsstring(handshake_payload.deliver_name);
m_PipeOperator.read_pod(handshake_payload.deliver_device);
2026-01-10 17:10:14 +08:00
m_PipeOperator.read_bsstring(handshake_payload.object_loader_name);
m_PipeOperator.read_bsstring(handshake_payload.object_loader_file);
m_PipeOperator.read_bsstring(handshake_payload.anime_loader_name);
m_PipeOperator.read_bsstring(handshake_payload.anime_loader_file);
2026-01-06 19:50:45 +08:00
// Send handshake response (code 0x62) back to Trainer
2026-01-06 21:24:47 +08:00
ProtocolCode response_code = ProtocolCode::HANDSHAKE_RESPONSE;
2026-01-08 19:37:25 +08:00
m_PipeOperator.write_pod(response_code);
2026-01-06 19:50:45 +08:00
2026-01-06 21:24:47 +08:00
// Set status and return
2026-01-08 19:37:25 +08:00
m_Status = CmdClientStatus::Running;
2026-01-06 21:24:47 +08:00
return handshake_payload;
2026-01-06 19:50:45 +08:00
}
2026-01-08 19:37:25 +08:00
bool CmdClient::tick(bool actively_stop) {
if (m_Status != CmdClientStatus::Running) {
2026-01-06 21:24:47 +08:00
throw std::runtime_error("unexcpected client status");
2026-01-06 19:50:45 +08:00
}
2026-01-06 21:24:47 +08:00
// If actively stop, send actively stop code to Trainer first
2026-01-06 19:50:45 +08:00
if (actively_stop) {
2026-01-09 19:34:37 +08:00
ProtocolCode actively_stop_code = ProtocolCode::ACTIVELY_STOP;
2026-01-08 19:37:25 +08:00
m_PipeOperator.write_pod(actively_stop_code);
2026-01-06 19:50:45 +08:00
}
// Send data ready code to Trainer
2026-01-08 19:37:25 +08:00
ProtocolCode data_ready_code = ProtocolCode::DATA_READY;
m_PipeOperator.write_pod(data_ready_code);
2026-01-06 19:50:45 +08:00
2026-01-06 21:24:47 +08:00
// Process the response from Trainer
while (true) {
ProtocolCode recv_code;
2026-01-08 19:37:25 +08:00
m_PipeOperator.read_pod(recv_code);
2026-01-06 19:50:45 +08:00
2026-01-06 21:24:47 +08:00
switch (recv_code) {
2026-01-08 19:37:25 +08:00
case ProtocolCode::DATA_RECEIVED: {
2026-01-06 21:24:47 +08:00
// Normal response, continue processing
return false; // Not stopping
2026-01-08 19:37:25 +08:00
}
case ProtocolCode::STOP_REQUEST: {
2026-01-06 21:24:47 +08:00
// Trainer wants to stop
2026-01-08 19:37:25 +08:00
// We sent response first
ProtocolCode stop_response_code = ProtocolCode::STOP_RESPONSE;
m_PipeOperator.write_pod(stop_response_code);
// Set status and return.
m_Status = CmdClientStatus::Stop;
2026-01-06 21:24:47 +08:00
return true; // Should stop
2026-01-08 19:37:25 +08:00
}
2026-01-06 21:24:47 +08:00
default:
throw std::runtime_error("unexpected code when running");
}
2026-01-06 19:50:45 +08:00
}
}
2026-01-08 19:37:25 +08:00
} // namespace basalt::presenter::cmd_client