summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-07-15 07:42:18 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-07-15 07:42:18 (GMT)
commit0930cd940963be0edfe7c80b4925babca0e01443 (patch)
treeb2a57761bfdf1a14ea75ea1a9871c70d85ff8024 /Swiften/Network
parentd2625df30861a4caa984031a6990d19dfebc3367 (diff)
downloadswift-contrib-0930cd940963be0edfe7c80b4925babca0e01443.zip
swift-contrib-0930cd940963be0edfe7c80b4925babca0e01443.tar.bz2
Use shared_ptr for EventLoop owners.
Diffstat (limited to 'Swiften/Network')
-rw-r--r--Swiften/Network/BoostConnection.cpp21
-rw-r--r--Swiften/Network/BoostConnection.h4
-rw-r--r--Swiften/Network/Timer.cpp10
-rw-r--r--Swiften/Network/Timer.h10
4 files changed, 22 insertions, 23 deletions
diff --git a/Swiften/Network/BoostConnection.cpp b/Swiften/Network/BoostConnection.cpp
index ec15c96..ff53289 100644
--- a/Swiften/Network/BoostConnection.cpp
+++ b/Swiften/Network/BoostConnection.cpp
@@ -42,7 +42,7 @@ BoostConnection::BoostConnection(boost::asio::io_service* ioService) :
}
BoostConnection::~BoostConnection() {
- MainEventLoop::removeEventsFromOwner(this);
+ MainEventLoop::removeEventsFromOwner(shared_from_this());
}
void BoostConnection::listen() {
@@ -55,10 +55,9 @@ void BoostConnection::connect(const String& domain) {
HostAddressPort addressPort = resolver.resolve(domain.getUTF8String());
boost::asio::ip::tcp::endpoint endpoint(
boost::asio::ip::address::from_string(addressPort.getAddress().toString()), addressPort.getPort());
- // Use shared_from_this
socket_.async_connect(
endpoint,
- boost::bind(&BoostConnection::handleConnectFinished, this, boost::asio::placeholders::error));
+ boost::bind(&BoostConnection::handleConnectFinished, shared_from_this(), boost::asio::placeholders::error));
}
catch (const DomainNameResolveException& e) {
onError(DomainNameResolveError);
@@ -70,41 +69,39 @@ void BoostConnection::disconnect() {
}
void BoostConnection::write(const ByteArray& data) {
- // Use shared_from_this
boost::asio::async_write(socket_, SharedBuffer(data),
- boost::bind(&BoostConnection::handleDataWritten, this, boost::asio::placeholders::error));
+ boost::bind(&BoostConnection::handleDataWritten, shared_from_this(), boost::asio::placeholders::error));
}
void BoostConnection::handleConnectFinished(const boost::system::error_code& error) {
if (!error) {
- MainEventLoop::postEvent(boost::bind(boost::ref(onConnected)), this);
+ MainEventLoop::postEvent(boost::bind(boost::ref(onConnected)), shared_from_this());
doRead();
}
else if (error != boost::asio::error::operation_aborted) {
- MainEventLoop::postEvent(boost::bind(boost::ref(onError), ConnectionError), this);
+ MainEventLoop::postEvent(boost::bind(boost::ref(onError), ConnectionError), shared_from_this());
}
}
void BoostConnection::doRead() {
- // Use shared_from_this
socket_.async_read_some(
boost::asio::buffer(readBuffer_),
- boost::bind(&BoostConnection::handleSocketRead, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
+ boost::bind(&BoostConnection::handleSocketRead, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred));
}
void BoostConnection::handleSocketRead(const boost::system::error_code& error, size_t bytesTransferred) {
if (!error) {
- MainEventLoop::postEvent(boost::bind(boost::ref(onDataRead), ByteArray(&readBuffer_[0], bytesTransferred)), this);
+ MainEventLoop::postEvent(boost::bind(boost::ref(onDataRead), ByteArray(&readBuffer_[0], bytesTransferred)), shared_from_this());
doRead();
}
else if (error != boost::asio::error::operation_aborted) {
- MainEventLoop::postEvent(boost::bind(boost::ref(onError), ReadError), this);
+ MainEventLoop::postEvent(boost::bind(boost::ref(onError), ReadError), shared_from_this());
}
}
void BoostConnection::handleDataWritten(const boost::system::error_code& error) {
if (error && error != boost::asio::error::operation_aborted) {
- MainEventLoop::postEvent(boost::bind(boost::ref(onError), WriteError), this);
+ MainEventLoop::postEvent(boost::bind(boost::ref(onError), WriteError), shared_from_this());
}
}
diff --git a/Swiften/Network/BoostConnection.h b/Swiften/Network/BoostConnection.h
index 85de926..76b6588 100644
--- a/Swiften/Network/BoostConnection.h
+++ b/Swiften/Network/BoostConnection.h
@@ -1,8 +1,10 @@
#pragma once
#include <boost/asio.hpp>
+#include <boost/enable_shared_from_this.hpp>
#include "Swiften/Network/Connection.h"
+#include "Swiften/EventLoop/EventOwner.h"
namespace boost {
class thread;
@@ -12,7 +14,7 @@ namespace boost {
}
namespace Swift {
- class BoostConnection : public Connection {
+ class BoostConnection : public Connection, public EventOwner, public boost::enable_shared_from_this<BoostConnection> {
public:
BoostConnection(boost::asio::io_service* ioService);
~BoostConnection();
diff --git a/Swiften/Network/Timer.cpp b/Swiften/Network/Timer.cpp
index 8b2b57f..bab41e4 100644
--- a/Swiften/Network/Timer.cpp
+++ b/Swiften/Network/Timer.cpp
@@ -12,7 +12,7 @@ Timer::Timer(int milliseconds) :
}
Timer::~Timer() {
- MainEventLoop::removeEventsFromOwner(this);
+ MainEventLoop::removeEventsFromOwner(shared_from_this());
ioService_->stop();
thread_->join();
delete timer_;
@@ -21,20 +21,20 @@ Timer::~Timer() {
}
void Timer::start() {
- thread_ = new boost::thread(boost::bind(&Timer::doStart, this));
+ thread_ = new boost::thread(boost::bind(&Timer::doStart, shared_from_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));
+ timer_->async_wait(boost::bind(&Timer::handleTimerTick, shared_from_this()));
ioService_->run();
}
void Timer::handleTimerTick() {
- MainEventLoop::postEvent(boost::bind(boost::ref(onTick)), this);
+ 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, this));
+ timer_->async_wait(boost::bind(&Timer::handleTimerTick, shared_from_this()));
}
}
diff --git a/Swiften/Network/Timer.h b/Swiften/Network/Timer.h
index 8e4b4c2..de97c13 100644
--- a/Swiften/Network/Timer.h
+++ b/Swiften/Network/Timer.h
@@ -1,12 +1,14 @@
-#ifndef SWIFTEN_Timer_H
-#define SWIFTEN_Timer_H
+#pragma once
#include <boost/asio.hpp>
#include <boost/signals.hpp>
#include <boost/thread.hpp>
+#include <boost/enable_shared_from_this.hpp>
+
+#include "Swiften/EventLoop/EventOwner.h"
namespace Swift {
- class Timer {
+ class Timer : public EventOwner, public boost::enable_shared_from_this<Timer> {
public:
Timer(int milliseconds);
~Timer();
@@ -27,5 +29,3 @@ namespace Swift {
boost::asio::deadline_timer* timer_;
};
}
-
-#endif