Simplify use of ByteArray.

PiperOrigin-RevId: 855304597
This commit is contained in:
Francis Tsui
2026-01-12 11:15:52 -08:00
committed by Copybara-Service
parent a64526b161
commit b5796d2394
9 changed files with 32 additions and 20 deletions
+5
View File
@@ -234,6 +234,7 @@ cc_library(
"//internal/platform:logging",
"//internal/platform:mac_address",
"//internal/platform:types",
"//internal/platform/implementation:types",
"@com_google_absl//absl/functional:any_invocable",
"@com_google_absl//absl/functional:bind_front",
"@com_google_absl//absl/strings",
@@ -414,9 +415,11 @@ cc_test(
"//connections/implementation/analytics",
"//internal/platform:base",
"//internal/platform:types",
"//internal/platform/implementation:types",
"//internal/platform/implementation/g3", # build_cleaner: keep
"//proto:connections_enums_cc_proto",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest_main",
"@com_google_ukey2//:ukey2",
@@ -442,6 +445,7 @@ cc_test(
"//proto:connections_enums_cc_proto",
"//testing/fuzzing:fuzztest",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/synchronization",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest_main",
@@ -485,6 +489,7 @@ cc_test(
"//internal/platform/implementation/g3", # build_cleaner: keep
"//proto:connections_enums_cc_proto",
"@com_github_protobuf_matchers//protobuf-matchers",
"@com_google_absl//absl/strings:string_view",
"@com_google_absl//absl/time",
"@com_google_googletest//:gtest_main",
],
@@ -192,10 +192,10 @@ ExceptionOr<ByteArray> BaseEndpointChannel::Read(
Exception BaseEndpointChannel::Write(const ByteArray& data) {
PacketMetaData packet_meta_data;
return Write(data, packet_meta_data);
return Write(data.AsStringView(), packet_meta_data);
}
Exception BaseEndpointChannel::Write(const ByteArray& data,
Exception BaseEndpointChannel::Write(absl::string_view data,
PacketMetaData& packet_meta_data) {
{
MutexLock pause_lock(&is_paused_mutex_);
@@ -204,8 +204,9 @@ Exception BaseEndpointChannel::Write(const ByteArray& data,
}
}
ByteArray encrypted_data;
const ByteArray* data_to_write = &data;
absl::string_view data_to_write = data;
// Make sure encrypted message is value until end of function.
std::unique_ptr<std::string> encrypted;
{
// Holding both mutexes is necessary to prevent the keep alive and payload
// threads from writing encrypted messages out of order which causes a
@@ -217,19 +218,17 @@ Exception BaseEndpointChannel::Write(const ByteArray& data,
if (IsEncryptionEnabledLocked()) {
// If encryption is enabled, encode the message.
packet_meta_data.StartEncryption();
std::unique_ptr<std::string> encrypted =
crypto_context_->EncodeMessageToPeer(std::string(data));
encrypted = crypto_context_->EncodeMessageToPeer(data);
packet_meta_data.StopEncryption();
if (!encrypted) {
LOG(WARNING) << __func__ << ": Failed to encrypt data.";
return {Exception::kIo};
}
encrypted_data = ByteArray(std::move(*encrypted));
data_to_write = &encrypted_data;
data_to_write = *encrypted;
}
}
size_t data_size = data_to_write->size();
size_t data_size = data_to_write.size();
if (data_size < 0 || data_size > max_allowed_read_bytes_) {
LOG(WARNING) << __func__
<< ": Write an invalid number of bytes: " << data_size;
@@ -251,7 +250,7 @@ Exception BaseEndpointChannel::Write(const ByteArray& data,
<< ": Failed to write header: " << write_exception.value;
return write_exception;
}
write_exception = writer_->Write(data_to_write->AsStringView());
write_exception = writer_->Write(data_to_write);
if (write_exception.Raised()) {
LOG(WARNING) << __func__
<< ": Failed to write data: " << write_exception.value;
@@ -488,7 +487,7 @@ std::unique_ptr<std::string> BaseEndpointChannel::EncodeMessageForTests(
absl::string_view data) {
MutexLock lock(&crypto_mutex_);
DCHECK(IsEncryptionEnabledLocked());
return crypto_context_->EncodeMessageToPeer(std::string(data));
return crypto_context_->EncodeMessageToPeer(data);
}
} // namespace connections
@@ -56,7 +56,7 @@ class BaseEndpointChannel : public EndpointChannel {
ABSL_LOCKS_EXCLUDED(reader_mutex_, crypto_mutex_,
last_read_mutex_) override;
Exception Write(const ByteArray& data) override;
Exception Write(const ByteArray& data, PacketMetaData& packet_meta_data)
Exception Write(absl::string_view data, PacketMetaData& packet_meta_data)
ABSL_LOCKS_EXCLUDED(writer_mutex_, crypto_mutex_) override;
void Close() ABSL_LOCKS_EXCLUDED(is_paused_mutex_) override;
void Close(location::nearby::proto::connections::DisconnectionReason reason)
@@ -22,6 +22,7 @@
#include "gmock/gmock.h"
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "connections/implementation/analytics/analytics_recorder.h"
#include "connections/implementation/endpoint_channel.h"
@@ -40,7 +41,7 @@ class MockEndpointChannel : public EndpointChannel {
MOCK_METHOD(ExceptionOr<ByteArray>, Read, (), (override));
MOCK_METHOD(ExceptionOr<ByteArray>, Read, (PacketMetaData&), (override));
MOCK_METHOD(Exception, Write, (const ByteArray& data), (override));
MOCK_METHOD(Exception, Write, (const ByteArray&, PacketMetaData&),
MOCK_METHOD(Exception, Write, (absl::string_view data, PacketMetaData&),
(override));
MOCK_METHOD(void, Close, (), (override));
MOCK_METHOD(
@@ -16,9 +16,11 @@
#include <cstddef>
#include <cstdint>
#include <memory>
#include <string>
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "connections/implementation/analytics/analytics_recorder.h"
#include "connections/implementation/client_proxy.h"
@@ -26,6 +28,7 @@
#include "internal/platform/byte_array.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/system_clock.h"
#include "internal/platform/input_stream.h"
#include "internal/platform/output_stream.h"
#include "internal/platform/pipe.h"
@@ -55,10 +58,10 @@ class FakeEndpointChannel : public EndpointChannel {
write_timestamp_ = SystemClock::ElapsedRealtime();
return out_ ? out_->Write(data.AsStringView()) : Exception{Exception::kIo};
}
Exception Write(const ByteArray& data,
Exception Write(absl::string_view data,
PacketMetaData& packet_meta_data) override {
write_timestamp_ = SystemClock::ElapsedRealtime();
return out_ ? out_->Write(data.AsStringView()) : Exception{Exception::kIo};
return out_ ? out_->Write(data) : Exception{Exception::kIo};
}
void Close() override {
if (in_) in_->Close();
@@ -20,6 +20,7 @@
#include <string>
#include "securegcm/d2d_connection_context_v1.h"
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "connections/implementation/analytics/analytics_recorder.h"
#include "connections/implementation/analytics/packet_meta_data.h"
@@ -45,7 +46,7 @@ class EndpointChannel {
virtual Exception Write(const ByteArray& data) = 0; // throws Exception::IO
virtual Exception Write(
const ByteArray& data,
absl::string_view data,
PacketMetaData& packet_meta_data) = 0; // throws Exception::IO
// Closes this EndpointChannel, without tracking the closure in analytics.
@@ -960,7 +960,8 @@ std::vector<std::string> EndpointManager::SendTransferFrameBytes(
continue;
}
Exception write_exception = channel->Write(bytes, packet_meta_data);
Exception write_exception =
channel->Write(bytes.AsStringView(), packet_meta_data);
if (!write_exception.Ok()) {
failed_endpoint_ids.push_back(endpoint_id);
LOG(INFO) << "Failed to send packet; endpoint_id=" << endpoint_id;
@@ -14,7 +14,6 @@
#include "connections/implementation/endpoint_manager.h"
#include <atomic>
#include <cstdint>
#include <memory>
#include <string>
@@ -25,6 +24,7 @@
#include "protobuf-matchers/protocol-buffer-matchers.h"
#include "gtest/gtest.h"
#include "testing/fuzzing/fuzztest.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
@@ -70,7 +70,7 @@ class MockEndpointChannel : public EndpointChannel {
(override));
MOCK_METHOD(Exception, Write, (const ByteArray& data), (override));
MOCK_METHOD(Exception, Write,
(const ByteArray& data, PacketMetaData& packet_meta_data),
(absl::string_view data, PacketMetaData& packet_meta_data),
(override));
MOCK_METHOD(void, Close, (), (override));
MOCK_METHOD(void, Close, (DisconnectionReason reason), (override));
@@ -19,11 +19,13 @@
#include <memory>
#include <string>
#include "absl/strings/string_view.h"
#include "absl/time/time.h"
#include "connections/implementation/analytics/analytics_recorder.h"
#include "connections/implementation/endpoint_channel.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/system_clock.h"
namespace nearby {
namespace connections {
@@ -52,7 +54,7 @@ class FakeEndpointChannel : public EndpointChannel {
write_timestamp_ = SystemClock::ElapsedRealtime();
return write_output_;
}
Exception Write(const ByteArray& data,
Exception Write(absl::string_view data,
PacketMetaData& packet_meta_data) override {
write_timestamp_ = SystemClock::ElapsedRealtime();
return write_output_;