stream.h 7.3 KB

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