BufferedStream.h 4.3 KB

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