summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRemko Tronçon <git@el-tramo.be>2009-07-14 17:27:32 (GMT)
committerRemko Tronçon <git@el-tramo.be>2009-07-14 17:27:32 (GMT)
commitee3a46975986865fe5064f9d87a27277fc6c235a (patch)
tree51bae2ed1669ca881f761ca7547fbaf22432ae12 /Swiften/Network
parentc34f50d3371f47c88d2ee927a5d59af8e135304c (diff)
downloadswift-contrib-ee3a46975986865fe5064f9d87a27277fc6c235a.zip
swift-contrib-ee3a46975986865fe5064f9d87a27277fc6c235a.tar.bz2
Consolidating IncomingConnection & Connection.
BoostConnections no longer have their own thread, but are managed from a central Boost IO thread instead.
Diffstat (limited to 'Swiften/Network')
-rw-r--r--Swiften/Network/BoostConnection.cpp85
-rw-r--r--Swiften/Network/BoostConnection.h23
-rw-r--r--Swiften/Network/BoostConnectionFactory.cpp6
-rw-r--r--Swiften/Network/BoostConnectionFactory.h9
-rw-r--r--Swiften/Network/Connection.h16
-rw-r--r--Swiften/Network/ConnectionFactory.h3
-rw-r--r--Swiften/Network/ConnectionServer.h4
-rw-r--r--Swiften/Network/IncomingConnection.cpp8
-rw-r--r--Swiften/Network/IncomingConnection.h17
-rw-r--r--Swiften/Network/Makefile.inc1
10 files changed, 76 insertions, 96 deletions
diff --git a/Swiften/Network/BoostConnection.cpp b/Swiften/Network/BoostConnection.cpp
index f055f6a..ec15c96 100644
--- a/Swiften/Network/BoostConnection.cpp
+++ b/Swiften/Network/BoostConnection.cpp
@@ -14,53 +14,67 @@ namespace Swift {
static const size_t BUFFER_SIZE = 4096;
-BoostConnection::BoostConnection(const String& domain) :
- Connection(domain), ioService_(0), thread_(0), socket_(0), readBuffer_(BUFFER_SIZE) {
- ioService_ = new boost::asio::io_service();
+// -----------------------------------------------------------------------------
+
+// A reference-counted non-modifiable buffer class.
+class SharedBuffer {
+ public:
+ SharedBuffer(const ByteArray& data) :
+ data_(new std::vector<char>(data.begin(), data.end())),
+ buffer_(boost::asio::buffer(*data_)) {
+ }
+
+ // ConstBufferSequence requirements.
+ typedef boost::asio::const_buffer value_type;
+ typedef const boost::asio::const_buffer* const_iterator;
+ const boost::asio::const_buffer* begin() const { return &buffer_; }
+ const boost::asio::const_buffer* end() const { return &buffer_ + 1; }
+
+ private:
+ boost::shared_ptr< std::vector<char> > data_;
+ boost::asio::const_buffer buffer_;
+};
+
+// -----------------------------------------------------------------------------
+
+BoostConnection::BoostConnection(boost::asio::io_service* ioService) :
+ socket_(*ioService), readBuffer_(BUFFER_SIZE) {
}
BoostConnection::~BoostConnection() {
MainEventLoop::removeEventsFromOwner(this);
- ioService_->stop();
- thread_->join();
- delete socket_;
- delete thread_;
- delete ioService_;
}
-void BoostConnection::connect() {
- thread_ = new boost::thread(boost::bind(&BoostConnection::doConnect, this));
+void BoostConnection::listen() {
+ doRead();
}
-void BoostConnection::disconnect() {
- if (ioService_) {
- ioService_->post(boost::bind(&BoostConnection::doDisconnect, this));
- }
-}
-
-void BoostConnection::write(const ByteArray& data) {
- if (ioService_) {
- ioService_->post(boost::bind(&BoostConnection::doWrite, this, data));
- }
-}
-
-void BoostConnection::doConnect() {
+void BoostConnection::connect(const String& domain) {
DomainNameResolver resolver;
try {
- HostAddressPort addressPort = resolver.resolve(getDomain().getUTF8String());
- socket_ = new boost::asio::ip::tcp::socket(*ioService_);
+ HostAddressPort addressPort = resolver.resolve(domain.getUTF8String());
boost::asio::ip::tcp::endpoint endpoint(
boost::asio::ip::address::from_string(addressPort.getAddress().toString()), addressPort.getPort());
- socket_->async_connect(
+ // Use shared_from_this
+ socket_.async_connect(
endpoint,
boost::bind(&BoostConnection::handleConnectFinished, this, boost::asio::placeholders::error));
- ioService_->run();
}
catch (const DomainNameResolveException& e) {
- MainEventLoop::postEvent(boost::bind(boost::ref(onError), DomainNameResolveError), this);
+ onError(DomainNameResolveError);
}
}
+void BoostConnection::disconnect() {
+ socket_.close();
+}
+
+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));
+}
+
void BoostConnection::handleConnectFinished(const boost::system::error_code& error) {
if (!error) {
MainEventLoop::postEvent(boost::bind(boost::ref(onConnected)), this);
@@ -72,15 +86,12 @@ void BoostConnection::handleConnectFinished(const boost::system::error_code& err
}
void BoostConnection::doRead() {
- socket_->async_read_some(
- boost::asio::buffer(readBuffer_),
+ // 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));
}
-void BoostConnection::doWrite(const ByteArray& data) {
- boost::asio::write(*socket_, boost::asio::buffer(static_cast<const char*>(data.getData()), data.getSize()));
-}
-
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);
@@ -91,9 +102,9 @@ void BoostConnection::handleSocketRead(const boost::system::error_code& error, s
}
}
-void BoostConnection::doDisconnect() {
- if (socket_) {
- socket_->close();
+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);
}
}
diff --git a/Swiften/Network/BoostConnection.h b/Swiften/Network/BoostConnection.h
index f8fa514..85de926 100644
--- a/Swiften/Network/BoostConnection.h
+++ b/Swiften/Network/BoostConnection.h
@@ -1,5 +1,4 @@
-#ifndef SWIFTEN_BoostConnection_H
-#define SWIFTEN_BoostConnection_H
+#pragma once
#include <boost/asio.hpp>
@@ -15,29 +14,27 @@ namespace boost {
namespace Swift {
class BoostConnection : public Connection {
public:
- BoostConnection(const String& domain);
+ BoostConnection(boost::asio::io_service* ioService);
~BoostConnection();
- virtual void connect();
+ virtual void listen();
+ virtual void connect(const String& domain);
virtual void disconnect();
virtual void write(const ByteArray& data);
- private:
- void doConnect();
- void doDisconnect();
+ boost::asio::ip::tcp::socket& getSocket() {
+ return socket_;
+ }
+ private:
void handleConnectFinished(const boost::system::error_code& error);
void handleSocketRead(const boost::system::error_code& error, size_t bytesTransferred);
+ void handleDataWritten(const boost::system::error_code& error);
void doRead();
- void doWrite(const ByteArray&);
private:
- boost::asio::io_service* ioService_;
- boost::thread* thread_;
- boost::asio::ip::tcp::socket* socket_;
+ boost::asio::ip::tcp::socket socket_;
std::vector<char> readBuffer_;
bool disconnecting_;
};
}
-
-#endif
diff --git a/Swiften/Network/BoostConnectionFactory.cpp b/Swiften/Network/BoostConnectionFactory.cpp
index 9c542ac..a1bef15 100644
--- a/Swiften/Network/BoostConnectionFactory.cpp
+++ b/Swiften/Network/BoostConnectionFactory.cpp
@@ -2,11 +2,11 @@
namespace Swift {
-BoostConnectionFactory::BoostConnectionFactory() {
+BoostConnectionFactory::BoostConnectionFactory(boost::asio::io_service* ioService) : ioService(ioService) {
}
-BoostConnection* BoostConnectionFactory::createConnection(const String& domain) {
- return new BoostConnection(domain);
+BoostConnection* BoostConnectionFactory::createConnection() {
+ return new BoostConnection(ioService);
}
}
diff --git a/Swiften/Network/BoostConnectionFactory.h b/Swiften/Network/BoostConnectionFactory.h
index b6a27b2..d15770d 100644
--- a/Swiften/Network/BoostConnectionFactory.h
+++ b/Swiften/Network/BoostConnectionFactory.h
@@ -1,6 +1,8 @@
#ifndef SWIFTEN_BoostConnectionFactory_H
#define SWIFTEN_BoostConnectionFactory_H
+#include <boost/asio.hpp>
+
#include "Swiften/Network/ConnectionFactory.h"
#include "Swiften/Network/BoostConnection.h"
@@ -9,9 +11,12 @@ namespace Swift {
class BoostConnectionFactory : public ConnectionFactory {
public:
- BoostConnectionFactory();
+ BoostConnectionFactory(boost::asio::io_service*);
+
+ virtual BoostConnection* createConnection();
- virtual BoostConnection* createConnection(const String& domain);
+ private:
+ boost::asio::io_service* ioService;
};
}
diff --git a/Swiften/Network/Connection.h b/Swiften/Network/Connection.h
index 6d05eee..16d5d7b 100644
--- a/Swiften/Network/Connection.h
+++ b/Swiften/Network/Connection.h
@@ -12,13 +12,15 @@ namespace Swift {
enum Error {
DomainNameResolveError,
ConnectionError,
- ReadError
+ ReadError,
+ WriteError
};
- Connection(const String& domain) : domain_(domain) {}
+ Connection() {}
virtual ~Connection() {}
- virtual void connect() = 0;
+ virtual void listen() = 0;
+ virtual void connect(const String& domain) = 0;
virtual void disconnect() = 0;
virtual void write(const ByteArray& data) = 0;
@@ -26,14 +28,6 @@ namespace Swift {
boost::signal<void ()> onConnected;
boost::signal<void (Error)> onError;
boost::signal<void (const ByteArray&)> onDataRead;
-
- protected:
- const String& getDomain() const {
- return domain_;
- }
-
- private:
- String domain_;
};
}
diff --git a/Swiften/Network/ConnectionFactory.h b/Swiften/Network/ConnectionFactory.h
index 2af9ebf..ef4eec9 100644
--- a/Swiften/Network/ConnectionFactory.h
+++ b/Swiften/Network/ConnectionFactory.h
@@ -2,14 +2,13 @@
#define SWIFTEN_ConnectionFactory_H
namespace Swift {
- class String;
class Connection;
class ConnectionFactory {
public:
virtual ~ConnectionFactory();
- virtual Connection* createConnection(const String& domain) = 0;
+ virtual Connection* createConnection() = 0;
};
}
diff --git a/Swiften/Network/ConnectionServer.h b/Swiften/Network/ConnectionServer.h
index d376308..539367d 100644
--- a/Swiften/Network/ConnectionServer.h
+++ b/Swiften/Network/ConnectionServer.h
@@ -3,13 +3,13 @@
#include <boost/shared_ptr.hpp>
#include <boost/signal.hpp>
-#include "Swiften/Network/IncomingConnection.h"
+#include "Swiften/Network/Connection.h"
namespace Swift {
class ConnectionServer {
public:
virtual ~ConnectionServer();
- boost::signal<void (boost::shared_ptr<IncomingConnection>)> onNewConnection;
+ boost::signal<void (boost::shared_ptr<Connection>)> onNewConnection;
};
}
diff --git a/Swiften/Network/IncomingConnection.cpp b/Swiften/Network/IncomingConnection.cpp
deleted file mode 100644
index 669f68e..0000000
--- a/Swiften/Network/IncomingConnection.cpp
+++ /dev/null
@@ -1,8 +0,0 @@
-#include "Swiften/Network/IncomingConnection.h"
-
-namespace Swift {
-
-IncomingConnection::~IncomingConnection() {
-}
-
-}
diff --git a/Swiften/Network/IncomingConnection.h b/Swiften/Network/IncomingConnection.h
deleted file mode 100644
index eaa2ad6..0000000
--- a/Swiften/Network/IncomingConnection.h
+++ /dev/null
@@ -1,17 +0,0 @@
-#pragma once
-
-#include <boost/signal.hpp>
-
-#include "Swiften/Base/ByteArray.h"
-
-namespace Swift {
- class IncomingConnection {
- public:
- virtual ~IncomingConnection();
-
- virtual void write(const ByteArray& data) = 0;
-
- //boost::signal<void (Error)> onError;
- boost::signal<void (const ByteArray&)> onDataRead;
- };
-}
diff --git a/Swiften/Network/Makefile.inc b/Swiften/Network/Makefile.inc
index 3e4ece6..d8c1ee9 100644
--- a/Swiften/Network/Makefile.inc
+++ b/Swiften/Network/Makefile.inc
@@ -5,7 +5,6 @@ SWIFTEN_SOURCES += \
Swiften/Network/BoostConnection.cpp \
Swiften/Network/BoostConnectionFactory.cpp \
Swiften/Network/ConnectionServer.cpp \
- Swiften/Network/IncomingConnection.cpp \
Swiften/Network/BoostIOServiceThread.cpp \
Swiften/Network/Timer.cpp