it works
This commit is contained in:
@@ -1,77 +1,68 @@
|
||||
#include "command_client.hpp"
|
||||
#include <basalt/char_types.hpp>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace Basalt::Presenter {
|
||||
|
||||
CommandClient::CommandClient(const std::basic_string_view<BSCHAR> pipe_name)
|
||||
: m_PipeOperator(pipe_name) {
|
||||
}
|
||||
CommandClient::CommandClient() : m_PipeOperator(BSTEXT("ed0e3f1f-d214-4880-9562-640bce15e72e")), m_Status(ClientStatus::Ready) {}
|
||||
|
||||
CommandClient::~CommandClient() {
|
||||
}
|
||||
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");
|
||||
HandshakePayload CommandClient::WaitHandshake() {
|
||||
if (m_Status != ClientStatus::Ready) {
|
||||
throw std::runtime_error("unexcpected client status");
|
||||
}
|
||||
|
||||
// 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)));
|
||||
ProtocolCode request_code;
|
||||
m_PipeOperator.Read(&request_code, sizeof(request_code));
|
||||
if (request_code != ProtocolCode::HANDSHAKE_REQUEST) {
|
||||
throw std::runtime_error("unexpcted handshake code");
|
||||
}
|
||||
// Accept payload
|
||||
HandshakePayload handshake_payload;
|
||||
m_PipeOperator.Read(&handshake_payload, sizeof(HandshakePayload));
|
||||
|
||||
// Send handshake response (code 0x62) back to Trainer
|
||||
std::uint8_t handshake_response = HANDSHAKE_CODE_RESPONSE;
|
||||
m_PipeOperator.Write(&handshake_response, sizeof(handshake_response));
|
||||
ProtocolCode response_code = ProtocolCode::HANDSHAKE_RESPONSE;
|
||||
m_PipeOperator.Write(&response_code, sizeof(response_code));
|
||||
|
||||
// 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;
|
||||
// Set status and return
|
||||
m_Status = ClientStatus::Running;
|
||||
return handshake_payload;
|
||||
}
|
||||
|
||||
bool CommandClient::Tick(bool actively_stop) {
|
||||
if (!m_Handshaked) {
|
||||
throw std::runtime_error("Handshake must be completed before calling Tick");
|
||||
if (m_Status != ClientStatus::Running) {
|
||||
throw std::runtime_error("unexcpected client status");
|
||||
}
|
||||
|
||||
// If actively_stop is true, send actively stop code to Trainer
|
||||
// If actively stop, send actively stop code to Trainer first
|
||||
if (actively_stop) {
|
||||
std::uint8_t stop_code = ACTIVELY_STOP_CODE;
|
||||
m_PipeOperator.Write(&stop_code, sizeof(stop_code));
|
||||
ProtocolCode sent_code = ProtocolCode::STOP_REQUEST;
|
||||
m_PipeOperator.Write(&sent_code, sizeof(sent_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));
|
||||
ProtocolCode sent_code = ProtocolCode::DATA_READY;
|
||||
m_PipeOperator.Write(&sent_code, sizeof(sent_code));
|
||||
|
||||
// Wait for response from Trainer
|
||||
std::uint8_t received_code;
|
||||
m_PipeOperator.Read(&received_code, sizeof(received_code));
|
||||
// Process the response from Trainer
|
||||
while (true) {
|
||||
ProtocolCode recv_code;
|
||||
m_PipeOperator.Read(&recv_code, sizeof(recv_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");
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,38 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <basalt/pipe_operator.hpp>
|
||||
#include <cstdint>
|
||||
|
||||
namespace Basalt::Presenter {
|
||||
|
||||
// Protocol codes
|
||||
enum class ProtocolCode : std::uint8_t {
|
||||
HANDSHAKE_REQUEST = 0x61, //< Trainer -> Presenter
|
||||
HANDSHAKE_RESPONSE = 0x62, //< Presenter -> Trainer
|
||||
DATA_READY = 0x01, //< Presenter -> Trainer
|
||||
DATA_RECEIVED = 0x02, //< Trainer -> Presenter
|
||||
STOP_REQUEST = 0x71, //< Both directions
|
||||
STOP = 0x71 //< Both directions (same code)
|
||||
};
|
||||
|
||||
// Pixel kind values
|
||||
enum class PixelKind : std::uint8_t {
|
||||
GRAY_FLOAT32 = 0x01, ///< Grayscale represented by one float32
|
||||
GRAY_U8 = 0x02, ///< Grayscale represented by one u8
|
||||
RGB_FLOAT32 = 0x03, ///< RGB represented by three float32
|
||||
RGB_U8 = 0x04 ///< RGB represented by three u8
|
||||
};
|
||||
|
||||
#pragma pack(push, 1)
|
||||
struct HandshakePayload {
|
||||
PixelKind pixel_kind;
|
||||
std::uint32_t width;
|
||||
std::uint32_t height;
|
||||
};
|
||||
#pragma pack(pop)
|
||||
|
||||
// Status
|
||||
enum class ClientStatus {
|
||||
Ready,
|
||||
Running,
|
||||
Stop,
|
||||
};
|
||||
|
||||
class CommandClient {
|
||||
public:
|
||||
// Protocol codes
|
||||
static constexpr std::uint8_t HANDSHAKE_CODE_REQUEST = 0x61; // Trainer -> Presenter
|
||||
static constexpr std::uint8_t HANDSHAKE_CODE_RESPONSE = 0x62; // Presenter -> Trainer
|
||||
static constexpr std::uint8_t DATA_READY_CODE = 0x01; // Presenter -> Trainer
|
||||
static constexpr std::uint8_t DATA_RECEIVED_CODE = 0x02; // Trainer -> Presenter
|
||||
static constexpr std::uint8_t ACTIVELY_STOP_CODE = 0x71; // Both directions
|
||||
static constexpr std::uint8_t STOP_CODE = 0x71; // Both directions (same code)
|
||||
|
||||
// Pixel kind values
|
||||
static constexpr std::uint8_t PIXEL_GRAY_FLOAT32 = 0x01; // Grayscale represented by one float32
|
||||
static constexpr std::uint8_t PIXEL_GRAY_U8 = 0x02; // Grayscale represented by one u8
|
||||
static constexpr std::uint8_t PIXEL_RGB_FLOAT32 = 0x03; // RGB represented by three float32
|
||||
static constexpr std::uint8_t PIXEL_RGB_U8 = 0x04; // RGB represented by three u8
|
||||
|
||||
CommandClient(const std::basic_string_view<BSCHAR> pipe_name = BSTEXT("ed0e3f1f-d214-4880-9562-640bce15e72e"));
|
||||
CommandClient();
|
||||
~CommandClient();
|
||||
|
||||
public:
|
||||
// Wait for handshake from Trainer, send response with data properties
|
||||
void WaitHandshake(std::uint8_t pixel_kind, std::uint32_t width, std::uint32_t height);
|
||||
|
||||
HandshakePayload WaitHandshake();
|
||||
// Tick function called every frame to send data ready and wait for response
|
||||
bool Tick(bool actively_stop = false);
|
||||
|
||||
private:
|
||||
Basalt::Shared::PipeOperator m_PipeOperator;
|
||||
bool m_Handshaked = false; // Track handshake state
|
||||
Shared::PipeOperator m_PipeOperator;
|
||||
ClientStatus m_Status;
|
||||
};
|
||||
|
||||
} // namespace Basalt::Presenter
|
||||
@@ -1,4 +1,5 @@
|
||||
#include "dll_loader.hpp"
|
||||
#include "command_client.hpp"
|
||||
#include <basalt/char_types.hpp>
|
||||
#include <basalt/kernel.hpp>
|
||||
|
||||
@@ -8,14 +9,20 @@ namespace Kernel = ::Basalt::Shared::Kernel;
|
||||
int main(int argc, char* argv[]) {
|
||||
auto engine_dll = Presenter::DllLoader(Presenter::DllKind::Engine, BSTEXT("BasaltDirectX11Engine"));
|
||||
auto deliver_dll = Presenter::DllLoader(Presenter::DllKind::Deliver, BSTEXT("BasaltPipeDeliver"));
|
||||
|
||||
auto client = Presenter::CommandClient();
|
||||
auto payload = client.WaitHandshake();
|
||||
|
||||
auto* engine = engine_dll.CreateInstance<Kernel::IEngine>();
|
||||
auto* deliver = deliver_dll.CreateInstance<Kernel::IDeliver>();
|
||||
|
||||
Kernel::EngineConfig engine_config{.headless = false, .title = BSTEXT("Fuck You"), .width = 800, .height = 600};
|
||||
Kernel::EngineConfig engine_config{.headless = false, .title = BSTEXT("Fuck You"), .width = payload.width, .height = payload.height};
|
||||
engine->Startup(std::move(engine_config));
|
||||
|
||||
while (true) {
|
||||
if (engine->Tick()) break;
|
||||
auto req_stop = engine->Tick();
|
||||
auto can_stop = client.Tick(req_stop);
|
||||
if (can_stop) break;
|
||||
}
|
||||
|
||||
engine->Shutdown();
|
||||
|
||||
Reference in New Issue
Block a user