1
0
Files
BasaltMeter/BasaltPresenter/Presenter/command_client.cpp

69 lines
2.5 KiB
C++
Raw Normal View History

2026-01-06 19:50:45 +08:00
#include "command_client.hpp"
2026-01-06 21:24:47 +08:00
#include <basalt/char_types.hpp>
2026-01-06 19:50:45 +08:00
#include <stdexcept>
namespace Basalt::Presenter {
2026-01-06 21:24:47 +08:00
CommandClient::CommandClient() : m_PipeOperator(BSTEXT("ed0e3f1f-d214-4880-9562-640bce15e72e")), m_Status(ClientStatus::Ready) {}
2026-01-06 19:50:45 +08:00
2026-01-06 21:24:47 +08:00
CommandClient::~CommandClient() {}
2026-01-06 19:50:45 +08:00
2026-01-06 21:24:47 +08:00
HandshakePayload CommandClient::WaitHandshake() {
if (m_Status != ClientStatus::Ready) {
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;
m_PipeOperator.Read(&request_code, sizeof(request_code));
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;
m_PipeOperator.Read(&handshake_payload, sizeof(HandshakePayload));
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;
m_PipeOperator.Write(&response_code, sizeof(response_code));
2026-01-06 19:50:45 +08:00
2026-01-06 21:24:47 +08:00
// Set status and return
m_Status = ClientStatus::Running;
return handshake_payload;
2026-01-06 19:50:45 +08:00
}
bool CommandClient::Tick(bool actively_stop) {
2026-01-06 21:24:47 +08:00
if (m_Status != ClientStatus::Running) {
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-06 21:24:47 +08:00
ProtocolCode sent_code = ProtocolCode::STOP_REQUEST;
m_PipeOperator.Write(&sent_code, sizeof(sent_code));
2026-01-06 19:50:45 +08:00
}
// Send data ready code to Trainer
2026-01-06 21:24:47 +08:00
ProtocolCode sent_code = ProtocolCode::DATA_READY;
m_PipeOperator.Write(&sent_code, sizeof(sent_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;
m_PipeOperator.Read(&recv_code, sizeof(recv_code));
2026-01-06 19:50:45 +08:00
2026-01-06 21:24:47 +08:00
switch (recv_code) {
case ProtocolCode::DATA_RECEIVED:
// Normal response, continue processing
return false; // Not stopping
case Basalt::Presenter::ProtocolCode::STOP:
// Trainer wants to stop
m_Status = ClientStatus::Stop;
return true; // Should stop
default:
throw std::runtime_error("unexpected code when running");
}
2026-01-06 19:50:45 +08:00
}
}
} // namespace Basalt::Presenter