From 353f7d5a1d34bd6c2af638709a780eae25f6ee1a Mon Sep 17 00:00:00 2001 From: Francis Tsui Date: Wed, 6 May 2026 13:33:27 -0700 Subject: [PATCH] Remove dependency on multiplex sockets. PiperOrigin-RevId: 911520556 --- connections/implementation/mediums/BUILD | 1 - connections/implementation/mediums/awdl.cc | 8 -- connections/implementation/mediums/awdl.h | 4 - .../mediums/bluetooth_classic.cc | 116 +-------------- .../mediums/bluetooth_classic.h | 8 -- .../implementation/mediums/multiplex/BUILD | 1 + .../implementation/mediums/wifi_lan.cc | 134 +----------------- connections/implementation/mediums/wifi_lan.h | 8 -- 8 files changed, 3 insertions(+), 277 deletions(-) diff --git a/connections/implementation/mediums/BUILD b/connections/implementation/mediums/BUILD index beca2b4e..df6b27fe 100644 --- a/connections/implementation/mediums/BUILD +++ b/connections/implementation/mediums/BUILD @@ -58,7 +58,6 @@ cc_library( "//connections/implementation/mediums/ble:ble_advertisement_header", "//connections/implementation/mediums/ble:ble_socket", "//connections/implementation/mediums/ble:bloom_filter", - "//connections/implementation/mediums/multiplex", "//connections/implementation/mediums/webrtc", "//connections/implementation/proto:offline_wire_formats_cc_proto", "//internal/flags:nearby_flags", diff --git a/connections/implementation/mediums/awdl.cc b/connections/implementation/mediums/awdl.cc index cf380fcb..81698ab3 100644 --- a/connections/implementation/mediums/awdl.cc +++ b/connections/implementation/mediums/awdl.cc @@ -15,7 +15,6 @@ #include "connections/implementation/mediums/awdl.h" #include -#include #include #include #include @@ -23,28 +22,21 @@ #include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" #include "absl/strings/string_view.h" -#include "connections/implementation/mediums/multiplex/multiplex_socket.h" #include "connections/implementation/mediums/utils.h" -#include "connections/medium_selector.h" #include "internal/platform/awdl.h" -#include "internal/platform/base64_utils.h" #include "internal/platform/byte_array.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/exception.h" #include "internal/platform/expected.h" #include "internal/platform/implementation/psk_info.h" -#include "internal/platform/implementation/wifi_utils.h" #include "internal/platform/logging.h" #include "internal/platform/mutex_lock.h" #include "internal/platform/nsd_service_info.h" -#include "internal/platform/socket.h" -#include "internal/platform/types.h" namespace nearby { namespace connections { namespace { -using MultiplexSocket = mediums::multiplex::MultiplexSocket; using location::nearby::proto::connections::OperationResultCode; constexpr absl::string_view kAwdlServiceIdSuffixForServiceType = "_AWDL"; diff --git a/connections/implementation/mediums/awdl.h b/connections/implementation/mediums/awdl.h index b2a620d5..c6336946 100644 --- a/connections/implementation/mediums/awdl.h +++ b/connections/implementation/mediums/awdl.h @@ -24,12 +24,8 @@ #include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_set.h" #include "absl/functional/any_invocable.h" -#include "connections/implementation/flags/nearby_connections_feature_flags.h" -#include "connections/implementation/mediums/multiplex/multiplex_socket.h" -#include "internal/flags/nearby_flags.h" #include "internal/platform/awdl.h" #include "internal/platform/cancellation_flag.h" -#include "internal/platform/exception.h" #include "internal/platform/expected.h" #include "internal/platform/implementation/psk_info.h" #include "internal/platform/multi_thread_executor.h" diff --git a/connections/implementation/mediums/bluetooth_classic.cc b/connections/implementation/mediums/bluetooth_classic.cc index 290defb5..2a971d8a 100644 --- a/connections/implementation/mediums/bluetooth_classic.cc +++ b/connections/implementation/mediums/bluetooth_classic.cc @@ -18,11 +18,7 @@ #include #include -#include "connections/implementation/flags/nearby_connections_feature_flags.h" #include "connections/implementation/mediums/bluetooth_radio.h" -#include "connections/implementation/mediums/multiplex/multiplex_socket.h" -#include "connections/medium_selector.h" -#include "internal/flags/nearby_flags.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/bluetooth_classic.h" #include "internal/platform/cancellation_flag.h" @@ -30,8 +26,6 @@ #include "internal/platform/logging.h" #include "internal/platform/mac_address.h" #include "internal/platform/mutex_lock.h" -#include "internal/platform/socket.h" -#include "internal/platform/types.h" #include "internal/platform/uuid.h" namespace nearby { @@ -54,8 +48,6 @@ std::string ScanModeToString(BluetoothAdapter::ScanMode mode) { } } // namespace -using MultiplexSocket = mediums::multiplex::MultiplexSocket; - BluetoothClassic::BluetoothClassic(BluetoothRadio& radio) : BluetoothClassic(radio, std::make_unique( radio.GetBluetoothAdapter())) {} @@ -74,20 +66,6 @@ BluetoothClassic::~BluetoothClassic() { } TurnOffDiscoverability(); - { - MutexLock lock(&mutex_); - LOG(INFO) << "Closing multiplex sockets for " << multiplex_sockets_.size() - << " devices"; - if (is_multiplex_enabled_) { - for (auto& [bt_mac, multiplex_socket] : multiplex_sockets_) { - LOG(INFO) << "Closing multiplex sockets for " - << GetRemoteDevice(bt_mac).GetName(); - multiplex_socket->Shutdown(); - } - } - multiplex_sockets_.clear(); - } - // All the AcceptLoopRunnable objects in here should already have gotten an // opportunity to shut themselves down cleanly in the calls to // StopAcceptingConnections() above. @@ -376,24 +354,13 @@ ErrorOr BluetoothClassic::StartAcceptingConnections( auto owned_socket = server_sockets_.emplace(service_id, std::move(socket)).first->second; - if (is_multiplex_enabled_) { - MultiplexSocket::ListenForIncomingConnection( - service_id, Medium::BLUETOOTH, - [&callback](const std::string& listening_service_id, - std::shared_ptr virtual_socket) mutable { - if (callback) { - callback(listening_service_id, - *(down_cast(virtual_socket.get()))); - } - }); - } // Start the accept loop on a dedicated thread - this stays alive and // listening for new incoming connections until StopAcceptingConnections() // is invoked. accept_loops_runner_.Execute("bt-accept", [callback = std::move(callback), server_socket = std::move(owned_socket), - service_id, this]() mutable { + service_id]() mutable { while (true) { BluetoothSocket client_socket = server_socket.Accept(); if (!client_socket.IsValid()) { @@ -403,35 +370,6 @@ ErrorOr BluetoothClassic::StartAcceptingConnections( } LOG(INFO) << "Accepted connection for " << service_id; bool callback_called = false; - { - MutexLock lock(&mutex_); - if (is_multiplex_enabled_) { - BluetoothSocket client_socket_bak = client_socket; - auto physical_socket_ptr = - std::make_shared(client_socket_bak); - MultiplexSocket* multiplex_socket = - MultiplexSocket::CreateIncomingSocket(physical_socket_ptr, - service_id, 0); - - if (multiplex_socket != nullptr) { - if (auto virtual_socket = - multiplex_socket->GetVirtualSocket(service_id)) { - multiplex_sockets_.emplace( - client_socket.GetRemoteDevice().GetAddress(), - multiplex_socket); - MultiplexSocket::StopListeningForIncomingConnection( - service_id, Medium::BLUETOOTH); - LOG(INFO) << "Multiplex virtaul socket created for " - << client_socket.GetRemoteDevice().GetName(); - if (callback) { - callback(service_id, *(down_cast( - virtual_socket.get()))); - callback_called = true; - } - } - } - } - } if (callback && !callback_called) { LOG(INFO) << "Call back triggered for physical socket."; callback(service_id, std::move(client_socket)); @@ -468,10 +406,6 @@ bool BluetoothClassic::StopAcceptingConnections(const std::string& service_id) { << " because it was never started."; return false; } - if (is_multiplex_enabled_) { - MultiplexSocket::StopListeningForIncomingConnection(service_id, - Medium::BLUETOOTH); - } // Closing the BluetoothServerSocket will kick off the suicide of the thread // in accept_loops_thread_pool_ that blocks on @@ -500,30 +434,6 @@ bool BluetoothClassic::StopAcceptingConnections(const std::string& service_id) { ErrorOr BluetoothClassic::Connect( BluetoothDevice& bluetooth_device, const std::string& service_id, CancellationFlag* cancellation_flag) { - { - MutexLock lock(&mutex_); - if (is_multiplex_enabled_) { - LOG(INFO) << "multiplex_sockets_ size:" << multiplex_sockets_.size(); - auto it = multiplex_sockets_.find(bluetooth_device.GetAddress()); - if (it != multiplex_sockets_.end()) { - MultiplexSocket* multiplex_socket = it->second; - if (multiplex_socket->IsEnabled()) { - std::shared_ptr virtual_socket = - multiplex_socket->EstablishVirtualSocket(service_id); - // Should not happen. - auto* bluetooth_socket = - down_cast(virtual_socket.get()); - if (bluetooth_socket == nullptr) { - LOG(INFO) << "Failed to cast to BluetoothSocket for " << service_id - << " with " << bluetooth_device.GetName(); - return {Error(OperationResultCode:: - NEARBY_BT_VIRTUAL_SOCKET_CREATION_FAILURE)}; - } - return *bluetooth_socket; - } - } - } - } service_id_to_connect_attempts_count_map_[service_id] = 1; while (service_id_to_connect_attempts_count_map_[service_id] <= kConnectAttemptsLimit) { @@ -599,30 +509,6 @@ ErrorOr BluetoothClassic::AttemptToConnect( return {Error( OperationResultCode::CONNECTIVITY_BT_CLIENT_SOCKET_CREATION_FAILURE)}; } - - if (is_multiplex_enabled_) { - // New MultiplexSocket but default disabled, should be enabled after - // negotiated - auto physical_socket_ptr = std::make_shared(socket); - MultiplexSocket* multiplex_socket = MultiplexSocket::CreateOutgoingSocket( - std::move(physical_socket_ptr), service_id); - - std::shared_ptr virtual_socket = - multiplex_socket->GetVirtualSocket(service_id); - - auto* bluetooth_socket = down_cast(virtual_socket.get()); - if (bluetooth_socket == nullptr) { - LOG(INFO) << "Failed to cast to BluetoothSocket for " << service_id - << " with " << bluetooth_device.GetName(); - return {Error( - OperationResultCode::NEARBY_BT_VIRTUAL_SOCKET_CREATION_FAILURE)}; - } - LOG(INFO) << "Multiplex socket created for " << bluetooth_device.GetName(); - multiplex_sockets_.emplace(bluetooth_device.GetAddress(), - multiplex_socket); - return *bluetooth_socket; - } - return socket; } diff --git a/connections/implementation/mediums/bluetooth_classic.h b/connections/implementation/mediums/bluetooth_classic.h index 984112da..37bf8bd3 100644 --- a/connections/implementation/mediums/bluetooth_classic.h +++ b/connections/implementation/mediums/bluetooth_classic.h @@ -23,7 +23,6 @@ #include "absl/container/flat_hash_map.h" #include "absl/functional/any_invocable.h" #include "connections/implementation/mediums/bluetooth_radio.h" -#include "connections/implementation/mediums/multiplex/multiplex_socket.h" #include "internal/platform/bluetooth_adapter.h" #include "internal/platform/bluetooth_classic.h" #include "internal/platform/cancellation_flag.h" @@ -232,13 +231,6 @@ class BluetoothClassic { mutable Mutex discovery_callbacks_mutex_; absl::flat_hash_map discovery_callbacks_ ABSL_GUARDED_BY(discovery_callbacks_mutex_); - - // Whether the multiplex feature is enabled. - bool is_multiplex_enabled_ = false; - - // A map of Bluetooth MacAddress -> MultiplexSocket. - absl::flat_hash_map - multiplex_sockets_ ABSL_GUARDED_BY(mutex_); }; } // namespace connections diff --git a/connections/implementation/mediums/multiplex/BUILD b/connections/implementation/mediums/multiplex/BUILD index fc1a59ff..63c05094 100644 --- a/connections/implementation/mediums/multiplex/BUILD +++ b/connections/implementation/mediums/multiplex/BUILD @@ -54,6 +54,7 @@ cc_test( "multiplex_output_stream_test.cc", "multiplex_socket_test.cc", ], + tags = ["notap"], deps = [ ":multiplex", "//connections/implementation:internal", diff --git a/connections/implementation/mediums/wifi_lan.cc b/connections/implementation/mediums/wifi_lan.cc index 9f30ea5d..51ae771a 100644 --- a/connections/implementation/mediums/wifi_lan.cc +++ b/connections/implementation/mediums/wifi_lan.cc @@ -15,36 +15,28 @@ #include "connections/implementation/mediums/wifi_lan.h" #include -#include #include #include #include #include "absl/strings/str_cat.h" #include "absl/strings/str_format.h" -#include "connections/implementation/mediums/multiplex/multiplex_socket.h" #include "connections/implementation/mediums/utils.h" -#include "connections/medium_selector.h" -#include "internal/platform/base64_utils.h" #include "internal/platform/byte_array.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/exception.h" #include "internal/platform/expected.h" #include "internal/platform/implementation/upgrade_address_info.h" -#include "internal/platform/implementation/wifi_utils.h" #include "internal/platform/logging.h" #include "internal/platform/mutex_lock.h" #include "internal/platform/nsd_service_info.h" #include "internal/platform/service_address.h" -#include "internal/platform/socket.h" -#include "internal/platform/types.h" #include "internal/platform/wifi_lan.h" namespace nearby { namespace connections { namespace { -using MultiplexSocket = mediums::multiplex::MultiplexSocket; using location::nearby::proto::connections::OperationResultCode; } // namespace @@ -59,18 +51,6 @@ WifiLan::~WifiLan() { while (!advertising_info_.nsd_service_infos.empty()) { StopAdvertising(advertising_info_.nsd_service_infos.begin()->first); } - { - MutexLock lock(&mutex_); - if (is_multiplex_enabled_) { - LOG(INFO) << "Closing multiplex sockets for " << multiplex_sockets_.size() - << " IPs"; - for (auto& [ip_addr, multiplex_socket] : multiplex_sockets_) { - LOG(INFO) << "Closing multiplex sockets for: " << ip_addr; - multiplex_socket->~MultiplexSocket(); - } - multiplex_sockets_.clear(); - } - } // All the AcceptLoopRunnable objects in here should already have gotten an // opportunity to shut themselves down cleanly in the calls to // StopAcceptingConnections() above. @@ -265,18 +245,6 @@ ErrorOr WifiLan::StartAcceptingConnectionsLocked( server_sockets_.insert({service_id, std::move(server_socket)}) .first->second; - // Register the callback to listen for incoming multiplex virtual socket. - if (is_multiplex_enabled_) { - MultiplexSocket::ListenForIncomingConnection( - service_id, Medium::WIFI_LAN, - [&callback](const std::string& listening_service_id, - std::shared_ptr virtual_socket) mutable { - if (callback) { - callback(listening_service_id, - *(down_cast(virtual_socket.get()))); - } - }); - } port = owned_server_socket.GetPort(); // Start the accept loop on a dedicated thread - this stays alive and // listening for new incoming connections until StopAcceptingConnections() is @@ -284,7 +252,7 @@ ErrorOr WifiLan::StartAcceptingConnectionsLocked( accept_loops_runner_.Execute( "wifi-lan-accept", [callback = std::move(callback), server_socket = std::move(owned_server_socket), - service_id, this]() mutable { + service_id]() mutable { while (true) { WifiLanSocket client_socket = server_socket.Accept(); if (!client_socket.IsValid()) { @@ -293,54 +261,6 @@ ErrorOr WifiLan::StartAcceptingConnectionsLocked( } LOG(INFO) << "Accepted connection for " << service_id; bool callback_called = false; - { - MutexLock lock(&mutex_); - if (is_multiplex_enabled_) { - // Observed from the log that when the sender tries to connect to - // the receiver's server socket, the server side will somehow - // receive 3 connection request events(don’t know what’s happening - // in Windows’s lower layer code). The 2nd normally is the real - // one. The other two will result in a failed data receiving in - // Windows platform layer. To avoid creating multiplex - // IncomingSocket, we will check if the first read is successful - // or not. If not, discard it. If yes, save that packet - // content(the first frame length), then create the multiplex - // socket, then feed that content to that multiplex socket. - ExceptionOr read_int = - Base64Utils::ReadInt(&client_socket.GetInputStream()); - if (!read_int.ok()) { - LOG(WARNING) - << __func__ - << "Failed to read. Exception:" << read_int.exception() - << "Discard the connection."; - continue; - } - WifiLanSocket client_socket_bak = client_socket; - auto physical_socket_ptr = - std::make_shared(client_socket_bak); - - MultiplexSocket* multiplex_socket = - MultiplexSocket::CreateIncomingSocket( - physical_socket_ptr, service_id, read_int.result()); - if (multiplex_socket != nullptr) { - std::shared_ptr virtual_socket = - multiplex_socket->GetVirtualSocket(service_id); - if (virtual_socket) { - multiplex_sockets_.emplace(server_socket.GetIPAddress(), - multiplex_socket); - MultiplexSocket::StopListeningForIncomingConnection( - service_id, Medium::WIFI_LAN); - LOG(INFO) << "Multiplex virtaul socket created for " - << server_socket.GetIPAddress(); - if (callback) { - callback(service_id, *(down_cast( - virtual_socket.get()))); - callback_called = true; - } - } - } - } - } if (callback && !callback_called) { LOG(INFO) << "Call back triggered for physical socket."; callback(service_id, std::move(client_socket)); @@ -403,10 +323,6 @@ bool WifiLan::StopAcceptingConnectionsLocked(const std::string& service_id) { << " because it was never started."; return false; } - if (is_multiplex_enabled_) { - MultiplexSocket::StopListeningForIncomingConnection(service_id, - Medium::WIFI_LAN); - } // Closing the WifiLanServerSocket will kick off the suicide of the thread // in accept_loops_thread_pool_ that blocks on WifiLanServerSocket.accept(). @@ -556,60 +472,12 @@ ErrorOr WifiLan::Connect(const std::string& service_id, ExceptionOr WifiLan::ConnectWithMultiplexSocketLocked( const std::string& service_id, const std::string& ip_address) { - if (is_multiplex_enabled_) { - LOG(INFO) << "multiplex_sockets_ size:" << multiplex_sockets_.size(); - auto it = multiplex_sockets_.find(ip_address); - if (it != multiplex_sockets_.end()) { - MultiplexSocket* multiplex_socket = it->second; - if (multiplex_socket->IsShutdown()) { - LOG(INFO) << "Erase multiplex_socket(already shutdown) for ip_address: " - << WifiUtils::GetHumanReadableIpAddress(ip_address); - multiplex_socket->~MultiplexSocket(); - multiplex_sockets_.erase(it); - return ExceptionOr(Exception::kFailed); - } - if (multiplex_socket->IsEnabled()) { - std::shared_ptr virtual_socket = - multiplex_socket->EstablishVirtualSocket(service_id); - // Should not happen. - auto* wlan_socket = down_cast(virtual_socket.get()); - if (wlan_socket == nullptr) { - LOG(INFO) << "Failed to cast to WifiLanSocket for " << service_id - << " with ip_address: " - << WifiUtils::GetHumanReadableIpAddress(ip_address); - return ExceptionOr(Exception::kFailed); - } - return ExceptionOr(*wlan_socket); - } - } - } return ExceptionOr(Exception::kFailed); } ExceptionOr WifiLan::CreateOutgoingMultiplexSocketLocked( WifiLanSocket& socket, const std::string& service_id, const std::string& ip_address) { - if (is_multiplex_enabled_) { - // Create MultiplexSocket, but set it to be disabled as default. It will be - // enabled if both side support multiplex for WIFI_LAN - auto physical_socket_ptr = std::make_shared(socket); - MultiplexSocket* multiplex_socket = - MultiplexSocket::CreateOutgoingSocket(physical_socket_ptr, service_id); - - std::shared_ptr virtual_socket = - multiplex_socket->GetVirtualSocket(service_id); - auto* wlan_socket = down_cast(virtual_socket.get()); - if (wlan_socket == nullptr) { - LOG(INFO) << "Failed to cast to WifiLanSocket for " << service_id - << " with ip_address: " - << WifiUtils::GetHumanReadableIpAddress(ip_address); - return ExceptionOr(Exception::kFailed); - } - LOG(INFO) << "Multiplex socket created for ip_address: " - << WifiUtils::GetHumanReadableIpAddress(ip_address); - multiplex_sockets_.emplace(ip_address, multiplex_socket); - return ExceptionOr(*wlan_socket); - } return ExceptionOr(Exception::kFailed); } diff --git a/connections/implementation/mediums/wifi_lan.h b/connections/implementation/mediums/wifi_lan.h index 0307806f..39a5cc6a 100644 --- a/connections/implementation/mediums/wifi_lan.h +++ b/connections/implementation/mediums/wifi_lan.h @@ -23,7 +23,6 @@ #include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_set.h" #include "absl/functional/any_invocable.h" -#include "connections/implementation/mediums/multiplex/multiplex_socket.h" #include "internal/platform/cancellation_flag.h" #include "internal/platform/exception.h" #include "internal/platform/expected.h" @@ -215,13 +214,6 @@ class WifiLan { absl::flat_hash_map server_sockets_ ABSL_GUARDED_BY(mutex_); - // Whether the multiplex feature is enabled. - bool is_multiplex_enabled_ = false; - - // A map of IpAddress -> MultiplexSocket. - absl::flat_hash_map - multiplex_sockets_ ABSL_GUARDED_BY(mutex_); - std::string last_mdns_service_name_ ABSL_GUARDED_BY(mutex_); int last_server_port_ ABSL_GUARDED_BY(mutex_) = 0; };