Initial commit of the CancellationFlag implementation

PiperOrigin-RevId: 389276621
This commit is contained in:
hai007
2021-08-06 15:37:44 -07:00
committed by Copybara-Service
parent 434a50d3e2
commit 38a2f6a1fc
3 changed files with 25 additions and 5 deletions
@@ -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<api::BluetoothSocket> BluetoothClassicMedium::ConnectToService(
std::unique_ptr<BluetoothSocket> rfcommSocket =
std::make_unique<BluetoothSocket>();
location::nearby::CancellationFlagListener cancellationFlagListener(
cancellation_flag,
[&rfcommSocket]() { rfcommSocket.get()->CancelIOAsync().get();
});
try {
rfcommSocket
->ConnectAsync(requestedService.ConnectionHostName(),
@@ -82,6 +82,12 @@ ExceptionOr<ByteArray> 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<StreamSocket>().CancelIOAsync();
}
Exception BluetoothSocket::BluetoothInputStream::Close() {
try {
winrt_stream_.Close();
@@ -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 {