summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-06-01 08:48:42 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-06-01 09:24:28 (GMT)
commit2812bddd81f8a1b804c7460f4e14cd0aa393d129 (patch)
treed46294f35150c4f0f43deaf2d31fceaf945ae715 /Swiften/Network/Timer.cpp
downloadswift-2812bddd81f8a1b804c7460f4e14cd0aa393d129.zip
swift-2812bddd81f8a1b804c7460f4e14cd0aa393d129.tar.bz2
Import.
Diffstat (limited to 'Swiften/Network/Timer.cpp')
-rw-r--r--Swiften/Network/Timer.cpp40
1 files changed, 40 insertions, 0 deletions
diff --git a/Swiften/Network/Timer.cpp b/Swiften/Network/Timer.cpp
new file mode 100644
index 0000000..8b2b57f
--- /dev/null
+++ b/Swiften/Network/Timer.cpp
@@ -0,0 +1,40 @@
+#include "Swiften/Network/Timer.h"
+
+#include <boost/date_time/posix_time/posix_time.hpp>
+
+#include "Swiften/EventLoop/MainEventLoop.h"
+
+namespace Swift {
+
+Timer::Timer(int milliseconds) :
+ timeout_(milliseconds), ioService_(0), thread_(0), timer_(0) {
+ ioService_ = new boost::asio::io_service();
+}
+
+Timer::~Timer() {
+ MainEventLoop::removeEventsFromOwner(this);
+ ioService_->stop();
+ thread_->join();
+ delete timer_;
+ delete thread_;
+ delete ioService_;
+}
+
+void Timer::start() {
+ thread_ = new boost::thread(boost::bind(&Timer::doStart, this));
+}
+
+void Timer::doStart() {
+ timer_ = new boost::asio::deadline_timer(*ioService_);
+ timer_->expires_from_now(boost::posix_time::milliseconds(timeout_));
+ timer_->async_wait(boost::bind(&Timer::handleTimerTick, this));
+ ioService_->run();
+}
+
+void Timer::handleTimerTick() {
+ MainEventLoop::postEvent(boost::bind(boost::ref(onTick)), this);
+ timer_->expires_from_now(boost::posix_time::milliseconds(timeout_));
+ timer_->async_wait(boost::bind(&Timer::handleTimerTick, this));
+}
+
+}