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