You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

184 lines
7.2 KiB

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