From 7f4f1882ae8c6f3cac7033af3e8f6694e80dc7ec Mon Sep 17 00:00:00 2001 From: dijunkun Date: Mon, 17 Aug 2026 00:07:01 +0800 Subject: [PATCH] [fix] sync signal status after server switch --- .../features/settings/settings_manager.cpp | 31 ++++++++++++++----- src/gui/features/settings/settings_manager.h | 4 +++ src/gui/runtime/gui_runtime.cpp | 6 ++++ 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/gui/features/settings/settings_manager.cpp b/src/gui/features/settings/settings_manager.cpp index 428a122..633a0e8 100644 --- a/src/gui/features/settings/settings_manager.cpp +++ b/src/gui/features/settings/settings_manager.cpp @@ -134,14 +134,7 @@ int SettingsManager::Load() { lock.unlock(); - const char *at_pos = std::strchr(owner_.client_id_with_password_, '@'); - if (at_pos != nullptr) { - const std::string id(owner_.client_id_with_password_, - at_pos - owner_.client_id_with_password_); - const std::string password(at_pos + 1); - CopyString(owner_.client_id_, id.c_str()); - CopyString(owner_.password_saved_, password.c_str()); - } + ActivateCachedPublicIdentity(); owner_.thumbnail_ = std::make_shared(owner_.cache_path_ + "/thumbnails/", @@ -195,6 +188,8 @@ bool SettingsManager::LoadCachedSelfHostedIdentity() { std::lock_guard lock(cache_mutex_); if (!ReadV2Locked() || cache_v2_.self_hosted_id[0] == '\0') { std::memset(owner_.self_hosted_id_, 0, sizeof(owner_.self_hosted_id_)); + std::memset(owner_.client_id_, 0, sizeof(owner_.client_id_)); + std::memset(owner_.password_saved_, 0, sizeof(owner_.password_saved_)); return false; } @@ -212,6 +207,26 @@ bool SettingsManager::LoadCachedSelfHostedIdentity() { return true; } +bool SettingsManager::ActivateCachedPublicIdentity() { + if (owner_.client_id_with_password_[0] == '\0') { + std::memset(owner_.client_id_, 0, sizeof(owner_.client_id_)); + std::memset(owner_.password_saved_, 0, sizeof(owner_.password_saved_)); + return false; + } + + const char *at_pos = std::strchr(owner_.client_id_with_password_, '@'); + if (at_pos == nullptr) { + CopyString(owner_.client_id_, owner_.client_id_with_password_); + std::memset(owner_.password_saved_, 0, sizeof(owner_.password_saved_)); + } else { + const std::string id(owner_.client_id_with_password_, + at_pos - owner_.client_id_with_password_); + CopyString(owner_.client_id_, id.c_str()); + CopyString(owner_.password_saved_, at_pos + 1); + } + return owner_.client_id_[0] != '\0'; +} + void SettingsManager::PersistSelfHostedIdentity(const char *client_id) { if (!client_id) { return; diff --git a/src/gui/features/settings/settings_manager.h b/src/gui/features/settings/settings_manager.h index f907567..dc36c8a 100644 --- a/src/gui/features/settings/settings_manager.h +++ b/src/gui/features/settings/settings_manager.h @@ -33,6 +33,10 @@ public: // Loads the cached self-hosted identity into the owner's active connection // fields. Returns true only when a non-empty identity was restored. bool LoadCachedSelfHostedIdentity(); + // Restores the public-server identity kept in the in-memory cache fields. + // This must run when switching away from a self-hosted server so signal + // callbacks are matched against the identity used by the replacement peer. + bool ActivateCachedPublicIdentity(); void PersistSelfHostedIdentity(const char *client_id); private: diff --git a/src/gui/runtime/gui_runtime.cpp b/src/gui/runtime/gui_runtime.cpp index eff0bd1..6bfde6e 100644 --- a/src/gui/runtime/gui_runtime.cpp +++ b/src/gui/runtime/gui_runtime.cpp @@ -57,6 +57,7 @@ int GuiRuntime::CreateConnectionPeer() { signal_server_ip = config_center_->GetDefaultServerHost(); signal_server_port = config_center_->GetDefaultSignalServerPort(); coturn_server_port = config_center_->GetDefaultCoturnServerPort(); + settings_.ActivateCachedPublicIdentity(); params_.user_id = client_id_with_password_; } @@ -125,6 +126,11 @@ int GuiRuntime::CreateConnectionPeer() { params_.user_data = &peer_events_; + // The previous peer may have left a terminal status behind. Reset it before + // Init() starts emitting callbacks for the newly selected server. + signal_connected_ = false; + signal_status_ = SignalStatus::SignalConnecting; + peer_ = CreatePeer(¶ms_); if (peer_) { LOG_INFO("Create peer instance [{}] successful", client_id_);