From 692a67d16edadd8827b44641883079448be2c943 Mon Sep 17 00:00:00 2001 From: MistEO Date: Sun, 5 Jun 2022 02:28:38 +0800 Subject: [PATCH] =?UTF-8?q?fix.=E4=BF=AE=E5=A4=8Dsocket=E8=BD=AC=E5=8F=91?= =?UTF-8?q?=E6=88=AA=E5=9B=BE=E5=8F=8D=E4=BB=A3=E5=A4=B1=E6=95=88=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=EF=BC=8C=E5=B9=B6=E5=8F=96=E6=B6=88gzip?= =?UTF-8?q?=E5=8E=8B=E7=BC=A9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- resource/config.json | 32 +-- src/MeoAssistant/Controller.cpp | 284 +++++++++++++++------------ src/MeoAssistant/Controller.h | 19 +- src/MeoAssistant/GeneralConfiger.cpp | 3 +- src/MeoAssistant/GeneralConfiger.h | 3 +- 5 files changed, 185 insertions(+), 156 deletions(-) diff --git a/resource/config.json b/resource/config.json index deae952fed..757d9b1d59 100644 --- a/resource/config.json +++ b/resource/config.json @@ -28,8 +28,8 @@ "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", - "screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"", - "ncPort": 6054, + "screencapRawByNC": "[Adb] -s [AdbSerial] exec-out \"screencap | nc -w 3 [NcAddress] [NcPort]\"", + "ncPort": 6723, "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", @@ -44,8 +44,8 @@ "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", - "screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"", - "ncPort": 6054, + "screencapRawByNC": "[Adb] -s [AdbSerial] exec-out \"screencap | nc -w 3 [NcAddress] [NcPort]\"", + "ncPort": 6723, "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", @@ -60,8 +60,8 @@ "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", - "screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"", - "ncPort": 6054, + "screencapRawByNC": "[Adb] -s [AdbSerial] exec-out \"screencap | nc -w 3 [NcAddress] [NcPort]\"", + "ncPort": 6723, "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", @@ -76,8 +76,8 @@ "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", - "screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"", - "ncPort": 6054, + "screencapRawByNC": "[Adb] -s [AdbSerial] exec-out \"screencap | nc -w 3 [NcAddress] [NcPort]\"", + "ncPort": 6723, "ncPortReverse": "[Adb] -s [AdbSerial] reverse tcp:[NcPort] tcp:[NcPort]", "screencapRawWithGzip": "[Adb] -s [AdbSerial] shell \"screencap | gzip -1\"", "screencapEncode": "[Adb] -s [AdbSerial] shell screencap -p", @@ -92,8 +92,8 @@ "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", - "screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"", - "ncPort": 6054, + "screencapRawByNC": "[Adb] -s [AdbSerial] exec-out \"screencap | nc -w 3 [NcAddress] [NcPort]\"", + "ncPort": 6723, "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", @@ -108,8 +108,8 @@ "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", - "screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"", - "ncPort": 6054, + "screencapRawByNC": "[Adb] -s [AdbSerial] exec-out \"screencap | nc -w 3 [NcAddress] [NcPort]\"", + "ncPort": 6723, "ncPortReverse": "[Adb] -s [AdbSerial] reverse tcp:[NcPort] tcp:[NcPort]", "screencapRawWithGzip": "[Adb] -s [AdbSerial] shell \"screencap | gzip -1\"", "screencapEncode": "[Adb] -s [AdbSerial] shell screencap -p", @@ -125,8 +125,8 @@ "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", - "screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"", - "ncPort": 6054, + "screencapRawByNC": "[Adb] -s [AdbSerial] exec-out \"screencap | nc -w 3 [NcAddress] [NcPort]\"", + "ncPort": 6723, "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", @@ -141,8 +141,8 @@ "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", - "screencapRawWithGzipAndNC": "[Adb] -s [AdbSerial] exec-out \"screencap | gzip -1 | nc [NcAddress] [NcPort]\"", - "ncPort": 6054, + "screencapRawByNC": "[Adb] -s [AdbSerial] exec-out \"screencap | nc -w 3 [NcAddress] [NcPort]\"", + "ncPort": 6723, "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", diff --git a/src/MeoAssistant/Controller.cpp b/src/MeoAssistant/Controller.cpp index 57c1569f9b..0fde0d7dab 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -84,17 +84,13 @@ asst::Controller::Controller(AsstCallback callback, void* callback_arg) m_child_startup_info.hStdOutput = m_pipe_child_write; m_child_startup_info.hStdError = m_pipe_child_write; - m_support_netcat = false; + m_support_socket = 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; + m_support_socket = true; } while (0); #else @@ -113,7 +109,7 @@ asst::Controller::Controller(AsstCallback callback, void* callback_arg) if (!m_pipe_buffer) { throw "controller pipe buffer allocated failed"; } - if (m_support_netcat) { + if (m_support_socket) { m_socket_buffer = std::make_unique(SocketBuffSize); if (!m_socket_buffer) { throw "controller socket buffer allocated failed"; @@ -146,17 +142,13 @@ asst::Controller::~Controller() ::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 //{ @@ -250,7 +242,7 @@ void asst::Controller::pipe_working_proc() } } -std::optional> asst::Controller::call_command(const std::string& cmd, int64_t timeout) +std::optional> asst::Controller::call_command(const std::string& cmd, int64_t timeout, bool recv_by_socket) { LogTraceFunction; @@ -270,21 +262,39 @@ std::optional> asst::Controller::call_command(const s PROCESS_INFORMATION process_info = { 0 }; // 进程信息结构体 BOOL create_ret = ::CreateProcessA(nullptr, const_cast(cmd.c_str()), nullptr, nullptr, TRUE, CREATE_NO_WINDOW, nullptr, nullptr, &m_child_startup_info, &process_info); if (!create_ret) { + Log.error("Call `", cmd, "` create error, ret", create_ret); return std::nullopt; } - DWORD peek_num = 0; - DWORD read_num = 0; - do { - //DWORD write_num = 0; - //WriteFile(parent_write, cmd.c_str(), cmd.size(), &write_num, nullptr); - while (::PeekNamedPipe(m_pipe_read, nullptr, 0, nullptr, &peek_num, nullptr) && peek_num > 0) { - if (::ReadFile(m_pipe_read, m_pipe_buffer.get(), PipeBuffSize, &read_num, nullptr)) { - pipe_data.insert(pipe_data.end(), m_pipe_buffer.get(), m_pipe_buffer.get() + read_num); + if (!recv_by_socket) { + DWORD peek_num = 0; + DWORD read_num = 0; + do { + //DWORD write_num = 0; + //WriteFile(parent_write, cmd.c_str(), cmd.size(), &write_num, nullptr); + while (::PeekNamedPipe(m_pipe_read, nullptr, 0, nullptr, &peek_num, nullptr) && peek_num > 0) { + if (::ReadFile(m_pipe_read, m_pipe_buffer.get(), PipeBuffSize, &read_num, nullptr)) { + pipe_data.insert(pipe_data.end(), m_pipe_buffer.get(), m_pipe_buffer.get() + read_num); + } } - } - } while (::WaitForSingleObject(process_info.hProcess, 0) == WAIT_TIMEOUT && !check_timeout()); - - DWORD exit_ret = 255; + } while (::WaitForSingleObject(process_info.hProcess, 0) == WAIT_TIMEOUT && !check_timeout()); + } + else { + SOCKADDR client_addr = { 0 }; + int size = sizeof(SOCKADDR); + SOCKET client_sock = ::accept(m_server_sock, (SOCKADDR*)&client_addr, &size); + int recv_size = 0; + do { + recv_size = ::recv(client_sock, (char*)m_socket_buffer.get(), SocketBuffSize, NULL); + if (recv_size < 0) { + Log.error("recv error", recv_size); + break; + } + pipe_data.insert(pipe_data.end(), m_socket_buffer.get(), m_socket_buffer.get() + recv_size); + } while (recv_size > 0 && !check_timeout()); + ::closesocket(client_sock); + ::WaitForSingleObject(process_info.hProcess, static_cast(timeout)); + } + DWORD exit_ret = 0; ::GetExitCodeProcess(process_info.hProcess, &exit_ret); ::CloseHandle(process_info.hProcess); ::CloseHandle(process_info.hThread); @@ -325,7 +335,7 @@ std::optional> 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()); @@ -341,57 +351,6 @@ std::optional> asst::Controller::call_command(const s } } -std::optional> asst::Controller::recv_data_by_socket(int64_t timeout) -{ - LogTraceFunction; - - if (!m_support_netcat) { - return std::nullopt; - } - - std::vector socket_data; - - std::unique_lock 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::steady_clock::now() - start_time) - .count(); - }; - -#ifdef _WIN32 - SOCKADDR client_addr = { 0 }; - int size = sizeof(SOCKADDR); - - std::unique_ptr> 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& data) { LogTraceFunction; @@ -476,6 +435,11 @@ void asst::Controller::clear_info() noexcept m_height = 0; m_control_scale = 1.0; m_scale_size = { WindowWidthDefault, WindowHeightDefault }; + m_server_started = false; + if (m_support_socket && m_server_started && m_server_sock) { + ::closesocket(m_server_sock); + m_server_sock = 0U; + } --m_instance_count; } @@ -489,6 +453,54 @@ int asst::Controller::push_cmd(const std::string& cmd) return ++m_push_id; } +std::optional asst::Controller::try_init_socket(const std::string& local_address, unsigned short try_port, unsigned short try_times) +{ + LogTraceFunction; + +#ifdef _WIN32 + m_server_sock = ::socket(PF_INET, SOCK_STREAM, IPPROTO_TCP); + if (m_server_sock == INVALID_SOCKET) { + return std::nullopt; + } + m_server_addr.sin_family = PF_INET; + m_server_addr.sin_addr.s_addr = inet_addr(local_address.c_str()); +#else + // Linux, todo +#endif + + bool server_start = false; + u_short port_result = 0; + + u_short max_port = try_port + try_times; + for (u_short port = try_port; port < max_port; ++port) { + Log.trace("try bind port", port); + +#ifdef _WIN32 + m_server_addr.sin_port = htons(port); + int bind_ret = ::bind(m_server_sock, (SOCKADDR*)&m_server_addr, sizeof(SOCKADDR)); + int listen_ret = ::listen(m_server_sock, 3); + server_start = bind_ret == 0 && listen_ret == 0; +#else + // todo +#endif + if (server_start) { + port_result = port; + break; + } + } + + if (!server_start) { + Log.info("not supports netcat"); + return std::nullopt; + } + + // Todo: detect remote address, and check remote port + // reference from https://github.com/ArknightsAutoHelper/ArknightsAutoHelper/blob/master/automator/connector/ADBConnector.py#L436 + std::string remote_address = "10.0.2.2"; + Log.info("server_start", remote_address, port_result); + return SocketInfo{ remote_address, port_result }; +} + void asst::Controller::wait(unsigned id) const noexcept { while (id > m_completed_id) { @@ -507,7 +519,35 @@ bool asst::Controller::screencap() // return true; //} - DecodeFunc decode_raw_with_gzip = [&](const std::vector& data) -> bool { + DecodeFunc decode_raw = [&](std::vector& data) -> bool { + if (data.empty()) { + return false; + } + size_t std_size = m_width * m_height * 4; + if (data.size() < std_size) { + return false; + } + size_t header_size = data.size() - std_size; + Log.trace("header size:", header_size); + + bool is_all_zero = std::all_of(data.data() + header_size, data.data() + std_size, + [](uchar uch) -> bool { + return uch == 0; + }); + if (is_all_zero) { + return false; + } + cv::Mat temp(m_height, m_width, CV_8UC4, data.data() + header_size); + if (temp.empty()) { + return false; + } + cv::cvtColor(temp, temp, cv::COLOR_RGB2BGR); + std::unique_lock image_lock(m_image_mutex); + m_cache_image = temp; + return true; + }; + + DecodeFunc decode_raw_with_gzip = [&](std::vector& data) -> bool { auto unzip_data = gzip::decompress(data.data(), data.size()); Log.trace("unzip data size:", unzip_data.size()); if (unzip_data.empty()) { @@ -537,7 +577,7 @@ bool asst::Controller::screencap() return true; }; - DecodeFunc decode_encode = [&](const std::vector& data) -> bool { + DecodeFunc decode_encode = [&](std::vector& data) -> bool { cv::Mat temp = cv::imdecode(data, cv::IMREAD_COLOR); if (temp.empty()) { return false; @@ -550,10 +590,10 @@ bool asst::Controller::screencap() switch (m_adb.screencap_method) { case AdbProperty::ScreencapMethod::UnknownYet: { - 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"); + if (m_support_socket && m_server_started && + screencap(m_adb.screencap_raw_by_nc, decode_raw, true)) { + m_adb.screencap_method = AdbProperty::ScreencapMethod::RawByNc; + Log.info("screencap by RawByNc"); return true; } else if (screencap(m_adb.screencap_raw_with_gzip, decode_raw_with_gzip)) { @@ -570,9 +610,9 @@ bool asst::Controller::screencap() return false; } } - case AdbProperty::ScreencapMethod::RawWithGzipAndNc: + case AdbProperty::ScreencapMethod::RawByNc: { - return screencap(m_adb.screencap_raw_with_gzip_and_nc, decode_raw_with_gzip, true); + return screencap(m_adb.screencap_raw_by_nc, decode_raw, true); } break; case AdbProperty::ScreencapMethod::RawWithGzip: @@ -592,15 +632,11 @@ bool asst::Controller::screencap() bool asst::Controller::screencap(const std::string& cmd, DecodeFunc decode_func, bool by_socket) { - if (!m_support_netcat && by_socket) { + if ((!m_support_socket || !m_server_started) && by_socket) { return false; } - auto ret = call_command(cmd); - - if (by_socket && ret) { - ret = recv_data_by_socket(); - } + auto ret = call_command(cmd, 20000, by_socket); if (!ret || ret.value().empty()) { Log.error("data is empty!"); @@ -958,53 +994,33 @@ 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; + if (m_support_socket && !m_server_started) { + std::string bind_address; + if (size_t pos = address.rfind(':'); + pos != std::string::npos) { + bind_address = address.substr(0, pos); } else { - Log.info("not supports netcat"); - m_support_netcat = false; + bind_address = "127.0.0.1"; + } + + auto socket_opt = try_init_socket(bind_address, adb_cfg.nc_port); + if (socket_opt) { + auto& socket_info = socket_opt.value(); + nc_address = socket_info.remote_address; + nc_port = socket_info.remote_port; + m_adb.screencap_raw_by_nc = cmd_replace(adb_cfg.screencap_raw_by_nc); + m_server_started = true; + } + else { + m_server_started = false; } - m_adb.screencap_raw_with_gzip_and_nc = cmd_replace(adb_cfg.screencap_raw_with_gzip_and_nc); } ++m_instance_count; @@ -1013,6 +1029,14 @@ bool asst::Controller::connect(const std::string& adb_path, const std::string& a bool asst::Controller::release() { +#ifdef _WIN32 + if (m_server_sock) { + m_server_started = false; + ::closesocket(m_server_sock); + m_server_sock = 0U; + } +#endif + #ifndef _WIN32 if (m_child) #else @@ -1021,7 +1045,7 @@ bool asst::Controller::release() { return call_command(m_adb.release).has_value(); } -} + } const std::string& asst::Controller::get_uuid() const { diff --git a/src/MeoAssistant/Controller.h b/src/MeoAssistant/Controller.h index 1029b4c2cd..92da86f57f 100644 --- a/src/MeoAssistant/Controller.h +++ b/src/MeoAssistant/Controller.h @@ -60,11 +60,17 @@ namespace asst private: void pipe_working_proc(); - std::optional> call_command(const std::string& cmd, int64_t timeout = 20000); - std::optional> recv_data_by_socket(int64_t timeout = 20000); + std::optional> call_command(const std::string& cmd, int64_t timeout = 20000, bool recv_by_socket = false); int push_cmd(const std::string& cmd); - using DecodeFunc = std::function&)>; + struct SocketInfo + { + std::string remote_address; + unsigned short remote_port = 0U; + }; + std::optional try_init_socket(const std::string& local_address, unsigned short try_port, unsigned short try_times = 10U); + + using DecodeFunc = std::function&)>; bool screencap(); bool screencap(const std::string& cmd, DecodeFunc decode_func, bool by_nc = false); cv::Mat get_resized_image() const; @@ -113,7 +119,7 @@ namespace asst std::string click; std::string swipe; - std::string screencap_raw_with_gzip_and_nc; + std::string screencap_raw_by_nc; std::string screencap_raw_with_gzip; std::string screencap_encode; std::string release; @@ -131,7 +137,7 @@ namespace asst { UnknownYet, Default, - RawWithGzipAndNc, + RawByNc, RawWithGzip, Encode } screencap_method = ScreencapMethod::UnknownYet; @@ -142,7 +148,8 @@ namespace asst double m_control_scale = 1.0; int m_width = 0; int m_height = 0; - bool m_support_netcat = false; + bool m_support_socket = false; + bool m_server_started = false; inline static int m_instance_count = 0; diff --git a/src/MeoAssistant/GeneralConfiger.cpp b/src/MeoAssistant/GeneralConfiger.cpp index 30f50ec666..a8cbd7ac9c 100644 --- a/src/MeoAssistant/GeneralConfiger.cpp +++ b/src/MeoAssistant/GeneralConfiger.cpp @@ -43,9 +43,8 @@ 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.screencap_raw_by_nc = cfg_json.at("screencapRawByNC").as_string(); adb.nc_port = static_cast(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(); diff --git a/src/MeoAssistant/GeneralConfiger.h b/src/MeoAssistant/GeneralConfiger.h index f57c464086..42c478871c 100644 --- a/src/MeoAssistant/GeneralConfiger.h +++ b/src/MeoAssistant/GeneralConfiger.h @@ -51,9 +51,8 @@ namespace asst std::string swipe; std::string display; std::string screencap_raw_with_gzip; - std::string screencap_raw_with_gzip_and_nc; + std::string screencap_raw_by_nc; unsigned short nc_port = 0U; - std::string nc_port_reverse; std::string screencap_encode; std::string release; };