diff --git a/internal/platform/implementation/windows/bluetooth_classic_socket.cc b/internal/platform/implementation/windows/bluetooth_classic_socket.cc index 5bb96f6c..d12401ec 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_socket.cc +++ b/internal/platform/implementation/windows/bluetooth_classic_socket.cc @@ -14,6 +14,7 @@ #include "internal/platform/implementation/windows/bluetooth_classic_socket.h" +#include #include #include #include @@ -190,10 +191,18 @@ ExceptionOr BluetoothSocket::BluetoothInputStream::Read( return {Exception::kIo}; } - Buffer buffer = Buffer(size); + if (size <= 0 || size > kMaxTransmitPacketSize) { + NEARBY_LOGS(ERROR) << __func__ + << ": Invalid transmit packet size: " << size; + return {Exception::kIo}; + } + + // Init the read buffer. + read_buffer_.Length(0); auto ibuffer = - winrt_stream_.ReadAsync(buffer, size, InputStreamOptions::None).get(); + winrt_stream_.ReadAsync(read_buffer_, size, InputStreamOptions::None) + .get(); if (ibuffer.Length() != size) { NEARBY_LOGS(WARNING) << __func__ << ": Got " << ibuffer.Length() @@ -254,11 +263,16 @@ Exception BluetoothSocket::BluetoothOutputStream::Write(const ByteArray& data) { return {Exception::kIo}; } - Buffer buffer = Buffer(data.size()); - std::memcpy(buffer.data(), data.data(), data.size()); - buffer.Length(data.size()); + if (data.size() > kMaxTransmitPacketSize) { + NEARBY_LOGS(ERROR) << __func__ << ": Transmit packet size " + << data.size() << " is too big."; + return {Exception::kIo}; + } - winrt::hresult hresult = winrt_stream_.WriteAsync(buffer).get(); + std::memcpy(write_buffer_.data(), data.data(), data.size()); + write_buffer_.Length(data.size()); + + winrt::hresult hresult = winrt_stream_.WriteAsync(write_buffer_).get(); return {Exception::kSuccess}; } catch (std::exception exception) { NEARBY_LOGS(ERROR) << __func__ << ": Exception: " << exception.what(); diff --git a/internal/platform/implementation/windows/bluetooth_classic_socket.h b/internal/platform/implementation/windows/bluetooth_classic_socket.h index 731a7afc..3259d441 100644 --- a/internal/platform/implementation/windows/bluetooth_classic_socket.h +++ b/internal/platform/implementation/windows/bluetooth_classic_socket.h @@ -22,6 +22,7 @@ #include "internal/platform/implementation/windows/generated/winrt/Windows.Foundation.h" #include "internal/platform/implementation/windows/generated/winrt/Windows.Networking.Sockets.h" #include "internal/platform/implementation/windows/generated/winrt/Windows.Storage.Streams.h" +#include "internal/platform/implementation/windows/generated/winrt/impl/Windows.Storage.Streams.0.h" namespace nearby { namespace windows { @@ -101,6 +102,8 @@ class BluetoothSocket : public api::BluetoothSocket { IAsyncAction CancelIOAsync(); private: + static constexpr int kMaxTransmitPacketSize = 4096; + class BluetoothInputStream : public InputStream { public: explicit BluetoothInputStream(IInputStream stream); @@ -111,6 +114,7 @@ class BluetoothSocket : public api::BluetoothSocket { private: IInputStream winrt_stream_; + Buffer read_buffer_{kMaxTransmitPacketSize}; }; class BluetoothOutputStream : public OutputStream { @@ -125,6 +129,7 @@ class BluetoothSocket : public api::BluetoothSocket { private: IOutputStream winrt_stream_; + Buffer write_buffer_{kMaxTransmitPacketSize}; }; winrt::fire_and_forget Listener_ConnectionStatusChanged(