PageRenderTime 98ms CodeModel.GetById 20ms app.highlight 52ms RepoModel.GetById 18ms app.codeStats 1ms

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