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

78 lines
3.0 KiB
C++
Raw Normal View History

2026-01-06 19:50:45 +08:00
#include "command_client.hpp"
#include <stdexcept>
namespace Basalt::Presenter {
CommandClient::CommandClient(const std::basic_string_view<BSCHAR> pipe_name)
: m_PipeOperator(pipe_name) {
}
CommandClient::~CommandClient() {
}
void CommandClient::WaitHandshake(std::uint8_t pixel_kind, std::uint32_t width, std::uint32_t height) {
if (m_Handshaked) {
throw std::runtime_error("Handshake already completed");
}
// Wait for handshake request from Trainer (code 0x61)
std::uint8_t received_code;
m_PipeOperator.Read(&received_code, sizeof(received_code));
if (received_code != HANDSHAKE_CODE_REQUEST) {
throw std::runtime_error("Expected handshake code 0x61, got 0x" +
std::to_string(static_cast<int>(received_code)));
}
// Send handshake response (code 0x62) back to Trainer
std::uint8_t handshake_response = HANDSHAKE_CODE_RESPONSE;
m_PipeOperator.Write(&handshake_response, sizeof(handshake_response));
// Send data properties after handshake
m_PipeOperator.Write(&pixel_kind, sizeof(pixel_kind));
m_PipeOperator.Write(&width, sizeof(width));
m_PipeOperator.Write(&height, sizeof(height));
m_Handshaked = true;
}
bool CommandClient::Tick(bool actively_stop) {
if (!m_Handshaked) {
throw std::runtime_error("Handshake must be completed before calling Tick");
}
// If actively_stop is true, send actively stop code to Trainer
if (actively_stop) {
std::uint8_t stop_code = ACTIVELY_STOP_CODE;
m_PipeOperator.Write(&stop_code, sizeof(stop_code));
}
// Send data ready code to Trainer
std::uint8_t data_ready_code = DATA_READY_CODE;
m_PipeOperator.Write(&data_ready_code, sizeof(data_ready_code));
// Wait for response from Trainer
std::uint8_t received_code;
m_PipeOperator.Read(&received_code, sizeof(received_code));
// Handle the received code
if (received_code == DATA_RECEIVED_CODE) {
// Normal response, continue processing
return false; // Not stopping
} else if (received_code == STOP_CODE) {
// Trainer wants to stop
return true; // Should stop
} else if (received_code == HANDSHAKE_CODE_REQUEST || received_code == HANDSHAKE_CODE_RESPONSE) {
// Unexpected handshake code during Tick
throw std::runtime_error("Unexpected handshake code 0x" +
std::to_string(static_cast<int>(received_code)) +
" received during Tick operation");
} else {
// Unknown code
throw std::runtime_error("Unknown code 0x" +
std::to_string(static_cast<int>(received_code)) +
" received during Tick operation");
}
}
} // namespace Basalt::Presenter