From efd8b7c0ff31c1c03ebf658ee9f638b04ee5fd44 Mon Sep 17 00:00:00 2001 From: dijunkun Date: Tue, 28 Jul 2026 17:43:25 +0800 Subject: [PATCH] fix: let ICE drive connection failure state --- src/gui/application/gui_application.cpp | 15 ++++---- src/gui/runtime/connection_runtime.cpp | 47 +---------------------- src/gui/runtime/gui_runtime.h | 2 +- src/gui/runtime/peer_event_handler.cpp | 4 -- src/gui/runtime/remote_session.h | 8 ++-- tests/connection_status_protocol_test.cpp | 20 ++++++---- 6 files changed, 25 insertions(+), 71 deletions(-) diff --git a/src/gui/application/gui_application.cpp b/src/gui/application/gui_application.cpp index 40c8f48..51fd321 100644 --- a/src/gui/application/gui_application.cpp +++ b/src/gui/application/gui_application.cpp @@ -1833,7 +1833,7 @@ void GuiApplication::Tick() { } HandleConnectionStatusChange(); HandlePendingPresenceProbe(); - HandleConnectionTimeouts(); + HandlePresenceProbeTimeout(); HandleWindowsServiceIntegration(); #if defined(__linux__) && !defined(__APPLE__) SyncXWaylandWindowActivation(); @@ -2839,11 +2839,11 @@ void GuiApplication::SelectStreamTab(int index) { } } const auto selected = remote_sessions_.find(selected_remote_id); - start_keyboard_capturer_ = - selected != remote_sessions_.end() && selected->second && - selected->second->control_mouse_ && - selected->second->connection_status_.load() == - ConnectionStatus::Connected; + start_keyboard_capturer_ = selected != remote_sessions_.end() && + selected->second && + selected->second->control_mouse_ && + selected->second->connection_status_.load() == + ConnectionStatus::Connected; } void GuiApplication::ReorderStreamTab(int from, float drop_x, float tab_width) { @@ -3043,8 +3043,7 @@ void GuiApplication::SendKeyInput(const std::string& text, bool pressed, // Native hooks see the same physical key before Slint does. When a native // hook is active, forwarding the FocusScope callback as well would duplicate // the event on platforms whose hook does not consume the local key. - if (keyboard_capturer_is_started_ && - !keyboard_capturer_uses_window_events_) { + if (keyboard_capturer_is_started_ && !keyboard_capturer_uses_window_events_) { return; } diff --git a/src/gui/runtime/connection_runtime.cpp b/src/gui/runtime/connection_runtime.cpp index b7a0b9c..24a577a 100644 --- a/src/gui/runtime/connection_runtime.cpp +++ b/src/gui/runtime/connection_runtime.cpp @@ -17,11 +17,6 @@ namespace crossdesk { namespace { constexpr auto kPresenceProbeTimeout = std::chrono::seconds(5); -constexpr auto kConnectionAttemptTimeout = std::chrono::seconds(20); -bool IsConnectionAttemptPending(ConnectionStatus status) { - return status == ConnectionStatus::Connecting || - status == ConnectionStatus::Gathering; -} } // namespace void GuiRuntime::HandleConnectionStatusChange() { @@ -95,7 +90,7 @@ void GuiRuntime::HandlePendingPresenceProbe() { show_offline_warning_window_ = true; } -void GuiRuntime::HandleConnectionTimeouts() { +void GuiRuntime::HandlePresenceProbeTimeout() { const auto now = std::chrono::steady_clock::now(); bool presence_probe_timed_out = false; @@ -121,44 +116,6 @@ void GuiRuntime::HandleConnectionTimeouts() { show_offline_warning_window_ = true; LOG_WARN("Presence probe timed out for [{}]", presence_remote_id); } - - bool rejoin_state_changed = false; - for (auto& [_, props] : remote_sessions_) { - if (!props || !props->connection_attempt_active_.load()) { - continue; - } - - const ConnectionStatus status = props->connection_status_.load(); - if (!IsConnectionAttemptPending(status)) { - props->connection_attempt_active_.store(false); - continue; - } - - if (now - props->connection_attempt_started_at_ < - kConnectionAttemptTimeout) { - continue; - } - - LOG_WARN("Connection to [{}] timed out, status={}", props->remote_id_, - static_cast(status)); - props->connection_attempt_active_.store(false); - props->connection_established_ = false; - props->rejoin_ = false; - props->connection_status_.store(ConnectionStatus::Failed); - focused_remote_id_ = props->remote_id_; - show_connection_status_window_ = true; - rejoin_state_changed = true; - } - - if (rejoin_state_changed) { - need_to_rejoin_ = false; - for (const auto& [_, props] : remote_sessions_) { - if (props && props->rejoin_) { - need_to_rejoin_ = true; - break; - } - } - } } void GuiRuntime::HandleServerControllerDisconnected( @@ -455,8 +412,6 @@ int GuiRuntime::ConnectTo(const std::string& remote_id, const char* password, auto props = remote_sessions_[remote_id]; if (!props->connection_established_) { props->connection_status_.store(ConnectionStatus::Connecting); - props->connection_attempt_active_.store(true); - props->connection_attempt_started_at_ = std::chrono::steady_clock::now(); show_connection_status_window_ = true; props->remember_password_ = remember_password; diff --git a/src/gui/runtime/gui_runtime.h b/src/gui/runtime/gui_runtime.h index 442b4b4..374534e 100644 --- a/src/gui/runtime/gui_runtime.h +++ b/src/gui/runtime/gui_runtime.h @@ -45,7 +45,7 @@ class GuiRuntime : protected gui_detail::GuiState { void HandleRecentConnections(); void HandleConnectionStatusChange(); void HandlePendingPresenceProbe(); - void HandleConnectionTimeouts(); + void HandlePresenceProbeTimeout(); void HandleServerControllerDisconnected(const std::string& remote_id, const char* reason); void HandleWindowsServiceIntegration(); diff --git a/src/gui/runtime/peer_event_handler.cpp b/src/gui/runtime/peer_event_handler.cpp index b87fa92..1fa30e4 100644 --- a/src/gui/runtime/peer_event_handler.cpp +++ b/src/gui/runtime/peer_event_handler.cpp @@ -170,10 +170,6 @@ void PeerEventHandler::OnConnectionStatus(ConnectionStatus status, runtime->is_client_mode_ = true; runtime->show_connection_status_window_ = true; props->connection_status_.store(status); - if (status != ConnectionStatus::Connecting && - status != ConnectionStatus::Gathering) { - props->connection_attempt_active_.store(false); - } switch (status) { case ConnectionStatus::Connected: { diff --git a/src/gui/runtime/remote_session.h b/src/gui/runtime/remote_session.h index c1c6faa..3b90dfb 100644 --- a/src/gui/runtime/remote_session.h +++ b/src/gui/runtime/remote_session.h @@ -58,7 +58,7 @@ struct FileTransferState { // connection, media, input, window and transfer data that share one lifetime. struct RemoteSession { Params params_; - PeerPtr *peer_ = nullptr; + PeerPtr* peer_ = nullptr; std::string audio_label_ = "control_audio"; std::string data_label_ = "data"; std::string mouse_label_ = "mouse"; @@ -74,8 +74,6 @@ struct RemoteSession { SignalStatus signal_status_ = SignalStatus::SignalClosed; bool connection_established_ = false; bool rejoin_ = false; - std::atomic connection_attempt_active_ = false; - std::chrono::steady_clock::time_point connection_attempt_started_at_; bool net_traffic_stats_button_pressed_ = false; bool enable_mouse_control_ = true; bool mouse_controller_is_started_ = false; @@ -160,6 +158,6 @@ struct RemoteSession { using RemoteSessionPtr = std::shared_ptr; -} // namespace crossdesk::gui_detail +} // namespace crossdesk::gui_detail -#endif // CROSSDESK_GUI_REMOTE_SESSION_H_ +#endif // CROSSDESK_GUI_REMOTE_SESSION_H_ diff --git a/tests/connection_status_protocol_test.cpp b/tests/connection_status_protocol_test.cpp index 874b8f7..572d152 100644 --- a/tests/connection_status_protocol_test.cpp +++ b/tests/connection_status_protocol_test.cpp @@ -104,16 +104,22 @@ int main() { "return localization::device_offline[language];"); ok &= ExpectContains("runtime_state.h", runtime_state_h, "pending_presence_probe_started_at_"); - ok &= ExpectContains("remote_session.h", remote_session_h, - "connection_attempt_started_at_"); ok &= ExpectContains("connection_runtime.cpp", connection_runtime_cpp, - "HandleConnectionTimeouts"); + "HandlePresenceProbeTimeout"); ok &= ExpectContains("connection_runtime.cpp", connection_runtime_cpp, "kPresenceProbeTimeout"); - ok &= ExpectContains("connection_runtime.cpp", connection_runtime_cpp, - "kConnectionAttemptTimeout"); - ok &= ExpectContains("connection_runtime.cpp", connection_runtime_cpp, - "connection_attempt_started_at_"); + ok &= ExpectNotContains("remote_session.h", remote_session_h, + "connection_attempt_started_at_"); + ok &= ExpectNotContains("remote_session.h", remote_session_h, + "connection_attempt_active_"); + ok &= ExpectNotContains("connection_runtime.cpp", connection_runtime_cpp, + "kConnectionAttemptTimeout"); + ok &= ExpectNotContains("connection_runtime.cpp", connection_runtime_cpp, + "ConnectionStatus::Failed"); + ok &= ExpectContains("peer_event_handler.cpp", peer_event_handler_cpp, + "props->connection_status_.store(status);"); + ok &= ExpectContains("peer_event_handler.cpp", peer_event_handler_cpp, + "case ConnectionStatus::Failed:"); ok &= ExpectContains("connection_runtime.cpp", connection_runtime_cpp, "HandleServerControllerDisconnected"); ok &= ExpectContains("peer_event_handler.cpp", peer_event_handler_cpp,