Deprecate ByteBufferWriter::WriteBytes

and switch usages to ByteBufferWriter::Write
This is part of getting rid of "pointer + length" arguments.

Bug: webrtc:42225170
Change-Id: I65a9b9550868022c0eb1f63b547195dadfbea678
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/377461
Reviewed-by: Evan Shrubsole <eshr@webrtc.org>
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#43916}
This commit is contained in:
Harald Alvestrand 2025-02-18 15:10:32 +00:00 committed by WebRTC LUCI CQ
parent 418a8c2c83
commit 9acd4d1201
6 changed files with 28 additions and 13 deletions

View File

@ -806,7 +806,7 @@ void StunAttribute::WritePadding(ByteBufferWriter* buf) const {
int remainder = length_ % 4; int remainder = length_ % 4;
if (remainder > 0) { if (remainder > 0) {
uint8_t zeroes[4] = {0}; uint8_t zeroes[4] = {0};
buf->WriteBytes(zeroes, 4 - remainder); buf->Write(webrtc::ArrayView<const uint8_t>(zeroes, 4 - remainder));
} }
} }
@ -940,12 +940,14 @@ bool StunAddressAttribute::Write(ByteBufferWriter* buf) const {
switch (address_.family()) { switch (address_.family()) {
case AF_INET: { case AF_INET: {
in_addr v4addr = address_.ipaddr().ipv4_address(); in_addr v4addr = address_.ipaddr().ipv4_address();
buf->WriteBytes(reinterpret_cast<uint8_t*>(&v4addr), sizeof(v4addr)); buf->Write(webrtc::ArrayView<const uint8_t>(
reinterpret_cast<uint8_t*>(&v4addr), sizeof(v4addr)));
break; break;
} }
case AF_INET6: { case AF_INET6: {
in6_addr v6addr = address_.ipaddr().ipv6_address(); in6_addr v6addr = address_.ipaddr().ipv6_address();
buf->WriteBytes(reinterpret_cast<uint8_t*>(&v6addr), sizeof(v6addr)); buf->Write(webrtc::ArrayView<const uint8_t>(
reinterpret_cast<uint8_t*>(&v6addr), sizeof(v6addr)));
break; break;
} }
} }
@ -1030,14 +1032,14 @@ bool StunXorAddressAttribute::Write(ByteBufferWriter* buf) const {
switch (xored_ip.family()) { switch (xored_ip.family()) {
case AF_INET: { case AF_INET: {
in_addr v4addr = xored_ip.ipv4_address(); in_addr v4addr = xored_ip.ipv4_address();
buf->WriteBytes(reinterpret_cast<const uint8_t*>(&v4addr), buf->Write(webrtc::ArrayView<const uint8_t>(
sizeof(v4addr)); reinterpret_cast<const uint8_t*>(&v4addr), sizeof(v4addr)));
break; break;
} }
case AF_INET6: { case AF_INET6: {
in6_addr v6addr = xored_ip.ipv6_address(); in6_addr v6addr = xored_ip.ipv6_address();
buf->WriteBytes(reinterpret_cast<const uint8_t*>(&v6addr), buf->Write(webrtc::ArrayView<const uint8_t>(
sizeof(v6addr)); reinterpret_cast<const uint8_t*>(&v6addr), sizeof(v6addr)));
break; break;
} }
} }
@ -1163,7 +1165,7 @@ bool StunByteStringAttribute::Write(ByteBufferWriter* buf) const {
if (!LengthValid(type(), length())) { if (!LengthValid(type(), length())) {
return false; return false;
} }
buf->WriteBytes(bytes_, length()); buf->Write(webrtc::ArrayView<const uint8_t>(bytes_, length()));
WritePadding(buf); WritePadding(buf);
return true; return true;
} }

View File

@ -1852,7 +1852,8 @@ int TurnEntry::Send(const void* data,
// If the channel is bound, we can send the data as a Channel Message. // If the channel is bound, we can send the data as a Channel Message.
buf.WriteUInt16(channel_id_); buf.WriteUInt16(channel_id_);
buf.WriteUInt16(static_cast<uint16_t>(size)); buf.WriteUInt16(static_cast<uint16_t>(size));
buf.WriteBytes(reinterpret_cast<const uint8_t*>(data), size); buf.Write(webrtc::ArrayView<const uint8_t>(
reinterpret_cast<const uint8_t*>(data), size));
} }
rtc::PacketOptions modified_options(options); rtc::PacketOptions modified_options(options);
modified_options.info_signaled_after_sent.turn_overhead_bytes = modified_options.info_signaled_after_sent.turn_overhead_bytes =

View File

@ -795,7 +795,7 @@ void TurnServerAllocation::OnExternalPacket(rtc::AsyncPacketSocket* socket,
rtc::ByteBufferWriter buf; rtc::ByteBufferWriter buf;
buf.WriteUInt16(channel->id); buf.WriteUInt16(channel->id);
buf.WriteUInt16(static_cast<uint16_t>(packet.payload().size())); buf.WriteUInt16(static_cast<uint16_t>(packet.payload().size()));
buf.WriteBytes(packet.payload().data(), packet.payload().size()); buf.Write(webrtc::ArrayView<const uint8_t>(packet.payload()));
server_->Send(&conn_, buf); server_->Send(&conn_, buf);
} else if (!server_->enable_permission_checks_ || } else if (!server_->enable_permission_checks_ ||
HasPermission(packet.source_address().ipaddr())) { HasPermission(packet.source_address().ipaddr())) {

View File

@ -97,6 +97,7 @@ class ByteBufferWriterT {
val.size()); val.size());
} }
// Write an array of bytes (uint8_t) // Write an array of bytes (uint8_t)
[[deprecated("issues.webrtc.org/4225170 - use Write(ArrayView)")]]
void WriteBytes(const uint8_t* val, size_t len) { void WriteBytes(const uint8_t* val, size_t len) {
WriteBytesInternal(reinterpret_cast<const value_type*>(val), len); WriteBytesInternal(reinterpret_cast<const value_type*>(val), len);
} }

View File

@ -168,7 +168,7 @@ TEST(ByteBufferTest, TestReadWriteBuffer) {
// Write and read bytes // Write and read bytes
uint8_t write_bytes[] = {3, 2, 1}; uint8_t write_bytes[] = {3, 2, 1};
buffer.WriteBytes(write_bytes, 3); buffer.Write(webrtc::ArrayView<const uint8_t>(write_bytes, 3));
ByteBufferReader read_buf7(buffer); ByteBufferReader read_buf7(buffer);
uint8_t read_bytes[3]; uint8_t read_bytes[3];
EXPECT_TRUE(read_buf7.ReadBytes(read_bytes)); EXPECT_TRUE(read_buf7.ReadBytes(read_bytes));
@ -176,6 +176,18 @@ TEST(ByteBufferTest, TestReadWriteBuffer) {
EXPECT_EQ(read_buf7.Length(), 0U); EXPECT_EQ(read_buf7.Length(), 0U);
buffer.Clear(); buffer.Clear();
// Write and read bytes with deprecated function
// TODO: issues.webrtc.org/42225170 - delete
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
buffer.WriteBytes(write_bytes, 3);
#pragma clang diagnostic pop
ByteBufferReader read_buf75(buffer);
EXPECT_TRUE(read_buf75.ReadBytes(read_bytes));
EXPECT_THAT(read_bytes, ElementsAreArray(write_bytes));
EXPECT_EQ(read_buf75.Length(), 0U);
buffer.Clear();
// Write and read reserved buffer space // Write and read reserved buffer space
uint8_t* write_dst = buffer.ReserveWriteBuffer(3); uint8_t* write_dst = buffer.ReserveWriteBuffer(3);
memcpy(write_dst, write_bytes, 3); memcpy(write_dst, write_bytes, 3);

View File

@ -131,8 +131,7 @@ class MockOnCompleteFrameCallback
void ClearExpectedBitstream() { buffer_.Clear(); } void ClearExpectedBitstream() { buffer_.Clear(); }
void AppendExpectedBitstream(const uint8_t data[], size_t size_in_bytes) { void AppendExpectedBitstream(const uint8_t data[], size_t size_in_bytes) {
// TODO(Johan): Let rtc::ByteBuffer handle uint8_t* instead of char*. buffer_.Write(ArrayView<const uint8_t>(data, size_in_bytes));
buffer_.WriteBytes(data, size_in_bytes);
} }
rtc::ByteBufferWriter buffer_; rtc::ByteBufferWriter buffer_;
}; };