stream.h 7.2 KB

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