diff options
author | Remko Tronçon <git@el-tramo.be> | 2009-09-11 13:46:08 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2009-09-11 14:02:39 (GMT) |
commit | 1b47266d6b955fb533b4c272b4bcc58693078a1a (patch) | |
tree | dd52b86408a03308c84c33674989d2d75af58893 /Swiften/Network/Timer.cpp | |
parent | 5e235d01612b2544fe02cc34816cd6e925ed564f (diff) | |
download | swift-contrib-1b47266d6b955fb533b4c272b4bcc58693078a1a.zip swift-contrib-1b47266d6b955fb533b4c272b4bcc58693078a1a.tar.bz2 |
Disable WhitespacePingLayer on disconnect + Timer refactoring.
Timer now no longer runs in its own thread, but in the main Boost
IOService thread.
Diffstat (limited to 'Swiften/Network/Timer.cpp')
-rw-r--r-- | Swiften/Network/Timer.cpp | 35 |
1 files changed, 16 insertions, 19 deletions
diff --git a/Swiften/Network/Timer.cpp b/Swiften/Network/Timer.cpp index f3b296c..d6120e3 100644 --- a/Swiften/Network/Timer.cpp +++ b/Swiften/Network/Timer.cpp @@ -6,35 +6,32 @@ namespace Swift { -Timer::Timer(int milliseconds) : - timeout_(milliseconds), ioService_(0), thread_(0), timer_(0) { - ioService_ = new boost::asio::io_service(); +Timer::Timer(int milliseconds, boost::asio::io_service* service) : + timeout(milliseconds), timer(*service) { } Timer::~Timer() { - //MainEventLoop::removeEventsFromOwner(shared_from_this()); - ioService_->stop(); - thread_->join(); - delete timer_; - delete thread_; - delete ioService_; + stop(); } void Timer::start() { - thread_ = new boost::thread(boost::bind(&Timer::doStart, shared_from_this())); + timer.expires_from_now(boost::posix_time::milliseconds(timeout)); + timer.async_wait(boost::bind(&Timer::handleTimerTick, shared_from_this(), boost::asio::placeholders::error)); } -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, shared_from_this())); - ioService_->run(); +void Timer::stop() { + timer.cancel(); } -void Timer::handleTimerTick() { - MainEventLoop::postEvent(boost::bind(boost::ref(onTick)), shared_from_this()); - timer_->expires_from_now(boost::posix_time::milliseconds(timeout_)); - timer_->async_wait(boost::bind(&Timer::handleTimerTick, shared_from_this())); +void Timer::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(&Timer::handleTimerTick, shared_from_this(), boost::asio::placeholders::error)); + } } } |