diff options
author | Chris Wilson <chris+github@qwirx.com> | 2011-01-12 00:11:53 +0000 |
---|---|---|
committer | Chris Wilson <chris+github@qwirx.com> | 2011-01-12 00:11:53 +0000 |
commit | 8ee2327add953c8282807c988dcaef7a2d03a7f1 (patch) | |
tree | c97d2b6c91b4c7d3c4d4d4c720ae3d0599b505df /lib/common/RateLimitingStream.h | |
parent | bc6ccea0d973ce4a0eb370191c3bbcd22d6cb45b (diff) |
Add an implementation of a stream wrapper that limits reading rate, to
control bandwidth usage.
Diffstat (limited to 'lib/common/RateLimitingStream.h')
-rw-r--r-- | lib/common/RateLimitingStream.h | 71 |
1 files changed, 71 insertions, 0 deletions
diff --git a/lib/common/RateLimitingStream.h b/lib/common/RateLimitingStream.h new file mode 100644 index 00000000..a322b99b --- /dev/null +++ b/lib/common/RateLimitingStream.h @@ -0,0 +1,71 @@ +// -------------------------------------------------------------------------- +// +// File +// Name: RateLimitingStream.h +// Purpose: Rate-limiting write-only wrapper around IOStreams +// Created: 2011/01/11 +// +// -------------------------------------------------------------------------- + +#ifndef RATELIMITINGSTREAM__H +#define RATELIMITINGSTREAM__H + +#include "BoxTime.h" +#include "IOStream.h" + +class RateLimitingStream : public IOStream +{ +private: + IOStream& mrSink; + box_time_t mStartTime; + uint64_t mTotalBytesRead; + size_t mTargetBytesPerSecond; + +public: + RateLimitingStream(IOStream& rSink, size_t targetBytesPerSecond); + virtual ~RateLimitingStream() { } + + // This is the only magic + virtual int Read(void *pBuffer, int NBytes, + int Timeout = IOStream::TimeOutInfinite); + + // Everything else is delegated to the sink + virtual void Write(const void *pBuffer, int NBytes) + { + Write(pBuffer, NBytes); + } + virtual pos_type BytesLeftToRead() + { + return mrSink.BytesLeftToRead(); + } + virtual pos_type GetPosition() const + { + return mrSink.GetPosition(); + } + virtual void Seek(IOStream::pos_type Offset, int SeekType) + { + mrSink.Seek(Offset, SeekType); + } + virtual void Flush(int Timeout = IOStream::TimeOutInfinite) + { + mrSink.Flush(Timeout); + } + virtual void Close() + { + mrSink.Close(); + } + virtual bool StreamDataLeft() + { + return mrSink.StreamDataLeft(); + } + virtual bool StreamClosed() + { + return mrSink.StreamClosed(); + } + +private: + RateLimitingStream(const RateLimitingStream &rToCopy) + : mrSink(rToCopy.mrSink) { /* do not call */ } +}; + +#endif // RATELIMITINGSTREAM__H |