diff options
Diffstat (limited to 'Swiften/Client')
-rw-r--r-- | Swiften/Client/Client.cpp | 33 | ||||
-rw-r--r-- | Swiften/Client/Client.h | 6 |
2 files changed, 22 insertions, 17 deletions
diff --git a/Swiften/Client/Client.cpp b/Swiften/Client/Client.cpp index 85db4ac..6614bf7 100644 --- a/Swiften/Client/Client.cpp +++ b/Swiften/Client/Client.cpp @@ -2,11 +2,11 @@ #include <boost/bind.hpp> -#include "Swiften/Network/DomainNameResolver.h" #include "Swiften/Network/MainBoostIOServiceThread.h" #include "Swiften/Network/BoostIOServiceThread.h" #include "Swiften/Client/ClientSession.h" #include "Swiften/StreamStack/PlatformTLSLayerFactory.h" +#include "Swiften/Network/Connector.h" #include "Swiften/Network/BoostConnectionFactory.h" #include "Swiften/Network/DomainNameResolveException.h" #include "Swiften/TLS/PKCS12Certificate.h" @@ -33,24 +33,22 @@ bool Client::isAvailable() { } void Client::connect() { - assert(!connection_); - DomainNameResolver resolver; - try { - HostAddressPort remote = resolver.resolve(jid_.getDomain().getUTF8String()); - connection_ = connectionFactory_->createConnection(); - connection_->onConnectFinished.connect(boost::bind(&Client::handleConnectionConnectFinished, this, _1)); - connection_->connect(remote); - } - catch (const DomainNameResolveException& e) { - onError(ClientError::DomainNameResolveError); - } + assert(!connector_); + connector_ = boost::shared_ptr<Connector>(new Connector(jid_.getDomain(), &resolver_, connectionFactory_)); + connector_->onConnectFinished.connect(boost::bind(&Client::handleConnectorFinished, this, _1)); + connector_->start(); } -void Client::handleConnectionConnectFinished(bool error) { - if (error) { +void Client::handleConnectorFinished(boost::shared_ptr<Connection> connection) { + // TODO: Add domain name resolver error + connector_.reset(); + if (!connection) { onError(ClientError::ConnectionError); } else { + assert(!connection_); + connection_ = connection; + assert(!sessionStream_); sessionStream_ = boost::shared_ptr<BasicSessionStream>(new BasicSessionStream(connection_, &payloadParserFactories_, &payloadSerializers_, tlsLayerFactory_)); if (!certificate_.isEmpty()) { @@ -78,6 +76,9 @@ void Client::disconnect() { } void Client::closeConnection() { + if (sessionStream_) { + sessionStream_.reset(); + } if (connection_) { connection_->disconnect(); connection_.reset(); @@ -186,11 +187,11 @@ void Client::handleNeedCredentials() { } void Client::handleDataRead(const String& data) { - onDataRead(data); + onDataRead(data); } void Client::handleDataWritten(const String& data) { - onDataWritten(data); + onDataWritten(data); } } diff --git a/Swiften/Client/Client.h b/Swiften/Client/Client.h index 3f7d350..f09c916 100644 --- a/Swiften/Client/Client.h +++ b/Swiften/Client/Client.h @@ -4,6 +4,7 @@ #include <boost/signals.hpp> #include <boost/shared_ptr.hpp> +#include "Swiften/Network/PlatformDomainNameResolver.h" #include "Swiften/Base/Error.h" #include "Swiften/Client/ClientSession.h" #include "Swiften/Client/ClientError.h" @@ -22,6 +23,7 @@ namespace Swift { class ConnectionFactory; class ClientSession; class BasicSessionStream; + class Connector; class Client : public StanzaChannel, public IQRouter, public boost::bsignals::trackable { public: @@ -46,7 +48,7 @@ namespace Swift { boost::signal<void (const String&)> onDataWritten; private: - void handleConnectionConnectFinished(bool error); + void handleConnectorFinished(boost::shared_ptr<Connection>); void send(boost::shared_ptr<Stanza>); virtual String getNewIQID(); void handleElement(boost::shared_ptr<Element>); @@ -58,9 +60,11 @@ namespace Swift { void closeConnection(); private: + PlatformDomainNameResolver resolver_; JID jid_; String password_; IDGenerator idGenerator_; + boost::shared_ptr<Connector> connector_; ConnectionFactory* connectionFactory_; TLSLayerFactory* tlsLayerFactory_; FullPayloadParserFactoryCollection payloadParserFactories_; |