Delete unused rtc::Stream subclasses.

Also move StringStream to the only test using it.

Bug: webrtc:6424
Change-Id: Iad79c7becaa2764ac954c18711eaae4faf46ae72
Reviewed-on: https://webrtc-review.googlesource.com/84320
Reviewed-by: Karl Wiberg <kwiberg@webrtc.org>
Commit-Queue: Niels Moller <nisse@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23721}
This commit is contained in:
Niels Möller 2018-06-20 10:27:53 +02:00 committed by Commit Bot
parent ac5bbd940e
commit ff3dd0c49c
3 changed files with 108 additions and 345 deletions

View File

@ -19,6 +19,112 @@
namespace rtc { namespace rtc {
namespace {
class StringStream : public StreamInterface {
public:
explicit StringStream(std::string* str);
explicit StringStream(const std::string& str);
StreamState GetState() const override;
StreamResult Read(void* buffer,
size_t buffer_len,
size_t* read,
int* error) override;
StreamResult Write(const void* data,
size_t data_len,
size_t* written,
int* error) override;
void Close() override;
bool SetPosition(size_t position) override;
bool GetPosition(size_t* position) const override;
bool GetSize(size_t* size) const override;
bool GetAvailable(size_t* size) const override;
bool ReserveSize(size_t size) override;
private:
std::string& str_;
size_t read_pos_;
bool read_only_;
};
StringStream::StringStream(std::string* str)
: str_(*str), read_pos_(0), read_only_(false) {}
StringStream::StringStream(const std::string& str)
: str_(const_cast<std::string&>(str)), read_pos_(0), read_only_(true) {}
StreamState StringStream::GetState() const {
return SS_OPEN;
}
StreamResult StringStream::Read(void* buffer,
size_t buffer_len,
size_t* read,
int* error) {
size_t available = std::min(buffer_len, str_.size() - read_pos_);
if (!available)
return SR_EOS;
memcpy(buffer, str_.data() + read_pos_, available);
read_pos_ += available;
if (read)
*read = available;
return SR_SUCCESS;
}
StreamResult StringStream::Write(const void* data,
size_t data_len,
size_t* written,
int* error) {
if (read_only_) {
if (error) {
*error = -1;
}
return SR_ERROR;
}
str_.append(static_cast<const char*>(data),
static_cast<const char*>(data) + data_len);
if (written)
*written = data_len;
return SR_SUCCESS;
}
void StringStream::Close() {}
bool StringStream::SetPosition(size_t position) {
if (position > str_.size())
return false;
read_pos_ = position;
return true;
}
bool StringStream::GetPosition(size_t* position) const {
if (position)
*position = read_pos_;
return true;
}
bool StringStream::GetSize(size_t* size) const {
if (size)
*size = str_.size();
return true;
}
bool StringStream::GetAvailable(size_t* size) const {
if (size)
*size = str_.size() - read_pos_;
return true;
}
bool StringStream::ReserveSize(size_t size) {
if (read_only_)
return false;
str_.reserve(size);
return true;
}
} // namespace
template <typename Base> template <typename Base>
class LogSinkImpl : public LogSink, public Base { class LogSinkImpl : public LogSink, public Base {
public: public:
@ -170,9 +276,6 @@ class LogThread {
// Ensure we don't crash when adding/removing streams while threads are going. // Ensure we don't crash when adding/removing streams while threads are going.
// We should restore the correct global state at the end. // We should restore the correct global state at the end.
// This test also makes sure that the 'noop' stream() singleton object, can be
// safely used from mutiple threads since the threads log at LS_SENSITIVE
// (by default 'noop' entries).
TEST(LogTest, MultipleThreads) { TEST(LogTest, MultipleThreads) {
int sev = LogMessage::GetLogToStream(nullptr); int sev = LogMessage::GetLogToStream(nullptr);
@ -181,7 +284,8 @@ TEST(LogTest, MultipleThreads) {
thread2.Start(); thread2.Start();
thread3.Start(); thread3.Start();
LogSinkImpl<NullStream> stream1, stream2, stream3; std::string s1, s2, s3;
LogSinkImpl<StringStream> stream1(&s1), stream2(&s2), stream3(&s3);
for (int i = 0; i < 1000; ++i) { for (int i = 0; i < 1000; ++i) {
LogMessage::AddLogToStream(&stream1, LS_INFO); LogMessage::AddLogToStream(&stream1, LS_INFO);
LogMessage::AddLogToStream(&stream2, LS_VERBOSE); LogMessage::AddLogToStream(&stream2, LS_VERBOSE);

View File

@ -238,99 +238,6 @@ void StreamAdapterInterface::OnEvent(StreamInterface* stream,
SignalEvent(this, events, err); SignalEvent(this, events, err);
} }
///////////////////////////////////////////////////////////////////////////////
// StreamTap
///////////////////////////////////////////////////////////////////////////////
StreamTap::StreamTap(StreamInterface* stream, StreamInterface* tap)
: StreamAdapterInterface(stream),
tap_(),
tap_result_(SR_SUCCESS),
tap_error_(0) {
AttachTap(tap);
}
StreamTap::~StreamTap() = default;
void StreamTap::AttachTap(StreamInterface* tap) {
tap_.reset(tap);
}
StreamInterface* StreamTap::DetachTap() {
return tap_.release();
}
StreamResult StreamTap::GetTapResult(int* error) {
if (error) {
*error = tap_error_;
}
return tap_result_;
}
StreamResult StreamTap::Read(void* buffer,
size_t buffer_len,
size_t* read,
int* error) {
size_t backup_read;
if (!read) {
read = &backup_read;
}
StreamResult res =
StreamAdapterInterface::Read(buffer, buffer_len, read, error);
if ((res == SR_SUCCESS) && (tap_result_ == SR_SUCCESS)) {
tap_result_ = tap_->WriteAll(buffer, *read, nullptr, &tap_error_);
}
return res;
}
StreamResult StreamTap::Write(const void* data,
size_t data_len,
size_t* written,
int* error) {
size_t backup_written;
if (!written) {
written = &backup_written;
}
StreamResult res =
StreamAdapterInterface::Write(data, data_len, written, error);
if ((res == SR_SUCCESS) && (tap_result_ == SR_SUCCESS)) {
tap_result_ = tap_->WriteAll(data, *written, nullptr, &tap_error_);
}
return res;
}
///////////////////////////////////////////////////////////////////////////////
// NullStream
///////////////////////////////////////////////////////////////////////////////
NullStream::NullStream() {}
NullStream::~NullStream() {}
StreamState NullStream::GetState() const {
return SS_OPEN;
}
StreamResult NullStream::Read(void* buffer,
size_t buffer_len,
size_t* read,
int* error) {
if (error)
*error = -1;
return SR_ERROR;
}
StreamResult NullStream::Write(const void* data,
size_t data_len,
size_t* written,
int* error) {
if (written)
*written = data_len;
return SR_SUCCESS;
}
void NullStream::Close() {}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// FileStream // FileStream
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -893,108 +800,6 @@ StreamResult FifoBuffer::WriteOffsetLocked(const void* buffer,
return SR_SUCCESS; return SR_SUCCESS;
} }
///////////////////////////////////////////////////////////////////////////////
// StringStream - Reads/Writes to an external std::string
///////////////////////////////////////////////////////////////////////////////
StringStream::StringStream(std::string* str)
: str_(*str), read_pos_(0), read_only_(false) {}
StringStream::StringStream(const std::string& str)
: str_(const_cast<std::string&>(str)), read_pos_(0), read_only_(true) {}
StreamState StringStream::GetState() const {
return SS_OPEN;
}
StreamResult StringStream::Read(void* buffer,
size_t buffer_len,
size_t* read,
int* error) {
size_t available = std::min(buffer_len, str_.size() - read_pos_);
if (!available)
return SR_EOS;
memcpy(buffer, str_.data() + read_pos_, available);
read_pos_ += available;
if (read)
*read = available;
return SR_SUCCESS;
}
StreamResult StringStream::Write(const void* data,
size_t data_len,
size_t* written,
int* error) {
if (read_only_) {
if (error) {
*error = -1;
}
return SR_ERROR;
}
str_.append(static_cast<const char*>(data),
static_cast<const char*>(data) + data_len);
if (written)
*written = data_len;
return SR_SUCCESS;
}
void StringStream::Close() {}
bool StringStream::SetPosition(size_t position) {
if (position > str_.size())
return false;
read_pos_ = position;
return true;
}
bool StringStream::GetPosition(size_t* position) const {
if (position)
*position = read_pos_;
return true;
}
bool StringStream::GetSize(size_t* size) const {
if (size)
*size = str_.size();
return true;
}
bool StringStream::GetAvailable(size_t* size) const {
if (size)
*size = str_.size() - read_pos_;
return true;
}
bool StringStream::ReserveSize(size_t size) {
if (read_only_)
return false;
str_.reserve(size);
return true;
}
///////////////////////////////////////////////////////////////////////////////
// StreamReference
///////////////////////////////////////////////////////////////////////////////
StreamReference::StreamReference(StreamInterface* stream)
: StreamAdapterInterface(stream, false) {
// owner set to false so the destructor does not free the stream.
stream_ref_count_ = new StreamRefCount(stream);
}
StreamInterface* StreamReference::NewReference() {
stream_ref_count_->AddReference();
return new StreamReference(stream_ref_count_, stream());
}
StreamReference::~StreamReference() {
stream_ref_count_->Release();
}
StreamReference::StreamReference(StreamRefCount* stream_ref_count,
StreamInterface* stream)
: StreamAdapterInterface(stream, false),
stream_ref_count_(stream_ref_count) {}
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////

View File

@ -315,60 +315,6 @@ class StreamAdapterInterface : public StreamInterface,
RTC_DISALLOW_COPY_AND_ASSIGN(StreamAdapterInterface); RTC_DISALLOW_COPY_AND_ASSIGN(StreamAdapterInterface);
}; };
///////////////////////////////////////////////////////////////////////////////
// StreamTap is a non-modifying, pass-through adapter, which copies all data
// in either direction to the tap. Note that errors or blocking on writing to
// the tap will prevent further tap writes from occurring.
///////////////////////////////////////////////////////////////////////////////
class StreamTap : public StreamAdapterInterface {
public:
explicit StreamTap(StreamInterface* stream, StreamInterface* tap);
~StreamTap() override;
void AttachTap(StreamInterface* tap);
StreamInterface* DetachTap();
StreamResult GetTapResult(int* error);
// StreamAdapterInterface Interface
StreamResult Read(void* buffer,
size_t buffer_len,
size_t* read,
int* error) override;
StreamResult Write(const void* data,
size_t data_len,
size_t* written,
int* error) override;
private:
std::unique_ptr<StreamInterface> tap_;
StreamResult tap_result_;
int tap_error_;
RTC_DISALLOW_COPY_AND_ASSIGN(StreamTap);
};
///////////////////////////////////////////////////////////////////////////////
// NullStream gives errors on read, and silently discards all written data.
///////////////////////////////////////////////////////////////////////////////
class NullStream : public StreamInterface {
public:
NullStream();
~NullStream() override;
// StreamInterface Interface
StreamState GetState() const override;
StreamResult Read(void* buffer,
size_t buffer_len,
size_t* read,
int* error) override;
StreamResult Write(const void* data,
size_t data_len,
size_t* written,
int* error) override;
void Close() override;
};
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// FileStream is a simple implementation of a StreamInterface, which does not // FileStream is a simple implementation of a StreamInterface, which does not
// support asynchronous notification. // support asynchronous notification.
@ -572,98 +518,6 @@ class FifoBuffer : public StreamInterface {
RTC_DISALLOW_COPY_AND_ASSIGN(FifoBuffer); RTC_DISALLOW_COPY_AND_ASSIGN(FifoBuffer);
}; };
///////////////////////////////////////////////////////////////////////////////
// StringStream - Reads/Writes to an external std::string
///////////////////////////////////////////////////////////////////////////////
class StringStream : public StreamInterface {
public:
explicit StringStream(std::string* str);
explicit StringStream(const std::string& str);
StreamState GetState() const override;
StreamResult Read(void* buffer,
size_t buffer_len,
size_t* read,
int* error) override;
StreamResult Write(const void* data,
size_t data_len,
size_t* written,
int* error) override;
void Close() override;
bool SetPosition(size_t position) override;
bool GetPosition(size_t* position) const override;
bool GetSize(size_t* size) const override;
bool GetAvailable(size_t* size) const override;
bool ReserveSize(size_t size) override;
private:
std::string& str_;
size_t read_pos_;
bool read_only_;
};
///////////////////////////////////////////////////////////////////////////////
// StreamReference - A reference counting stream adapter
///////////////////////////////////////////////////////////////////////////////
// Keep in mind that the streams and adapters defined in this file are
// not thread-safe, so this has limited uses.
// A StreamRefCount holds the reference count and a pointer to the
// wrapped stream. It deletes the wrapped stream when there are no
// more references. We can then have multiple StreamReference
// instances pointing to one StreamRefCount, all wrapping the same
// stream.
class StreamReference : public StreamAdapterInterface {
class StreamRefCount;
public:
// Constructor for the first reference to a stream
// Note: get more references through NewReference(). Use this
// constructor only once on a given stream.
explicit StreamReference(StreamInterface* stream);
StreamInterface* GetStream() { return stream(); }
StreamInterface* NewReference();
~StreamReference() override;
private:
class StreamRefCount {
public:
explicit StreamRefCount(StreamInterface* stream)
: stream_(stream), ref_count_(1) {}
void AddReference() {
CritScope lock(&cs_);
++ref_count_;
}
void Release() {
int ref_count;
{ // Atomic ops would have been a better fit here.
CritScope lock(&cs_);
ref_count = --ref_count_;
}
if (ref_count == 0) {
delete stream_;
delete this;
}
}
private:
StreamInterface* stream_;
int ref_count_;
CriticalSection cs_;
RTC_DISALLOW_COPY_AND_ASSIGN(StreamRefCount);
};
// Constructor for adding references
explicit StreamReference(StreamRefCount* stream_ref_count,
StreamInterface* stream);
StreamRefCount* stream_ref_count_;
RTC_DISALLOW_COPY_AND_ASSIGN(StreamReference);
};
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
// Flow attempts to move bytes from source to sink via buffer of size // Flow attempts to move bytes from source to sink via buffer of size