Skip to content

Commit 8b56922

Browse files
committed
chore: 临时保存摸了摸了
1 parent 8af6578 commit 8b56922

21 files changed

+439
-174
lines changed
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
#include "MaaAgentServerDef.h"
4+
5+
#ifdef __cplusplus
6+
extern "C"
7+
{
8+
#endif
9+
10+
MAA_AGENT_SERVER_API MaaBool MaaAgentServerStartUp(const MaaStringListBuffer* args);
11+
MAA_AGENT_SERVER_API void MaaAgentServerShutDown();
12+
13+
MAA_AGENT_SERVER_API MaaBool
14+
MaaAgentServerRegisterCustomRecognition(const char* name, MaaCustomRecognitionCallback recognition, void* trans_arg);
15+
16+
MAA_AGENT_SERVER_API MaaBool MaaAgentServerRegisterCustomAction(const char* name, MaaCustomActionCallback action, void* trans_arg);
17+
18+
#ifdef __cplusplus
19+
}
20+
#endif
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#pragma once
2+
3+
#include "MaaFramework/MaaDef.h" // IWYU pragma: export

include/MaaFramework/MaaPort.h

+6
Original file line numberDiff line numberDiff line change
@@ -59,3 +59,9 @@
5959
#else
6060
#define MAA_AGENT_CLIENT_API MAA_DLL_IMPORT
6161
#endif
62+
63+
#ifdef MAA_AGENT_SERVER_EXPORTS
64+
#define MAA_AGENT_SERVER_API MAA_DLL_EXPORT
65+
#else
66+
#define MAA_AGENT_SERVER_API MAA_DLL_IMPORT
67+
#endif

source/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -17,5 +17,6 @@ add_subdirectory(MaaFramework)
1717
add_subdirectory(MaaToolkit)
1818
add_subdirectory(MaaProjectInterface)
1919
add_subdirectory(MaaAgentClient)
20+
add_subdirectory(MaaAgentServer)
2021

2122
add_subdirectory(binding)
File renamed without changes.
File renamed without changes.

source/MaaFramework/API/MaaController.cpp source/Common/MaaController.cpp

+2-111
Original file line numberDiff line numberDiff line change
@@ -1,120 +1,11 @@
1-
#include "MaaFramework/Instance/MaaController.h"
1+
#include "MaaFramework/MaaAPI.h"
22

3-
#include "Controller/CustomControllerAgent.h"
4-
#include "Controller/GeneralControllerAgent.h"
5-
#include "LibraryHolder/ControlUnit.h"
3+
#include "Common/MaaTypes.h"
64
#include "Utils/Buffer/ImageBuffer.hpp"
75
#include "Utils/Buffer/StringBuffer.hpp"
86
#include "Utils/Logger.h"
97
#include "Utils/Platform.h"
108

11-
MaaController* MaaAdbControllerCreate(
12-
const char* adb_path,
13-
const char* address,
14-
MaaAdbScreencapMethod screencap_methods,
15-
MaaAdbInputMethod input_methods,
16-
const char* config,
17-
const char* agent_path,
18-
MaaNotificationCallback notify,
19-
void* notify_trans_arg)
20-
{
21-
LogFunc << VAR(adb_path) << VAR(address) << VAR(screencap_methods) << VAR(input_methods) << VAR(config) << VAR(agent_path)
22-
<< VAR_VOIDP(notify) << VAR_VOIDP(notify_trans_arg);
23-
24-
auto control_unit =
25-
MAA_NS::AdbControlUnitLibraryHolder::create_control_unit(adb_path, address, screencap_methods, input_methods, config, agent_path);
26-
27-
if (!control_unit) {
28-
LogError << "Failed to create control unit";
29-
return nullptr;
30-
}
31-
32-
return new MAA_CTRL_NS::GeneralControllerAgent(std::move(control_unit), notify, notify_trans_arg);
33-
}
34-
35-
MaaController* MaaWin32ControllerCreate(
36-
void* hWnd,
37-
MaaWin32ScreencapMethod screencap_method,
38-
MaaWin32InputMethod input_method,
39-
MaaNotificationCallback notify,
40-
void* notify_trans_arg)
41-
{
42-
LogFunc << VAR_VOIDP(hWnd) << VAR(screencap_method) << VAR(input_method) << VAR_VOIDP(notify) << VAR_VOIDP(notify_trans_arg);
43-
44-
#ifndef _WIN32
45-
46-
LogError << "This API" << __FUNCTION__ << "is only available on Windows";
47-
return nullptr;
48-
49-
#else
50-
51-
if (!hWnd) {
52-
LogWarn << "hWnd is nullptr";
53-
}
54-
55-
auto control_unit = MAA_NS::Win32ControlUnitLibraryHolder::create_control_unit(hWnd, screencap_method, input_method);
56-
57-
if (!control_unit) {
58-
LogError << "Failed to create control unit";
59-
return nullptr;
60-
}
61-
62-
return new MAA_CTRL_NS::GeneralControllerAgent(std::move(control_unit), notify, notify_trans_arg);
63-
64-
#endif
65-
}
66-
67-
MaaController* MaaCustomControllerCreate(
68-
MaaCustomControllerCallbacks* controller,
69-
void* controller_arg,
70-
MaaNotificationCallback notify,
71-
void* notify_trans_arg)
72-
{
73-
LogFunc << VAR(controller) << VAR(controller_arg) << VAR_VOIDP(notify) << VAR_VOIDP(notify_trans_arg);
74-
75-
if (!controller) {
76-
LogError << "controller is null";
77-
return nullptr;
78-
}
79-
80-
return new MAA_CTRL_NS::CustomControllerAgent(controller, controller_arg, notify, notify_trans_arg);
81-
}
82-
83-
MaaController* MaaDbgControllerCreate(
84-
const char* read_path,
85-
const char* write_path,
86-
MaaDbgControllerType type,
87-
const char* config,
88-
MaaNotificationCallback notify,
89-
void* notify_trans_arg)
90-
{
91-
LogFunc << VAR(read_path) << VAR(write_path) << VAR(type) << VAR_VOIDP(notify) << VAR_VOIDP(notify_trans_arg);
92-
93-
std::ignore = write_path;
94-
std::ignore = config;
95-
96-
auto control_unit = MAA_NS::DbgControlUnitLibraryHolder::create_control_unit(type, read_path);
97-
98-
if (!control_unit) {
99-
LogError << "Failed to create control unit";
100-
return nullptr;
101-
}
102-
103-
return new MAA_CTRL_NS::GeneralControllerAgent(std::move(control_unit), notify, notify_trans_arg);
104-
}
105-
106-
void MaaControllerDestroy(MaaController* ctrl)
107-
{
108-
LogFunc << VAR_VOIDP(ctrl);
109-
110-
if (ctrl == nullptr) {
111-
LogError << "handle is null";
112-
return;
113-
}
114-
115-
delete ctrl;
116-
}
117-
1189
MaaBool MaaControllerSetOption(MaaController* ctrl, MaaCtrlOption key, MaaOptionValue value, MaaOptionValueSize val_size)
11910
{
12011
LogFunc << VAR_VOIDP(ctrl) << VAR(key) << VAR_VOIDP(value) << VAR(val_size);

source/MaaFramework/API/MaaResource.cpp source/Common/MaaResource.cpp

+1-22
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,10 @@
11
#include "MaaFramework/MaaAPI.h"
22

3-
#include <meojson/json.hpp>
4-
5-
#include "Resource/ResourceMgr.h"
3+
#include "Common/MaaTypes.h"
64
#include "Utils/Buffer/StringBuffer.hpp"
75
#include "Utils/Logger.h"
86
#include "Utils/Platform.h"
97

10-
MaaResource* MaaResourceCreate(MaaNotificationCallback notify, void* notify_trans_arg)
11-
{
12-
LogFunc << VAR_VOIDP(notify) << VAR_VOIDP(notify_trans_arg);
13-
14-
return new MAA_RES_NS::ResourceMgr(notify, notify_trans_arg);
15-
}
16-
17-
void MaaResourceDestroy(MaaResource* res)
18-
{
19-
LogFunc << VAR_VOIDP(res);
20-
21-
if (res == nullptr) {
22-
LogError << "handle is null";
23-
return;
24-
}
25-
26-
delete res;
27-
}
28-
298
MaaBool MaaResourceRegisterCustomRecognition(MaaResource* res, const char* name, MaaCustomRecognitionCallback recognition, void* trans_arg)
309
{
3110
LogFunc << VAR_VOIDP(res) << VAR(name) << VAR_VOIDP(recognition) << VAR_VOIDP(trans_arg);

source/MaaFramework/API/MaaTasker.cpp source/Common/MaaTasker.cpp

+1-20
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,10 @@
11
#include "MaaFramework/Instance/MaaTasker.h"
22

3-
#include "Tasker/Tasker.h"
3+
#include "Common/MaaTypes.h"
44
#include "Utils/Buffer/ImageBuffer.hpp"
55
#include "Utils/Buffer/StringBuffer.hpp"
66
#include "Utils/Logger.h"
77

8-
MaaTasker* MaaTaskerCreate(MaaNotificationCallback notify, void* notify_trans_arg)
9-
{
10-
LogFunc << VAR_VOIDP(notify) << VAR_VOIDP(notify_trans_arg);
11-
12-
return new MAA_NS::Tasker(notify, notify_trans_arg);
13-
}
14-
15-
void MaaTaskerDestroy(MaaTasker* tasker)
16-
{
17-
LogFunc << VAR_VOIDP(tasker);
18-
19-
if (tasker == nullptr) {
20-
LogError << "handle is null";
21-
return;
22-
}
23-
24-
delete tasker;
25-
}
26-
278
MaaBool MaaTaskerSetOption(MaaTasker* tasker, MaaTaskerOption key, MaaOptionValue value, MaaOptionValueSize val_size)
289
{
2910
LogFunc << VAR_VOIDP(tasker) << VAR(key) << VAR_VOIDP(value) << VAR(val_size);

source/Common/MaaUtility.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#include "MaaFramework/MaaAPI.h"
2+
3+
#include "Utils/Logger.h"
4+
5+
const char* MaaVersion()
6+
{
7+
#pragma message("MaaFramework MAA_VERSION: " MAA_VERSION)
8+
9+
return MAA_VERSION;
10+
}

source/MaaAgentClient/Client/AgentClient.cpp

+51-2
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,11 @@ AgentClient::~AgentClient()
4141
if (child_ && child_.joinable()) {
4242
child_.join();
4343
}
44+
45+
msg_loop_running_ = false;
46+
if (msg_thread_.joinable()) {
47+
msg_thread_.join();
48+
}
4449
}
4550

4651
bool AgentClient::bind_resource(MaaResource* resource)
@@ -83,7 +88,20 @@ bool AgentClient::start_clild(const std::filesystem::path& child_exec, const std
8388
return false;
8489
}
8590

86-
return recv_and_handle_init_msg();
91+
bool inited = recv_and_handle_init_msg();
92+
if (!inited) {
93+
LogError << "failed to recv_and_handle_init_msg";
94+
return false;
95+
}
96+
97+
msg_loop_running_ = true;
98+
msg_thread_ = std::thread(std::bind(&AgentClient::msg_loop, this));
99+
if (!msg_thread_.joinable()) {
100+
LogError << "failed to start child_recv_thread";
101+
return false;
102+
}
103+
104+
return true;
87105
}
88106

89107
std::string AgentClient::create_socket()
@@ -111,7 +129,7 @@ std::optional<json::value> AgentClient::recv()
111129
return std::nullopt;
112130
}
113131

114-
const std::string& init_str = init_msg.to_string();
132+
std::string_view init_str = init_msg.to_string_view();
115133
LogInfo << VAR(init_str);
116134

117135
auto jopt = json::parse(init_str);
@@ -153,6 +171,37 @@ bool AgentClient::recv_and_handle_init_msg()
153171
return true;
154172
}
155173

174+
void AgentClient::msg_loop()
175+
{
176+
LogFunc;
177+
178+
while (msg_loop_running_) {
179+
auto msg_opt = recv();
180+
if (!msg_opt) {
181+
LogError << "failed to recv msg";
182+
continue;
183+
}
184+
const json::value& j = *msg_opt;
185+
LogInfo << VAR(j);
186+
187+
if (handle_context_run_task(j)) {
188+
}
189+
else {
190+
LogError << "unknown msg" << VAR(j);
191+
}
192+
}
193+
}
194+
195+
bool AgentClient::handle_context_run_task(const json::value& j)
196+
{
197+
if (!j.is<ContextRunTaskMsg>()) {
198+
return false;
199+
}
200+
LogInfo << VAR(j);
201+
202+
return true;
203+
}
204+
156205
MaaBool AgentClient::reco_agent(
157206
MaaContext* context,
158207
MaaTaskId task_id,

source/MaaAgentClient/Client/AgentClient.h

+6
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ class AgentClient : public MaaAgentClient
3939

4040
bool recv_and_handle_init_msg();
4141

42+
void msg_loop();
43+
bool handle_context_run_task(const json::value& j);
44+
4245
public:
4346
static MaaBool reco_agent(
4447
MaaContext* context,
@@ -69,6 +72,9 @@ class AgentClient : public MaaAgentClient
6972

7073
zmq::context_t child_ctx_;
7174
zmq::socket_t child_sock_;
75+
76+
bool msg_loop_running_ = false;
77+
std::thread msg_thread_;
7278
};
7379

7480
MAA_AGENT_CLIENT_NS_END
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include "MaaAgentServer/MaaAgentServerAPI.h"
2+
3+
#include "Server/AgentServer.h"
4+
#include "Utils/Buffer/StringBuffer.hpp"
5+
#include "Utils/Logger.h"
6+
7+
MaaBool MaaAgentServerStartUp(const MaaStringListBuffer* args)
8+
{
9+
LogFunc << VAR(args);
10+
11+
if (!args) {
12+
LogError << "args is null";
13+
return false;
14+
}
15+
16+
std::vector<std::string> args_vec;
17+
18+
size_t size = args->size();
19+
for (size_t i = 0; i < size; ++i) {
20+
args_vec.emplace_back(args->at(i).get());
21+
}
22+
23+
return MAA_AGENT_SERVER_NS::AgentServer::get_instance().start_up(args_vec);
24+
}
25+
26+
void MaaAgentServerShutDown()
27+
{
28+
LogFunc;
29+
30+
MAA_AGENT_SERVER_NS::AgentServer::get_instance().shut_down();
31+
}
32+
33+
MaaBool MaaAgentServerRegisterCustomRecognition(const char* name, MaaCustomRecognitionCallback recognition, void* trans_arg)
34+
{
35+
LogFunc << VAR(name) << VAR_VOIDP(recognition) << VAR_VOIDP(trans_arg);
36+
37+
if (!name || !recognition) {
38+
LogError << "name or recognition is null";
39+
return false;
40+
}
41+
42+
return MAA_AGENT_SERVER_NS::AgentServer::get_instance().register_custom_recognition(name, recognition, trans_arg);
43+
}
44+
45+
MaaBool MaaAgentServerRegisterCustomAction(const char* name, MaaCustomActionCallback action, void* trans_arg)
46+
{
47+
LogFunc << VAR(name) << VAR_VOIDP(action) << VAR_VOIDP(trans_arg);
48+
49+
if (!name || !action) {
50+
LogError << "name or action is null";
51+
return false;
52+
}
53+
54+
return MAA_AGENT_SERVER_NS::AgentServer::get_instance().register_custom_action(name, action, trans_arg);
55+
}

0 commit comments

Comments
 (0)