Skip to content

Commit

Permalink
feat: client init msg基本架子
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Feb 15, 2025
1 parent b184c35 commit 8af6578
Show file tree
Hide file tree
Showing 22 changed files with 293 additions and 42 deletions.
3 changes: 1 addition & 2 deletions include/MaaAgentClient/MaaAgentClientAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,10 @@ extern "C"

MAA_AGENT_CLIENT_API void MaaAgentClientDestroy(MaaAgentClient* client);

MAA_AGENT_CLIENT_API MaaBool MaaAgentClientBindResource(MaaAgentClient* client, MaaResource* res);
MAA_AGENT_CLIENT_API MaaBool
MaaAgentClientStartChild(MaaAgentClient* client, const char* child_exec, const MaaStringListBuffer* child_args);

MAA_AGENT_CLIENT_API MaaBool MaaAgentClientBindResource(MaaAgentClient* client, MaaResource* res);

#ifdef __cplusplus
}
#endif
24 changes: 12 additions & 12 deletions source/MaaAgentClient/API/MaaAgentClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,18 @@ void MaaAgentClientDestroy(MaaAgentClient* client)
delete client;
}

MaaBool MaaAgentClientBindResource(MaaAgentClient* client, MaaResource* res)
{
LogFunc << VAR_VOIDP(client) << VAR_VOIDP(res);

if (!client || !res) {
LogError << "handle is null";
return false;
}

return client->bind_resource(res);
}

MaaBool MaaAgentClientStartChild(MaaAgentClient* client, const char* child_exec, const MaaStringListBuffer* child_args)
{
LogFunc << VAR_VOIDP(client) << VAR(child_exec) << VAR(child_args);
Expand All @@ -44,15 +56,3 @@ MaaBool MaaAgentClientStartChild(MaaAgentClient* client, const char* child_exec,

return client->start_clild(MAA_NS::path(child_exec), args);
}

MaaBool MaaAgentClientBindResource(MaaAgentClient* client, MaaResource* res)
{
LogFunc << VAR_VOIDP(client) << VAR_VOIDP(res);

if (!client || !res) {
LogError << "handle is null";
return false;
}

return client->bind_resource(res);
}
2 changes: 1 addition & 1 deletion source/MaaAgentClient/API/MaaAgentClientTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ struct MaaAgentClient
public:
virtual ~MaaAgentClient() = default;

virtual bool start_clild(const std::filesystem::path& child_exec, const std::vector<std::string>& child_args) = 0;
virtual bool bind_resource(MaaResource* resource) = 0;
virtual bool start_clild(const std::filesystem::path& child_exec, const std::vector<std::string>& child_args) = 0;
};
185 changes: 182 additions & 3 deletions source/MaaAgentClient/Client/AgentClient.cpp
Original file line number Diff line number Diff line change
@@ -1,18 +1,46 @@
#include "AgentClient.h"

#include <sstream>

#include <meojson/json.hpp>

#include "Common/MaaTypes.h"
#include "MaaAgent/Message.hpp"
#include "Utils/Codec.h"
#include "Utils/Logger.h"
#include "Utils/Platform.h"

MAA_AGENT_CLIENT_NS_BEGIN

#ifdef _WIN32
std::vector<std::wstring> conv_args(const std::vector<std::string>& args)
{
std::vector<std::wstring> wargs;
for (const auto& arg : args) {
wargs.emplace_back(to_u16(arg));
}
return wargs;
}
#else
std::vector<std::string> conv_args(const std::vector<std::string>& args)
{
return args;
}
#endif

AgentClient::AgentClient(MaaNotificationCallback notify, void* notify_trans_arg)
: notifier_(notify, notify_trans_arg)
{
LogFunc << VAR_VOIDP(notify) << VAR_VOIDP(notify_trans_arg);
}

bool AgentClient::start_clild(const std::filesystem::path& child_exec, const std::vector<std::string>& child_args)
AgentClient::~AgentClient()
{
LogFunc << VAR(child_exec) << VAR(child_args);
LogFunc;

return false;
if (child_ && child_.joinable()) {
child_.join();
}
}

bool AgentClient::bind_resource(MaaResource* resource)
Expand All @@ -24,4 +52,155 @@ bool AgentClient::bind_resource(MaaResource* resource)
return true;
}

bool AgentClient::start_clild(const std::filesystem::path& child_exec, const std::vector<std::string>& child_args)
{
LogFunc << VAR(child_exec) << VAR(child_args);

if (!resource_) {
LogError << "resource is not bound, please bind resource first";
return false;
}

if (child_) {
LogError << "child is already running";
return false;
}

std::filesystem::path exec = boost::process::search_path(child_exec);
if (!std::filesystem::exists(exec)) {
LogError << "exec not found" << VAR(child_exec);
return false;
}

std::string addr = create_socket();

std::vector<std::string> args = child_args;
args.emplace_back(addr);

child_ = boost::process::child(exec, conv_args(args));
if (!child_) {
LogError << "failed to start child" << VAR(exec) << VAR(child_args);
return false;
}

return recv_and_handle_init_msg();
}

std::string AgentClient::create_socket()
{
constexpr std::string_view kAddrFormat = "ipc://maafw-agent-{}";
std::stringstream ss;
ss << resource_;
std::string addr = std::format(kAddrFormat, std::move(ss).str());
LogInfo << VAR(addr);

child_sock_ = zmq::socket_t(child_ctx_, zmq::socket_type::pair);
child_sock_.bind(addr);

return addr;
}

std::optional<json::value> AgentClient::recv()
{
LogFunc;

zmq::message_t init_msg;
auto size_opt = child_sock_.recv(init_msg);
if (!size_opt || *size_opt == 0) {
LogError << "failed to recv init_msg";
return std::nullopt;
}

const std::string& init_str = init_msg.to_string();
LogInfo << VAR(init_str);

auto jopt = json::parse(init_str);
if (!jopt) {
LogError << "failed to parse init_msg";
return std::nullopt;
}

return *jopt;
}

bool AgentClient::recv_and_handle_init_msg()
{
LogFunc;

if (!resource_) {
LogError << "resource is not bound";
return false;
}

auto msg_opt = recv<InitMsg>();
if (!msg_opt) {
LogError << "failed to recv init_msg";
return false;
}

const InitMsg& msg = *msg_opt;
LogInfo << VAR(msg);

for (const auto& reco : msg.recognitions) {
LogTrace << VAR(reco);
resource_->register_custom_recognition(reco, reco_agent, this);
}
for (const auto& act : msg.actions) {
LogTrace << VAR(act);
resource_->register_custom_action(act, action_agent, this);
}

return true;
}

MaaBool AgentClient::reco_agent(
MaaContext* context,
MaaTaskId task_id,
const char* node_name,
const char* custom_recognition_name,
const char* custom_recognition_param,
const MaaImageBuffer* image,
const MaaRect* roi,
void* trans_arg,
MaaRect* out_box,
MaaStringBuffer* out_detail)
{
if (!trans_arg) {
LogError << "trans_arg is null";
return false;
}

AgentClient* pthis = reinterpret_cast<AgentClient*>(trans_arg);
if (!pthis) {
LogError << "pthis is null";
return false;
}

return true;
}

MaaBool AgentClient::action_agent(
MaaContext* context,
MaaTaskId task_id,
const char* node_name,
const char* custom_action_name,
const char* custom_action_param,
MaaRecoId reco_id,
const MaaRect* box,
void* trans_arg)
{
if (!trans_arg) {
LogError << "trans_arg is null";
return false;
}

AgentClient* pthis = reinterpret_cast<AgentClient*>(trans_arg);
if (!pthis) {
LogError << "pthis is null";
return false;
}

return true;
}

MAA_AGENT_CLIENT_NS_END
53 changes: 51 additions & 2 deletions source/MaaAgentClient/Client/AgentClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@

#include <filesystem>

#include <meojson/json.hpp>
#include <zmq.hpp>

#include "API/MaaAgentClientTypes.h"
#include "Conf/Conf.h"
#include "Utils/IOStream/BoostIO.hpp"
#include "Utils/MessageNotifier.hpp"

MAA_AGENT_CLIENT_NS_BEGIN
Expand All @@ -12,14 +16,59 @@ class AgentClient : public MaaAgentClient
{
public:
AgentClient(MaaNotificationCallback notify, void* notify_trans_arg);
virtual ~AgentClient() override = default;
virtual ~AgentClient() override;

virtual bool start_clild(const std::filesystem::path& child_exec, const std::vector<std::string>& child_args) override;
virtual bool bind_resource(MaaResource* resource) override;
virtual bool start_clild(const std::filesystem::path& child_exec, const std::vector<std::string>& child_args) override;

public:
std::string create_socket();

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_init_msg();

public:
static MaaBool reco_agent(
MaaContext* context,
MaaTaskId task_id,
const char* node_name,
const char* custom_recognition_name,
const char* custom_recognition_param,
const MaaImageBuffer* image,
const MaaRect* roi,
void* trans_arg,
/* out */ MaaRect* out_box,
/* out */ MaaStringBuffer* out_detail);

static MaaBool action_agent(
MaaContext* context,
MaaTaskId task_id,
const char* node_name,
const char* custom_action_name,
const char* custom_action_param,
MaaRecoId reco_id,
const MaaRect* box,
void* trans_arg);

private:
MaaResource* resource_ = nullptr;
MessageNotifier notifier_;
boost::process::child child_;

zmq::context_t child_ctx_;
zmq::socket_t child_sock_;
};

MAA_AGENT_CLIENT_NS_END
2 changes: 1 addition & 1 deletion source/MaaFramework/API/MaaContext.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "MaaFramework/Instance/MaaContext.h"

#include "API/MaaTypes.h"
#include "Common/MaaTypes.h"
#include "Utils/Buffer/ImageBuffer.hpp"
#include "Utils/Buffer/StringBuffer.hpp"
#include "Utils/Logger.h"
Expand Down
2 changes: 1 addition & 1 deletion source/MaaFramework/Controller/ControllerAgent.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include <meojson/json.hpp>

#include "API/MaaTypes.h"
#include "Common/MaaTypes.h"
#include "Base/AsyncRunner.hpp"
#include "Utils/MessageNotifier.hpp"
#include "Utils/NoWarningCVMat.hpp"
Expand Down
2 changes: 1 addition & 1 deletion source/MaaFramework/Resource/ResourceMgr.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <atomic>

#include "API/MaaTypes.h"
#include "Common/MaaTypes.h"
#include "Base/AsyncRunner.hpp"
#include "DefaultPipelineMgr.h"
#include "MaaFramework/Instance/MaaResource.h"
Expand Down
2 changes: 1 addition & 1 deletion source/MaaFramework/Task/Component/Actuator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <meojson/json.hpp>

#include "API/MaaTypes.h"
#include "Common/MaaTypes.h"
#include "Conf/Conf.h"
#include "Controller/ControllerAgent.h"
#include "Resource/PipelineTypes.h"
Expand Down
2 changes: 1 addition & 1 deletion source/MaaFramework/Task/Component/CustomAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include "Conf/Conf.h"

#include "API/MaaTypes.h"
#include "Common/MaaTypes.h"
#include "MaaFramework/MaaDef.h"
#include "Resource/ResourceMgr.h"
#include "Task/Context.h"
Expand Down
2 changes: 1 addition & 1 deletion source/MaaFramework/Task/Component/CustomRecognition.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

#include <vector>

#include "API/MaaTypes.h"
#include "Common/MaaTypes.h"
#include "MaaFramework/MaaDef.h"
#include "Resource/ResourceMgr.h"
#include "Task/Context.h"
Expand Down
Loading

0 comments on commit 8af6578

Please sign in to comment.