In rtc::ByteBuffer drop support for ORDER_HOST as unused

Bug: None
Change-Id: Ideab428b13d981cddf9784cfd07fb7dfb2e914fe
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/159698
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Danil Chapovalov <danilchap@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#29803}
This commit is contained in:
Danil Chapovalov 2019-11-14 17:40:23 +01:00 committed by Commit Bot
parent 74f35e48d5
commit 7b46e17c31
7 changed files with 163 additions and 233 deletions

View file

@ -1190,7 +1190,7 @@ void PseudoTcp::disableWindowScale() {
}
void PseudoTcp::queueConnectMessage() {
rtc::ByteBufferWriter buf(rtc::ByteBuffer::ORDER_NETWORK);
rtc::ByteBufferWriter buf;
buf.WriteUInt8(CTL_CONNECT);
if (m_support_wnd_scale) {

View file

@ -31,8 +31,7 @@ uint32_t ReduceTransactionId(const std::string& transaction_id) {
RTC_DCHECK(transaction_id.length() == cricket::kStunTransactionIdLength ||
transaction_id.length() ==
cricket::kStunLegacyTransactionIdLength);
ByteBufferReader reader(transaction_id.c_str(), transaction_id.length(),
rtc::ByteBuffer::ORDER_NETWORK);
ByteBufferReader reader(transaction_id.c_str(), transaction_id.length());
uint32_t result = 0;
uint32_t next;
while (reader.ReadUInt32(&next)) {

View file

@ -169,8 +169,7 @@ bool WriteDataChannelOpenMessage(const std::string& label,
}
rtc::ByteBufferWriter buffer(NULL,
20 + label.length() + config.protocol.length(),
rtc::ByteBuffer::ORDER_NETWORK);
20 + label.length() + config.protocol.length());
// TODO(tommi): Add error handling and check resulting length.
buffer.WriteUInt8(DATA_CHANNEL_OPEN_MESSAGE_TYPE);
buffer.WriteUInt8(channel_type);

View file

@ -16,41 +16,22 @@ namespace rtc {
ByteBufferWriter::ByteBufferWriter() : ByteBufferWriterT() {}
ByteBufferWriter::ByteBufferWriter(ByteOrder byte_order)
: ByteBufferWriterT(byte_order) {}
ByteBufferWriter::ByteBufferWriter(const char* bytes, size_t len)
: ByteBufferWriterT(bytes, len) {}
ByteBufferWriter::ByteBufferWriter(const char* bytes,
size_t len,
ByteOrder byte_order)
: ByteBufferWriterT(bytes, len, byte_order) {}
ByteBufferReader::ByteBufferReader(const char* bytes, size_t len)
: ByteBuffer(ORDER_NETWORK) {
ByteBufferReader::ByteBufferReader(const char* bytes, size_t len) {
Construct(bytes, len);
}
ByteBufferReader::ByteBufferReader(const char* bytes,
size_t len,
ByteOrder byte_order)
: ByteBuffer(byte_order) {
Construct(bytes, len);
}
ByteBufferReader::ByteBufferReader(const char* bytes)
: ByteBuffer(ORDER_NETWORK) {
ByteBufferReader::ByteBufferReader(const char* bytes) {
Construct(bytes, strlen(bytes));
}
ByteBufferReader::ByteBufferReader(const Buffer& buf)
: ByteBuffer(ORDER_NETWORK) {
ByteBufferReader::ByteBufferReader(const Buffer& buf) {
Construct(buf.data<char>(), buf.size());
}
ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf)
: ByteBuffer(buf.Order()) {
ByteBufferReader::ByteBufferReader(const ByteBufferWriter& buf) {
Construct(buf.Data(), buf.Length());
}
@ -76,7 +57,7 @@ bool ByteBufferReader::ReadUInt16(uint16_t* val) {
if (!ReadBytes(reinterpret_cast<char*>(&v), 2)) {
return false;
} else {
*val = (Order() == ORDER_NETWORK) ? NetworkToHost16(v) : v;
*val = NetworkToHost16(v);
return true;
}
}
@ -87,14 +68,12 @@ bool ByteBufferReader::ReadUInt24(uint32_t* val) {
uint32_t v = 0;
char* read_into = reinterpret_cast<char*>(&v);
if (Order() == ORDER_NETWORK || IsHostBigEndian()) {
++read_into;
}
++read_into;
if (!ReadBytes(read_into, 3)) {
return false;
} else {
*val = (Order() == ORDER_NETWORK) ? NetworkToHost32(v) : v;
*val = NetworkToHost32(v);
return true;
}
}
@ -107,7 +86,7 @@ bool ByteBufferReader::ReadUInt32(uint32_t* val) {
if (!ReadBytes(reinterpret_cast<char*>(&v), 4)) {
return false;
} else {
*val = (Order() == ORDER_NETWORK) ? NetworkToHost32(v) : v;
*val = NetworkToHost32(v);
return true;
}
}
@ -120,7 +99,7 @@ bool ByteBufferReader::ReadUInt64(uint64_t* val) {
if (!ReadBytes(reinterpret_cast<char*>(&v), 8)) {
return false;
} else {
*val = (Order() == ORDER_NETWORK) ? NetworkToHost64(v) : v;
*val = NetworkToHost64(v);
return true;
}
}

View file

@ -20,42 +20,14 @@
#include "rtc_base/byte_order.h"
#include "rtc_base/constructor_magic.h"
// Reads/Writes from/to buffer using network byte order (big endian)
namespace rtc {
class ByteBuffer {
public:
enum ByteOrder {
ORDER_NETWORK = 0, // Default, use network byte order (big endian).
ORDER_HOST, // Use the native order of the host.
};
explicit ByteBuffer(ByteOrder byte_order) : byte_order_(byte_order) {}
ByteOrder Order() const { return byte_order_; }
private:
ByteOrder byte_order_;
RTC_DISALLOW_COPY_AND_ASSIGN(ByteBuffer);
};
template <class BufferClassT>
class ByteBufferWriterT : public ByteBuffer {
class ByteBufferWriterT {
public:
// |byte_order| defines order of bytes in the buffer.
ByteBufferWriterT() : ByteBuffer(ORDER_NETWORK) {
Construct(nullptr, kDefaultCapacity);
}
explicit ByteBufferWriterT(ByteOrder byte_order) : ByteBuffer(byte_order) {
Construct(nullptr, kDefaultCapacity);
}
ByteBufferWriterT(const char* bytes, size_t len) : ByteBuffer(ORDER_NETWORK) {
Construct(bytes, len);
}
ByteBufferWriterT(const char* bytes, size_t len, ByteOrder byte_order)
: ByteBuffer(byte_order) {
Construct(bytes, len);
}
ByteBufferWriterT() { Construct(nullptr, kDefaultCapacity); }
ByteBufferWriterT(const char* bytes, size_t len) { Construct(bytes, len); }
const char* Data() const { return buffer_.data(); }
size_t Length() const { return buffer_.size(); }
@ -67,23 +39,21 @@ class ByteBufferWriterT : public ByteBuffer {
WriteBytes(reinterpret_cast<const char*>(&val), 1);
}
void WriteUInt16(uint16_t val) {
uint16_t v = (Order() == ORDER_NETWORK) ? HostToNetwork16(val) : val;
uint16_t v = HostToNetwork16(val);
WriteBytes(reinterpret_cast<const char*>(&v), 2);
}
void WriteUInt24(uint32_t val) {
uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
uint32_t v = HostToNetwork32(val);
char* start = reinterpret_cast<char*>(&v);
if (Order() == ORDER_NETWORK || IsHostBigEndian()) {
++start;
}
++start;
WriteBytes(start, 3);
}
void WriteUInt32(uint32_t val) {
uint32_t v = (Order() == ORDER_NETWORK) ? HostToNetwork32(val) : val;
uint32_t v = HostToNetwork32(val);
WriteBytes(reinterpret_cast<const char*>(&v), 4);
}
void WriteUInt64(uint64_t val) {
uint64_t v = (Order() == ORDER_NETWORK) ? HostToNetwork64(val) : val;
uint64_t v = HostToNetwork64(val);
WriteBytes(reinterpret_cast<const char*>(&v), 8);
}
// Serializes an unsigned varint in the format described by
@ -139,11 +109,8 @@ class ByteBufferWriterT : public ByteBuffer {
class ByteBufferWriter : public ByteBufferWriterT<BufferT<char>> {
public:
// |byte_order| defines order of bytes in the buffer.
ByteBufferWriter();
explicit ByteBufferWriter(ByteOrder byte_order);
ByteBufferWriter(const char* bytes, size_t len);
ByteBufferWriter(const char* bytes, size_t len, ByteOrder byte_order);
private:
RTC_DISALLOW_COPY_AND_ASSIGN(ByteBufferWriter);
@ -151,10 +118,9 @@ class ByteBufferWriter : public ByteBufferWriterT<BufferT<char>> {
// The ByteBufferReader references the passed data, i.e. the pointer must be
// valid during the lifetime of the reader.
class ByteBufferReader : public ByteBuffer {
class ByteBufferReader {
public:
ByteBufferReader(const char* bytes, size_t len);
ByteBufferReader(const char* bytes, size_t len, ByteOrder byte_order);
// Initializes buffer from a zero-terminated string.
explicit ByteBufferReader(const char* bytes);

View file

@ -82,180 +82,169 @@ TEST(ByteBufferTest, TestBufferLength) {
}
TEST(ByteBufferTest, TestReadWriteBuffer) {
ByteBufferWriter::ByteOrder orders[2] = {ByteBufferWriter::ORDER_HOST,
ByteBufferWriter::ORDER_NETWORK};
for (size_t i = 0; i < arraysize(orders); i++) {
ByteBufferWriter buffer(orders[i]);
EXPECT_EQ(orders[i], buffer.Order());
ByteBufferReader read_buf(nullptr, 0, orders[i]);
EXPECT_EQ(orders[i], read_buf.Order());
uint8_t ru8;
EXPECT_FALSE(read_buf.ReadUInt8(&ru8));
ByteBufferWriter buffer;
ByteBufferReader read_buf(nullptr, 0);
uint8_t ru8;
EXPECT_FALSE(read_buf.ReadUInt8(&ru8));
// Write and read uint8_t.
uint8_t wu8 = 1;
buffer.WriteUInt8(wu8);
ByteBufferReader read_buf1(buffer.Data(), buffer.Length(), orders[i]);
EXPECT_TRUE(read_buf1.ReadUInt8(&ru8));
EXPECT_EQ(wu8, ru8);
EXPECT_EQ(0U, read_buf1.Length());
buffer.Clear();
// Write and read uint8_t.
uint8_t wu8 = 1;
buffer.WriteUInt8(wu8);
ByteBufferReader read_buf1(buffer.Data(), buffer.Length());
EXPECT_TRUE(read_buf1.ReadUInt8(&ru8));
EXPECT_EQ(wu8, ru8);
EXPECT_EQ(0U, read_buf1.Length());
buffer.Clear();
// Write and read uint16_t.
uint16_t wu16 = (1 << 8) + 1;
buffer.WriteUInt16(wu16);
ByteBufferReader read_buf2(buffer.Data(), buffer.Length(), orders[i]);
uint16_t ru16;
EXPECT_TRUE(read_buf2.ReadUInt16(&ru16));
EXPECT_EQ(wu16, ru16);
EXPECT_EQ(0U, read_buf2.Length());
buffer.Clear();
// Write and read uint16_t.
uint16_t wu16 = (1 << 8) + 1;
buffer.WriteUInt16(wu16);
ByteBufferReader read_buf2(buffer.Data(), buffer.Length());
uint16_t ru16;
EXPECT_TRUE(read_buf2.ReadUInt16(&ru16));
EXPECT_EQ(wu16, ru16);
EXPECT_EQ(0U, read_buf2.Length());
buffer.Clear();
// Write and read uint24.
uint32_t wu24 = (3 << 16) + (2 << 8) + 1;
buffer.WriteUInt24(wu24);
ByteBufferReader read_buf3(buffer.Data(), buffer.Length(), orders[i]);
uint32_t ru24;
EXPECT_TRUE(read_buf3.ReadUInt24(&ru24));
EXPECT_EQ(wu24, ru24);
EXPECT_EQ(0U, read_buf3.Length());
buffer.Clear();
// Write and read uint24.
uint32_t wu24 = (3 << 16) + (2 << 8) + 1;
buffer.WriteUInt24(wu24);
ByteBufferReader read_buf3(buffer.Data(), buffer.Length());
uint32_t ru24;
EXPECT_TRUE(read_buf3.ReadUInt24(&ru24));
EXPECT_EQ(wu24, ru24);
EXPECT_EQ(0U, read_buf3.Length());
buffer.Clear();
// Write and read uint32_t.
uint32_t wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1;
buffer.WriteUInt32(wu32);
ByteBufferReader read_buf4(buffer.Data(), buffer.Length(), orders[i]);
uint32_t ru32;
EXPECT_TRUE(read_buf4.ReadUInt32(&ru32));
EXPECT_EQ(wu32, ru32);
EXPECT_EQ(0U, read_buf3.Length());
buffer.Clear();
// Write and read uint32_t.
uint32_t wu32 = (4 << 24) + (3 << 16) + (2 << 8) + 1;
buffer.WriteUInt32(wu32);
ByteBufferReader read_buf4(buffer.Data(), buffer.Length());
uint32_t ru32;
EXPECT_TRUE(read_buf4.ReadUInt32(&ru32));
EXPECT_EQ(wu32, ru32);
EXPECT_EQ(0U, read_buf3.Length());
buffer.Clear();
// Write and read uint64_t.
uint32_t another32 = (8 << 24) + (7 << 16) + (6 << 8) + 5;
uint64_t wu64 = (static_cast<uint64_t>(another32) << 32) + wu32;
buffer.WriteUInt64(wu64);
ByteBufferReader read_buf5(buffer.Data(), buffer.Length(), orders[i]);
uint64_t ru64;
EXPECT_TRUE(read_buf5.ReadUInt64(&ru64));
EXPECT_EQ(wu64, ru64);
EXPECT_EQ(0U, read_buf5.Length());
buffer.Clear();
// Write and read uint64_t.
uint32_t another32 = (8 << 24) + (7 << 16) + (6 << 8) + 5;
uint64_t wu64 = (static_cast<uint64_t>(another32) << 32) + wu32;
buffer.WriteUInt64(wu64);
ByteBufferReader read_buf5(buffer.Data(), buffer.Length());
uint64_t ru64;
EXPECT_TRUE(read_buf5.ReadUInt64(&ru64));
EXPECT_EQ(wu64, ru64);
EXPECT_EQ(0U, read_buf5.Length());
buffer.Clear();
// Write and read string.
std::string write_string("hello");
buffer.WriteString(write_string);
ByteBufferReader read_buf6(buffer.Data(), buffer.Length(), orders[i]);
std::string read_string;
EXPECT_TRUE(read_buf6.ReadString(&read_string, write_string.size()));
EXPECT_EQ(write_string, read_string);
EXPECT_EQ(0U, read_buf6.Length());
buffer.Clear();
// Write and read string.
std::string write_string("hello");
buffer.WriteString(write_string);
ByteBufferReader read_buf6(buffer.Data(), buffer.Length());
std::string read_string;
EXPECT_TRUE(read_buf6.ReadString(&read_string, write_string.size()));
EXPECT_EQ(write_string, read_string);
EXPECT_EQ(0U, read_buf6.Length());
buffer.Clear();
// Write and read bytes
char write_bytes[] = "foo";
buffer.WriteBytes(write_bytes, 3);
ByteBufferReader read_buf7(buffer.Data(), buffer.Length(), orders[i]);
char read_bytes[3];
EXPECT_TRUE(read_buf7.ReadBytes(read_bytes, 3));
for (int i = 0; i < 3; ++i) {
EXPECT_EQ(write_bytes[i], read_bytes[i]);
}
EXPECT_EQ(0U, read_buf7.Length());
buffer.Clear();
// Write and read reserved buffer space
char* write_dst = buffer.ReserveWriteBuffer(3);
memcpy(write_dst, write_bytes, 3);
ByteBufferReader read_buf8(buffer.Data(), buffer.Length(), orders[i]);
memset(read_bytes, 0, 3);
EXPECT_TRUE(read_buf8.ReadBytes(read_bytes, 3));
for (int i = 0; i < 3; ++i) {
EXPECT_EQ(write_bytes[i], read_bytes[i]);
}
EXPECT_EQ(0U, read_buf8.Length());
buffer.Clear();
// Write and read in order.
buffer.WriteUInt8(wu8);
buffer.WriteUInt16(wu16);
buffer.WriteUInt24(wu24);
buffer.WriteUInt32(wu32);
buffer.WriteUInt64(wu64);
ByteBufferReader read_buf9(buffer.Data(), buffer.Length(), orders[i]);
EXPECT_TRUE(read_buf9.ReadUInt8(&ru8));
EXPECT_EQ(wu8, ru8);
EXPECT_TRUE(read_buf9.ReadUInt16(&ru16));
EXPECT_EQ(wu16, ru16);
EXPECT_TRUE(read_buf9.ReadUInt24(&ru24));
EXPECT_EQ(wu24, ru24);
EXPECT_TRUE(read_buf9.ReadUInt32(&ru32));
EXPECT_EQ(wu32, ru32);
EXPECT_TRUE(read_buf9.ReadUInt64(&ru64));
EXPECT_EQ(wu64, ru64);
EXPECT_EQ(0U, read_buf9.Length());
buffer.Clear();
// Write and read bytes
char write_bytes[] = "foo";
buffer.WriteBytes(write_bytes, 3);
ByteBufferReader read_buf7(buffer.Data(), buffer.Length());
char read_bytes[3];
EXPECT_TRUE(read_buf7.ReadBytes(read_bytes, 3));
for (int i = 0; i < 3; ++i) {
EXPECT_EQ(write_bytes[i], read_bytes[i]);
}
EXPECT_EQ(0U, read_buf7.Length());
buffer.Clear();
// Write and read reserved buffer space
char* write_dst = buffer.ReserveWriteBuffer(3);
memcpy(write_dst, write_bytes, 3);
ByteBufferReader read_buf8(buffer.Data(), buffer.Length());
memset(read_bytes, 0, 3);
EXPECT_TRUE(read_buf8.ReadBytes(read_bytes, 3));
for (int i = 0; i < 3; ++i) {
EXPECT_EQ(write_bytes[i], read_bytes[i]);
}
EXPECT_EQ(0U, read_buf8.Length());
buffer.Clear();
// Write and read in order.
buffer.WriteUInt8(wu8);
buffer.WriteUInt16(wu16);
buffer.WriteUInt24(wu24);
buffer.WriteUInt32(wu32);
buffer.WriteUInt64(wu64);
ByteBufferReader read_buf9(buffer.Data(), buffer.Length());
EXPECT_TRUE(read_buf9.ReadUInt8(&ru8));
EXPECT_EQ(wu8, ru8);
EXPECT_TRUE(read_buf9.ReadUInt16(&ru16));
EXPECT_EQ(wu16, ru16);
EXPECT_TRUE(read_buf9.ReadUInt24(&ru24));
EXPECT_EQ(wu24, ru24);
EXPECT_TRUE(read_buf9.ReadUInt32(&ru32));
EXPECT_EQ(wu32, ru32);
EXPECT_TRUE(read_buf9.ReadUInt64(&ru64));
EXPECT_EQ(wu64, ru64);
EXPECT_EQ(0U, read_buf9.Length());
buffer.Clear();
}
TEST(ByteBufferTest, TestReadWriteUVarint) {
ByteBufferWriter::ByteOrder orders[2] = {ByteBufferWriter::ORDER_HOST,
ByteBufferWriter::ORDER_NETWORK};
for (ByteBufferWriter::ByteOrder& order : orders) {
ByteBufferWriter write_buffer(order);
size_t size = 0;
EXPECT_EQ(size, write_buffer.Length());
ByteBufferWriter write_buffer;
size_t size = 0;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(1u);
++size;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(1u);
++size;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(2u);
++size;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(2u);
++size;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(27u);
++size;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(27u);
++size;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(149u);
size += 2;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(149u);
size += 2;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(68719476736u);
size += 6;
EXPECT_EQ(size, write_buffer.Length());
write_buffer.WriteUVarint(68719476736u);
size += 6;
EXPECT_EQ(size, write_buffer.Length());
ByteBufferReader read_buffer(write_buffer.Data(), write_buffer.Length(),
order);
EXPECT_EQ(size, read_buffer.Length());
uint64_t val1, val2, val3, val4, val5;
ByteBufferReader read_buffer(write_buffer.Data(), write_buffer.Length());
EXPECT_EQ(size, read_buffer.Length());
uint64_t val1, val2, val3, val4, val5;
ASSERT_TRUE(read_buffer.ReadUVarint(&val1));
EXPECT_EQ(1u, val1);
--size;
EXPECT_EQ(size, read_buffer.Length());
ASSERT_TRUE(read_buffer.ReadUVarint(&val1));
EXPECT_EQ(1u, val1);
--size;
EXPECT_EQ(size, read_buffer.Length());
ASSERT_TRUE(read_buffer.ReadUVarint(&val2));
EXPECT_EQ(2u, val2);
--size;
EXPECT_EQ(size, read_buffer.Length());
ASSERT_TRUE(read_buffer.ReadUVarint(&val2));
EXPECT_EQ(2u, val2);
--size;
EXPECT_EQ(size, read_buffer.Length());
ASSERT_TRUE(read_buffer.ReadUVarint(&val3));
EXPECT_EQ(27u, val3);
--size;
EXPECT_EQ(size, read_buffer.Length());
ASSERT_TRUE(read_buffer.ReadUVarint(&val3));
EXPECT_EQ(27u, val3);
--size;
EXPECT_EQ(size, read_buffer.Length());
ASSERT_TRUE(read_buffer.ReadUVarint(&val4));
EXPECT_EQ(149u, val4);
size -= 2;
EXPECT_EQ(size, read_buffer.Length());
ASSERT_TRUE(read_buffer.ReadUVarint(&val4));
EXPECT_EQ(149u, val4);
size -= 2;
EXPECT_EQ(size, read_buffer.Length());
ASSERT_TRUE(read_buffer.ReadUVarint(&val5));
EXPECT_EQ(68719476736u, val5);
size -= 6;
EXPECT_EQ(size, read_buffer.Length());
}
ASSERT_TRUE(read_buffer.ReadUVarint(&val5));
EXPECT_EQ(68719476736u, val5);
size -= 6;
EXPECT_EQ(size, read_buffer.Length());
}
} // namespace rtc

View file

@ -72,8 +72,6 @@ class MockKeyFrameRequestSender : public KeyFrameRequestSender {
class MockOnCompleteFrameCallback
: public video_coding::OnCompleteFrameCallback {
public:
MockOnCompleteFrameCallback() : buffer_(rtc::ByteBuffer::ORDER_NETWORK) {}
MOCK_METHOD1(DoOnCompleteFrame, void(video_coding::EncodedFrame* frame));
MOCK_METHOD1(DoOnCompleteFrameFailNullptr,
void(video_coding::EncodedFrame* frame));