Convert MemoryStream to use new StreamInterface

Bug: webrtc:14632
Change-Id: Id6a7e011a6102e829a14de246d07a9aab1e6934f
Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/283620
Commit-Queue: Harald Alvestrand <hta@webrtc.org>
Reviewed-by: Tomas Gunnarsson <tommi@webrtc.org>
Cr-Commit-Position: refs/heads/main@{#38639}
This commit is contained in:
Harald Alvestrand 2022-11-16 07:29:57 +00:00 committed by WebRTC LUCI CQ
parent da4c102cbd
commit dd4c4068d9
4 changed files with 73 additions and 48 deletions

View file

@ -232,8 +232,10 @@ class PseudoTcpTest : public PseudoTcpTestBase {
// Create some dummy data to send.
send_stream_.ReserveSize(size);
for (int i = 0; i < size; ++i) {
char ch = static_cast<char>(i);
send_stream_.Write(&ch, 1, NULL, NULL);
uint8_t ch = static_cast<uint8_t>(i);
size_t written;
int error;
send_stream_.Write(rtc::MakeArrayView(&ch, 1), written, error);
}
send_stream_.Rewind();
// Prepare the receive stream.
@ -296,7 +298,11 @@ class PseudoTcpTest : public PseudoTcpTestBase {
do {
rcvd = remote_.Recv(block, sizeof(block));
if (rcvd != -1) {
recv_stream_.Write(block, rcvd, NULL, NULL);
size_t written;
int error;
recv_stream_.Write(
rtc::MakeArrayView(reinterpret_cast<uint8_t*>(block), rcvd),
written, error);
recv_stream_.GetPosition(&position);
RTC_LOG(LS_VERBOSE) << "Received: " << position;
}
@ -308,8 +314,10 @@ class PseudoTcpTest : public PseudoTcpTestBase {
char block[kBlockSize];
do {
send_stream_.GetPosition(&position);
if (send_stream_.Read(block, sizeof(block), &tosend, NULL) !=
rtc::SR_EOS) {
int error;
if (send_stream_.Read(
rtc::MakeArrayView(reinterpret_cast<uint8_t*>(block), kBlockSize),
tosend, error) != rtc::SR_EOS) {
sent = local_.Send(block, tosend);
UpdateLocalClock();
if (sent != -1) {
@ -347,8 +355,10 @@ class PseudoTcpTestPingPong : public PseudoTcpTestBase {
// Create some dummy data to send.
send_stream_.ReserveSize(size);
for (int i = 0; i < size; ++i) {
char ch = static_cast<char>(i);
send_stream_.Write(&ch, 1, NULL, NULL);
uint8_t ch = static_cast<uint8_t>(i);
size_t written;
int error;
send_stream_.Write(rtc::MakeArrayView(&ch, 1), written, error);
}
send_stream_.Rewind();
// Prepare the receive stream.
@ -411,7 +421,11 @@ class PseudoTcpTestPingPong : public PseudoTcpTestBase {
do {
rcvd = receiver_->Recv(block, sizeof(block));
if (rcvd != -1) {
recv_stream_.Write(block, rcvd, NULL, NULL);
size_t written;
int error;
recv_stream_.Write(
rtc::MakeArrayView(reinterpret_cast<const uint8_t*>(block), rcvd),
written, error);
recv_stream_.GetPosition(&position);
RTC_LOG(LS_VERBOSE) << "Received: " << position;
}
@ -424,7 +438,10 @@ class PseudoTcpTestPingPong : public PseudoTcpTestBase {
do {
send_stream_.GetPosition(&position);
tosend = bytes_per_send_ ? bytes_per_send_ : sizeof(block);
if (send_stream_.Read(block, tosend, &tosend, NULL) != rtc::SR_EOS) {
int error;
if (send_stream_.Read(
rtc::MakeArrayView(reinterpret_cast<uint8_t*>(block), tosend),
tosend, error) != rtc::SR_EOS) {
sent = sender_->Send(block, tosend);
UpdateLocalClock();
if (sent != -1) {
@ -458,8 +475,10 @@ class PseudoTcpTestReceiveWindow : public PseudoTcpTestBase {
// Create some dummy data to send.
send_stream_.ReserveSize(size);
for (int i = 0; i < size; ++i) {
char ch = static_cast<char>(i);
send_stream_.Write(&ch, 1, NULL, NULL);
uint8_t ch = static_cast<uint8_t>(i);
size_t written;
int error;
send_stream_.Write(rtc::MakeArrayView(&ch, 1), written, error);
}
send_stream_.Rewind();
@ -510,7 +529,11 @@ class PseudoTcpTestReceiveWindow : public PseudoTcpTestBase {
do {
rcvd = remote_.Recv(block, sizeof(block));
if (rcvd != -1) {
recv_stream_.Write(block, rcvd, NULL, NULL);
size_t written;
int error;
recv_stream_.Write(
rtc::MakeArrayView(reinterpret_cast<uint8_t*>(block), rcvd),
written, error);
recv_stream_.GetPosition(&position);
RTC_LOG(LS_VERBOSE) << "Received: " << position;
}
@ -534,8 +557,11 @@ class PseudoTcpTestReceiveWindow : public PseudoTcpTestBase {
char block[kBlockSize];
do {
send_stream_.GetPosition(&position);
if (send_stream_.Read(block, sizeof(block), &tosend, NULL) !=
rtc::SR_EOS) {
int error;
if (send_stream_.Read(
rtc::MakeArrayView(reinterpret_cast<uint8_t*>(block),
sizeof(block)),
tosend, error) != rtc::SR_EOS) {
sent = local_.Send(block, tosend);
UpdateLocalClock();
if (sent != -1) {

View file

@ -23,38 +23,37 @@ StreamState MemoryStream::GetState() const {
return SS_OPEN;
}
StreamResult MemoryStream::Read(void* buffer,
size_t bytes,
size_t* bytes_read,
int* error) {
StreamResult MemoryStream::Read(rtc::ArrayView<uint8_t> buffer,
size_t& bytes_read,
int& error) {
if (seek_position_ >= data_length_) {
return SR_EOS;
}
size_t available = data_length_ - seek_position_;
if (bytes > available) {
size_t bytes;
if (buffer.size() > available) {
// Read partial buffer
bytes = available;
} else {
bytes = buffer.size();
}
memcpy(buffer, &buffer_[seek_position_], bytes);
memcpy(buffer.data(), &buffer_[seek_position_], bytes);
seek_position_ += bytes;
if (bytes_read) {
*bytes_read = bytes;
}
bytes_read = bytes;
return SR_SUCCESS;
}
StreamResult MemoryStream::Write(const void* buffer,
size_t bytes,
size_t* bytes_written,
int* error) {
StreamResult MemoryStream::Write(rtc::ArrayView<const uint8_t> buffer,
size_t& bytes_written,
int& error) {
size_t available = buffer_length_ - seek_position_;
if (0 == available) {
// Increase buffer size to the larger of:
// a) new position rounded up to next 256 bytes
// b) double the previous length
size_t new_buffer_length =
std::max(((seek_position_ + bytes) | 0xFF) + 1, buffer_length_ * 2);
StreamResult result = DoReserve(new_buffer_length, error);
size_t new_buffer_length = std::max(
((seek_position_ + buffer.size()) | 0xFF) + 1, buffer_length_ * 2);
StreamResult result = DoReserve(new_buffer_length, &error);
if (SR_SUCCESS != result) {
return result;
}
@ -62,17 +61,16 @@ StreamResult MemoryStream::Write(const void* buffer,
available = buffer_length_ - seek_position_;
}
size_t bytes = buffer.size();
if (bytes > available) {
bytes = available;
}
memcpy(&buffer_[seek_position_], buffer, bytes);
memcpy(&buffer_[seek_position_], buffer.data(), bytes);
seek_position_ += bytes;
if (data_length_ < seek_position_) {
data_length_ = seek_position_;
}
if (bytes_written) {
*bytes_written = bytes;
}
bytes_written = bytes;
return SR_SUCCESS;
}

View file

@ -25,14 +25,12 @@ class MemoryStream final : public StreamInterface {
~MemoryStream() override;
StreamState GetState() const override;
StreamResult Read(void* buffer,
size_t bytes,
size_t* bytes_read,
int* error) override;
StreamResult Write(const void* buffer,
size_t bytes,
size_t* bytes_written,
int* error) override;
StreamResult Read(rtc::ArrayView<uint8_t> buffer,
size_t& bytes_read,
int& error) override;
StreamResult Write(rtc::ArrayView<const uint8_t> buffer,
size_t& bytes_written,
int& error) override;
void Close() override;
bool GetSize(size_t* size) const;
bool ReserveSize(size_t size);

View file

@ -815,8 +815,10 @@ class SSLStreamAdapterTestTLS
send_stream_.ReserveSize(size);
for (int i = 0; i < size; ++i) {
char ch = static_cast<char>(i);
send_stream_.Write(&ch, 1, nullptr, nullptr);
uint8_t ch = static_cast<uint8_t>(i);
size_t written;
int error;
send_stream_.Write(rtc::MakeArrayView(&ch, 1), written, error);
}
send_stream_.Rewind();
@ -849,8 +851,8 @@ class SSLStreamAdapterTestTLS
for (;;) {
send_stream_.GetPosition(&position);
if (send_stream_.Read(block, sizeof(block), &tosend, nullptr) !=
rtc::SR_EOS) {
int dummy_error;
if (send_stream_.Read(block, tosend, dummy_error) != rtc::SR_EOS) {
int error;
rv = client_ssl_->Write(rtc::MakeArrayView(block, tosend), sent, error);
@ -895,8 +897,9 @@ class SSLStreamAdapterTestTLS
ASSERT_EQ(rtc::SR_SUCCESS, r);
RTC_LOG(LS_VERBOSE) << "Read " << bread;
recv_stream_.Write(buffer, bread, nullptr, nullptr);
size_t written;
int error;
recv_stream_.Write(rtc::MakeArrayView(buffer, bread), written, error);
}
}