Skip to content

Commit

Permalink
feat: Server基本框架完成
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Feb 23, 2025
1 parent 2d73bc7 commit 675a989
Show file tree
Hide file tree
Showing 8 changed files with 208 additions and 28 deletions.
24 changes: 8 additions & 16 deletions source/MaaAgentClient/Client/AgentClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

#include <sstream>

#include <cpp-base64/base64.hpp>
#include <meojson/json.hpp>

#include "Common/MaaTypes.h"
Expand Down Expand Up @@ -244,13 +243,20 @@ MaaBool AgentClient::reco_agent(
return false;
}

if (!image) {
LogError << "image is null";
return false;
}

const cv::Mat& mat = image->get();

CustomRecognitionRequest req {
.context_id = pthis->context_id(context),
.task_id = task_id,
.node_name = node_name,
.custom_recognition_name = custom_recognition_name,
.custom_recognition_param = custom_recognition_param,
.image = encode_image(image),
.image = encode_image(mat),
.roi = roi ? std::array<int32_t, 4> { roi->x, roi->y, roi->width, roi->height } : std::array<int32_t, 4> {},
};

Expand Down Expand Up @@ -345,18 +351,4 @@ MaaContext* AgentClient::query_context(const std::string& context_id)
return it->second;
}

std::string AgentClient::encode_image(const MaaImageBuffer* image)
{
if (!image) {
LogError << "image is null";
return {};
}

cv::Mat mat = image->get();
std::vector<uint8_t> buf;
cv::imencode(".png", mat, buf);

return base64::base64_encode(buf.data(), buf.size());
}

MAA_AGENT_CLIENT_NS_END
1 change: 0 additions & 1 deletion source/MaaAgentClient/Client/AgentClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ class AgentClient : public MaaAgentClient

std::string context_id(MaaContext* context);
MaaContext* query_context(const std::string& context_id);
static std::string encode_image(const MaaImageBuffer* image);

private:
MaaResource* resource_ = nullptr;
Expand Down
2 changes: 1 addition & 1 deletion source/MaaAgentServer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ target_compile_definitions(MaaAgentServer PRIVATE MAA_AGENT_SERVER_EXPORTS)
# 复用 MaaFramework 导出接口
target_compile_definitions(MaaAgentServer PRIVATE MAA_FRAMEWORK_EXPORTS)

target_link_libraries(MaaAgentServer PRIVATE MaaUtils LibraryHolder HeaderOnlyLibraries)
target_link_libraries(MaaAgentServer PRIVATE cppzmq-static MaaUtils LibraryHolder HeaderOnlyLibraries)

add_dependencies(MaaAgentServer MaaUtils)

Expand Down
54 changes: 54 additions & 0 deletions source/MaaAgentServer/RemoteInstance/RemoteContext.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include "RemoteContext.h"

MAA_AGENT_SERVER_NS_BEGIN

RemoteContext::RemoteContext(const std::string& context_id)
: context_id_(context_id)
{
}

MaaTaskId RemoteContext::run_task(const std::string& entry, const json::object& pipeline_override)
{
return MaaTaskId();
}

MaaRecoId RemoteContext::run_recognition(const std::string& entry, const json::object& pipeline_override, const cv::Mat& image)
{
return MaaRecoId();
}

MaaNodeId RemoteContext::run_action(
const std::string& entry,
const json::object& pipeline_override,
const cv::Rect& box,
const std::string& reco_detail)
{
return MaaNodeId();
}

bool RemoteContext::override_pipeline(const json::object& pipeline_override)
{
return false;
}

bool RemoteContext::override_next(const std::string& node_name, const std::vector<std::string>& next)
{
return false;
}

MaaContext* RemoteContext::clone() const
{
return nullptr;
}

MaaTaskId RemoteContext::task_id() const
{
return MaaTaskId();
}

MaaTasker* RemoteContext::tasker() const
{
return nullptr;
}

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

#include "Common/MaaTypes.h"

MAA_AGENT_SERVER_NS_BEGIN

class RemoteContext : public MaaContext
{
public:
RemoteContext(const std::string& context_id);
virtual ~RemoteContext() = default;

virtual MaaTaskId run_task(const std::string& entry, const json::object& pipeline_override) override;
virtual MaaRecoId run_recognition(const std::string& entry, const json::object& pipeline_override, const cv::Mat& image) override;
virtual MaaNodeId
run_action(const std::string& entry, const json::object& pipeline_override, const cv::Rect& box, const std::string& reco_detail)
override;
virtual bool override_pipeline(const json::object& pipeline_override) override;
virtual bool override_next(const std::string& node_name, const std::vector<std::string>& next) override;

virtual MaaContext* clone() const override;

virtual MaaTaskId task_id() const override;
virtual MaaTasker* tasker() const override;

private:
std::string context_id_ = 0;
};

MAA_AGENT_SERVER_NS_END
104 changes: 94 additions & 10 deletions source/MaaAgentServer/Server/AgentServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <ranges>

#include "MaaAgent/Message.hpp"
#include "RemoteInstance/RemoteContext.h"
#include "Utils/Buffer/ImageBuffer.hpp"
#include "Utils/Buffer/StringBuffer.hpp"
#include "Utils/Codec.h"
#include "Utils/Logger.h"

MAA_AGENT_SERVER_NS_BEGIN
Expand Down Expand Up @@ -52,13 +56,6 @@ void AgentServer::shut_down()

msg_loop_running_ = false;

if (parent_sock_) {
parent_sock_.close();
}
if (parent_ctx_) {
parent_ctx_.close();
}

if (msg_thread_.joinable()) {
msg_thread_.join();
}
Expand Down Expand Up @@ -128,7 +125,7 @@ bool AgentServer::send(const json::value& j)
std::string jstr = j.dumps();
zmq::message_t msg(jstr.size());
std::memcpy(msg.data(), jstr.data(), jstr.size());
bool sent = parent_sock_.send(msg);
bool sent = parent_sock_.send(msg, zmq::send_flags::none).has_value();
if (!sent) {
LogError << "failed to send msg" << VAR(j) << VAR(ipc_addr_);
return false;
Expand Down Expand Up @@ -178,12 +175,99 @@ std::optional<json::value> AgentServer::recv()

bool AgentServer::recv_and_handle_recognition_request(const json::value& j)
{
return false;
if (!j.is<CustomRecognitionRequest>()) {
return false;
}

const CustomRecognitionRequest& req = j.as<CustomRecognitionRequest>();
LogInfo << VAR(req) << VAR(ipc_addr_);

auto it = custom_recognitions_.find(req.custom_recognition_name);
if (it == custom_recognitions_.end()) {
LogError << "custom_recognition not found" << VAR(req);
return true;
}

const CustomRecognitionSession& session = it->second;
if (!session.recognition) {
LogError << "recognition is null" << VAR(req);
return true;
}

RemoteContext context(req.context_id);
cv::Mat mat = decode_image(req.image);
ImageBuffer mat_buffer(mat);
MaaRect rect { req.roi[0], req.roi[1], req.roi[2], req.roi[3] };

MaaRect out_box {};
StringBuffer out_detail;

MaaBool ret = session.recognition(
&context,
req.task_id,
req.node_name.c_str(),
req.custom_recognition_name.c_str(),
req.custom_recognition_param.c_str(),
&mat_buffer,
&rect,
session.trans_arg,
&out_box,
&out_detail);

CustomRecognitionResponse resp {
.ret = static_cast<bool>(ret),
.out_box = { out_box.x, out_box.y, out_box.width, out_box.height },
.out_detail = out_detail.get(),
};
LogInfo << VAR(resp) << VAR(ipc_addr_);

send(resp);

return true;
}

bool AgentServer::recv_and_handle_action_request(const json::value& j)
{
return false;
if (!j.is<CustomActionRequest>()) {
return false;
}

const CustomActionRequest& req = j.as<CustomActionRequest>();
LogInfo << VAR(req) << VAR(ipc_addr_);

auto it = custom_actions_.find(req.custom_action_name);
if (it == custom_actions_.end()) {
LogError << "custom_action not found" << VAR(req);
return true;
}

const CustomActionSession& session = it->second;
if (!session.action) {
LogError << "action is null" << VAR(req);
return true;
}

RemoteContext context(req.context_id);
MaaRect rect { req.box[0], req.box[1], req.box[2], req.box[3] };

MaaBool ret = session.action(
&context,
req.task_id,
req.node_name.c_str(),
req.custom_action_name.c_str(),
req.custom_action_param.c_str(),
req.reco_id,
&rect,
session.trans_arg);

CustomActionResponse resp {
.ret = static_cast<bool>(ret),
};
LogInfo << VAR(resp) << VAR(ipc_addr_);

send(resp);

return true;
}

bool AgentServer::recv_and_handle_shut_down_request(const json::value& j)
Expand Down
17 changes: 17 additions & 0 deletions source/MaaUtils/Codec/Codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@
#include <regex>
#include <string>

#include <cpp-base64/base64.hpp>

#include "Utils/Logger.h"
#include "Utils/NoWarningCV.hpp"

MAA_NS_BEGIN

Expand Down Expand Up @@ -153,4 +156,18 @@ bool regex_valid(const std::wstring& regex)
return true;
}

cv::Mat decode_image(const std::string& data)
{
std::string base64 = base64::base64_decode(data);
std::vector<uchar> buffer(std::make_move_iterator(base64.begin()), std::make_move_iterator(base64.end()));
return cv::imdecode(buffer, cv::IMREAD_COLOR);
}

std::string encode_image(const cv::Mat& image)
{
std::vector<uchar> buffer;
cv::imencode(".png", image, buffer);
return base64::base64_encode(buffer.data(), buffer.size());
}

MAA_NS_END
4 changes: 4 additions & 0 deletions source/include/Utils/Codec.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

#include "Conf/Conf.h"
#include "MaaFramework/MaaPort.h"
#include "Utils/NoWarningCVMat.hpp"

MAA_NS_BEGIN

Expand All @@ -13,4 +14,7 @@ MAA_UTILS_API std::string from_u16(std::wstring_view u16str);

MAA_UTILS_API bool regex_valid(const std::wstring& regex);

MAA_UTILS_API std::string encode_image(const cv::Mat& image);
MAA_UTILS_API cv::Mat decode_image(const std::string& data);

MAA_NS_END

0 comments on commit 675a989

Please sign in to comment.