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