PageRenderTime 11ms CodeModel.GetById 0ms RepoModel.GetById 1ms app.codeStats 0ms

/mordor/streams/timeout.h

http://github.com/mozy/mordor
C Header | 105 lines | 70 code | 21 blank | 14 comment | 1 complexity | c475b44ed119f5d5c632c9670bcb9ad7 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. #ifndef __MORDOR_TIMEOUT_STREAM__
  2. #define __MORDOR_TIMEOUT_STREAM__
  3. // Copyright (c) 2010 - Mozy, Inc.
  4. #include <boost/enable_shared_from_this.hpp>
  5. #include "filter.h"
  6. #include "mordor/fibersynchronization.h"
  7. namespace Mordor {
  8. class TimerManager;
  9. class Timer;
  10. class TimeoutHandler : public boost::enable_shared_from_this<TimeoutHandler>
  11. {
  12. public:
  13. typedef boost::function<void ()> TimeoutDg;
  14. private:
  15. enum STATUS {
  16. NONE,
  17. TIMING,
  18. TIMEDOUT
  19. };
  20. public:
  21. TimeoutHandler(TimerManager &timerManager, bool autoRestart = false):
  22. m_timeout(~0ull),
  23. m_lastTimedOut(NONE),
  24. m_permaTimedOut(NONE),
  25. m_autoStart(autoRestart),
  26. m_timerManager(timerManager)
  27. {}
  28. ~TimeoutHandler();
  29. unsigned long long getTimeout() const { return m_timeout; }
  30. void setTimeout(unsigned long long timeout, TimeoutDg dg);
  31. bool isTimeoutSet() const { return m_timeout != ~0ull; }
  32. /// start timer
  33. /// @throws TimedOutException if it already timed out
  34. void startTimer();
  35. /// cancel the timer
  36. /// @return if it already timed out before cancelling
  37. bool cancelTimer();
  38. /// refresh the timer to restart
  39. /// @return if it already timed out before refreshing
  40. bool refreshTimer();
  41. private:
  42. void onTimeout();
  43. private:
  44. unsigned long long m_timeout;
  45. STATUS m_lastTimedOut, m_permaTimedOut;
  46. bool m_autoStart;
  47. TimeoutDg m_timeoutDg;
  48. boost::shared_ptr<Timer> m_timer;
  49. TimerManager &m_timerManager;
  50. };
  51. /// @brief Timeout Stream
  52. /// @details
  53. /// Provide timeout mechanism for read/write operations.
  54. /// 3 kinds of timeouts are supported:
  55. /// - Read: timed out when read() takes so long.
  56. /// - Write: timed out when write() takes so long.
  57. /// - Idle: timed out when no IO(read/write) on the stream.
  58. class TimeoutStream : public FilterStream
  59. {
  60. public:
  61. typedef boost::shared_ptr<TimeoutStream> ptr;
  62. public:
  63. TimeoutStream(Stream::ptr parent, TimerManager &timerManager, bool own = true)
  64. : FilterStream(parent, own),
  65. m_reader(new TimeoutHandler(timerManager, false)),
  66. m_writer(new TimeoutHandler(timerManager, false)),
  67. m_idler(new TimeoutHandler(timerManager, true))
  68. {}
  69. unsigned long long readTimeout() const { return m_reader->getTimeout(); }
  70. void readTimeout(unsigned long long readTimeout);
  71. unsigned long long writeTimeout() const { return m_writer->getTimeout(); }
  72. void writeTimeout(unsigned long long writeTimeout);
  73. unsigned long long idleTimeout() const { return m_idler->getTimeout(); }
  74. void idleTimeout(unsigned long long idleTimeout);
  75. using FilterStream::read;
  76. size_t read(Buffer &buffer, size_t length);
  77. using FilterStream::write;
  78. size_t write(const Buffer &buffer, size_t length);
  79. private:
  80. boost::shared_ptr<TimeoutHandler> m_reader, m_writer, m_idler;
  81. FiberMutex m_mutex;
  82. };
  83. }
  84. #endif