fix: reinit minitouch when reconnect

This commit is contained in:
shxyke
2023-04-11 03:52:26 +00:00
parent 67174f3081
commit e79bde1599
4 changed files with 82 additions and 54 deletions

View File

@@ -45,6 +45,66 @@ asst::AdbController::~AdbController()
kill_adb_daemon();
}
std::optional<std::string> 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<std::string> asst::AdbController::call_command(const std::string& cmd, int64_t timeout,
bool allow_reconnect, bool recv_by_socket)
{
@@ -91,58 +151,7 @@ std::optional<std::string> 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;

View File

@@ -51,6 +51,8 @@ namespace asst
std::optional<std::string> call_command(const std::string& cmd, int64_t timeout = 20000,
bool allow_reconnect = true, bool recv_by_socket = false);
virtual std::optional<std::string> reconnect(const std::string& cmd, int64_t timeout, bool recv_by_socket);
void release();
void kill_adb_daemon();
void make_instance_inited(bool inited);

View File

@@ -89,9 +89,25 @@ bool asst::MinitouchController::call_and_hup_minitouch()
m_minitouch_props.x_scaling = static_cast<double>(m_minitouch_props.max_x) / m_width;
m_minitouch_props.y_scaling = static_cast<double>(m_minitouch_props.max_y) / m_height;
m_minitoucher = std::make_unique<Minitoucher>(
std::bind(&MinitouchController::input_to_minitouch, this, std::placeholders::_1), m_minitouch_props);
return true;
}
std::optional<std::string> 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<Minitoucher>(
std::bind(&MinitouchController::input_to_minitouch, this, std::placeholders::_1), m_minitouch_props);
return true;
}

View File

@@ -36,6 +36,9 @@ namespace asst
MinitouchController& operator=(MinitouchController&&) = delete;
protected:
virtual std::optional<std::string> 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<std::string(const std::string&)> cmd_replace);