mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Use read/write buffer in bluetooth socket
PiperOrigin-RevId: 506815067
This commit is contained in:
committed by
Copybara-Service
parent
c52f1a4000
commit
26973fada5
@@ -14,6 +14,7 @@
|
||||
|
||||
#include "internal/platform/implementation/windows/bluetooth_classic_socket.h"
|
||||
|
||||
#include <cstring>
|
||||
#include <exception>
|
||||
#include <memory>
|
||||
#include <utility>
|
||||
@@ -190,10 +191,18 @@ ExceptionOr<ByteArray> 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();
|
||||
|
||||
@@ -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(
|
||||
|
||||
Reference in New Issue
Block a user