diff --git a/cpp/core/internal/client_proxy.cc b/cpp/core/internal/client_proxy.cc index f9b97169..010d9712 100644 --- a/cpp/core/internal/client_proxy.cc +++ b/cpp/core/internal/client_proxy.cc @@ -70,8 +70,6 @@ void ClientProxy::StartedAdvertising( const ConnectionListener& listener, absl::Span mediums) { MutexLock lock(&mutex_); - - if (connections_.empty()) local_endpoint_id_.clear(); advertising_info_ = {service_id, listener}; } @@ -81,7 +79,7 @@ void ClientProxy::StoppedAdvertising() { if (IsAdvertising()) { advertising_info_.Clear(); } - if (connections_.empty()) local_endpoint_id_.clear(); + ResetLocalEndpointIdIfNeeded(); } bool ClientProxy::IsAdvertising() const { @@ -97,10 +95,8 @@ std::string ClientProxy::GetAdvertisingServiceId() const { std::string ClientProxy::GetServiceId() const { MutexLock lock(&mutex_); - if (IsAdvertising()) - return advertising_info_.service_id; - if (IsDiscovering()) - return discovery_info_.service_id; + if (IsAdvertising()) return advertising_info_.service_id; + if (IsDiscovering()) return discovery_info_.service_id; return "idle_service_id"; } @@ -109,8 +105,6 @@ void ClientProxy::StartedDiscovery( const DiscoveryListener& listener, absl::Span mediums) { MutexLock lock(&mutex_); - - if (connections_.empty()) local_endpoint_id_.clear(); discovery_info_ = DiscoveryInfo{service_id, listener}; } @@ -121,7 +115,7 @@ void ClientProxy::StoppedDiscovery() { discovered_endpoint_ids_.clear(); discovery_info_.Clear(); } - if (connections_.empty()) local_endpoint_id_.clear(); + ResetLocalEndpointIdIfNeeded(); } bool ClientProxy::IsDiscoveringServiceId(const std::string& service_id) const { @@ -266,7 +260,7 @@ void ClientProxy::OnDisconnected(const std::string& endpoint_id, bool notify) { item->connection_listener.disconnected_cb({endpoint_id}); } connections_.erase(endpoint_id); - if (connections_.empty()) local_endpoint_id_.clear(); + ResetLocalEndpointIdIfNeeded(); } } @@ -516,6 +510,13 @@ void ClientProxy::RemoveAllEndpoints() { local_endpoint_id_.clear(); } +void ClientProxy::ResetLocalEndpointIdIfNeeded() { + MutexLock lock(&mutex_); + if (connections_.empty() && !IsAdvertising() && !IsDiscovering()) { + local_endpoint_id_.clear(); + } +} + bool ClientProxy::ConnectionStatusesContains( const std::string& endpoint_id, Connection::Status status_to_match) const { const Connection* item = LookupConnection(endpoint_id); diff --git a/cpp/core/internal/client_proxy.h b/cpp/core/internal/client_proxy.h index c94ac0cf..3aa5b667 100644 --- a/cpp/core/internal/client_proxy.h +++ b/cpp/core/internal/client_proxy.h @@ -196,6 +196,7 @@ class ClientProxy final { }; void RemoveAllEndpoints(); + void ResetLocalEndpointIdIfNeeded(); bool ConnectionStatusesContains(const std::string& endpoint_id, Connection::Status status_to_match) const; void AppendConnectionStatus(const std::string& endpoint_id,