From 38a2f6a1fcbd2e03ebc0aa322ac8ee5c4a317d46 Mon Sep 17 00:00:00 2001 From: hai007 Date: Fri, 6 Aug 2021 15:37:02 -0700 Subject: [PATCH] Initial commit of the CancellationFlag implementation PiperOrigin-RevId: 389276621 --- .../impl/windows/bluetooth_classic_medium.cc | 14 +++++++++++--- .../impl/windows/bluetooth_classic_socket.cc | 6 ++++++ .../impl/windows/bluetooth_classic_socket.h | 10 ++++++++-- 3 files changed, 25 insertions(+), 5 deletions(-) diff --git a/cpp/platform/impl/windows/bluetooth_classic_medium.cc b/cpp/platform/impl/windows/bluetooth_classic_medium.cc index 41d0d29c..179ad101 100644 --- a/cpp/platform/impl/windows/bluetooth_classic_medium.cc +++ b/cpp/platform/impl/windows/bluetooth_classic_medium.cc @@ -14,15 +14,18 @@ #include "platform/impl/windows/bluetooth_classic_medium.h" -#include "platform/base/exception.h" -#include "platform/impl/windows/bluetooth_classic_device.h" -#include "platform/impl/windows/bluetooth_classic_socket.h" #include "platform/impl/windows/generated/winrt/Windows.Devices.Bluetooth.Rfcomm.h" #include "platform/impl/windows/generated/winrt/Windows.Devices.Bluetooth.h" #include "platform/impl/windows/generated/winrt/Windows.Devices.Enumeration.h" #include "platform/impl/windows/generated/winrt/Windows.Foundation.Collections.h" #include "platform/impl/windows/generated/winrt/base.h" + +#include "platform/base/exception.h" +#include "platform/impl/windows/bluetooth_classic_device.h" +#include "platform/impl/windows/bluetooth_classic_socket.h" #include "platform/public/logging.h" +#include "platform/base/cancellation_flag.h" +#include "platform/base/cancellation_flag_listener.h" namespace location { namespace nearby { @@ -136,6 +139,11 @@ std::unique_ptr BluetoothClassicMedium::ConnectToService( std::unique_ptr rfcommSocket = std::make_unique(); + location::nearby::CancellationFlagListener cancellationFlagListener( + cancellation_flag, + [&rfcommSocket]() { rfcommSocket.get()->CancelIOAsync().get(); + }); + try { rfcommSocket ->ConnectAsync(requestedService.ConnectionHostName(), diff --git a/cpp/platform/impl/windows/bluetooth_classic_socket.cc b/cpp/platform/impl/windows/bluetooth_classic_socket.cc index 2a4613bf..2c26d223 100644 --- a/cpp/platform/impl/windows/bluetooth_classic_socket.cc +++ b/cpp/platform/impl/windows/bluetooth_classic_socket.cc @@ -82,6 +82,12 @@ ExceptionOr BluetoothSocket::BluetoothInputStream::Read( return ExceptionOr(data); } +IAsyncAction BluetoothSocket::CancelIOAsync() { + // Cancels pending reads and writes over a StreamSocket object. + // https://docs.microsoft.com/en-us/uwp/api/windows.networking.sockets.streamsocket.cancelioasync?view=winrt-20348 + return windows_socket_.as().CancelIOAsync(); +} + Exception BluetoothSocket::BluetoothInputStream::Close() { try { winrt_stream_.Close(); diff --git a/cpp/platform/impl/windows/bluetooth_classic_socket.h b/cpp/platform/impl/windows/bluetooth_classic_socket.h index 34421153..ab9b3ff6 100644 --- a/cpp/platform/impl/windows/bluetooth_classic_socket.h +++ b/cpp/platform/impl/windows/bluetooth_classic_socket.h @@ -56,6 +56,10 @@ using winrt::Windows::Storage::Streams::InputStreamOptions; // https://docs.microsoft.com/en-us/uwp/api/windows.storage.streams.datareader?view=winrt-20348 using winrt::Windows::Storage::Streams::DataReader; +// Represents an asynchronous action. +// https://docs.microsoft.com/en-us/uwp/api/windows.foundation.iasyncaction?view=winrt-20348 +using winrt::Windows::Foundation::IAsyncAction; + // https://developer.android.com/reference/android/bluetooth/BluetoothSocket.html. class BluetoothSocket : public api::BluetoothSocket { public: @@ -88,8 +92,10 @@ class BluetoothSocket : public api::BluetoothSocket { api::BluetoothDevice* GetRemoteDevice() override; // Connect asynchronously to the target remote device - winrt::Windows::Foundation::IAsyncAction ConnectAsync( - HostName connectionHostName, winrt::hstring connectionServiceName); + IAsyncAction ConnectAsync(HostName connectionHostName, + winrt::hstring connectionServiceName); + + IAsyncAction CancelIOAsync(); private: class BluetoothInputStream : public InputStream {