blob: b535f168821984a2964f2988305eeb862717109f [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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 Bonadei92ea95e2017-09-15 06:47:31 +020011#ifndef RTC_BASE_STREAM_H_
12#define RTC_BASE_STREAM_H_
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000013
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020014#include <stdio.h>
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000015
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020016#include <memory>
17
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020018#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080019#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 Titove41c4332018-07-25 15:04:28 +020023#include "rtc_base/third_party/sigslot/sigslot.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020024
25namespace 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.
39enum 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.
43enum 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
51enum StreamEvent { SE_OPEN = 1, SE_READ = 2, SE_WRITE = 4, SE_CLOSE = 8 };
52
53class Thread;
54
55struct StreamEventData : public MessageData {
56 int events, error;
Yves Gerey665174f2018-06-19 15:03:05 +020057 StreamEventData(int ev, int er) : events(ev), error(er) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020058};
59
60class StreamInterface : public MessageHandler {
61 public:
Yves Gerey665174f2018-06-19 15:03:05 +020062 enum { MSG_POST_EVENT = 0xF1F1, MSG_MAX = MSG_POST_EVENT };
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020063
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 Gerey665174f2018-06-19 15:03:05 +020081 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 Kjellanderec78f1c2017-06-29 07:52:50 +020089 // 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 Kjellanderec78f1c2017-06-29 07:52:50 +0200110 // Return true if flush is successful.
111 virtual bool Flush();
112
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200113 //
114 // CONVENIENCE METHODS
115 //
116 // These methods are implemented in terms of other methods, for convenience.
117 //
118
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200119 // 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 Gerey665174f2018-06-19 15:03:05 +0200124 StreamResult WriteAll(const void* data,
125 size_t data_len,
126 size_t* written,
127 int* error);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200128
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200129 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
147class 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 Kjellanderec78f1c2017-06-29 07:52:50 +0200164 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 Kjellanderec78f1c2017-06-29 07:52:50 +0200184// FileStream is a simple implementation of a StreamInterface, which does not
185// support asynchronous notification.
186///////////////////////////////////////////////////////////////////////////////
187
Niels Möller23213d92019-01-22 11:01:24 +0100188// TODO(bugs.webrtc.org/6463): Delete this class.
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200189class 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 Gerey665174f2018-06-19 15:03:05 +0200196 virtual bool OpenShare(const std::string& filename,
197 const char* mode,
198 int shflag,
199 int* error);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200200
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öller1a86b782019-01-14 12:48:53 +0100215 virtual bool SetPosition(size_t position);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200216
217 bool Flush() override;
218
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200219 protected:
220 virtual void DoClose();
221
222 FILE* file_;
223
224 private:
225 RTC_DISALLOW_COPY_AND_ASSIGN(FileStream);
226};
227
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200228} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000229
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200230#endif // RTC_BASE_STREAM_H_