From b5debb4b23ee87d92111d0649905f2ec6af4d4ff Mon Sep 17 00:00:00 2001 From: MistEO Date: Sun, 23 Feb 2025 22:40:18 +0800 Subject: [PATCH] =?UTF-8?q?chore:=20=E4=B8=80=E4=BA=9B=E5=B0=81=E8=A3=85?= =?UTF-8?q?=E8=B0=83=E6=95=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/MaaAgentClient/Client/AgentClient.cpp | 23 ++++++++----- source/MaaAgentClient/Client/AgentClient.h | 13 -------- .../RemoteInstance/RemoteContext.cpp | 2 ++ .../RemoteInstance/RemoteObject.cpp | 5 +++ .../RemoteInstance/RemoteObject.h | 22 +++++++++++++ source/MaaAgentServer/Server/AgentServer.cpp | 33 +++++++++++-------- source/MaaAgentServer/Server/AgentServer.h | 23 ++++--------- 7 files changed, 68 insertions(+), 53 deletions(-) create mode 100644 source/MaaAgentServer/RemoteInstance/RemoteObject.cpp create mode 100644 source/MaaAgentServer/RemoteInstance/RemoteObject.h diff --git a/source/MaaAgentClient/Client/AgentClient.cpp b/source/MaaAgentClient/Client/AgentClient.cpp index e0ea0675e..687e1ff4c 100644 --- a/source/MaaAgentClient/Client/AgentClient.cpp +++ b/source/MaaAgentClient/Client/AgentClient.cpp @@ -138,7 +138,7 @@ std::optional AgentClient::recv() zmq::message_t msg; auto size_opt = child_sock_.recv(msg); if (!size_opt || *size_opt == 0) { - LogError << "failed to recv msg"; + LogError << "failed to recv msg" << VAR(ipc_addr_); return std::nullopt; } @@ -147,7 +147,7 @@ std::optional AgentClient::recv() auto jopt = json::parse(str); if (!jopt) { - LogError << "failed to parse msg"; + LogError << "failed to parse msg" << VAR(ipc_addr_); return std::nullopt; } @@ -163,20 +163,25 @@ bool AgentClient::recv_and_handle_start_up_response() return false; } - auto msg_opt = recv(); - if (!msg_opt) { - LogError << "failed to recv StartUpResponse"; + auto jopt = recv(); + if (!jopt) { + LogError << "failed to recv msg" << VAR(ipc_addr_); + return false; + } + const json::value& j = *jopt; + if (!j.is()) { + LogError << "unexpected msg" << VAR(j) << VAR(ipc_addr_); return false; } - const StartUpResponse& msg = *msg_opt; - LogInfo << VAR(msg); + auto resp = j.as(); + LogInfo << VAR(resp); - for (const auto& reco : msg.recognitions) { + for (const auto& reco : resp.recognitions) { LogTrace << VAR(reco); resource_->register_custom_recognition(reco, reco_agent, this); } - for (const auto& act : msg.actions) { + for (const auto& act : resp.actions) { LogTrace << VAR(act); resource_->register_custom_action(act, action_agent, this); } diff --git a/source/MaaAgentClient/Client/AgentClient.h b/source/MaaAgentClient/Client/AgentClient.h index d8d1fb3dc..c3a30f9af 100644 --- a/source/MaaAgentClient/Client/AgentClient.h +++ b/source/MaaAgentClient/Client/AgentClient.h @@ -23,22 +23,9 @@ class AgentClient : public MaaAgentClient private: std::string create_socket(); - bool send(const json::value& j); - std::optional recv(); - template - std::optional recv() - { - auto jopt = recv(); - if (!jopt) { - return std::nullopt; - } - const json::value& j = *jopt; - return j.is() ? std::make_optional(j.as()) : std::nullopt; - } - template std::optional send_and_recv(const RequestT& req, std::function reverse_request_handler = nullptr) { diff --git a/source/MaaAgentServer/RemoteInstance/RemoteContext.cpp b/source/MaaAgentServer/RemoteInstance/RemoteContext.cpp index db1bf70cb..ea7c14f7d 100644 --- a/source/MaaAgentServer/RemoteInstance/RemoteContext.cpp +++ b/source/MaaAgentServer/RemoteInstance/RemoteContext.cpp @@ -1,5 +1,7 @@ #include "RemoteContext.h" +#include "MaaAgent/Message.hpp" + MAA_AGENT_SERVER_NS_BEGIN RemoteContext::RemoteContext(const std::string& context_id) diff --git a/source/MaaAgentServer/RemoteInstance/RemoteObject.cpp b/source/MaaAgentServer/RemoteInstance/RemoteObject.cpp new file mode 100644 index 000000000..4222409aa --- /dev/null +++ b/source/MaaAgentServer/RemoteInstance/RemoteObject.cpp @@ -0,0 +1,5 @@ +#include "RemoteObject.h" + +MAA_AGENT_SERVER_NS_BEGIN + +MAA_AGENT_SERVER_NS_END diff --git a/source/MaaAgentServer/RemoteInstance/RemoteObject.h b/source/MaaAgentServer/RemoteInstance/RemoteObject.h new file mode 100644 index 000000000..7f9f6a28a --- /dev/null +++ b/source/MaaAgentServer/RemoteInstance/RemoteObject.h @@ -0,0 +1,22 @@ +#pragma once + +#include "Common/MaaTypes.h" + +MAA_AGENT_SERVER_NS_BEGIN + +class RemoteObject +{ +public: + virtual ~RemoteObject() = default; + + template + std::optional send_and_recv(const RequestT& req) + { + } + +private: + bool send(const json::value& j); + std::optional recv(); +}; + +MAA_AGENT_SERVER_NS_END diff --git a/source/MaaAgentServer/Server/AgentServer.cpp b/source/MaaAgentServer/Server/AgentServer.cpp index 54ed61b92..39363b649 100644 --- a/source/MaaAgentServer/Server/AgentServer.cpp +++ b/source/MaaAgentServer/Server/AgentServer.cpp @@ -173,7 +173,7 @@ std::optional AgentServer::recv() return *jopt; } -bool AgentServer::recv_and_handle_recognition_request(const json::value& j) +bool AgentServer::handle_recognition_request(const json::value& j) { if (!j.is()) { return false; @@ -226,7 +226,7 @@ bool AgentServer::recv_and_handle_recognition_request(const json::value& j) return true; } -bool AgentServer::recv_and_handle_action_request(const json::value& j) +bool AgentServer::handle_action_request(const json::value& j) { if (!j.is()) { return false; @@ -270,7 +270,7 @@ bool AgentServer::recv_and_handle_action_request(const json::value& j) return true; } -bool AgentServer::recv_and_handle_shut_down_request(const json::value& j) +bool AgentServer::handle_shut_down_request(const json::value& j) { if (!j.is()) { return false; @@ -293,20 +293,25 @@ void AgentServer::request_msg_loop() auto msg_opt = recv(); if (!msg_opt) { LogError << "failed to recv msg" << VAR(ipc_addr_); - continue; + return; } const json::value& j = *msg_opt; - LogInfo << VAR(j) << VAR(ipc_addr_); + handle_request(j); + } +} - if (recv_and_handle_recognition_request(j)) { - } - else if (recv_and_handle_action_request(j)) { - } - else if (recv_and_handle_shut_down_request(j)) { - } - else { - LogError << "unknown msg" << VAR(j); - } +void AgentServer::handle_request(const json::value& j) +{ + LogInfo << VAR(j) << VAR(ipc_addr_); + + if (handle_recognition_request(j)) { + } + else if (handle_action_request(j)) { + } + else if (handle_shut_down_request(j)) { + } + else { + LogError << "unknown msg" << VAR(j); } } diff --git a/source/MaaAgentServer/Server/AgentServer.h b/source/MaaAgentServer/Server/AgentServer.h index 23db71b67..0071ab923 100644 --- a/source/MaaAgentServer/Server/AgentServer.h +++ b/source/MaaAgentServer/Server/AgentServer.h @@ -41,28 +41,17 @@ class AgentServer : public SingletonHolder private: bool create_socket(const std::string& ipc_addr); - bool send(const json::value& j); - bool send_start_up_response(); - std::optional recv(); - template - std::optional recv() - { - auto jopt = recv(); - if (!jopt) { - return std::nullopt; - } - const json::value& j = *jopt; - return j.is() ? std::make_optional(j.as()) : std::nullopt; - } - - bool recv_and_handle_recognition_request(const json::value& j); - bool recv_and_handle_action_request(const json::value& j); - bool recv_and_handle_shut_down_request(const json::value& j); + bool send_start_up_response(); + + bool handle_recognition_request(const json::value& j); + bool handle_action_request(const json::value& j); + bool handle_shut_down_request(const json::value& j); void request_msg_loop(); + void handle_request(const json::value& j); private: std::unordered_map custom_recognitions_;