| 6 | * tree. An additional intellectual property rights grant can be found |
| 7 | * in the file PATENTS. All contributing project authors may |
| 8 | * be found in the AUTHORS file in the root of the source tree. |
| 9 | */ |
| 10 | #include "rtc_base/stream.h" |
| 11 |
| 12 | #include <errno.h> |
| 13 | #include <string.h> |
| 14 |
| 15 | #include <algorithm> |
| 16 | #include <string> |
| 17 |
| 18 | #include "rtc_base/checks.h" |
| 19 | #include "rtc_base/location.h" |
| 20 | #include "rtc_base/thread.h" |
| 21 |
| 22 | namespace rtc { |
| 23 |
| 24 | /////////////////////////////////////////////////////////////////////////////// |
| 25 | // StreamInterface |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | StreamInterface::~StreamInterface() {} |
| 28 |
| 29 | StreamResult StreamInterface::WriteAll(const void* data, |
| 30 | size_t data_len, |
| 31 | size_t* written, |
| 32 | int* error) { |
| 33 | StreamResult result = SR_SUCCESS; |
| 34 | size_t total_written = 0, current_written; |
| 35 | while (total_written < data_len) { |
| 36 | result = Write(static_cast<const char*>(data) + total_written, |
| 37 | data_len - total_written, ¤t_written, error); |
| 38 | if (result != SR_SUCCESS) |
| 39 | break; |
| 40 | total_written += current_written; |
| 41 | } |
| 42 | if (written) |
| 43 | *written = total_written; |
| 44 | return result; |
| 45 | } |
| 46 |
| 47 | void StreamInterface::PostEvent(Thread* t, int events, int err) { |
| 48 | t->Post(RTC_FROM_HERE, this, MSG_POST_EVENT, |
| 49 | new StreamEventData(events, err)); |
| 50 | } |
| 51 |
| 52 | void StreamInterface::PostEvent(int events, int err) { |
| 53 | PostEvent(Thread::Current(), events, err); |
| 54 | } |
| 55 |
| 56 | bool StreamInterface::Flush() { |
| 57 | return false; |
| 58 | } |
| 59 |
| 60 | StreamInterface::StreamInterface() {} |
| 61 |
| 62 | void StreamInterface::OnMessage(Message* msg) { |
| 63 | if (MSG_POST_EVENT == msg->message_id) { |
| 64 | StreamEventData* pe = static_cast<StreamEventData*>(msg->pdata); |
| 65 | SignalEvent(this, pe->events, pe->error); |
| 66 | delete msg->pdata; |
| 67 | } |
| 68 | } |
| 69 |
| 70 | /////////////////////////////////////////////////////////////////////////////// |
| 71 | // StreamAdapterInterface |
| 72 | /////////////////////////////////////////////////////////////////////////////// |
| 73 |
| 74 | StreamAdapterInterface::StreamAdapterInterface(StreamInterface* stream, |
| 75 | bool owned) |
| 76 | : stream_(stream), owned_(owned) { |
| 77 | if (nullptr != stream_) |
| 78 | stream_->SignalEvent.connect(this, &StreamAdapterInterface::OnEvent); |
| 79 | } |
| 80 |
| 81 | StreamState StreamAdapterInterface::GetState() const { |
| 82 | return stream_->GetState(); |
| 83 | } |
| 84 | StreamResult StreamAdapterInterface::Read(void* buffer, |
| 85 | size_t buffer_len, |
| 86 | size_t* read, |
| 87 | int* error) { |
| 88 | return stream_->Read(buffer, buffer_len, read, error); |
| 89 | } |
| 90 | StreamResult StreamAdapterInterface::Write(const void* data, |
| 91 | size_t data_len, |
| 92 | size_t* written, |
| 93 | int* error) { |
| 94 | return stream_->Write(data, data_len, written, error); |
| 95 | } |
| 96 | void StreamAdapterInterface::Close() { |
| 97 | stream_->Close(); |
| 98 | } |
| 99 |
| 100 | bool StreamAdapterInterface::Flush() { |
| 101 | return stream_->Flush(); |
| 102 | } |
| 103 |
| 104 | void StreamAdapterInterface::Attach(StreamInterface* stream, bool owned) { |
| 105 | if (nullptr != stream_) |
| 106 | stream_->SignalEvent.disconnect(this); |
| 107 | if (owned_) |
| 108 | delete stream_; |
| 109 | stream_ = stream; |
| 110 | owned_ = owned; |
| 111 | if (nullptr != stream_) |
| 112 | stream_->SignalEvent.connect(this, &StreamAdapterInterface::OnEvent); |
| 113 | } |
| 114 |
| 115 | StreamInterface* StreamAdapterInterface::Detach() { |
| 116 | if (nullptr != stream_) |
| 117 | stream_->SignalEvent.disconnect(this); |
| 118 | StreamInterface* stream = stream_; |
| 119 | stream_ = nullptr; |
| 120 | return stream; |
| 121 | } |
| 122 |
| 123 | StreamAdapterInterface::~StreamAdapterInterface() { |
| 124 | if (owned_) |
| 125 | delete stream_; |
| 126 | } |
| 127 |
| 128 | void StreamAdapterInterface::OnEvent(StreamInterface* stream, |
| 129 | int events, |
| 130 | int err) { |
| 131 | SignalEvent(this, events, err); |
| 132 | } |
| 133 |
| 134 | } // namespace rtc |
| 135 |