summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-11-12 19:48:32 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-11-12 20:41:59 (GMT)
commit8231ecc07c1c7d5e260a2795b0660de157501850 (patch)
tree935baa855914d632086fcac5f26330c2f0261216 /Swiften/Network/BoostTimer.cpp
parent48bb7441f913144d6a20687ff79264e4631156f0 (diff)
downloadswift-contrib-8231ecc07c1c7d5e260a2795b0660de157501850.zip
swift-contrib-8231ecc07c1c7d5e260a2795b0660de157501850.tar.bz2
Created a TimerFactory, and abstracted Timer.
Diffstat (limited to 'Swiften/Network/BoostTimer.cpp')
-rw-r--r--Swiften/Network/BoostTimer.cpp34
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));
+ }
+}
+
+}