mirror of
https://github.com/MaaAssistantArknights/MaaAssistantArknights.git
synced 2026-07-20 02:55:38 +08:00
feat.初步完成socket通讯
This commit is contained in:
@@ -22,106 +22,130 @@
|
||||
"General": {
|
||||
"devices": "[Adb] devices",
|
||||
"addressRegex": "(.+)\tdevice",
|
||||
"connect": "[Adb] connect [Address]",
|
||||
"uuid": "[Adb] -s [Address] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [Address] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init=",
|
||||
"connect": "[Adb] connect [AdbSerial]",
|
||||
"uuid": "[Adb] -s [AdbSerial] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [AdbSerial] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] -s [AdbSerial] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [AdbSerial] shell dumpsys window displays | grep init=",
|
||||
"displayFormat": " init=%dx%d",
|
||||
"screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [Address] exec-out screencap -p",
|
||||
"screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"",
|
||||
"ncPort": 6054,
|
||||
"ncPortReverse": "[Adb] -s [AdbSerial] reverse tcp:[NcPort] tcp:[NcPort]",
|
||||
"screencapRawWithGzip": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [AdbSerial] exec-out screencap -p",
|
||||
"release": "[Adb] kill-server"
|
||||
},
|
||||
"BlueStacks": {
|
||||
"devices": "[Adb] devices",
|
||||
"addressRegex": "(.+)\tdevice",
|
||||
"connect": "[Adb] connect [Address]",
|
||||
"uuid": "[Adb] -s [Address] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [Address] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
|
||||
"connect": "[Adb] connect [AdbSerial]",
|
||||
"uuid": "[Adb] -s [AdbSerial] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [AdbSerial] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] -s [AdbSerial] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [AdbSerial] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
|
||||
"displayFormat": "cur=%dx%d",
|
||||
"screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [Address] exec-out screencap -p",
|
||||
"screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"",
|
||||
"ncPort": 6054,
|
||||
"ncPortReverse": "[Adb] -s [AdbSerial] reverse tcp:[NcPort] tcp:[NcPort]",
|
||||
"screencapRawWithGzip": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [AdbSerial] exec-out screencap -p",
|
||||
"release": "[Adb] kill-server"
|
||||
},
|
||||
"MuMuEmulator": {
|
||||
"devices": "[Adb] devices",
|
||||
"addressRegex": "(.+)\tdevice",
|
||||
"connect": "[Adb] connect [Address]",
|
||||
"uuid": "[Adb] -s [Address] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [Address] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
|
||||
"connect": "[Adb] connect [AdbSerial]",
|
||||
"uuid": "[Adb] -s [AdbSerial] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [AdbSerial] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] -s [AdbSerial] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [AdbSerial] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
|
||||
"displayFormat": "cur=%dx%d",
|
||||
"screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [Address] exec-out screencap -p",
|
||||
"screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"",
|
||||
"ncPort": 6054,
|
||||
"ncPortReverse": "[Adb] -s [AdbSerial] reverse tcp:[NcPort] tcp:[NcPort]",
|
||||
"screencapRawWithGzip": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [AdbSerial] exec-out screencap -p",
|
||||
"release": "[Adb] kill-server"
|
||||
},
|
||||
"LDPlayer": {
|
||||
"devices": "[Adb] devices",
|
||||
"addressRegex": "(.+)\tdevice",
|
||||
"connect": "[Adb] connect [Address]",
|
||||
"uuid": "[Adb] -s [Address] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [Address] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
|
||||
"connect": "[Adb] connect [AdbSerial]",
|
||||
"uuid": "[Adb] -s [AdbSerial] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [AdbSerial] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] -s [AdbSerial] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [AdbSerial] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
|
||||
"displayFormat": "cur=%dx%d",
|
||||
"screencapRawWithGzip": "[Adb] -s [Address] shell \"screencap | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [Address] shell screencap -p",
|
||||
"screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"",
|
||||
"ncPort": 6054,
|
||||
"ncPortReverse": "[Adb] -s [AdbSerial] reverse tcp:[NcPort] tcp:[NcPort]",
|
||||
"screencapRawWithGzip": "[Adb] -s [AdbSerial] shell \"screencap | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [AdbSerial] shell screencap -p",
|
||||
"release": "[Adb] kill-server"
|
||||
},
|
||||
"Nox": {
|
||||
"devices": "[Adb] devices",
|
||||
"addressRegex": "(.+)\tdevice",
|
||||
"connect": "[Adb] connect [Address]",
|
||||
"uuid": "[Adb] -s [Address] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [Address] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
|
||||
"connect": "[Adb] connect [AdbSerial]",
|
||||
"uuid": "[Adb] -s [AdbSerial] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [AdbSerial] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] -s [AdbSerial] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [AdbSerial] shell dumpsys window displays | grep init= | awk ' { print $3 } '",
|
||||
"displayFormat": "cur=%dx%d",
|
||||
"screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [Address] exec-out screencap -p",
|
||||
"screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"",
|
||||
"ncPort": 6054,
|
||||
"ncPortReverse": "[Adb] -s [AdbSerial] reverse tcp:[NcPort] tcp:[NcPort]",
|
||||
"screencapRawWithGzip": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [AdbSerial] exec-out screencap -p",
|
||||
"release": "[Adb] kill-server"
|
||||
},
|
||||
"XYAZ": {
|
||||
"devices": "[Adb] devices",
|
||||
"addressRegex": "(.+)\tdevice",
|
||||
"connect": "[Adb] connect [Address]",
|
||||
"uuid": "[Adb] -s [Address] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [Address] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init=",
|
||||
"connect": "[Adb] connect [AdbSerial]",
|
||||
"uuid": "[Adb] -s [AdbSerial] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [AdbSerial] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] -s [AdbSerial] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [AdbSerial] shell dumpsys window displays | grep init=",
|
||||
"displayFormat": " init=%dx%d",
|
||||
"screencapRawWithGzip": "[Adb] -s [Address] shell \"screencap | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [Address] shell screencap -p",
|
||||
"screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"",
|
||||
"ncPort": 6054,
|
||||
"ncPortReverse": "[Adb] -s [AdbSerial] reverse tcp:[NcPort] tcp:[NcPort]",
|
||||
"screencapRawWithGzip": "[Adb] -s [AdbSerial] shell \"screencap | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [AdbSerial] shell screencap -p",
|
||||
"release": "[Adb] kill-server"
|
||||
},
|
||||
"WSA": {
|
||||
"devices": "[Adb] devices",
|
||||
"addressRegex": "(.+)\tdevice",
|
||||
"connect": "[Adb] connect [Address]",
|
||||
"displayId": "[Adb] -s [Address] shell dumpsys display | grep mUniqueId=virtual:com.microsoft.windows.systemapp:com.hypergryph.arknights",
|
||||
"uuid": "[Adb] -s [Address] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [Address] shell input -d [DisplayId] tap [x] [y]",
|
||||
"swipe": "[Adb] -s [Address] shell input -d [DisplayId] swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [Address] shell \"wm size -d [DisplayId]\"",
|
||||
"connect": "[Adb] connect [AdbSerial]",
|
||||
"displayId": "[Adb] -s [AdbSerial] shell dumpsys display | grep mUniqueId=virtual:com.microsoft.windows.systemapp:com.hypergryph.arknights",
|
||||
"uuid": "[Adb] -s [AdbSerial] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [AdbSerial] shell input -d [DisplayId] tap [x] [y]",
|
||||
"swipe": "[Adb] -s [AdbSerial] shell input -d [DisplayId] swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [AdbSerial] shell \"wm size -d [DisplayId]\"",
|
||||
"displayFormat": "Physical size:%dx%d",
|
||||
"screencapRawWithGzip": "[Adb] -s [Address] shell \"screencap -d [DisplayId] | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [Address] shell screencap -d [DisplayId] -p",
|
||||
"screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"",
|
||||
"ncPort": 6054,
|
||||
"ncPortReverse": "[Adb] -s [AdbSerial] reverse tcp:[NcPort] tcp:[NcPort]",
|
||||
"screencapRawWithGzip": "[Adb] -s [AdbSerial] shell \"screencap -d [DisplayId] | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [AdbSerial] shell screencap -d [DisplayId] -p",
|
||||
"release": "[Adb] kill-server"
|
||||
},
|
||||
"Compatible": {
|
||||
"devices": "[Adb] devices",
|
||||
"addressRegex": "(.+)\tdevice",
|
||||
"connect": "[Adb] connect [Address]",
|
||||
"uuid": "[Adb] -s [Address] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [Address] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] -s [Address] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [Address] shell dumpsys window displays | grep init=",
|
||||
"connect": "[Adb] connect [AdbSerial]",
|
||||
"uuid": "[Adb] -s [AdbSerial] shell settings get secure android_id",
|
||||
"click": "[Adb] -s [AdbSerial] shell input tap [x] [y]",
|
||||
"swipe": "[Adb] -s [AdbSerial] shell input swipe [x1] [y1] [x2] [y2] [duration]",
|
||||
"display": "[Adb] -s [AdbSerial] shell dumpsys window displays | grep init=",
|
||||
"displayFormat": " init=%dx%d",
|
||||
"screencapRawWithGzip": "[Adb] -s [Address] exec-out \"screencap | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [Address] exec-out screencap -p",
|
||||
"screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"",
|
||||
"ncPort": 6054,
|
||||
"ncPortReverse": "[Adb] -s [AdbSerial] reverse tcp:[NcPort] tcp:[NcPort]",
|
||||
"screencapRawWithGzip": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1\"",
|
||||
"screencapEncode": "[Adb] -s [AdbSerial] exec-out screencap -p",
|
||||
"release": "[Adb] kill-server"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -14,6 +14,7 @@
|
||||
#include <regex>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
#include <memory>
|
||||
|
||||
#include <opencv2/opencv.hpp>
|
||||
|
||||
@@ -26,6 +27,33 @@
|
||||
#include "Logger.hpp"
|
||||
#include "Resource.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
// for Windows socket
|
||||
class WsaHelper
|
||||
{
|
||||
public:
|
||||
~WsaHelper()
|
||||
{
|
||||
WSACleanup();
|
||||
}
|
||||
static WsaHelper& get_instance()
|
||||
{
|
||||
static WsaHelper instance;
|
||||
return instance;
|
||||
}
|
||||
bool operator()() const noexcept { return m_supports; }
|
||||
private:
|
||||
WsaHelper()
|
||||
{
|
||||
m_supports =
|
||||
WSAStartup(MAKEWORD(2, 2), &m_wsa_data) == 0 &&
|
||||
m_wsa_data.wVersion == MAKEWORD(2, 2);
|
||||
}
|
||||
WSADATA m_wsa_data = { 0 };
|
||||
bool m_supports = false;
|
||||
};
|
||||
#endif
|
||||
|
||||
asst::Controller::Controller(AsstCallback callback, void* callback_arg)
|
||||
: m_callback(callback),
|
||||
m_callback_arg(callback_arg),
|
||||
@@ -55,6 +83,20 @@ asst::Controller::Controller(AsstCallback callback, void* callback_arg)
|
||||
m_child_startup_info.hStdInput = m_pipe_child_read;
|
||||
m_child_startup_info.hStdOutput = m_pipe_child_write;
|
||||
m_child_startup_info.hStdError = m_pipe_child_write;
|
||||
|
||||
m_support_netcat = false;
|
||||
do {
|
||||
if (!WsaHelper::get_instance()()) {
|
||||
Log.error("WSA not supports");
|
||||
break;
|
||||
}
|
||||
m_server_sock = ::socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
||||
if (m_server_sock == INVALID_SOCKET) {
|
||||
break;
|
||||
}
|
||||
m_support_netcat = true;
|
||||
} while (0);
|
||||
|
||||
#else
|
||||
int pipe_in_ret = pipe(m_pipe_in);
|
||||
int pipe_out_ret = pipe(m_pipe_out);
|
||||
@@ -64,11 +106,19 @@ asst::Controller::Controller(AsstCallback callback, void* callback_arg)
|
||||
throw "controller pipe created failed";
|
||||
}
|
||||
|
||||
// todo
|
||||
m_support_netcat = false;
|
||||
#endif
|
||||
m_pipe_buffer = std::make_unique<uchar[]>(PipeBuffSize);
|
||||
if (!m_pipe_buffer) {
|
||||
throw "controller pipe buffer allocated failed";
|
||||
}
|
||||
if (m_support_netcat) {
|
||||
m_socket_buffer = std::make_unique<char[]>(SocketBuffSize);
|
||||
if (!m_socket_buffer) {
|
||||
throw "controller socket buffer allocated failed";
|
||||
}
|
||||
}
|
||||
|
||||
m_cmd_thread = std::thread(&Controller::pipe_working_proc, this);
|
||||
}
|
||||
@@ -86,20 +136,27 @@ asst::Controller::~Controller()
|
||||
m_cmd_thread.join();
|
||||
}
|
||||
|
||||
disconnect();
|
||||
if (--m_instance_count) {
|
||||
release();
|
||||
}
|
||||
|
||||
#ifdef _WIN32
|
||||
::CloseHandle(m_pipe_read);
|
||||
::CloseHandle(m_pipe_write);
|
||||
::CloseHandle(m_pipe_child_read);
|
||||
::CloseHandle(m_pipe_child_write);
|
||||
|
||||
if (m_server_sock) {
|
||||
::closesocket(m_server_sock);
|
||||
m_server_sock = 0U;
|
||||
}
|
||||
#else
|
||||
close(m_pipe_in[PIPE_READ]);
|
||||
close(m_pipe_in[PIPE_WRITE]);
|
||||
close(m_pipe_out[PIPE_READ]);
|
||||
close(m_pipe_out[PIPE_WRITE]);
|
||||
#endif
|
||||
}
|
||||
}
|
||||
|
||||
//asst::Rect asst::Controller::shaped_correct(const Rect & rect) const
|
||||
//{
|
||||
@@ -199,8 +256,7 @@ std::optional<std::vector<unsigned char>> asst::Controller::call_command(const s
|
||||
|
||||
std::vector<uchar> pipe_data;
|
||||
|
||||
static std::mutex pipe_mutex;
|
||||
std::unique_lock<std::mutex> pipe_lock(pipe_mutex);
|
||||
std::unique_lock<std::mutex> pipe_lock(m_pipe_mutex);
|
||||
|
||||
auto start_time = std::chrono::steady_clock::now();
|
||||
auto check_timeout = [&]() -> bool {
|
||||
@@ -269,7 +325,7 @@ std::optional<std::vector<unsigned char>> asst::Controller::call_command(const s
|
||||
else {
|
||||
// failed to create child process
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
Log.trace("Call `", cmd, "` ret", exit_ret, ", output size:", pipe_data.size());
|
||||
@@ -285,6 +341,57 @@ std::optional<std::vector<unsigned char>> asst::Controller::call_command(const s
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::vector<unsigned char>> asst::Controller::recv_data_by_socket(int64_t timeout)
|
||||
{
|
||||
LogTraceFunction;
|
||||
|
||||
if (!m_support_netcat) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
std::vector<uchar> socket_data;
|
||||
|
||||
std::unique_lock<std::mutex> nc_lock(m_socket_mutex);
|
||||
|
||||
auto start_time = std::chrono::steady_clock::now();
|
||||
auto check_timeout = [&]() -> bool {
|
||||
return timeout &&
|
||||
timeout < std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
std::chrono::steady_clock::now() - start_time)
|
||||
.count();
|
||||
};
|
||||
|
||||
#ifdef _WIN32
|
||||
SOCKADDR client_addr = { 0 };
|
||||
int size = sizeof(SOCKADDR);
|
||||
|
||||
std::unique_ptr<SOCKET, std::function<void(SOCKET*)>> client_sock_ptr(
|
||||
new SOCKET(::accept(m_server_sock, (SOCKADDR*)&client_addr, &size)),
|
||||
[](SOCKET* socket) { if (*socket) { ::closesocket(*socket); *socket = 0U; }});
|
||||
|
||||
while (!check_timeout()) {
|
||||
int recv_size = ::recv(*client_sock_ptr, m_socket_buffer.get(), SocketBuffSize, NULL);
|
||||
if (recv_size < 0) {
|
||||
Log.error("recv error", recv_size);
|
||||
return std::nullopt;
|
||||
}
|
||||
if (recv_size == 0) {
|
||||
break;
|
||||
}
|
||||
socket_data.insert(socket_data.end(), m_socket_buffer.get(), m_socket_buffer.get() + recv_size);
|
||||
}
|
||||
#else
|
||||
// todo
|
||||
#endif // _WIN32
|
||||
|
||||
Log.trace("socket data size:", socket_data.size());
|
||||
|
||||
if (socket_data.empty()) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return socket_data;
|
||||
}
|
||||
|
||||
void asst::Controller::convert_lf(std::vector<unsigned char>& data)
|
||||
{
|
||||
LogTraceFunction;
|
||||
@@ -369,6 +476,7 @@ void asst::Controller::clear_info() noexcept
|
||||
m_height = 0;
|
||||
m_control_scale = 1.0;
|
||||
m_scale_size = { WindowWidthDefault, WindowHeightDefault };
|
||||
--m_instance_count;
|
||||
}
|
||||
|
||||
int asst::Controller::push_cmd(const std::string& cmd)
|
||||
@@ -442,18 +550,31 @@ bool asst::Controller::screencap()
|
||||
switch (m_adb.screencap_method) {
|
||||
case AdbProperty::ScreencapMethod::UnknownYet:
|
||||
{
|
||||
if (screencap(m_adb.screencap_raw_with_gzip, decode_raw_with_gzip)) {
|
||||
if (m_support_netcat &&
|
||||
screencap(m_adb.screencap_raw_with_gzip_and_nc, decode_raw_with_gzip, true)) {
|
||||
m_adb.screencap_method = AdbProperty::ScreencapMethod::RawWithGzipAndNc;
|
||||
Log.info("screencap by RawWithGzipAndNc");
|
||||
return true;
|
||||
}
|
||||
else if (screencap(m_adb.screencap_raw_with_gzip, decode_raw_with_gzip)) {
|
||||
m_adb.screencap_method = AdbProperty::ScreencapMethod::RawWithGzip;
|
||||
Log.info("screencap by RawWithGzip");
|
||||
return true;
|
||||
}
|
||||
else if (screencap(m_adb.screencap_encode, decode_encode)) {
|
||||
m_adb.screencap_method = AdbProperty::ScreencapMethod::Encode;
|
||||
Log.info("screencap by Encode");
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
case AdbProperty::ScreencapMethod::RawWithGzipAndNc:
|
||||
{
|
||||
return screencap(m_adb.screencap_raw_with_gzip_and_nc, decode_raw_with_gzip, true);
|
||||
}
|
||||
break;
|
||||
case AdbProperty::ScreencapMethod::RawWithGzip:
|
||||
{
|
||||
return screencap(m_adb.screencap_raw_with_gzip, decode_raw_with_gzip);
|
||||
@@ -469,10 +590,18 @@ bool asst::Controller::screencap()
|
||||
return false;
|
||||
}
|
||||
|
||||
bool asst::Controller::screencap(const std::string& cmd, DecodeFunc decode_func)
|
||||
bool asst::Controller::screencap(const std::string& cmd, DecodeFunc decode_func, bool by_socket)
|
||||
{
|
||||
if (!m_support_netcat && by_socket) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto ret = call_command(cmd);
|
||||
|
||||
if (by_socket && ret) {
|
||||
ret = recv_data_by_socket();
|
||||
}
|
||||
|
||||
if (!ret || ret.value().empty()) {
|
||||
Log.error("data is empty!");
|
||||
return false;
|
||||
@@ -485,6 +614,7 @@ bool asst::Controller::screencap(const std::string& cmd, DecodeFunc decode_func)
|
||||
|
||||
if (decode_func(data)) {
|
||||
if (m_adb.screencap_end_of_line == AdbProperty::ScreencapEndOfLine::UnknownYet) {
|
||||
Log.info("screencap_end_of_line is LF");
|
||||
m_adb.screencap_end_of_line = AdbProperty::ScreencapEndOfLine::LF;
|
||||
}
|
||||
return true;
|
||||
@@ -498,6 +628,7 @@ bool asst::Controller::screencap(const std::string& cmd, DecodeFunc decode_func)
|
||||
|
||||
if (decode_func(data)) {
|
||||
m_adb.screencap_end_of_line = AdbProperty::ScreencapEndOfLine::CRLF;
|
||||
Log.info("screencap_end_of_line is CRLF");
|
||||
return true;
|
||||
}
|
||||
else {
|
||||
@@ -665,12 +796,16 @@ bool asst::Controller::connect(const std::string& adb_path, const std::string& a
|
||||
|
||||
const auto adb_cfg = std::move(adb_ret.value());
|
||||
std::string display_id;
|
||||
std::string nc_address;
|
||||
u_short nc_port = 0;
|
||||
|
||||
auto cmd_replace = [&](const std::string& cfg_cmd) -> std::string {
|
||||
const std::unordered_map<std::string, std::string> replacements = {
|
||||
{ "[Adb]", adb_path },
|
||||
{ "[Address]", address },
|
||||
{ "[DisplayId]", display_id }
|
||||
{ "[AdbSerial]", address },
|
||||
{ "[DisplayId]", display_id },
|
||||
{ "[NcPort]", std::to_string(nc_port) },
|
||||
{ "[NcAddress]", nc_address },
|
||||
};
|
||||
std::string formatted = cfg_cmd;
|
||||
for (const auto& [key, value] : replacements) {
|
||||
@@ -823,16 +958,60 @@ bool asst::Controller::connect(const std::string& adb_path, const std::string& a
|
||||
m_callback(AsstMsg::ConnectionInfo, info, m_callback_arg);
|
||||
}
|
||||
|
||||
if (size_t pos = address.rfind(':');
|
||||
pos != std::string::npos) {
|
||||
nc_address = address.substr(0, pos);
|
||||
}
|
||||
else {
|
||||
nc_address = "127.0.0.1";
|
||||
}
|
||||
|
||||
m_adb.click = cmd_replace(adb_cfg.click);
|
||||
m_adb.swipe = cmd_replace(adb_cfg.swipe);
|
||||
m_adb.screencap_raw_with_gzip = cmd_replace(adb_cfg.screencap_raw_with_gzip);
|
||||
m_adb.screencap_encode = cmd_replace(adb_cfg.screencap_encode);
|
||||
m_adb.release = cmd_replace(adb_cfg.release);
|
||||
|
||||
if (m_support_netcat) {
|
||||
#ifdef _WIN32
|
||||
m_server_addr.sin_family = PF_INET;
|
||||
m_server_addr.sin_addr.s_addr = inet_addr(nc_address.c_str());
|
||||
#else
|
||||
// todo
|
||||
#endif
|
||||
bool server_start = false;
|
||||
u_short max_port = adb_cfg.nc_port + 100;
|
||||
for (nc_port = adb_cfg.nc_port; nc_port < max_port; ++nc_port) {
|
||||
Log.trace("try nc port", nc_port);
|
||||
#ifdef _WIN32
|
||||
|
||||
m_server_addr.sin_port = htons(nc_port);
|
||||
int bind_ret = ::bind(m_server_sock, (SOCKADDR*)&m_server_addr, sizeof(SOCKADDR));
|
||||
int listen_ret = ::listen(m_server_sock, 20);
|
||||
server_start = bind_ret == 0 && listen_ret == 0;
|
||||
#else
|
||||
// todo
|
||||
#endif
|
||||
if (server_start) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (server_start && call_command(cmd_replace(adb_cfg.nc_port_reverse))) {
|
||||
Log.info("supports netcat");
|
||||
m_support_netcat = true;
|
||||
}
|
||||
else {
|
||||
Log.info("not supports netcat");
|
||||
m_support_netcat = false;
|
||||
}
|
||||
m_adb.screencap_raw_with_gzip_and_nc = cmd_replace(adb_cfg.screencap_raw_with_gzip_and_nc);
|
||||
}
|
||||
|
||||
++m_instance_count;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool asst::Controller::disconnect()
|
||||
bool asst::Controller::release()
|
||||
{
|
||||
#ifndef _WIN32
|
||||
if (m_child)
|
||||
|
||||
@@ -31,7 +31,7 @@ namespace asst
|
||||
~Controller();
|
||||
|
||||
bool connect(const std::string& adb_path, const std::string& address, const std::string& config);
|
||||
bool disconnect();
|
||||
bool release();
|
||||
|
||||
const std::string& get_uuid() const;
|
||||
cv::Mat get_image(bool raw = false);
|
||||
@@ -61,11 +61,12 @@ namespace asst
|
||||
private:
|
||||
void pipe_working_proc();
|
||||
std::optional<std::vector<unsigned char>> call_command(const std::string& cmd, int64_t timeout = 20000);
|
||||
std::optional<std::vector<unsigned char>> recv_data_by_socket(int64_t timeout = 20000);
|
||||
int push_cmd(const std::string& cmd);
|
||||
|
||||
using DecodeFunc = std::function<bool(const std::vector<uchar>&)>;
|
||||
bool screencap();
|
||||
bool screencap(const std::string& cmd, DecodeFunc decode_func);
|
||||
bool screencap(const std::string& cmd, DecodeFunc decode_func, bool by_nc = false);
|
||||
cv::Mat get_resized_image() const;
|
||||
|
||||
Point rand_point_in_rect(const Rect& rect);
|
||||
@@ -82,7 +83,11 @@ namespace asst
|
||||
std::minstd_rand m_rand_engine;
|
||||
|
||||
constexpr static int PipeBuffSize = 4 * 1024 * 1024; // 管道缓冲区大小
|
||||
constexpr static int SocketBuffSize = 4 * 1024 * 1024; // socket 缓冲区大小
|
||||
std::unique_ptr<uchar[]> m_pipe_buffer = nullptr;
|
||||
std::mutex m_pipe_mutex;
|
||||
std::unique_ptr<char[]> m_socket_buffer = nullptr;
|
||||
std::mutex m_socket_mutex;
|
||||
#ifdef _WIN32
|
||||
HANDLE m_pipe_read = nullptr; // 读管道句柄
|
||||
HANDLE m_pipe_write = nullptr; // 写管道句柄
|
||||
@@ -90,6 +95,10 @@ namespace asst
|
||||
HANDLE m_pipe_child_write = nullptr; // 子进程的写管道句柄
|
||||
SECURITY_ATTRIBUTES m_pipe_sec_attr = { 0 }; // 管道安全描述符
|
||||
STARTUPINFOA m_child_startup_info = { 0 }; // 子进程启动信息
|
||||
|
||||
WSADATA m_wsa_data = { 0 };
|
||||
SOCKET m_server_sock = 0ULL;
|
||||
sockaddr_in m_server_addr = { 0 };
|
||||
#else
|
||||
constexpr static int PIPE_READ = 0;
|
||||
constexpr static int PIPE_WRITE = 1;
|
||||
@@ -104,6 +113,7 @@ namespace asst
|
||||
std::string click;
|
||||
std::string swipe;
|
||||
|
||||
std::string screencap_raw_with_gzip_and_nc;
|
||||
std::string screencap_raw_with_gzip;
|
||||
std::string screencap_encode;
|
||||
std::string release;
|
||||
@@ -121,6 +131,7 @@ namespace asst
|
||||
{
|
||||
UnknownYet,
|
||||
Default,
|
||||
RawWithGzipAndNc,
|
||||
RawWithGzip,
|
||||
Encode
|
||||
} screencap_method = ScreencapMethod::UnknownYet;
|
||||
@@ -131,6 +142,9 @@ namespace asst
|
||||
double m_control_scale = 1.0;
|
||||
int m_width = 0;
|
||||
int m_height = 0;
|
||||
bool m_support_netcat = false;
|
||||
|
||||
inline static int m_instance_count = 0;
|
||||
|
||||
mutable std::shared_mutex m_image_mutex;
|
||||
cv::Mat m_cache_image;
|
||||
|
||||
@@ -43,6 +43,9 @@ bool asst::GeneralConfiger::parse(const json::value& json)
|
||||
adb.display = cfg_json.at("display").as_string();
|
||||
adb.display_format = cfg_json.at("displayFormat").as_string();
|
||||
adb.screencap_raw_with_gzip = cfg_json.at("screencapRawWithGzip").as_string();
|
||||
adb.screencap_raw_with_gzip_and_nc = cfg_json.at("screencapRawWithGzipAndNC").as_string();
|
||||
adb.nc_port = static_cast<unsigned short>(cfg_json.at("ncPort").as_integer());
|
||||
adb.nc_port_reverse = cfg_json.at("ncPortReverse").as_string();
|
||||
adb.screencap_encode = cfg_json.at("screencapEncode").as_string();
|
||||
adb.release = cfg_json.at("release").as_string();
|
||||
//adb.pullscreen = cfg_json.at("pullscreen").as_string();
|
||||
|
||||
@@ -51,6 +51,9 @@ namespace asst
|
||||
std::string swipe;
|
||||
std::string display;
|
||||
std::string screencap_raw_with_gzip;
|
||||
std::string screencap_raw_with_gzip_and_nc;
|
||||
unsigned short nc_port = 0U;
|
||||
std::string nc_port_reverse;
|
||||
std::string screencap_encode;
|
||||
std::string release;
|
||||
};
|
||||
|
||||
@@ -245,7 +245,7 @@
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>ppocr.lib;opencv_world453.lib;zlibstatic.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>ppocr.lib;opencv_world453.lib;zlibstatic.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<UACExecutionLevel>RequireAdministrator</UACExecutionLevel>
|
||||
<AdditionalLibraryDirectories>
|
||||
</AdditionalLibraryDirectories>
|
||||
@@ -297,7 +297,7 @@
|
||||
<EnableCOMDATFolding>true</EnableCOMDATFolding>
|
||||
<OptimizeReferences>true</OptimizeReferences>
|
||||
<GenerateDebugInformation>true</GenerateDebugInformation>
|
||||
<AdditionalDependencies>ppocr.lib;opencv_world453.lib;zlibstatic.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>ppocr.lib;opencv_world453.lib;zlibstatic.lib;ws2_32.lib;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<UACExecutionLevel>RequireAdministrator</UACExecutionLevel>
|
||||
<AdditionalLibraryDirectories>
|
||||
</AdditionalLibraryDirectories>
|
||||
|
||||
Reference in New Issue
Block a user