From f4dd64fb6ced6262afdead7d7a67f1d533d7a959 Mon Sep 17 00:00:00 2001 From: MistEO Date: Mon, 21 Mar 2022 11:15:07 +0800 Subject: [PATCH] =?UTF-8?q?fix.=E4=BF=AE=E5=A4=8Dadb=E5=91=BD=E4=BB=A4?= =?UTF-8?q?=E8=B6=85=E6=97=B6=E5=AF=BC=E8=87=B4=E5=8D=A1=E4=BD=8F=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/MeoAssistant/Controller.cpp | 22 ++++++++++++++++------ src/MeoAssistant/Controller.h | 2 +- 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/src/MeoAssistant/Controller.cpp b/src/MeoAssistant/Controller.cpp index 9c0b41bf56..1fb93372d9 100644 --- a/src/MeoAssistant/Controller.cpp +++ b/src/MeoAssistant/Controller.cpp @@ -234,7 +234,7 @@ void asst::Controller::pipe_working_proc() m_cmd_queue.pop(); cmd_queue_lock.unlock(); // todo 判断命令是否执行成功 - call_command(cmd); + call_command(cmd, 20 * 1000); ++m_completed_id; } //else if (!m_thread_idle) { // 队列中没有任务,又不是闲置的时候,就去截图 @@ -398,14 +398,22 @@ bool asst::Controller::try_capture(const EmulatorInfo & info, bool without_handl // } //} -std::optional> asst::Controller::call_command(const std::string & cmd) +std::optional> asst::Controller::call_command(const std::string & cmd, int64_t timeout) { LogTraceFunction; + std::vector pipe_data; + static std::mutex pipe_mutex; std::unique_lock pipe_lock(pipe_mutex); - std::vector pipe_data; + 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 PROCESS_INFORMATION process_info = { 0 }; // 进程信息结构体 @@ -423,10 +431,12 @@ std::optional> asst::Controller::call_command(const s pipe_data.insert(pipe_data.end(), m_pipe_buffer.get(), m_pipe_buffer.get() + read_num); } } - } while (::WaitForSingleObject(process_info.hProcess, 0) == WAIT_TIMEOUT); + } while (::WaitForSingleObject(process_info.hProcess, 0) == WAIT_TIMEOUT && !check_timeout()); DWORD exit_ret = 255; - ::GetExitCodeProcess(process_info.hProcess, &exit_ret); + if (!check_timeout()) { + ::GetExitCodeProcess(process_info.hProcess, &exit_ret); + } ::CloseHandle(process_info.hProcess); ::CloseHandle(process_info.hThread); @@ -462,7 +472,7 @@ std::optional> asst::Controller::call_command(const s pipe_data.insert(pipe_data.end(), m_pipe_buffer.get(), m_pipe_buffer.get() + read_num); read_num = read(m_pipe_out[PIPE_READ], m_pipe_buffer.get(), PipeBuffSize); }; - } while (::waitpid(m_child, &exit_ret, WNOHANG) == 0); + } while (::waitpid(m_child, &exit_ret, WNOHANG) == 0 && !check_timeout()); } else { // failed to create child process diff --git a/src/MeoAssistant/Controller.h b/src/MeoAssistant/Controller.h index 0c06954a60..dbdf3df123 100644 --- a/src/MeoAssistant/Controller.h +++ b/src/MeoAssistant/Controller.h @@ -67,7 +67,7 @@ namespace asst bool connect_adb(const std::string& address); void pipe_working_proc(); - std::optional> call_command(const std::string& cmd); + std::optional> call_command(const std::string& cmd, int64_t timeout = 0); int push_cmd(const std::string& cmd); using DecodeFunc = std::function&)>;