BufferedStream.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. #pragma once
  2. #include "ByteStream.h"
  3. #include "BellTask.h"
  4. #include "WrappedSemaphore.h"
  5. #include <atomic>
  6. #include <functional>
  7. #include <memory>
  8. #include <mutex>
  9. /**
  10. * This class implements a wrapper around an arbitrary bell::ByteStream,
  11. * providing a circular reading buffer with configurable thresholds.
  12. *
  13. * The BufferedStream runs a bell::Task when it's started, so the caller can
  14. * access the buffer's data asynchronously, whenever needed. The buffer is refilled
  15. * automatically from source stream.
  16. *
  17. * The class implements bell::ByteStream's methods, although for proper functioning,
  18. * the caller code should be modified to check isReady() and isNotReady() flags.
  19. *
  20. * If the actual reading code can't be modified, waitForReady allows to wait for buffer readiness
  21. * during reading. Keep in mind that using the semaphore is probably more resource effective.
  22. *
  23. * The source stream (passed to open() or returned by the reader) should implement the read()
  24. * method correctly, such as that 0 is returned if, and only if the stream ends.
  25. */
  26. class BufferedStream : public bell::ByteStream, bell::Task {
  27. public:
  28. typedef std::shared_ptr<bell::ByteStream> StreamPtr;
  29. typedef std::function<StreamPtr(uint32_t rangeStart)> StreamReader;
  30. public:
  31. /**
  32. * @param taskName name to use for the reading task
  33. * @param bufferSize total size of the reading buffer
  34. * @param readThreshold how much can be read before refilling the buffer
  35. * @param readSize amount of bytes to read from the source each time
  36. * @param readyThreshold minimum amount of available bytes to report isReady()
  37. * @param notReadyThreshold maximum amount of available bytes to report isNotReady()
  38. * @param waitForReady whether to wait for the buffer to be ready during reading
  39. * @param endWithSource whether to end the streaming as soon as source returns 0 from read()
  40. */
  41. BufferedStream(
  42. const std::string &taskName,
  43. uint32_t bufferSize,
  44. uint32_t readThreshold,
  45. uint32_t readSize,
  46. uint32_t readyThreshold,
  47. uint32_t notReadyThreshold,
  48. bool waitForReady = false);
  49. ~BufferedStream() override;
  50. bool open(const StreamPtr &stream);
  51. bool open(const StreamReader &newReader, uint32_t initialOffset = 0);
  52. void close() override;
  53. // inherited methods
  54. public:
  55. /**
  56. * Read len bytes from the buffer to dst. If waitForReady is enabled
  57. * and readAvailable is lower than notReadyThreshold, the function
  58. * will block until readyThreshold bytes is available.
  59. *
  60. * @returns number of bytes copied to dst (might be lower than len,
  61. * if the buffer does not contain len bytes available), or 0 if the source
  62. * stream is already closed and there is no reader attached.
  63. */
  64. size_t read(uint8_t *dst, size_t len) override;
  65. size_t skip(size_t len) override;
  66. size_t position() override;
  67. size_t size() override;
  68. // stream status
  69. public:
  70. /**
  71. * Total amount of bytes served to read().
  72. */
  73. uint32_t readTotal;
  74. /**
  75. * Total amount of bytes read from source.
  76. */
  77. uint32_t bufferTotal;
  78. /**
  79. * Amount of bytes available to read from the buffer.
  80. */
  81. std::atomic<uint32_t> readAvailable;
  82. /**
  83. * Whether the caller should start reading the data. This indicates that a safe
  84. * amount (determined by readyThreshold) of data is available in the buffer.
  85. */
  86. bool isReady() const;
  87. /**
  88. * Whether the caller should stop reading the data. This indicates that the amount of data
  89. * available for reading is decreasing to a non-safe value, as data is being read
  90. * faster than it can be buffered.
  91. */
  92. bool isNotReady() const;
  93. /**
  94. * Semaphore that is given when the buffer becomes ready (isReady() == true). Caller can
  95. * wait for the semaphore instead of continuously querying isReady().
  96. */
  97. bell::WrappedSemaphore readySem;
  98. private:
  99. std::mutex runningMutex;
  100. bool running = false;
  101. bool terminate = false;
  102. bell::WrappedSemaphore readSem; // signal to start writing to buffer after reading from it
  103. std::mutex readMutex; // mutex for locking read operations during writing, and vice versa
  104. uint32_t bufferSize;
  105. uint32_t readAt;
  106. uint32_t readSize;
  107. uint32_t readyThreshold;
  108. uint32_t notReadyThreshold;
  109. bool waitForReady;
  110. uint8_t *buf;
  111. uint8_t *bufEnd;
  112. uint8_t *bufReadPtr;
  113. uint8_t *bufWritePtr;
  114. StreamPtr source;
  115. StreamReader reader;
  116. void runTask() override;
  117. void reset();
  118. uint32_t lengthBetween(uint8_t *me, uint8_t *other);
  119. };