diff options
Diffstat (limited to 'Swiften/Network/BoostTimer.cpp')
-rw-r--r-- | Swiften/Network/BoostTimer.cpp | 34 |
1 files changed, 34 insertions, 0 deletions
diff --git a/Swiften/Network/BoostTimer.cpp b/Swiften/Network/BoostTimer.cpp new file mode 100644 index 0000000..fdbd45d --- /dev/null +++ b/Swiften/Network/BoostTimer.cpp @@ -0,0 +1,34 @@ +#include "Swiften/Network/BoostTimer.h" + +#include <boost/date_time/posix_time/posix_time.hpp> +#include <boost/asio.hpp> + +#include "Swiften/EventLoop/MainEventLoop.h" + +namespace Swift { + +BoostTimer::BoostTimer(int milliseconds, boost::asio::io_service* service) : + timeout(milliseconds), timer(*service) { +} + +void BoostTimer::start() { + timer.expires_from_now(boost::posix_time::milliseconds(timeout)); + timer.async_wait(boost::bind(&BoostTimer::handleTimerTick, shared_from_this(), boost::asio::placeholders::error)); +} + +void BoostTimer::stop() { + timer.cancel(); +} + +void BoostTimer::handleTimerTick(const boost::system::error_code& error) { + if (error) { + assert(error == boost::asio::error::operation_aborted); + } + else { + MainEventLoop::postEvent(boost::bind(boost::ref(onTick)), shared_from_this()); + timer.expires_from_now(boost::posix_time::milliseconds(timeout)); + timer.async_wait(boost::bind(&BoostTimer::handleTimerTick, shared_from_this(), boost::asio::placeholders::error)); + } +} + +} |