From 2526d46eafa1b9ffa2f9fa7533b51f0799f63a8d Mon Sep 17 00:00:00 2001 From: hai007 Date: Tue, 10 Nov 2020 11:47:39 -0800 Subject: [PATCH] patch cl/340877543 Signed-off-by: hai007 --- cpp/core/internal/mediums/webrtc.cc | 31 +++++++++++++------- cpp/core/internal/mediums/webrtc.h | 19 ++++++++++++ cpp/core/internal/p2p_cluster_pcp_handler.cc | 10 +++---- cpp/core/internal/webrtc_bwu_handler.cc | 3 +- cpp/platform/base/medium_environment.cc | 4 +-- 5 files changed, 47 insertions(+), 20 deletions(-) diff --git a/cpp/core/internal/mediums/webrtc.cc b/cpp/core/internal/mediums/webrtc.cc index 2ba4ce0e..d24b206c 100644 --- a/cpp/core/internal/mediums/webrtc.cc +++ b/cpp/core/internal/mediums/webrtc.cc @@ -54,6 +54,8 @@ bool WebRtc::IsAcceptingConnections() { } bool WebRtc::StartAcceptingConnections(const PeerId& self_id, + const std::string& service_id, + const std::string& local_endpoint_id, const LocationHint& location_hint, AcceptedConnectionCallback callback) { if (!IsAvailable()) { @@ -64,17 +66,10 @@ bool WebRtc::StartAcceptingConnections(const PeerId& self_id, return false; } - if (IsAcceptingConnections()) { - NEARBY_LOG(WARNING, "Already accepting WebRTC connections."); - return false; - } - { MutexLock lock(&mutex_); - if (role_ != Role::kNone) { - NEARBY_LOG(WARNING, - "Cannot start accepting WebRTC connections, current role %d", - role_); + if (self_id_.GetId() == self_id.GetId()) { + NEARBY_LOG(WARNING, "Already accepting WebRTC connections."); return false; } @@ -96,6 +91,8 @@ bool WebRtc::StartAcceptingConnections(const PeerId& self_id, // the actual transport can begin. ListenForWebRtcSocketFuture(connection_flow_->GetDataChannel(), std::move(callback)); + latest_service_id_ = service_id; + latest_local_endpoint_id_ = local_endpoint_id; NEARBY_LOG(INFO, "Started listening for WebRtc connections as %s", self_id.GetId().c_str()); } @@ -169,6 +166,19 @@ void WebRtc::StopAcceptingConnections() { NEARBY_LOG(INFO, "Stopped accepting WebRTC connections"); } +void WebRtc::StopAcceptingConnection(const std::string& service_id, + const std::string& local_endpoint_id) { + MutexLock lock(&mutex_); + if (service_id == latest_service_id_ && + local_endpoint_id == latest_local_endpoint_id_) { + StopAcceptingConnections(); + } else { + NEARBY_LOG(INFO, + "Skipped StopAcceptingConnection since we are not the latest" + "ongoing connection."); + } +} + Future WebRtc::ListenForWebRtcSocketFuture( Future> data_channel_future, @@ -256,8 +266,7 @@ bool WebRtc::InitWebRtcFlow(Role role, const PeerId& self_id, connection_flow_ = ConnectionFlow::Create(GetLocalIceCandidateListener(), GetDataChannelListener(), medium_); - if (!connection_flow_) - return false; + if (!connection_flow_) return false; return true; } diff --git a/cpp/core/internal/mediums/webrtc.h b/cpp/core/internal/mediums/webrtc.h index b2831fae..43d772d8 100644 --- a/cpp/core/internal/mediums/webrtc.h +++ b/cpp/core/internal/mediums/webrtc.h @@ -23,6 +23,7 @@ #include "platform/public/single_thread_executor.h" #include "platform/public/webrtc.h" #include "location/nearby/mediums/proto/web_rtc_signaling_frames.pb.h" +#include "absl/container/flat_hash_set.h" #include "webrtc/api/data_channel_interface.h" #include "webrtc/api/jsep.h" #include "webrtc/api/scoped_refptr.h" @@ -56,10 +57,18 @@ class WebRtc { // Runs on @MainThread. bool IsAcceptingConnections() ABSL_LOCKS_EXCLUDED(mutex_); + // Returns if the device is accepting connection with specific service id and + // local endpoint id. Runs on @MainThread. + bool IsAcceptingConnection(const std::string& service_id, + const std::string& local_endpoint_id) + ABSL_LOCKS_EXCLUDED(mutex_); + // Prepares the device to accept incoming WebRtc connections. Returns a // boolean value indicating if the device has started accepting connections. // Runs on @MainThread. bool StartAcceptingConnections(const PeerId& self_id, + const std::string& service_id, + const std::string& local_endpoint_id, const LocationHint& location_hint, AcceptedConnectionCallback callback) ABSL_LOCKS_EXCLUDED(mutex_); @@ -69,6 +78,13 @@ class WebRtc { // Runs on @MainThread. void StopAcceptingConnections() ABSL_LOCKS_EXCLUDED(mutex_); + // Try to stop (accepting) the specific connection with provided service id + // and local endpoint id. If the specific connection is not the latest one, + // then nothing will happen; if it's the latest one, + void StopAcceptingConnection(const std::string& service_id, + const std::string& local_endpoint_id) + ABSL_LOCKS_EXCLUDED(mutex_); + // Initiates a WebRtc connection with peer device identified by |peer_id|. // Runs on @MainThread. WebRtcSocketWrapper Connect(const PeerId& peer_id, @@ -168,6 +184,9 @@ class WebRtc { // Restarts the signaling messenger for receiving messages. ScheduledExecutor restart_receive_messages_executor_; CancelableAlarm restart_receive_messages_alarm_; + + std::string latest_service_id_ ABSL_GUARDED_BY(mutex_); + std::string latest_local_endpoint_id_ ABSL_GUARDED_BY(mutex_); }; } // namespace mediums diff --git a/cpp/core/internal/p2p_cluster_pcp_handler.cc b/cpp/core/internal/p2p_cluster_pcp_handler.cc index ba022bd3..780940d3 100644 --- a/cpp/core/internal/p2p_cluster_pcp_handler.cc +++ b/cpp/core/internal/p2p_cluster_pcp_handler.cc @@ -142,10 +142,6 @@ BasePcpHandler::StartOperationResult P2pClusterPcpHandler::StartAdvertisingImpl( }; } -// StopAcceptingConnections invokes for webrtc is suppressed for now to -// unblock CrOS dogfood integration. Disconnect will invoke ShutdownSignaling -// to release resources. -// TODO (hais): add corresponding logic back (b/172518506). Status P2pClusterPcpHandler::StopAdvertisingImpl(ClientProxy* client) { bluetooth_medium_.TurnOffDiscoverability(); bluetooth_medium_.StopAcceptingConnections(client->GetAdvertisingServiceId()); @@ -153,6 +149,9 @@ Status P2pClusterPcpHandler::StopAdvertisingImpl(ClientProxy* client) { ble_medium_.StopAdvertising(client->GetAdvertisingServiceId()); ble_medium_.StopAcceptingConnections(client->GetAdvertisingServiceId()); + webrtc_medium_.StopAcceptingConnection(client->GetAdvertisingServiceId(), + client->GetLocalEndpointId()); + wifi_lan_medium_.StopAdvertising(client->GetAdvertisingServiceId()); wifi_lan_medium_.StopAcceptingConnections(client->GetAdvertisingServiceId()); @@ -1161,7 +1160,8 @@ P2pClusterPcpHandler::StartListeningForWebRtcConnections( service_id, local_endpoint_id, local_endpoint_info); std::string empty_country_code; if (!webrtc_medium_.StartAcceptingConnections( - self_id, Utils::BuildLocationHint(empty_country_code), + self_id, service_id, local_endpoint_id, + Utils::BuildLocationHint(empty_country_code), {[this, client, local_endpoint_info](mediums::WebRtcSocketWrapper socket) { if (!socket.IsValid()) { diff --git a/cpp/core/internal/webrtc_bwu_handler.cc b/cpp/core/internal/webrtc_bwu_handler.cc index 4d217013..d37f8921 100644 --- a/cpp/core/internal/webrtc_bwu_handler.cc +++ b/cpp/core/internal/webrtc_bwu_handler.cc @@ -63,7 +63,8 @@ ByteArray WebrtcBwuHandler::InitializeUpgradedMediumForEndpoint( mediums::PeerId self_id{mediums::PeerId::FromRandom()}; if (!webrtc_.IsAcceptingConnections()) { if (!webrtc_.StartAcceptingConnections( - self_id, location_hint, + self_id, upgrade_service_id, client->GetLocalEndpointId(), + location_hint, { .accepted_cb = absl::bind_front( &WebrtcBwuHandler::OnIncomingWebrtcConnection, this, client, diff --git a/cpp/platform/base/medium_environment.cc b/cpp/platform/base/medium_environment.cc index 068dcd1a..7d70af45 100644 --- a/cpp/platform/base/medium_environment.cc +++ b/cpp/platform/base/medium_environment.cc @@ -290,9 +290,7 @@ void MediumEnvironment::UnregisterBluetoothMedium( RunOnMediumEnvironmentThread([this, &medium]() { auto item = bluetooth_mediums_.extract(&medium); if (item.empty()) return; - auto& context = item.mapped(); - NEARBY_LOG(INFO, "Unregistered medium for device=%s", - context.adapter->GetName().c_str()); + NEARBY_LOGS(INFO) << "Unregistered Bluetooth medium:" << &medium; }); }