blob: 70de65a75d83922ec162d40709814e3fb3a42ae1 [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"
Steve Anton10542f22019-01-11 09:11:00 -080018#include "rtc_base/message_handler.h"
Mirko Bonadei35214fc2019-09-23 14:54:28 +020019#include "rtc_base/system/rtc_export.h"
Artem Titove41c4332018-07-25 15:04:28 +020020#include "rtc_base/third_party/sigslot/sigslot.h"
Sebastian Jansson4db28b52020-01-08 14:07:15 +010021#include "rtc_base/thread.h"
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020022
23namespace rtc {
24
25///////////////////////////////////////////////////////////////////////////////
26// StreamInterface is a generic asynchronous stream interface, supporting read,
27// write, and close operations, and asynchronous signalling of state changes.
28// The interface is designed with file, memory, and socket implementations in
29// mind. Some implementations offer extended operations, such as seeking.
30///////////////////////////////////////////////////////////////////////////////
31
32// The following enumerations are declared outside of the StreamInterface
33// class for brevity in use.
34
35// The SS_OPENING state indicates that the stream will signal open or closed
36// in the future.
37enum StreamState { SS_CLOSED, SS_OPENING, SS_OPEN };
38
39// Stream read/write methods return this value to indicate various success
40// and failure conditions described below.
41enum StreamResult { SR_ERROR, SR_SUCCESS, SR_BLOCK, SR_EOS };
42
43// StreamEvents are used to asynchronously signal state transitionss. The flags
44// may be combined.
45// SE_OPEN: The stream has transitioned to the SS_OPEN state
46// SE_CLOSE: The stream has transitioned to the SS_CLOSED state
47// SE_READ: Data is available, so Read is likely to not return SR_BLOCK
48// SE_WRITE: Data can be written, so Write is likely to not return SR_BLOCK
49enum StreamEvent { SE_OPEN = 1, SE_READ = 2, SE_WRITE = 4, SE_CLOSE = 8 };
50
Tommi04482982020-10-05 12:43:53 +000051class RTC_EXPORT StreamInterface {
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020052 public:
Tommi04482982020-10-05 12:43:53 +000053 virtual ~StreamInterface() {}
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020054
55 virtual StreamState GetState() const = 0;
56
57 // Read attempts to fill buffer of size buffer_len. Write attempts to send
58 // data_len bytes stored in data. The variables read and write are set only
59 // on SR_SUCCESS (see below). Likewise, error is only set on SR_ERROR.
60 // Read and Write return a value indicating:
61 // SR_ERROR: an error occurred, which is returned in a non-null error
62 // argument. Interpretation of the error requires knowledge of the
63 // stream's concrete type, which limits its usefulness.
64 // SR_SUCCESS: some number of bytes were successfully written, which is
65 // returned in a non-null read/write argument.
66 // SR_BLOCK: the stream is in non-blocking mode, and the operation would
67 // block, or the stream is in SS_OPENING state.
68 // SR_EOS: the end-of-stream has been reached, or the stream is in the
69 // SS_CLOSED state.
Yves Gerey665174f2018-06-19 15:03:05 +020070 virtual StreamResult Read(void* buffer,
71 size_t buffer_len,
72 size_t* read,
73 int* error) = 0;
74 virtual StreamResult Write(const void* data,
75 size_t data_len,
76 size_t* written,
77 int* error) = 0;
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020078 // Attempt to transition to the SS_CLOSED state. SE_CLOSE will not be
79 // signalled as a result of this call.
80 virtual void Close() = 0;
81
82 // Streams may signal one or more StreamEvents to indicate state changes.
83 // The first argument identifies the stream on which the state change occured.
84 // The second argument is a bit-wise combination of StreamEvents.
85 // If SE_CLOSE is signalled, then the third argument is the associated error
86 // code. Otherwise, the value is undefined.
87 // Note: Not all streams will support asynchronous event signalling. However,
88 // SS_OPENING and SR_BLOCK returned from stream member functions imply that
89 // certain events will be raised in the future.
90 sigslot::signal3<StreamInterface*, int, int> SignalEvent;
91
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020092 // Return true if flush is successful.
93 virtual bool Flush();
94
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +020095 //
96 // CONVENIENCE METHODS
97 //
98 // These methods are implemented in terms of other methods, for convenience.
99 //
100
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200101 // WriteAll is a helper function which repeatedly calls Write until all the
102 // data is written, or something other than SR_SUCCESS is returned. Note that
103 // unlike Write, the argument 'written' is always set, and may be non-zero
104 // on results other than SR_SUCCESS. The remaining arguments have the
105 // same semantics as Write.
Yves Gerey665174f2018-06-19 15:03:05 +0200106 StreamResult WriteAll(const void* data,
107 size_t data_len,
108 size_t* written,
109 int* error);
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200110
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200111 protected:
112 StreamInterface();
113
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200114 private:
115 RTC_DISALLOW_COPY_AND_ASSIGN(StreamInterface);
116};
117
Henrik Kjellanderec78f1c2017-06-29 07:52:50 +0200118} // namespace rtc
henrike@webrtc.orgf0488722014-05-13 18:00:26 +0000119
Mirko Bonadei92ea95e2017-09-15 06:47:31 +0200120#endif // RTC_BASE_STREAM_H_