/* * Copyright (c) 2010-2018 Isode Limited. * All rights reserved. * See the COPYING file for more information. */ #pragma once #include #include #include #include #include #include #include #include #include namespace Swift { class DomainNameAddressQuery; class DomainNameResolver; class ConnectionFactory; class TimerFactory; class SWIFTEN_API ComponentConnector : public boost::signals2::trackable, public std::enable_shared_from_this { public: typedef std::shared_ptr ref; static ComponentConnector::ref create(const std::string& hostname, unsigned short port, DomainNameResolver* resolver, ConnectionFactory* connectionFactory, TimerFactory* timerFactory) { return ref(new ComponentConnector(hostname, port, resolver, connectionFactory, timerFactory)); } void setTimeoutMilliseconds(int milliseconds); void start(); void stop(); boost::signals2::signal)> onConnectFinished; private: ComponentConnector(const std::string& hostname, unsigned short port, DomainNameResolver*, ConnectionFactory*, TimerFactory*); void handleAddressQueryResult(const std::vector& address, boost::optional error); void tryNextAddress(); void tryConnect(const HostAddressPort& target); void handleConnectionConnectFinished(bool error); void finish(std::shared_ptr); void handleTimeout(); private: std::string hostname; unsigned short port; DomainNameResolver* resolver; ConnectionFactory* connectionFactory; TimerFactory* timerFactory; int timeoutMilliseconds; std::shared_ptr timer; std::shared_ptr addressQuery; std::deque addressQueryResults; std::shared_ptr currentConnection; }; }