Skip to content

Commit

Permalink
chore: 一些封装调整
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Feb 23, 2025
1 parent 675a989 commit b5debb4
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 53 deletions.
23 changes: 14 additions & 9 deletions source/MaaAgentClient/Client/AgentClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ std::optional<json::value> 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;
}

Expand All @@ -147,7 +147,7 @@ std::optional<json::value> 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;
}

Expand All @@ -163,20 +163,25 @@ bool AgentClient::recv_and_handle_start_up_response()
return false;
}

auto msg_opt = recv<StartUpResponse>();
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<StartUpResponse>()) {
LogError << "unexpected msg" << VAR(j) << VAR(ipc_addr_);
return false;
}

const StartUpResponse& msg = *msg_opt;
LogInfo << VAR(msg);
auto resp = j.as<StartUpResponse>();
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);
}
Expand Down
13 changes: 0 additions & 13 deletions source/MaaAgentClient/Client/AgentClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,9 @@ class AgentClient : public MaaAgentClient

private:
std::string create_socket();

bool send(const json::value& j);

std::optional<json::value> recv();

template <typename T>
std::optional<T> recv()
{
auto jopt = recv();
if (!jopt) {
return std::nullopt;
}
const json::value& j = *jopt;
return j.is<T>() ? std::make_optional(j.as<T>()) : std::nullopt;
}

template <typename ResponseT, typename RequestT>
std::optional<ResponseT> send_and_recv(const RequestT& req, std::function<bool(const json::value&)> reverse_request_handler = nullptr)
{
Expand Down
2 changes: 2 additions & 0 deletions source/MaaAgentServer/RemoteInstance/RemoteContext.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#include "RemoteContext.h"

#include "MaaAgent/Message.hpp"

MAA_AGENT_SERVER_NS_BEGIN

RemoteContext::RemoteContext(const std::string& context_id)
Expand Down
5 changes: 5 additions & 0 deletions source/MaaAgentServer/RemoteInstance/RemoteObject.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include "RemoteObject.h"

MAA_AGENT_SERVER_NS_BEGIN

MAA_AGENT_SERVER_NS_END
22 changes: 22 additions & 0 deletions source/MaaAgentServer/RemoteInstance/RemoteObject.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#pragma once

#include "Common/MaaTypes.h"

MAA_AGENT_SERVER_NS_BEGIN

class RemoteObject
{
public:
virtual ~RemoteObject() = default;

template <typename ResponseT, typename RequestT>
std::optional<ResponseT> send_and_recv(const RequestT& req)
{
}

private:
bool send(const json::value& j);
std::optional<json::value> recv();
};

MAA_AGENT_SERVER_NS_END
33 changes: 19 additions & 14 deletions source/MaaAgentServer/Server/AgentServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ std::optional<json::value> 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<CustomRecognitionRequest>()) {
return false;
Expand Down Expand Up @@ -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<CustomActionRequest>()) {
return false;
Expand Down Expand Up @@ -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<ShutDownRequest>()) {
return false;
Expand All @@ -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);
}
}

Expand Down
23 changes: 6 additions & 17 deletions source/MaaAgentServer/Server/AgentServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,28 +41,17 @@ class AgentServer : public SingletonHolder<AgentServer>

private:
bool create_socket(const std::string& ipc_addr);

bool send(const json::value& j);
bool send_start_up_response();

std::optional<json::value> recv();

template <typename T>
std::optional<T> recv()
{
auto jopt = recv();
if (!jopt) {
return std::nullopt;
}
const json::value& j = *jopt;
return j.is<T>() ? std::make_optional(j.as<T>()) : 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<std::string, CustomRecognitionSession> custom_recognitions_;
Expand Down

0 comments on commit b5debb4

Please sign in to comment.