Switch OutputStream::Write to use string_view.

PiperOrigin-RevId: 854225267
This commit is contained in:
Francis Tsui
2026-01-09 09:37:00 -08:00
committed by Copybara-Service
parent 3d92559201
commit fbf66ad7e3
61 changed files with 227 additions and 198 deletions
+1
View File
@@ -208,6 +208,7 @@ cc_test(
"//internal/platform/implementation/g3", # build_cleaner: keep
"//internal/test",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings:string_view",
"@com_google_googletest//:gtest_main",
],
)
@@ -72,6 +72,7 @@ cc_library(
"@com_google_absl//absl/base:core_headers",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/synchronization",
],
)
@@ -22,6 +22,7 @@
#include "absl/status/statusor.h"
#include "absl/strings/escaping.h"
#include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h"
#include "connections/implementation/flags/nearby_connections_feature_flags.h"
#include "connections/implementation/mediums/ble/ble_l2cap_packet.h"
#include "connections/implementation/mediums/ble/ble_packet.h"
@@ -49,7 +50,7 @@ ExceptionOr<ByteArray> BleInputStream::Read(std::int64_t size) {
Exception BleInputStream::Close() { return source_.Close(); }
Exception BleOutputStream::Write(const ByteArray& data) {
Exception BleOutputStream::Write(absl::string_view data) {
if (NearbyFlags::GetInstance().GetBoolFlag(
config_package_nearby::nearby_connections_feature::
kRefactorBleL2cap)) {
@@ -59,7 +60,7 @@ Exception BleOutputStream::Write(const ByteArray& data) {
// Prepend the packet length to the data.
std::string packet_str =
absl::StrCat(std::string(byte_utils::IntToBytes(payload_length_)),
std::string(data));
data);
payload_length_ = 0;
// Prepend the service id hash to the data with the payload length.
@@ -69,7 +70,8 @@ Exception BleOutputStream::Write(const ByteArray& data) {
if (!ble_packet_status_or.ok()) {
return {Exception::kFailed};
}
return source_.Write(ByteArray(ble_packet_status_or.value()));
return source_.Write(
ByteArray(ble_packet_status_or.value()).AsStringView());
} else {
return source_.Write(data);
}
@@ -80,7 +82,7 @@ Exception BleOutputStream::Flush() { return source_.Flush(); }
Exception BleOutputStream::Close() { return source_.Close(); }
Exception BleOutputStream::WriteControlPacket(const ByteArray& data) {
return source_.Write(data);
return source_.Write(data.AsStringView());
}
Exception BleOutputStream::WritePayloadLength(int payload_length) {
@@ -20,6 +20,7 @@
#include <utility>
#include "absl/base/thread_annotations.h"
#include "absl/strings/string_view.h"
#include "internal/platform/ble.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
@@ -98,11 +99,11 @@ class BleOutputStream : public OutputStream {
* The resulting serialized `BlePacket` is then written to the `source_`
* stream.
*
* @param data The raw `ByteArray` payload to write to the stream.
* @param data The raw `absl::string_view` payload to write to the stream.
* @return `Exception::kSuccess` if the write operation succeeds, or an
* exception code indicating the type of error.
*/
Exception Write(const ByteArray& data) override;
Exception Write(absl::string_view data) override;
Exception Flush() override;
Exception Close() override;
@@ -77,11 +77,11 @@ class FakeInputStream : public InputStream {
class FakeOutputStream : public OutputStream {
public:
Exception Write(const ByteArray& data) override {
Exception Write(absl::string_view data) override {
if (exception_on_write_) {
return {Exception::kIo};
}
absl::StrAppend(&buffer_, std::string(data));
absl::StrAppend(&buffer_, data);
return {Exception::kSuccess};
}
Exception Flush() override { return {Exception::kSuccess}; }
@@ -283,7 +283,7 @@ TEST_F(BleSocketBleMediumTest, WritePayloadLengthSucceedsIfCalledAfterWrite) {
config_package_nearby::nearby_connections_feature::kRefactorBleL2cap,
true);
constexpr int kPayloadLength = 12345;
ByteArray payload("payload");
absl::string_view payload = "payload";
EXPECT_TRUE(socket_->WritePayloadLength(kPayloadLength).Ok());
EXPECT_TRUE(socket_->GetOutputStream().Write(payload).Ok());
EXPECT_TRUE(socket_->WritePayloadLength(kPayloadLength).Ok());
@@ -498,7 +498,7 @@ TEST_F(BleL2capSocketBleMediumTest,
config_package_nearby::nearby_connections_feature::kRefactorBleL2cap,
true);
constexpr int kPayloadLength = 12345;
ByteArray payload("payload");
absl::string_view payload = "payload";
EXPECT_TRUE(socket_->WritePayloadLength(kPayloadLength).Ok());
EXPECT_TRUE(socket_->GetOutputStream().Write(payload).Ok());
EXPECT_TRUE(socket_->WritePayloadLength(kPayloadLength).Ok());
@@ -16,6 +16,11 @@
#define CORE_INTERNAL_MEDIUMS_BLE_BLOOM_FILTER_H_
#include <bitset>
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "internal/platform/byte_array.h"
@@ -17,6 +17,7 @@
#include <string>
#include <utility>
#include "absl/strings/string_view.h"
#include "connections/implementation/mediums/utils.h"
#include "internal/platform/base64_utils.h"
#include "internal/platform/byte_array.h"
@@ -121,7 +122,7 @@ ByteArray ForDisconnection(const std::string& service_id,
ByteArray ForData(const std::string& service_id,
const std::string& service_id_hash_salt,
bool should_pass_salt, const ByteArray& data) {
bool should_pass_salt, absl::string_view data) {
MultiplexFrame frame;
frame.set_frame_type(MultiplexFrame::DATA_FRAME);
@@ -133,7 +134,7 @@ ByteArray ForData(const std::string& service_id,
}
auto* data_frame = frame.mutable_data_frame();
data_frame->set_data(std::string(std::move(data)));
data_frame->set_data(data);
return ToBytes(std::move(frame));
}
@@ -18,6 +18,7 @@
#include <string>
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "proto/mediums/multiplex_frames.pb.h"
@@ -88,7 +89,7 @@ ByteArray ForDisconnection(const std::string& service_id,
// @param data The data to send.
ByteArray ForData(const std::string& service_id,
const std::string& service_id_hash_salt,
bool should_pass_salt, const ByteArray& data);
bool should_pass_salt, absl::string_view data);
ExceptionOr<location::nearby::mediums::MultiplexFrame> FromBytes(
const ByteArray& multiplex_frame_bytes);
@@ -150,7 +150,7 @@ TEST(MultiplexFrameTest, CanGenerateDisconnection) {
}
TEST(MultiplexFrameTest, CanGenerateData) {
ByteArray data("abcdefghijklmnopqrstuvwxyz");
absl::string_view data = "abcdefghijklmnopqrstuvwxyz";
ByteArray bytes =
ForData(std::string(kServiceId_1), "1234", true, data);
auto response = FromBytes(bytes);
@@ -246,13 +246,12 @@ void MultiplexOutputStream::MultiplexWriter::StartWriting() {
void MultiplexOutputStream::MultiplexWriter::Write(
EnqueuedFrame& enqueued_frame) {
MutexLock lock(&writer_mutex_);
if (!physical_writer_
->Write(Base64Utils::IntToBytes(enqueued_frame.data_.size()))
if (!Base64Utils::WriteInt(physical_writer_, enqueued_frame.data_.size())
.Ok()) {
enqueued_frame.future_->SetException({Exception::kIo});
return;
};
if (!physical_writer_->Write(enqueued_frame.data_).Ok()) {
if (!physical_writer_->Write(enqueued_frame.data_.AsStringView()).Ok()) {
enqueued_frame.future_->SetException({Exception::kIo});
return;
};
@@ -301,7 +300,7 @@ MultiplexOutputStream::VirtualOutputStream::VirtualOutputStream(
multiplex_output_stream_(multiplex_output_stream) {}
Exception MultiplexOutputStream::VirtualOutputStream::Write(
const ByteArray& data) {
absl::string_view data) {
if (is_closed_.Get()) {
LOG(WARNING) << "Failed to write data because the VirtualOutputStream for "
<< service_id_ << " closed";
@@ -20,6 +20,7 @@
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/strings/string_view.h"
#include "internal/platform/array_blocking_queue.h"
#include "internal/platform/atomic_boolean.h"
#include "internal/platform/byte_array.h"
@@ -174,7 +175,7 @@ class MultiplexOutputStream {
}
// Writes the data to the physical output stream.
Exception Write(const ByteArray& data) override;
Exception Write(absl::string_view data) override;
// Flushes the physical output stream.
Exception Flush() override;
// Closes the virtual output stream.
@@ -173,7 +173,7 @@ TEST_F(MultiplexOutputStreamTest, CreateVirtualStream_SendData) {
multiplex_output_stream_->CreateVirtualOutputStream(
std::string(kServiceId_1), std::string(kSalt_1));
const ByteArray data("abcdefghijklmnopqrstuvwxyz");
absl::string_view data = "abcdefghijklmnopqrstuvwxyz";
virtual_output_stream->Write(data);
virtual_output_stream->Flush();
auto frame_data = ReadFrame();
@@ -199,8 +199,8 @@ TEST_F(MultiplexOutputStreamTest, CreateTwoVirtualStreams_SendData) {
multiplex_output_stream_->CreateVirtualOutputStreamForFirstVirtualSocket(
std::string(kServiceId_2), std::string(kSalt_2));
const ByteArray data_1("abcdefg");
const ByteArray data_2("hijklmn");
absl::string_view data_1("abcdefg");
absl::string_view data_2("hijklmn");
MultiThreadExecutor executor(2);
CountDownLatch latch(2);
executor.Execute([&virtual_output_stream_1, &latch, &data_1]() {
@@ -199,8 +199,8 @@ TEST(MultiplexSocketTest, CreateIncomingSocketSuccess) {
});
auto& writer = socket->writer_1_;
LOG(INFO) << "writer_1_ Write start";
writer->Write(Base64Utils::IntToBytes(connection_req_frame.size()));
writer->Write(connection_req_frame);
Base64Utils::WriteInt(writer.get(), connection_req_frame.size());
writer->Write(connection_req_frame.AsStringView());
writer->Flush();
LOG(INFO) << "writer_1_ Write end";
});
@@ -269,8 +269,8 @@ TEST(MultiplexSocketTest, CreateIncomingVirtualSocketSuccess) {
std::string(SERVICE_ID_2), "J7frzSmHK-VBTHjCKpf4ew");
auto& writer = socket->writer_1_;
LOG(INFO) << "writer_1_ Write start";
writer->Write(Base64Utils::IntToBytes(connection_req_frame.size()));
writer->Write(connection_req_frame);
Base64Utils::WriteInt(writer.get(), connection_req_frame.size());
writer->Write(connection_req_frame.AsStringView());
writer->Flush();
LOG(INFO) << "writer_1_ Write end";
});
@@ -414,8 +414,8 @@ TEST(MultiplexSocketTest, EstablishVirtualSocket_RemoteAccepted) {
ConnectionResponseFrame::CONNECTION_ACCEPTED);
auto& writer = fake_socket_ptr->writer_1_;
LOG(INFO) << "writer_1_ Write start";
writer->Write(Base64Utils::IntToBytes(connection_response_frame.size()));
writer->Write(connection_response_frame);
Base64Utils::WriteInt(writer.get(), connection_response_frame.size());
writer->Write(connection_response_frame.AsStringView());
writer->Flush();
LOG(INFO) << "writer_1_ Write end";
absl::SleepFor(absl::Milliseconds(100));
@@ -427,17 +427,17 @@ TEST(MultiplexSocketTest, EstablishVirtualSocket_RemoteAccepted) {
LOG(INFO) << "Send Data frame on virtual socket for SERVICE_ID_2.";
ByteArray data_frame =
ForData(std::string(SERVICE_ID_2), service_id_hash_salt,
/*should_pass_salt=*/true, ByteArray("data"));
writer->Write(Base64Utils::IntToBytes(data_frame.size()));
writer->Write(data_frame);
/*should_pass_salt=*/true, absl::string_view("data"));
Base64Utils::WriteInt(writer.get(), data_frame.size());
writer->Write(data_frame.AsStringView());
writer->Flush();
absl::SleepFor(absl::Milliseconds(100));
LOG(INFO) << "Send disconnection frame on virtual socket for SERVICE_ID_2.";
ByteArray disconnect_frame =
ForDisconnection(std::string(SERVICE_ID_2), service_id_hash_salt);
writer->Write(Base64Utils::IntToBytes(disconnect_frame.size()));
writer->Write(disconnect_frame);
Base64Utils::WriteInt(writer.get(), disconnect_frame.size());
writer->Write(disconnect_frame.AsStringView());
writer->Flush();
absl::SleepFor(absl::Milliseconds(100));
EXPECT_EQ(multiplex_socket->GetVirtualSocketCount(), 1);
@@ -72,6 +72,7 @@ cc_library(
"//internal/platform:base",
"//internal/platform:logging",
"//internal/platform:types",
"@com_google_absl//absl/strings:string_view",
],
)
@@ -100,6 +101,7 @@ cc_test(
"//third_party/webrtc/files/stable/webrtc/api:libjingle_peerconnection_api",
"//third_party/webrtc/files/stable/webrtc/api:scoped_refptr",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest_main",
],
@@ -17,6 +17,7 @@
#include <tuple>
#include <utility>
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/input_stream.h"
@@ -32,7 +33,7 @@ namespace connections {
namespace mediums {
// OutputStreamImpl
Exception WebRtcSocket::OutputStreamImpl::Write(const ByteArray& data) {
Exception WebRtcSocket::OutputStreamImpl::Write(absl::string_view data) {
if (data.size() > kMaxDataSize) {
LOG(WARNING) << "Sending data larger than 1MB";
return {Exception::kIo};
@@ -45,7 +46,7 @@ Exception WebRtcSocket::OutputStreamImpl::Write(const ByteArray& data) {
return {Exception::kIo};
}
if (!socket_->SendMessage(data)) {
if (!socket_->SendMessage(ByteArray::FromStringView(data))) {
LOG(INFO) << "Unable to write data to socket.";
return {Exception::kIo};
}
@@ -135,7 +136,7 @@ void WebRtcSocket::OnMessage(const webrtc::DataBuffer& buffer) {
// we don't block signaling.
OffloadFromSignalingThread(
[this, buffer = ByteArray(buffer.data.data<char>(), buffer.size())] {
if (!pipe_output_->Write(buffer).Ok()) {
if (!pipe_output_->Write(buffer.AsStringView()).Ok()) {
Close();
return;
}
@@ -19,6 +19,7 @@
#include <string>
#include <memory>
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/listeners.h"
@@ -84,7 +85,7 @@ class WebRtcSocket : public Socket, public webrtc::DataChannelObserver {
OutputStreamImpl& operator=(const OutputStreamImpl& other) = delete;
// OutputStream:
Exception Write(const ByteArray& data) override;
Exception Write(absl::string_view data) override;
Exception Flush() override;
Exception Close() override;
@@ -20,6 +20,7 @@
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "webrtc/api/data_channel_interface.h"
@@ -97,7 +98,7 @@ TEST(WebRtcSocketTest, ReadMultipleMessages) {
}
TEST(WebRtcSocketTest, WriteToSocket) {
const ByteArray kMessage{"Message"};
absl::string_view kMessage{"Message"};
webrtc::scoped_refptr<MockDataChannel> mock_data_channel(
new MockDataChannel());
WebRtcSocket webrtc_socket(kSocketName, mock_data_channel);
@@ -108,7 +109,7 @@ TEST(WebRtcSocketTest, WriteToSocket) {
}
TEST(WebRtcSocketTest, SendDataBiggerThanMax) {
const ByteArray kMessage{kMaxDataSize + 1};
std::string kMessage(kMaxDataSize + 1, '0');
webrtc::scoped_refptr<MockDataChannel> mock_data_channel(
new MockDataChannel());
WebRtcSocket webrtc_socket(kSocketName, mock_data_channel);
@@ -119,7 +120,7 @@ TEST(WebRtcSocketTest, SendDataBiggerThanMax) {
}
TEST(WebRtcSocketTest, WriteToDataChannelFails) {
ByteArray kMessage{"Message"};
absl::string_view kMessage{"Message"};
webrtc::scoped_refptr<MockDataChannel> mock_data_channel(
new MockDataChannel());
WebRtcSocket webrtc_socket(kSocketName, mock_data_channel);
@@ -156,7 +157,7 @@ TEST(WebRtcSocketTest, Close) {
}
TEST(WebRtcSocketTest, WriteOnClosedChannel) {
ByteArray kMessage{"Message"};
absl::string_view kMessage{"Message"};
webrtc::scoped_refptr<MockDataChannel> mock_data_channel(
new MockDataChannel());
WebRtcSocket webrtc_socket(kSocketName, mock_data_channel);
@@ -168,7 +169,7 @@ TEST(WebRtcSocketTest, WriteOnClosedChannel) {
}
TEST(WebRtcSocketTest, ReadFromClosedChannel) {
ByteArray kMessage{"Message"};
absl::string_view kMessage{"Message"};
webrtc::scoped_refptr<MockDataChannel> mock_data_channel(
new MockDataChannel());
WebRtcSocket webrtc_socket(kSocketName, mock_data_channel);
@@ -35,7 +35,7 @@ class FakeInputStream : public InputStream {
class FakeOutputStream : public OutputStream {
public:
Exception Write(const ByteArray& data) override {
Exception Write(absl::string_view data) override {
return {Exception::kSuccess};
}
Exception Flush() override { return {Exception::kSuccess}; }