blob: 9147dadd0cdb675361f65e9f1a204d2d7924dabe [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 <memory>
15
Mirko Bonadei92ea95e2017-09-15 06:47:31 +020016#include "rtc_base/buffer.h"
Steve Anton10542f22019-01-11 09:11:00 -080017#include "rtc_base/constructor_magic.h"
18#include "rtc_base/critical_section.h"
19#include "rtc_base/message_handler.h"
20#include "rtc_base/message_queue.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020021#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 15:04:28 +020022#include "rtc_base/third_party/sigslot/sigslot.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020023
24namespace 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.
38enum 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.
42enum 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
50enum StreamEvent { SE_OPEN = 1, SE_READ = 2, SE_WRITE = 4, SE_CLOSE = 8 };
51
52class Thread;
53
54struct StreamEventData : public MessageData {
55 int events, error;
Yves Gerey665174f2018-06-19 15:03:05 +020056 StreamEventData(int ev, int er) : events(ev), error(er) {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020057};
58
Mirko Bonadei35214fc2019-09-23 14:54:28 +020059class RTC_EXPORT StreamInterface : public MessageHandler {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020060 public:
Yves Gerey665174f2018-06-19 15:03:05 +020061 enum { MSG_POST_EVENT = 0xF1F1, MSG_MAX = MSG_POST_EVENT };
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020062
63 ~StreamInterface() override;
64
65 virtual StreamState GetState() const = 0;
66
67 // Read attempts to fill buffer of size buffer_len. Write attempts to send
68 // data_len bytes stored in data. The variables read and write are set only
69 // on SR_SUCCESS (see below). Likewise, error is only set on SR_ERROR.
70 // Read and Write return a value indicating:
71 // SR_ERROR: an error occurred, which is returned in a non-null error
72 // argument. Interpretation of the error requires knowledge of the
73 // stream's concrete type, which limits its usefulness.
74 // SR_SUCCESS: some number of bytes were successfully written, which is
75 // returned in a non-null read/write argument.
76 // SR_BLOCK: the stream is in non-blocking mode, and the operation would
77 // block, or the stream is in SS_OPENING state.
78 // SR_EOS: the end-of-stream has been reached, or the stream is in the
79 // SS_CLOSED state.
Yves Gerey665174f2018-06-19 15:03:05 +020080 virtual StreamResult Read(void* buffer,
81 size_t buffer_len,
82 size_t* read,
83 int* error) = 0;
84 virtual StreamResult Write(const void* data,
85 size_t data_len,
86 size_t* written,
87 int* error) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020088 // Attempt to transition to the SS_CLOSED state. SE_CLOSE will not be
89 // signalled as a result of this call.
90 virtual void Close() = 0;
91
92 // Streams may signal one or more StreamEvents to indicate state changes.
93 // The first argument identifies the stream on which the state change occured.
94 // The second argument is a bit-wise combination of StreamEvents.
95 // If SE_CLOSE is signalled, then the third argument is the associated error
96 // code. Otherwise, the value is undefined.
97 // Note: Not all streams will support asynchronous event signalling. However,
98 // SS_OPENING and SR_BLOCK returned from stream member functions imply that
99 // certain events will be raised in the future.
100 sigslot::signal3<StreamInterface*, int, int> SignalEvent;
101
102 // Like calling SignalEvent, but posts a message to the specified thread,
103 // which will call SignalEvent. This helps unroll the stack and prevent
104 // re-entrancy.
105 void PostEvent(Thread* t, int events, int err);
106 // Like the aforementioned method, but posts to the current thread.
107 void PostEvent(int events, int err);
108
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200109 // Return true if flush is successful.
110 virtual bool Flush();
111
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200112 //
113 // CONVENIENCE METHODS
114 //
115 // These methods are implemented in terms of other methods, for convenience.
116 //
117
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200118 // WriteAll is a helper function which repeatedly calls Write until all the
119 // data is written, or something other than SR_SUCCESS is returned. Note that
120 // unlike Write, the argument 'written' is always set, and may be non-zero
121 // on results other than SR_SUCCESS. The remaining arguments have the
122 // same semantics as Write.
Yves Gerey665174f2018-06-19 15:03:05 +0200123 StreamResult WriteAll(const void* data,
124 size_t data_len,
125 size_t* written,
126 int* error);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200127
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200128 protected:
129 StreamInterface();
130
131 // MessageHandler Interface
132 void OnMessage(Message* msg) override;
133
134 private:
135 RTC_DISALLOW_COPY_AND_ASSIGN(StreamInterface);
136};
137
138///////////////////////////////////////////////////////////////////////////////
139// StreamAdapterInterface is a convenient base-class for adapting a stream.
140// By default, all operations are pass-through. Override the methods that you
141// require adaptation. Streams should really be upgraded to reference-counted.
142// In the meantime, use the owned flag to indicate whether the adapter should
143// own the adapted stream.
144///////////////////////////////////////////////////////////////////////////////
145
146class StreamAdapterInterface : public StreamInterface,
147 public sigslot::has_slots<> {
148 public:
149 explicit StreamAdapterInterface(StreamInterface* stream, bool owned = true);
150
151 // Core Stream Interface
152 StreamState GetState() const override;
153 StreamResult Read(void* buffer,
154 size_t buffer_len,
155 size_t* read,
156 int* error) override;
157 StreamResult Write(const void* data,
158 size_t data_len,
159 size_t* written,
160 int* error) override;
161 void Close() override;
162
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200163 bool Flush() override;
164
165 void Attach(StreamInterface* stream, bool owned = true);
166 StreamInterface* Detach();
167
168 protected:
169 ~StreamAdapterInterface() override;
170
171 // Note that the adapter presents itself as the origin of the stream events,
172 // since users of the adapter may not recognize the adapted object.
173 virtual void OnEvent(StreamInterface* stream, int events, int err);
174 StreamInterface* stream() { return stream_; }
175
176 private:
177 StreamInterface* stream_;
178 bool owned_;
179 RTC_DISALLOW_COPY_AND_ASSIGN(StreamAdapterInterface);
180};
181
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200182} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000183
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200184#endif // RTC_BASE_STREAM_H_