/* * 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 #include #include namespace Swift { class DomainNameAddressQuery; class DomainNameResolver; class ConnectionFactory; class TimerFactory; class SWIFTEN_API Connector : public boost::signals2::trackable, public std::enable_shared_from_this { public: typedef std::shared_ptr ref; static Connector::ref create(const std::string& hostname, unsigned short port, const boost::optional& serviceLookupPrefix, DomainNameResolver* resolver, ConnectionFactory* connectionFactory, TimerFactory* timerFactory) { return ref(new Connector(hostname, port, serviceLookupPrefix, resolver, connectionFactory, timerFactory)); } void setTimeoutMilliseconds(int milliseconds); /** * Start the connection attempt. * Note that after calling this method, the caller is responsible for calling #stop() * if it wants to cancel it. Not doing so can leak references. */ void start(); void stop(); boost::signals2::signal, std::shared_ptr)> onConnectFinished; private: Connector(const std::string& hostname, unsigned short port, const boost::optional& serviceLookupPrefix, DomainNameResolver*, ConnectionFactory*, TimerFactory*); void handleServiceQueryResult(const std::vector& result); void handleAddressQueryResult(const std::vector& address, boost::optional error); void queryAddress(const std::string& hostname); void tryNextServiceOrFallback(); 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; boost::optional serviceLookupPrefix; DomainNameResolver* resolver; ConnectionFactory* connectionFactory; TimerFactory* timerFactory; int timeoutMilliseconds; std::shared_ptr timer; std::shared_ptr serviceQuery; std::deque serviceQueryResults; std::shared_ptr addressQuery; std::deque addressQueryResults; bool queriedAllServices; std::shared_ptr currentConnection; bool foundSomeDNS; }; }