input and output stream implementation

PiperOrigin-RevId: 389229126
This commit is contained in:
hai007
2021-08-06 12:06:31 -07:00
committed by Copybara-Service
parent f483110a8d
commit 434a50d3e2
2 changed files with 127 additions and 30 deletions
@@ -15,12 +15,11 @@
#ifndef PLATFORM_IMPL_WINDOWS_BLUETOOTH_CLASSIC_SOCKET_H_
#define PLATFORM_IMPL_WINDOWS_BLUETOOTH_CLASSIC_SOCKET_H_
#include "platform/api/bluetooth_classic.h"
#include "platform/impl/windows/generated/winrt/Windows.Foundation.h"
#include "platform/impl/windows/generated/winrt/Windows.Networking.Sockets.h"
#include "platform/impl/windows/generated/winrt/Windows.Storage.Streams.h"
#include "platform/api/bluetooth_classic.h"
namespace location {
namespace nearby {
namespace windows {
@@ -31,12 +30,37 @@ using winrt::Windows::Networking::HostName;
// Supports network communication using a stream socket over Bluetooth RFCOMM.
// https://docs.microsoft.com/en-us/uwp/api/windows.networking.sockets.streamsocket?view=winrt-20348
using winrt::Windows::Networking::Sockets::IStreamSocket;
using winrt::Windows::Networking::Sockets::StreamSocket;
// Provides a default implementation of the IBuffer interface and its related
// interfaces.
// https://docs.microsoft.com/en-us/uwp/api/windows.storage.streams.buffer?view=winrt-20348
using winrt::Windows::Storage::Streams::Buffer;
// Represents a sequential stream of bytes to be read.
// https://docs.microsoft.com/en-us/uwp/api/windows.storage.streams.iinputstream?view=winrt-20348
using winrt::Windows::Storage::Streams::IInputStream;
// Represents a sequential stream of bytes to be written.
// https://docs.microsoft.com/en-us/uwp/api/windows.storage.streams.ioutputstream?view=winrt-20348
using winrt::Windows::Storage::Streams::IOutputStream;
// Specifies the read options for an input stream.
// This enumeration has a FlagsAttribute attribute that allows a bitwise
// combination of its member values.
// https://docs.microsoft.com/en-us/uwp/api/windows.storage.streams.inputstreamoptions?view=winrt-20348
using winrt::Windows::Storage::Streams::InputStreamOptions;
// Reads data from an input stream.
// https://docs.microsoft.com/en-us/uwp/api/windows.storage.streams.datareader?view=winrt-20348
using winrt::Windows::Storage::Streams::DataReader;
// https://developer.android.com/reference/android/bluetooth/BluetoothSocket.html.
class BluetoothSocket : public api::BluetoothSocket {
public:
BluetoothSocket();
// TODO(b/184975123): replace with real implementation.
~BluetoothSocket() override;
@@ -47,11 +71,9 @@ class BluetoothSocket : public api::BluetoothSocket {
// BluetoothServerSocket::Accept() for server side of connection.
// Returns the InputStream of this connected BluetoothSocket.
// TODO(b/184975123): replace with real implementation.
InputStream& GetInputStream() override;
// Returns the OutputStream of this connected BluetoothSocket.
// TODO(b/184975123): replace with real implementation.
OutputStream& GetOutputStream() override;
// Closes both input and output streams, marks Socket as closed.
@@ -63,7 +85,6 @@ class BluetoothSocket : public api::BluetoothSocket {
// https://developer.android.com/reference/android/bluetooth/BluetoothSocket.html#getRemoteDevice()
// Returns valid BluetoothDevice pointer if there is a connection, and
// nullptr otherwise.
// TODO(b/184975123): replace with real implementation.
api::BluetoothDevice* GetRemoteDevice() override;
// Connect asynchronously to the target remote device
@@ -71,28 +92,36 @@ class BluetoothSocket : public api::BluetoothSocket {
HostName connectionHostName, winrt::hstring connectionServiceName);
private:
class FakeInputStream : public InputStream {
class BluetoothInputStream : public InputStream {
public:
~FakeInputStream() override = default;
ExceptionOr<ByteArray> Read(std::int64_t size) override {
return ExceptionOr<ByteArray>(Exception::kFailed);
}
Exception Close() override { return {.value = Exception::kFailed}; }
BluetoothInputStream(IInputStream stream);
~BluetoothInputStream() override = default;
ExceptionOr<ByteArray> Read(std::int64_t size) override;
Exception Close() override;
private:
IInputStream winrt_stream_;
};
class FakeOutputStream : public OutputStream {
class BluetoothOutputStream : public OutputStream {
public:
~FakeOutputStream() override = default;
BluetoothOutputStream(IOutputStream stream);
~BluetoothOutputStream() override = default;
Exception Write(const ByteArray& data) override {
return {.value = Exception::kFailed};
}
Exception Flush() override { return {.value = Exception::kFailed}; }
Exception Close() override { return {.value = Exception::kFailed}; }
Exception Write(const ByteArray& data) override;
Exception Flush() override;
Exception Close() override;
private:
IOutputStream winrt_stream_;
};
FakeInputStream fake_input_stream_;
FakeOutputStream fake_output_stream_;
StreamSocket windows_socket_;
std::unique_ptr<BluetoothInputStream> input_stream_;
std::unique_ptr<BluetoothOutputStream> output_stream_;
IStreamSocket windows_socket_;
};
} // namespace windows