henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2004 The WebRTC Project Authors. All rights reserved. |
| 3 | * |
| 4 | * Use of this source code is governed by a BSD-style license |
| 5 | * that can be found in the LICENSE file in the root of the source |
| 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 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 11 | #ifndef RTC_BASE_STREAM_H_ |
| 12 | #define RTC_BASE_STREAM_H_ |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 13 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 14 | #include <memory> |
| 15 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 16 | #include "rtc_base/buffer.h" |
Steve Anton | 10542f2 | 2019-01-11 17:11:00 | [diff] [blame] | 17 | #include "rtc_base/constructor_magic.h" |
| 18 | #include "rtc_base/critical_section.h" |
| 19 | #include "rtc_base/message_handler.h" |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 20 | #include "rtc_base/system/rtc_export.h" |
Artem Titov | e41c433 | 2018-07-25 13:04:28 | [diff] [blame] | 21 | #include "rtc_base/third_party/sigslot/sigslot.h" |
Sebastian Jansson | 4db28b5 | 2020-01-08 13:07:15 | [diff] [blame] | 22 | #include "rtc_base/thread.h" |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 23 | |
| 24 | namespace rtc { |
| 25 | |
| 26 | /////////////////////////////////////////////////////////////////////////////// |
| 27 | // StreamInterface is a generic asynchronous stream interface, supporting read, |
| 28 | // write, and close operations, and asynchronous signalling of state changes. |
| 29 | // The interface is designed with file, memory, and socket implementations in |
| 30 | // mind. Some implementations offer extended operations, such as seeking. |
| 31 | /////////////////////////////////////////////////////////////////////////////// |
| 32 | |
| 33 | // The following enumerations are declared outside of the StreamInterface |
| 34 | // class for brevity in use. |
| 35 | |
| 36 | // The SS_OPENING state indicates that the stream will signal open or closed |
| 37 | // in the future. |
| 38 | enum StreamState { SS_CLOSED, SS_OPENING, SS_OPEN }; |
| 39 | |
| 40 | // Stream read/write methods return this value to indicate various success |
| 41 | // and failure conditions described below. |
| 42 | enum StreamResult { SR_ERROR, SR_SUCCESS, SR_BLOCK, SR_EOS }; |
| 43 | |
| 44 | // StreamEvents are used to asynchronously signal state transitionss. The flags |
| 45 | // may be combined. |
| 46 | // SE_OPEN: The stream has transitioned to the SS_OPEN state |
| 47 | // SE_CLOSE: The stream has transitioned to the SS_CLOSED state |
| 48 | // SE_READ: Data is available, so Read is likely to not return SR_BLOCK |
| 49 | // SE_WRITE: Data can be written, so Write is likely to not return SR_BLOCK |
| 50 | enum StreamEvent { SE_OPEN = 1, SE_READ = 2, SE_WRITE = 4, SE_CLOSE = 8 }; |
| 51 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 52 | struct StreamEventData : public MessageData { |
| 53 | int events, error; |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 54 | StreamEventData(int ev, int er) : events(ev), error(er) {} |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 55 | }; |
| 56 | |
Mirko Bonadei | 35214fc | 2019-09-23 12:54:28 | [diff] [blame] | 57 | class RTC_EXPORT StreamInterface : public MessageHandler { |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 58 | public: |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 59 | enum { MSG_POST_EVENT = 0xF1F1, MSG_MAX = MSG_POST_EVENT }; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 60 | |
| 61 | ~StreamInterface() override; |
| 62 | |
| 63 | virtual StreamState GetState() const = 0; |
| 64 | |
| 65 | // Read attempts to fill buffer of size buffer_len. Write attempts to send |
| 66 | // data_len bytes stored in data. The variables read and write are set only |
| 67 | // on SR_SUCCESS (see below). Likewise, error is only set on SR_ERROR. |
| 68 | // Read and Write return a value indicating: |
| 69 | // SR_ERROR: an error occurred, which is returned in a non-null error |
| 70 | // argument. Interpretation of the error requires knowledge of the |
| 71 | // stream's concrete type, which limits its usefulness. |
| 72 | // SR_SUCCESS: some number of bytes were successfully written, which is |
| 73 | // returned in a non-null read/write argument. |
| 74 | // SR_BLOCK: the stream is in non-blocking mode, and the operation would |
| 75 | // block, or the stream is in SS_OPENING state. |
| 76 | // SR_EOS: the end-of-stream has been reached, or the stream is in the |
| 77 | // SS_CLOSED state. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 78 | virtual StreamResult Read(void* buffer, |
| 79 | size_t buffer_len, |
| 80 | size_t* read, |
| 81 | int* error) = 0; |
| 82 | virtual StreamResult Write(const void* data, |
| 83 | size_t data_len, |
| 84 | size_t* written, |
| 85 | int* error) = 0; |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 86 | // Attempt to transition to the SS_CLOSED state. SE_CLOSE will not be |
| 87 | // signalled as a result of this call. |
| 88 | virtual void Close() = 0; |
| 89 | |
| 90 | // Streams may signal one or more StreamEvents to indicate state changes. |
| 91 | // The first argument identifies the stream on which the state change occured. |
| 92 | // The second argument is a bit-wise combination of StreamEvents. |
| 93 | // If SE_CLOSE is signalled, then the third argument is the associated error |
| 94 | // code. Otherwise, the value is undefined. |
| 95 | // Note: Not all streams will support asynchronous event signalling. However, |
| 96 | // SS_OPENING and SR_BLOCK returned from stream member functions imply that |
| 97 | // certain events will be raised in the future. |
| 98 | sigslot::signal3<StreamInterface*, int, int> SignalEvent; |
| 99 | |
| 100 | // Like calling SignalEvent, but posts a message to the specified thread, |
| 101 | // which will call SignalEvent. This helps unroll the stack and prevent |
| 102 | // re-entrancy. |
| 103 | void PostEvent(Thread* t, int events, int err); |
| 104 | // Like the aforementioned method, but posts to the current thread. |
| 105 | void PostEvent(int events, int err); |
| 106 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 107 | // Return true if flush is successful. |
| 108 | virtual bool Flush(); |
| 109 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 110 | // |
| 111 | // CONVENIENCE METHODS |
| 112 | // |
| 113 | // These methods are implemented in terms of other methods, for convenience. |
| 114 | // |
| 115 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 116 | // WriteAll is a helper function which repeatedly calls Write until all the |
| 117 | // data is written, or something other than SR_SUCCESS is returned. Note that |
| 118 | // unlike Write, the argument 'written' is always set, and may be non-zero |
| 119 | // on results other than SR_SUCCESS. The remaining arguments have the |
| 120 | // same semantics as Write. |
Yves Gerey | 665174f | 2018-06-19 13:03:05 | [diff] [blame] | 121 | StreamResult WriteAll(const void* data, |
| 122 | size_t data_len, |
| 123 | size_t* written, |
| 124 | int* error); |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 125 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 126 | protected: |
| 127 | StreamInterface(); |
| 128 | |
| 129 | // MessageHandler Interface |
| 130 | void OnMessage(Message* msg) override; |
| 131 | |
| 132 | private: |
| 133 | RTC_DISALLOW_COPY_AND_ASSIGN(StreamInterface); |
| 134 | }; |
| 135 | |
| 136 | /////////////////////////////////////////////////////////////////////////////// |
| 137 | // StreamAdapterInterface is a convenient base-class for adapting a stream. |
| 138 | // By default, all operations are pass-through. Override the methods that you |
| 139 | // require adaptation. Streams should really be upgraded to reference-counted. |
| 140 | // In the meantime, use the owned flag to indicate whether the adapter should |
| 141 | // own the adapted stream. |
| 142 | /////////////////////////////////////////////////////////////////////////////// |
| 143 | |
| 144 | class StreamAdapterInterface : public StreamInterface, |
| 145 | public sigslot::has_slots<> { |
| 146 | public: |
| 147 | explicit StreamAdapterInterface(StreamInterface* stream, bool owned = true); |
| 148 | |
| 149 | // Core Stream Interface |
| 150 | StreamState GetState() const override; |
| 151 | StreamResult Read(void* buffer, |
| 152 | size_t buffer_len, |
| 153 | size_t* read, |
| 154 | int* error) override; |
| 155 | StreamResult Write(const void* data, |
| 156 | size_t data_len, |
| 157 | size_t* written, |
| 158 | int* error) override; |
| 159 | void Close() override; |
| 160 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 161 | bool Flush() override; |
| 162 | |
| 163 | void Attach(StreamInterface* stream, bool owned = true); |
| 164 | StreamInterface* Detach(); |
| 165 | |
| 166 | protected: |
| 167 | ~StreamAdapterInterface() override; |
| 168 | |
| 169 | // Note that the adapter presents itself as the origin of the stream events, |
| 170 | // since users of the adapter may not recognize the adapted object. |
| 171 | virtual void OnEvent(StreamInterface* stream, int events, int err); |
| 172 | StreamInterface* stream() { return stream_; } |
| 173 | |
| 174 | private: |
| 175 | StreamInterface* stream_; |
| 176 | bool owned_; |
| 177 | RTC_DISALLOW_COPY_AND_ASSIGN(StreamAdapterInterface); |
| 178 | }; |
| 179 | |
Henrik Kjellander | ec78f1c | 2017-06-29 05:52:50 | [diff] [blame] | 180 | } // namespace rtc |
henrike@webrtc.org | f048872 | 2014-05-13 18:00:26 | [diff] [blame] | 181 | |
Mirko Bonadei | 92ea95e | 2017-09-15 04:47:31 | [diff] [blame] | 182 | #endif // RTC_BASE_STREAM_H_ |