#include "cmd_client.hpp" #include namespace basalt::presenter::cmd_client { CmdClient::CmdClient() : m_PipeOperator(BSTEXT("ed0e3f1f-d214-4880-9562-640bce15e72e")), m_Status(CmdClientStatus::Ready) {} CmdClient::~CmdClient() {} HandshakePayload CmdClient::wait_handshake() { if (m_Status != CmdClientStatus::Ready) { throw std::runtime_error("unexcpected client status"); } // Wait for handshake request from Trainer (code 0x61) ProtocolCode request_code; m_PipeOperator.read_pod(request_code); if (request_code != ProtocolCode::HANDSHAKE_REQUEST) { throw std::runtime_error("unexpcted handshake code"); } // Accept payload HandshakePayload handshake_payload; std::uint8_t raw_headless; m_PipeOperator.read_pod(raw_headless); handshake_payload.headless = static_cast(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); m_PipeOperator.read_bsstring(handshake_payload.deliver_name); m_PipeOperator.read_pod(handshake_payload.deliver_device); 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); // Send handshake response (code 0x62) back to Trainer ProtocolCode response_code = ProtocolCode::HANDSHAKE_RESPONSE; m_PipeOperator.write_pod(response_code); // Set status and return m_Status = CmdClientStatus::Running; return handshake_payload; } bool CmdClient::tick(bool actively_stop) { if (m_Status != CmdClientStatus::Running) { throw std::runtime_error("unexcpected client status"); } // If actively stop, send actively stop code to Trainer first if (actively_stop) { ProtocolCode actively_stop_code = ProtocolCode::ACTIVELY_STOP; m_PipeOperator.write_pod(actively_stop_code); } // Send data ready code to Trainer ProtocolCode data_ready_code = ProtocolCode::DATA_READY; m_PipeOperator.write_pod(data_ready_code); // Process the response from Trainer while (true) { ProtocolCode recv_code; m_PipeOperator.read_pod(recv_code); switch (recv_code) { case ProtocolCode::DATA_RECEIVED: { // Normal response, continue processing return false; // Not stopping } case ProtocolCode::STOP_REQUEST: { // Trainer wants to stop // 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; return true; // Should stop } default: throw std::runtime_error("unexpected code when running"); } } } } // namespace basalt::presenter::cmd_client