summaryrefslogtreecommitdiff
path: root/lib/common/RateLimitingStream.h
blob: 818c90af7e6879b7730c9cbfa13fa1ba368d1602 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// --------------------------------------------------------------------------
//
// 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,
		int Timeout = IOStream::TimeOutInfinite)
	{
		mrSink.Write(pBuffer, NBytes, Timeout);
	}
	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