diff --git a/src/MaaCore/Controller/AdbController.cpp b/src/MaaCore/Controller/AdbController.cpp index 9071c578f3..e6b34d3276 100644 --- a/src/MaaCore/Controller/AdbController.cpp +++ b/src/MaaCore/Controller/AdbController.cpp @@ -45,6 +45,66 @@ asst::AdbController::~AdbController() kill_adb_daemon(); } +std::optional asst::AdbController::reconnect(const std::string& cmd, int64_t timeout, bool recv_by_socket) +{ + LogTraceFunction; + + json::value reconnect_info = json::object { + { "uuid", m_uuid }, + { "what", "Reconnecting" }, + { "why", "" }, + { "details", + json::object { + { "reconnect", m_adb.connect }, + { "cmd", cmd }, + } }, + }; + static constexpr int ReconnectTimes = 5; + for (int i = 0; i < ReconnectTimes; ++i) { + if (need_exit()) { + break; + } + reconnect_info["details"]["times"] = i; + callback(AsstMsg::ConnectionInfo, reconnect_info); + + sleep(10 * 1000); + if (need_exit()) { + break; + } + auto reconnect_ret = call_command(m_adb.connect, 60LL * 1000, false /* 禁止重连避免无限递归 */); + if (need_exit()) { + break; + } + bool is_reconnect_success = false; + if (reconnect_ret) { + auto& reconnect_str = reconnect_ret.value(); + is_reconnect_success = reconnect_str.find("error") == std::string::npos; + } + if (is_reconnect_success) { + auto recall_ret = call_command(cmd, timeout, false /* 禁止重连避免无限递归 */, recv_by_socket); + if (recall_ret) { + // 重连并成功执行了 + reconnect_info["what"] = "Reconnected"; + callback(AsstMsg::ConnectionInfo, reconnect_info); + return recall_ret; + } + } + } + json::value info = json::object { + { "uuid", m_uuid }, + { "what", "Disconnect" }, + { "why", "Reconnect failed" }, + { "details", + json::object { + { "cmd", m_adb.connect }, + } }, + }; + make_instance_inited(false); // 重连失败,释放 + callback(AsstMsg::ConnectionInfo, info); + + return std::nullopt; +} + std::optional asst::AdbController::call_command(const std::string& cmd, int64_t timeout, bool allow_reconnect, bool recv_by_socket) { @@ -91,58 +151,7 @@ std::optional asst::AdbController::call_command(const std::string& } else if (inited() && allow_reconnect) { // 之前可以运行,突然运行不了了,这种情况多半是 adb 炸了。所以重新连接一下 - json::value reconnect_info = json::object { - { "uuid", m_uuid }, - { "what", "Reconnecting" }, - { "why", "" }, - { "details", - json::object { - { "reconnect", m_adb.connect }, - { "cmd", cmd }, - } }, - }; - static constexpr int ReconnectTimes = 5; - for (int i = 0; i < ReconnectTimes; ++i) { - if (need_exit()) { - break; - } - reconnect_info["details"]["times"] = i; - callback(AsstMsg::ConnectionInfo, reconnect_info); - - sleep(10 * 1000); - if (need_exit()) { - break; - } - auto reconnect_ret = call_command(m_adb.connect, 60LL * 1000, false /* 禁止重连避免无限递归 */); - if (need_exit()) { - break; - } - bool is_reconnect_success = false; - if (reconnect_ret) { - auto& reconnect_str = reconnect_ret.value(); - is_reconnect_success = reconnect_str.find("error") == std::string::npos; - } - if (is_reconnect_success) { - auto recall_ret = call_command(cmd, timeout, false /* 禁止重连避免无限递归 */, recv_by_socket); - if (recall_ret) { - // 重连并成功执行了 - reconnect_info["what"] = "Reconnected"; - callback(AsstMsg::ConnectionInfo, reconnect_info); - return recall_ret; - } - } - } - json::value info = json::object { - { "uuid", m_uuid }, - { "what", "Disconnect" }, - { "why", "Reconnect failed" }, - { "details", - json::object { - { "cmd", m_adb.connect }, - } }, - }; - make_instance_inited(false); // 重连失败,释放 - callback(AsstMsg::ConnectionInfo, info); + return reconnect(cmd, timeout, recv_by_socket); } return std::nullopt; diff --git a/src/MaaCore/Controller/AdbController.h b/src/MaaCore/Controller/AdbController.h index 0d543da2d1..d3c2cf2ec4 100644 --- a/src/MaaCore/Controller/AdbController.h +++ b/src/MaaCore/Controller/AdbController.h @@ -51,6 +51,8 @@ namespace asst std::optional call_command(const std::string& cmd, int64_t timeout = 20000, bool allow_reconnect = true, bool recv_by_socket = false); + virtual std::optional reconnect(const std::string& cmd, int64_t timeout, bool recv_by_socket); + void release(); void kill_adb_daemon(); void make_instance_inited(bool inited); diff --git a/src/MaaCore/Controller/MinitouchController.cpp b/src/MaaCore/Controller/MinitouchController.cpp index 4f4d69bec5..0bec33f90a 100644 --- a/src/MaaCore/Controller/MinitouchController.cpp +++ b/src/MaaCore/Controller/MinitouchController.cpp @@ -89,9 +89,25 @@ bool asst::MinitouchController::call_and_hup_minitouch() m_minitouch_props.x_scaling = static_cast(m_minitouch_props.max_x) / m_width; m_minitouch_props.y_scaling = static_cast(m_minitouch_props.max_y) / m_height; + m_minitoucher = std::make_unique( + std::bind(&MinitouchController::input_to_minitouch, this, std::placeholders::_1), m_minitouch_props); + return true; } +std::optional asst::MinitouchController::reconnect(const std::string& cmd, int64_t timeout, + bool recv_by_socket) +{ + LogTraceFunction; + + if (auto ret = asst::AdbController::reconnect(cmd, timeout, recv_by_socket); ret.has_value()) { + call_and_hup_minitouch(); + return ret; + } + + return std::nullopt; +} + bool asst::MinitouchController::input_to_minitouch(const std::string& cmd) { return m_minitouch_handler->write(cmd); @@ -353,7 +369,5 @@ bool asst::MinitouchController::connect(const std::string& adb_path, const std:: return false; } - m_minitoucher = std::make_unique( - std::bind(&MinitouchController::input_to_minitouch, this, std::placeholders::_1), m_minitouch_props); return true; } diff --git a/src/MaaCore/Controller/MinitouchController.h b/src/MaaCore/Controller/MinitouchController.h index 472762c920..6338a8376c 100644 --- a/src/MaaCore/Controller/MinitouchController.h +++ b/src/MaaCore/Controller/MinitouchController.h @@ -36,6 +36,9 @@ namespace asst MinitouchController& operator=(MinitouchController&&) = delete; protected: + virtual std::optional reconnect(const std::string& cmd, int64_t timeout, + bool recv_by_socket) override; + bool call_and_hup_minitouch(); bool probe_minitouch(const AdbCfg& adb_cfg, std::function cmd_replace);