From f2ef072ac1c090dac24fc02230abfe28a2a25aa8 Mon Sep 17 00:00:00 2001 From: Guogang Li Date: Thu, 8 May 2025 11:59:34 -0700 Subject: [PATCH] Avoid checking peer when getting streams from socket PiperOrigin-RevId: 756403871 --- .../implementation/ble_endpoint_channel.cc | 17 ++++++++++++----- .../ble_l2cap_endpoint_channel.cc | 5 ++--- .../implementation/ble_v2_endpoint_channel.cc | 13 ++++++++----- .../bluetooth_endpoint_channel.cc | 18 +++++++++++++----- 4 files changed, 35 insertions(+), 18 deletions(-) diff --git a/connections/implementation/ble_endpoint_channel.cc b/connections/implementation/ble_endpoint_channel.cc index 0578bb19..fb788c77 100644 --- a/connections/implementation/ble_endpoint_channel.cc +++ b/connections/implementation/ble_endpoint_channel.cc @@ -15,9 +15,13 @@ #include "connections/implementation/ble_endpoint_channel.h" #include +#include +#include "connections/implementation/base_endpoint_channel.h" #include "internal/platform/ble.h" +#include "internal/platform/input_stream.h" #include "internal/platform/logging.h" +#include "internal/platform/output_stream.h" namespace nearby { namespace connections { @@ -25,12 +29,16 @@ namespace connections { namespace { OutputStream* GetOutputStreamOrNull(BleSocket& socket) { - if (socket.GetRemotePeripheral().IsValid()) return &socket.GetOutputStream(); + if (socket.IsValid()) { + return &socket.GetOutputStream(); + } return nullptr; } InputStream* GetInputStreamOrNull(BleSocket& socket) { - if (socket.GetRemotePeripheral().IsValid()) return &socket.GetInputStream(); + if (socket.IsValid()) { + return &socket.GetInputStream(); + } return nullptr; } @@ -56,9 +64,8 @@ int BleEndpointChannel::GetMaxTransmitPacketSize() const { void BleEndpointChannel::CloseImpl() { auto status = ble_socket_.Close(); if (!status.Ok()) { - NEARBY_LOGS(INFO) - << "Failed to close underlying socket for BleEndpointChannel " - << GetName() << ": exception=" << status.value; + LOG(INFO) << "Failed to close underlying socket for BleEndpointChannel " + << GetName() << ": exception=" << status.value; } } diff --git a/connections/implementation/ble_l2cap_endpoint_channel.cc b/connections/implementation/ble_l2cap_endpoint_channel.cc index b9e09ca0..3a8b220f 100644 --- a/connections/implementation/ble_l2cap_endpoint_channel.cc +++ b/connections/implementation/ble_l2cap_endpoint_channel.cc @@ -32,15 +32,14 @@ namespace { constexpr int kDefaultBleL2capMaxTransmitPacketSize = 1024; // 1024 bytes OutputStream* GetOutputStreamOrNull(BleL2capSocket& socket) { - if (socket.GetRemotePeripheral().IsValid()) { + if (socket.IsValid()) { return &socket.GetOutputStream(); } - LOG(WARNING) << "GetOutputStreamOrNull: socket is not valid"; return nullptr; } InputStream* GetInputStreamOrNull(BleL2capSocket& socket) { - if (socket.GetRemotePeripheral().IsValid()) { + if (socket.IsValid()) { return &socket.GetInputStream(); } return nullptr; diff --git a/connections/implementation/ble_v2_endpoint_channel.cc b/connections/implementation/ble_v2_endpoint_channel.cc index 1145a8af..9a36d761 100644 --- a/connections/implementation/ble_v2_endpoint_channel.cc +++ b/connections/implementation/ble_v2_endpoint_channel.cc @@ -17,8 +17,12 @@ #include #include +#include "connections/implementation/base_endpoint_channel.h" #include "internal/platform/ble_v2.h" +#include "internal/platform/exception.h" +#include "internal/platform/input_stream.h" #include "internal/platform/logging.h" +#include "internal/platform/output_stream.h" namespace nearby { namespace connections { @@ -26,14 +30,14 @@ namespace connections { namespace { OutputStream* GetOutputStreamOrNull(BleV2Socket& socket) { - if (socket.GetRemotePeripheral().IsValid()) { + if (socket.IsValid()) { return &socket.GetOutputStream(); } return nullptr; } InputStream* GetInputStreamOrNull(BleV2Socket& socket) { - if (socket.GetRemotePeripheral().IsValid()) { + if (socket.IsValid()) { return &socket.GetInputStream(); } return nullptr; @@ -61,9 +65,8 @@ int BleV2EndpointChannel::GetMaxTransmitPacketSize() const { void BleV2EndpointChannel::CloseImpl() { Exception status = ble_socket_.Close(); if (!status.Ok()) { - NEARBY_LOGS(WARNING) - << "Failed to close underlying socket for BleEndpointChannel " - << GetName() << ": exception=" << status.value; + LOG(WARNING) << "Failed to close underlying socket for BleEndpointChannel " + << GetName() << ": exception=" << status.value; } } diff --git a/connections/implementation/bluetooth_endpoint_channel.cc b/connections/implementation/bluetooth_endpoint_channel.cc index 2b1aa65e..73d09d9c 100644 --- a/connections/implementation/bluetooth_endpoint_channel.cc +++ b/connections/implementation/bluetooth_endpoint_channel.cc @@ -15,9 +15,13 @@ #include "connections/implementation/bluetooth_endpoint_channel.h" #include +#include +#include "connections/implementation/base_endpoint_channel.h" #include "internal/platform/bluetooth_classic.h" +#include "internal/platform/input_stream.h" #include "internal/platform/logging.h" +#include "internal/platform/output_stream.h" namespace nearby { namespace connections { @@ -25,12 +29,16 @@ namespace connections { namespace { OutputStream* GetOutputStreamOrNull(BluetoothSocket& socket) { - if (socket.GetRemoteDevice().IsValid()) return &socket.GetOutputStream(); + if (socket.IsValid()) { + return &socket.GetOutputStream(); + } return nullptr; } InputStream* GetInputStreamOrNull(BluetoothSocket& socket) { - if (socket.GetRemoteDevice().IsValid()) return &socket.GetInputStream(); + if (socket.IsValid()) { + return &socket.GetInputStream(); + } return nullptr; } @@ -56,15 +64,15 @@ int BluetoothEndpointChannel::GetMaxTransmitPacketSize() const { void BluetoothEndpointChannel::CloseImpl() { auto status = bluetooth_socket_.Close(); if (!status.Ok()) { - NEARBY_LOGS(INFO) + LOG(WARNING) << "Failed to close underlying socket for BluetoothEndpointChannel " << GetName() << ": exception=" << status.value; } } bool BluetoothEndpointChannel::EnableMultiplexSocket() { - NEARBY_LOGS(INFO) << "BluetoothEndpointChannel MultiplexSocket will be " - "enabled if the Bluetooth MultiplexSocket is valid"; + LOG(INFO) << "BluetoothEndpointChannel MultiplexSocket will be " + "enabled if the Bluetooth MultiplexSocket is valid"; bluetooth_socket_.EnableMultiplexSocket(); return true; }