PageRenderTime 318ms CodeModel.GetById 155ms app.highlight 4ms RepoModel.GetById 157ms app.codeStats 0ms

/mordor/streams/throttle.h

http://github.com/mozy/mordor
C Header | 55 lines | 41 code | 9 blank | 5 comment | 0 complexity | 6c17ae1ae9e4620f3cac428254510e07 MD5 | raw file
 1#ifndef __MORDOR_THROTTLE_STREAM_H__
 2#define __MORDOR_THROTTLE_STREAM_H__
 3// Copyright (c) 2009 - Mozy, Inc.
 4
 5#include <boost/function.hpp>
 6
 7#include "filter.h"
 8
 9namespace Mordor {
10
11class TimerManager;
12
13/// @note In practice, ThrottleStream cannot throttle much slower than 800bps
14/// (due to refusing to sleep for more than a tenth of a second at a time)
15class ThrottleStream : public FilterStream
16{
17public:
18    /// @param dg Returns the current throttle value, in bps (BITS per second).
19    /// Either 0 or ~0u means to not throttle at the moment.
20    ThrottleStream(Stream::ptr parent, boost::function<unsigned int ()> dg,
21        TimerManager &timerManager, bool own = true)
22        : FilterStream(parent, own),
23          m_dg(dg),
24          m_read(0),
25          m_written(0),
26          m_readTimestamp(0),
27          m_writeTimestamp(0),
28          m_timerManager(&timerManager)
29    {}
30    ThrottleStream(Stream::ptr parent, boost::function<unsigned int ()> dg,
31        bool own = true)
32        : FilterStream(parent, own),
33          m_dg(dg),
34          m_read(0),
35          m_written(0),
36          m_readTimestamp(0),
37          m_writeTimestamp(0),
38          m_timerManager(NULL)
39    {}
40
41    using FilterStream::read;
42    size_t read(Buffer &b, size_t len);
43    using FilterStream::write;
44    size_t write(const Buffer &b, size_t len);
45
46private:
47    boost::function<unsigned int ()> m_dg;
48    size_t m_read, m_written;
49    unsigned long long m_readTimestamp, m_writeTimestamp;
50    TimerManager *m_timerManager;
51};
52
53}
54
55#endif