diff --git a/connections/implementation/BUILD b/connections/implementation/BUILD index 30d3fd8f..4f37d002 100644 --- a/connections/implementation/BUILD +++ b/connections/implementation/BUILD @@ -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", ], diff --git a/connections/implementation/base_endpoint_channel.cc b/connections/implementation/base_endpoint_channel.cc index 1128cc19..0687d7be 100644 --- a/connections/implementation/base_endpoint_channel.cc +++ b/connections/implementation/base_endpoint_channel.cc @@ -192,10 +192,10 @@ ExceptionOr 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 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 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 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 diff --git a/connections/implementation/base_endpoint_channel.h b/connections/implementation/base_endpoint_channel.h index 66faf4eb..0924c794 100644 --- a/connections/implementation/base_endpoint_channel.h +++ b/connections/implementation/base_endpoint_channel.h @@ -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) diff --git a/connections/implementation/connections_authentication_transport_test.cc b/connections/implementation/connections_authentication_transport_test.cc index a2293018..11ebd2eb 100644 --- a/connections/implementation/connections_authentication_transport_test.cc +++ b/connections/implementation/connections_authentication_transport_test.cc @@ -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, Read, (), (override)); MOCK_METHOD(ExceptionOr, 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( diff --git a/connections/implementation/encryption_runner_test.cc b/connections/implementation/encryption_runner_test.cc index d0cec875..c1e3211f 100644 --- a/connections/implementation/encryption_runner_test.cc +++ b/connections/implementation/encryption_runner_test.cc @@ -16,9 +16,11 @@ #include #include +#include #include #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(); diff --git a/connections/implementation/endpoint_channel.h b/connections/implementation/endpoint_channel.h index d1b1633b..b4e9cbe3 100644 --- a/connections/implementation/endpoint_channel.h +++ b/connections/implementation/endpoint_channel.h @@ -20,6 +20,7 @@ #include #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. diff --git a/connections/implementation/endpoint_manager.cc b/connections/implementation/endpoint_manager.cc index fec20138..82677bca 100644 --- a/connections/implementation/endpoint_manager.cc +++ b/connections/implementation/endpoint_manager.cc @@ -960,7 +960,8 @@ std::vector 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; diff --git a/connections/implementation/endpoint_manager_test.cc b/connections/implementation/endpoint_manager_test.cc index 740c6213..0f906e3c 100644 --- a/connections/implementation/endpoint_manager_test.cc +++ b/connections/implementation/endpoint_manager_test.cc @@ -14,7 +14,6 @@ #include "connections/implementation/endpoint_manager.h" -#include #include #include #include @@ -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)); diff --git a/connections/implementation/fake_endpoint_channel.h b/connections/implementation/fake_endpoint_channel.h index 795903f1..fa1bc3f7 100644 --- a/connections/implementation/fake_endpoint_channel.h +++ b/connections/implementation/fake_endpoint_channel.h @@ -19,11 +19,13 @@ #include #include +#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_;