update cmd client
This commit is contained in:
@@ -190,8 +190,8 @@ private:
|
|||||||
std::vector<BYTE> depth_data; ///< 深度数据
|
std::vector<BYTE> depth_data; ///< 深度数据
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual void Startup(EngineConfig&& config) override {
|
virtual void startup(EngineConfig&& config) override {
|
||||||
IEngine::Startup(std::move(config));
|
IEngine::startup(std::move(config));
|
||||||
|
|
||||||
// 创建Win32窗口并显示
|
// 创建Win32窗口并显示
|
||||||
if (this->config.headless) {
|
if (this->config.headless) {
|
||||||
@@ -320,8 +320,8 @@ public:
|
|||||||
//frameCount = 0;
|
//frameCount = 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual bool Tick() override {
|
virtual bool tick() override {
|
||||||
if (IEngine::Tick()) return true;
|
if (IEngine::tick()) return true;
|
||||||
|
|
||||||
// Event loop
|
// Event loop
|
||||||
if (EventLoop()) return true;
|
if (EventLoop()) return true;
|
||||||
@@ -368,8 +368,8 @@ public:
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual void Shutdown() override {
|
virtual void shutdown() override {
|
||||||
IEngine::Shutdown();
|
IEngine::shutdown();
|
||||||
|
|
||||||
//if (hPipe != INVALID_HANDLE_VALUE) {
|
//if (hPipe != INVALID_HANDLE_VALUE) {
|
||||||
// CloseHandle(hPipe);
|
// CloseHandle(hPipe);
|
||||||
|
|||||||
@@ -4,66 +4,72 @@
|
|||||||
|
|
||||||
namespace basalt::presenter::cmd_client {
|
namespace basalt::presenter::cmd_client {
|
||||||
|
|
||||||
CommandClient::CommandClient() : m_PipeOperator(BSTEXT("ed0e3f1f-d214-4880-9562-640bce15e72e")), m_Status(ClientStatus::Ready) {}
|
CmdClient::CmdClient() : m_PipeOperator(BSTEXT("ed0e3f1f-d214-4880-9562-640bce15e72e")), m_Status(CmdClientStatus::Ready) {}
|
||||||
|
|
||||||
CommandClient::~CommandClient() {}
|
CmdClient::~CmdClient() {}
|
||||||
|
|
||||||
HandshakePayload CommandClient::WaitHandshake() {
|
HandshakePayload CmdClient::wait_handshake() {
|
||||||
if (m_Status != ClientStatus::Ready) {
|
if (m_Status != CmdClientStatus::Ready) {
|
||||||
throw std::runtime_error("unexcpected client status");
|
throw std::runtime_error("unexcpected client status");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Wait for handshake request from Trainer (code 0x61)
|
// Wait for handshake request from Trainer (code 0x61)
|
||||||
ProtocolCode request_code;
|
ProtocolCode request_code;
|
||||||
m_PipeOperator.Read(&request_code, sizeof(request_code));
|
m_PipeOperator.read_pod(request_code);
|
||||||
if (request_code != ProtocolCode::HANDSHAKE_REQUEST) {
|
if (request_code != ProtocolCode::HANDSHAKE_REQUEST) {
|
||||||
throw std::runtime_error("unexpcted handshake code");
|
throw std::runtime_error("unexpcted handshake code");
|
||||||
}
|
}
|
||||||
// Accept payload
|
// Accept payload
|
||||||
HandshakePayload handshake_payload;
|
HandshakePayload handshake_payload;
|
||||||
m_PipeOperator.Read(&handshake_payload, sizeof(HandshakePayload));
|
m_PipeOperator.read_pod(handshake_payload);
|
||||||
|
|
||||||
// Send handshake response (code 0x62) back to Trainer
|
// Send handshake response (code 0x62) back to Trainer
|
||||||
ProtocolCode response_code = ProtocolCode::HANDSHAKE_RESPONSE;
|
ProtocolCode response_code = ProtocolCode::HANDSHAKE_RESPONSE;
|
||||||
m_PipeOperator.Write(&response_code, sizeof(response_code));
|
m_PipeOperator.write_pod(response_code);
|
||||||
|
|
||||||
// Set status and return
|
// Set status and return
|
||||||
m_Status = ClientStatus::Running;
|
m_Status = CmdClientStatus::Running;
|
||||||
return handshake_payload;
|
return handshake_payload;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool CommandClient::Tick(bool actively_stop) {
|
bool CmdClient::tick(bool actively_stop) {
|
||||||
if (m_Status != ClientStatus::Running) {
|
if (m_Status != CmdClientStatus::Running) {
|
||||||
throw std::runtime_error("unexcpected client status");
|
throw std::runtime_error("unexcpected client status");
|
||||||
}
|
}
|
||||||
|
|
||||||
// If actively stop, send actively stop code to Trainer first
|
// If actively stop, send actively stop code to Trainer first
|
||||||
if (actively_stop) {
|
if (actively_stop) {
|
||||||
ProtocolCode sent_code = ProtocolCode::STOP_REQUEST;
|
ProtocolCode actively_stop_code = ProtocolCode::STOP_REQUEST;
|
||||||
m_PipeOperator.Write(&sent_code, sizeof(sent_code));
|
m_PipeOperator.write_pod(actively_stop_code);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Send data ready code to Trainer
|
// Send data ready code to Trainer
|
||||||
ProtocolCode sent_code = ProtocolCode::DATA_READY;
|
ProtocolCode data_ready_code = ProtocolCode::DATA_READY;
|
||||||
m_PipeOperator.Write(&sent_code, sizeof(sent_code));
|
m_PipeOperator.write_pod(data_ready_code);
|
||||||
|
|
||||||
// Process the response from Trainer
|
// Process the response from Trainer
|
||||||
while (true) {
|
while (true) {
|
||||||
ProtocolCode recv_code;
|
ProtocolCode recv_code;
|
||||||
m_PipeOperator.Read(&recv_code, sizeof(recv_code));
|
m_PipeOperator.read_pod(recv_code);
|
||||||
|
|
||||||
switch (recv_code) {
|
switch (recv_code) {
|
||||||
case ProtocolCode::DATA_RECEIVED:
|
case ProtocolCode::DATA_RECEIVED: {
|
||||||
// Normal response, continue processing
|
// Normal response, continue processing
|
||||||
return false; // Not stopping
|
return false; // Not stopping
|
||||||
case Basalt::Presenter::ProtocolCode::STOP:
|
}
|
||||||
|
case ProtocolCode::STOP_REQUEST: {
|
||||||
// Trainer wants to stop
|
// Trainer wants to stop
|
||||||
m_Status = ClientStatus::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
|
return true; // Should stop
|
||||||
|
}
|
||||||
default:
|
default:
|
||||||
throw std::runtime_error("unexpected code when running");
|
throw std::runtime_error("unexpected code when running");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace Basalt::Presenter
|
} // namespace basalt::presenter::cmd_client
|
||||||
@@ -12,7 +12,7 @@ namespace basalt::presenter::cmd_client {
|
|||||||
DATA_RECEIVED = 0x02, ///< Trainer -> Presenter
|
DATA_RECEIVED = 0x02, ///< Trainer -> Presenter
|
||||||
ACTIVELY_STOP = 0x21, ///< Presenter-->Trainer
|
ACTIVELY_STOP = 0x21, ///< Presenter-->Trainer
|
||||||
STOP_REQUEST = 0X71, ///< Presenter<--Trainer
|
STOP_REQUEST = 0X71, ///< Presenter<--Trainer
|
||||||
STOP_RESPONSE = 0x72, //< Presenter-->Trainer
|
STOP_RESPONSE = 0x72, ///< Presenter-->Trainer
|
||||||
};
|
};
|
||||||
|
|
||||||
// Pixel kind values
|
// Pixel kind values
|
||||||
@@ -32,26 +32,26 @@ namespace basalt::presenter::cmd_client {
|
|||||||
#pragma pack(pop)
|
#pragma pack(pop)
|
||||||
|
|
||||||
// Status
|
// Status
|
||||||
enum class ClientStatus {
|
enum class CmdClientStatus {
|
||||||
Ready,
|
Ready,
|
||||||
Running,
|
Running,
|
||||||
Stop,
|
Stop,
|
||||||
};
|
};
|
||||||
|
|
||||||
class CommandClient {
|
class CmdClient {
|
||||||
public:
|
public:
|
||||||
CommandClient();
|
CmdClient();
|
||||||
~CommandClient();
|
~CmdClient();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
// Wait for handshake from Trainer, send response with data properties
|
// Wait for handshake from Trainer, send response with data properties
|
||||||
HandshakePayload WaitHandshake();
|
HandshakePayload wait_handshake();
|
||||||
// Tick function called every frame to send data ready and wait for response
|
// Tick function called every frame to send data ready and wait for response
|
||||||
bool Tick(bool actively_stop = false);
|
bool tick(bool actively_stop = false);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Shared::PipeOperator m_PipeOperator;
|
shared::pipe_operator::PipeOperator m_PipeOperator;
|
||||||
ClientStatus m_Status;
|
CmdClientStatus m_Status;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Basalt::Presenter
|
} // namespace basalt::presenter::cmd_client
|
||||||
@@ -50,4 +50,4 @@ namespace basalt::presenter::dll_loader {
|
|||||||
Handle m_Handle;
|
Handle m_Handle;
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace Basalt::Presenter
|
} // namespace basalt::presenter::dll_loader
|
||||||
|
|||||||
@@ -1,30 +1,32 @@
|
|||||||
#include "dll_loader.hpp"
|
#include "dll_loader.hpp"
|
||||||
#include "command_client.hpp"
|
#include "cmd_client.hpp"
|
||||||
#include <basalt/char_types.hpp>
|
#include <basalt/char_types.hpp>
|
||||||
#include <basalt/kernel.hpp>
|
|
||||||
|
|
||||||
namespace Presenter = ::Basalt::Presenter;
|
namespace dll_loader = ::basalt::presenter::dll_loader;
|
||||||
namespace Kernel = ::Basalt::Shared::Kernel;
|
using dll_loader::DllKind;
|
||||||
|
using dll_loader::DllLoader;
|
||||||
|
|
||||||
|
using ::basalt::presenter::cmd_client::CmdClient;
|
||||||
|
|
||||||
int main(int argc, char* argv[]) {
|
int main(int argc, char* argv[]) {
|
||||||
auto engine_dll = Presenter::DllLoader(Presenter::DllKind::Engine, BSTEXT("BasaltDirectX11Engine"));
|
auto engine_dll = DllLoader(DllKind::Engine, BSTEXT("BasaltDirectX11Engine"));
|
||||||
auto deliver_dll = Presenter::DllLoader(Presenter::DllKind::Deliver, BSTEXT("BasaltPipeDeliver"));
|
auto deliver_dll = DllLoader(DllKind::Deliver, BSTEXT("BasaltPipeDeliver"));
|
||||||
|
|
||||||
auto client = Presenter::CommandClient();
|
auto client = CmdClient();
|
||||||
auto payload = client.WaitHandshake();
|
auto payload = client.wait_handshake();
|
||||||
|
|
||||||
auto* engine = engine_dll.CreateInstance<Kernel::IEngine>();
|
//auto* engine = engine_dll.CreateInstance<Kernel::IEngine>();
|
||||||
auto* deliver = deliver_dll.CreateInstance<Kernel::IDeliver>();
|
//auto* deliver = deliver_dll.CreateInstance<Kernel::IDeliver>();
|
||||||
|
|
||||||
Kernel::EngineConfig engine_config{.headless = false, .title = BSTEXT("Fuck You"), .width = payload.width, .height = payload.height};
|
//Kernel::EngineConfig engine_config{.headless = false, .title = BSTEXT("Fuck You"), .width = payload.width, .height = payload.height};
|
||||||
engine->Startup(std::move(engine_config));
|
//engine->startup(std::move(engine_config));
|
||||||
|
|
||||||
while (true) {
|
while (true) {
|
||||||
auto req_stop = engine->Tick();
|
auto req_stop = false; //engine->tick();
|
||||||
auto can_stop = client.Tick(req_stop);
|
auto can_stop = client.tick(req_stop);
|
||||||
if (can_stop) break;
|
if (can_stop) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
engine->Shutdown();
|
//engine->shutdown();
|
||||||
engine_dll.DestroyInstance(engine);
|
//engine_dll.DestroyInstance(engine);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,23 +5,27 @@ namespace basalt::shared::deliver {
|
|||||||
|
|
||||||
IDeliver::IDeliver() {}
|
IDeliver::IDeliver() {}
|
||||||
|
|
||||||
IDeliver::~IDeliver() {}
|
IDeliver::~IDeliver() {
|
||||||
|
if (this->status != DeliverStatus::Stop) {
|
||||||
|
this->shutdown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
guid::Guid IDeliver::GetGuid() const {
|
guid::Guid IDeliver::get_guid() const {
|
||||||
throw std::logic_error("unimplemented function");
|
throw std::logic_error("unimplemented function");
|
||||||
}
|
}
|
||||||
|
|
||||||
void IDeliver::Startup(DeliverConfig &&config) {
|
void IDeliver::startup(DeliverConfig &&config) {
|
||||||
if (this->status != DeliverStatus::Ready) throw std::runtime_error("unexpected deliver status");
|
if (this->status != DeliverStatus::Ready) throw std::runtime_error("unexpected deliver status");
|
||||||
this->config = std::move(config);
|
this->config = std::move(config);
|
||||||
this->status = DeliverStatus::Running;
|
this->status = DeliverStatus::Running;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IDeliver::Transmit() {
|
void IDeliver::transmit() {
|
||||||
if (this->status != DeliverStatus::Running) throw std::runtime_error("unexpected deliver status");
|
if (this->status != DeliverStatus::Running) throw std::runtime_error("unexpected deliver status");
|
||||||
}
|
}
|
||||||
|
|
||||||
void IDeliver::Shutdown() {
|
void IDeliver::shutdown() {
|
||||||
if (this->status != DeliverStatus::Running) throw std::runtime_error("unexpected deliver status");
|
if (this->status != DeliverStatus::Running) throw std::runtime_error("unexpected deliver status");
|
||||||
this->status = DeliverStatus::Stop;
|
this->status = DeliverStatus::Stop;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -19,10 +19,10 @@ namespace basalt::shared::deliver {
|
|||||||
virtual ~IDeliver();
|
virtual ~IDeliver();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual guid::Guid GetGuid() const;
|
virtual guid::Guid get_guid() const;
|
||||||
virtual void Startup(DeliverConfig&& config);
|
virtual void startup(DeliverConfig&& config);
|
||||||
virtual void Transmit();
|
virtual void transmit();
|
||||||
virtual void Shutdown();
|
virtual void shutdown();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
DeliverConfig config;
|
DeliverConfig config;
|
||||||
|
|||||||
@@ -7,26 +7,26 @@ namespace basalt::shared::engine {
|
|||||||
|
|
||||||
IEngine::~IEngine() {
|
IEngine::~IEngine() {
|
||||||
if (this->status != EngineStatus::Stop) {
|
if (this->status != EngineStatus::Stop) {
|
||||||
this->Shutdown();
|
this->shutdown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
guid::Guid IEngine::GetGuid() const {
|
guid::Guid IEngine::get_guid() const {
|
||||||
throw std::logic_error("unimplemented function");
|
throw std::logic_error("unimplemented function");
|
||||||
}
|
}
|
||||||
|
|
||||||
void IEngine::Startup(EngineConfig &&config) {
|
void IEngine::startup(EngineConfig &&config) {
|
||||||
if (this->status != EngineStatus::Ready) throw std::runtime_error("unexpected engine status");
|
if (this->status != EngineStatus::Ready) throw std::runtime_error("unexpected engine status");
|
||||||
this->config = std::move(config);
|
this->config = std::move(config);
|
||||||
this->status = EngineStatus::Running;
|
this->status = EngineStatus::Running;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool IEngine::Tick() {
|
bool IEngine::tick() {
|
||||||
if (this->status != EngineStatus::Running) throw std::runtime_error("unexpected engine status");
|
if (this->status != EngineStatus::Running) throw std::runtime_error("unexpected engine status");
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void IEngine::Shutdown() {
|
void IEngine::shutdown() {
|
||||||
if (this->status != EngineStatus::Running) throw std::runtime_error("unexpected engine status");
|
if (this->status != EngineStatus::Running) throw std::runtime_error("unexpected engine status");
|
||||||
this->status = EngineStatus::Stop;
|
this->status = EngineStatus::Stop;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,14 +24,14 @@ namespace basalt::shared::engine {
|
|||||||
virtual ~IEngine();
|
virtual ~IEngine();
|
||||||
|
|
||||||
public:
|
public:
|
||||||
virtual guid::Guid GetGuid() const;
|
virtual guid::Guid get_guid() const;
|
||||||
virtual void Startup(EngineConfig&& config);
|
virtual void startup(EngineConfig&& config);
|
||||||
/**
|
/**
|
||||||
* @brief
|
* @brief
|
||||||
* @return True for active exit.
|
* @return True for active exit.
|
||||||
*/
|
*/
|
||||||
virtual bool Tick();
|
virtual bool tick();
|
||||||
virtual void Shutdown();
|
virtual void shutdown();
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
EngineConfig config;
|
EngineConfig config;
|
||||||
|
|||||||
@@ -78,7 +78,7 @@ namespace basalt::shared::pipe_operator {
|
|||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
void PipeOperator::Read(void *buffer, size_t size) {
|
void PipeOperator::read(void *buffer, size_t size) {
|
||||||
if (size == 0) {
|
if (size == 0) {
|
||||||
return; // 读取0字节直接返回
|
return; // 读取0字节直接返回
|
||||||
}
|
}
|
||||||
@@ -124,7 +124,7 @@ namespace basalt::shared::pipe_operator {
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void PipeOperator::Write(const void *buffer, size_t size) {
|
void PipeOperator::write(const void *buffer, size_t size) {
|
||||||
if (size == 0) {
|
if (size == 0) {
|
||||||
return; // 写入0字节直接返回
|
return; // 写入0字节直接返回
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -32,26 +32,26 @@ namespace basalt::shared::pipe_operator {
|
|||||||
PipeOperator& operator=(PipeOperator&& other) noexcept;
|
PipeOperator& operator=(PipeOperator&& other) noexcept;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
void Read(void* buffer, size_t size);
|
void read(void* buffer, size_t size);
|
||||||
void Write(const void* buffer, size_t size);
|
void write(const void* buffer, size_t size);
|
||||||
|
|
||||||
template<typename TPod>
|
template<typename TPod>
|
||||||
void ReadPod(TPod& buffer) {
|
void read_pod(TPod& buffer) {
|
||||||
Read(&buffer, sizeof(TPod));
|
read(&buffer, sizeof(TPod));
|
||||||
}
|
}
|
||||||
template<typename TPod>
|
template<typename TPod>
|
||||||
void WritePod(const TPod& buffer) {
|
void write_pod(const TPod& buffer) {
|
||||||
Write(&buffer, sizeof(TPod));
|
write(&buffer, sizeof(TPod));
|
||||||
}
|
}
|
||||||
void ReadString(std::string& buffer) {
|
void read_string(std::string& buffer) {
|
||||||
size_t length = 0;
|
size_t length = 0;
|
||||||
ReadPod(length);
|
read_pod(length);
|
||||||
buffer.resize(length);
|
buffer.resize(length);
|
||||||
Read(buffer.data(), length);
|
read(buffer.data(), length);
|
||||||
}
|
}
|
||||||
void WriteString(std::string_view& buffer) {
|
void write_string(std::string_view& buffer) {
|
||||||
WritePod(buffer.size());
|
write_pod(buffer.size());
|
||||||
Write(buffer.data(), buffer.size());
|
write(buffer.data(), buffer.size());
|
||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ CODE_PACKER: struct.Struct = struct.Struct("=B")
|
|||||||
HANDSHAKE_REQUEST_PACKER: struct.Struct = struct.Struct("=BII")
|
HANDSHAKE_REQUEST_PACKER: struct.Struct = struct.Struct("=BII")
|
||||||
|
|
||||||
|
|
||||||
class CommandServer:
|
class CmdServer:
|
||||||
"""
|
"""
|
||||||
Command server implementation for the Trainer side according to the protocol.
|
Command server implementation for the Trainer side according to the protocol.
|
||||||
"""
|
"""
|
||||||
@@ -1,4 +1,4 @@
|
|||||||
from command_server import CommandServer, HandshakePayload, PixelKind
|
from cmd_server import CmdServer, HandshakePayload, PixelKind
|
||||||
from while_stopper import WhileStopper
|
from while_stopper import WhileStopper
|
||||||
import logging
|
import logging
|
||||||
import signal
|
import signal
|
||||||
@@ -9,7 +9,7 @@ def receive_data() -> None:
|
|||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
server = CommandServer()
|
server = CmdServer()
|
||||||
|
|
||||||
print('Please launch BasaltPresenter now.')
|
print('Please launch BasaltPresenter now.')
|
||||||
print('Then press Enter to continue...')
|
print('Then press Enter to continue...')
|
||||||
|
|||||||
Reference in New Issue
Block a user