From 7ef1133ee60eab2ae204e6898dc504ca58ba4496 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Mon, 13 Aug 2012 20:30:58 +0200 Subject: Set timeout on each connection attempt, instead of global connect timeout. Resolves: #962 diff --git a/Swiften/Client/CoreClient.cpp b/Swiften/Client/CoreClient.cpp index ea89c06..485cd4a 100644 --- a/Swiften/Client/CoreClient.cpp +++ b/Swiften/Client/CoreClient.cpp @@ -112,7 +112,7 @@ void CoreClient::connect(const ClientOptions& o) { if (options.boshURL.empty()) { connector_ = boost::make_shared(host, port, o.manualHostname.empty(), networkFactories->getDomainNameResolver(), connectionFactories, networkFactories->getTimerFactory()); connector_->onConnectFinished.connect(boost::bind(&CoreClient::handleConnectorFinished, this, _1, _2)); - connector_->setTimeoutMilliseconds(60*1000); + connector_->setTimeoutMilliseconds(2*60*1000); connector_->start(); } else { diff --git a/Swiften/Network/Connector.cpp b/Swiften/Network/Connector.cpp index 02ebb02..5ab3b92 100644 --- a/Swiften/Network/Connector.cpp +++ b/Swiften/Network/Connector.cpp @@ -37,7 +37,6 @@ void Connector::start() { if (timeoutMilliseconds > 0) { timer = timerFactory->createTimer(timeoutMilliseconds); timer->onTick.connect(boost::bind(&Connector::handleTimeout, shared_from_this())); - timer->start(); } serviceQuery->run(); } @@ -129,10 +128,17 @@ void Connector::tryConnect(const HostAddressPort& target) { currentConnection = connectionFactory->createConnection(); currentConnection->onConnectFinished.connect(boost::bind(&Connector::handleConnectionConnectFinished, shared_from_this(), _1)); currentConnection->connect(target); + if (timer) { + timer->start(); + } } void Connector::handleConnectionConnectFinished(bool error) { SWIFT_LOG(debug) << "ConnectFinished: " << (error ? "error" : "success") << std::endl; + if (timer) { + timer->stop(); + timer.reset(); + } currentConnection->onConnectFinished.disconnect(boost::bind(&Connector::handleConnectionConnectFinished, shared_from_this(), _1)); if (error) { currentConnection.reset(); @@ -174,7 +180,7 @@ void Connector::finish(boost::shared_ptr connection) { void Connector::handleTimeout() { SWIFT_LOG(debug) << "Timeout" << std::endl; - finish(boost::shared_ptr()); + handleConnectionConnectFinished(true); } }; diff --git a/Swiften/Network/UnitTest/ConnectorTest.cpp b/Swiften/Network/UnitTest/ConnectorTest.cpp index 2d96bda..fe18340 100644 --- a/Swiften/Network/UnitTest/ConnectorTest.cpp +++ b/Swiften/Network/UnitTest/ConnectorTest.cpp @@ -33,8 +33,9 @@ class ConnectorTest : public CppUnit::TestFixture { CPPUNIT_TEST(testConnect_AllSRVHostsFailWithoutFallbackHost); CPPUNIT_TEST(testConnect_AllSRVHostsFailWithFallbackHost); CPPUNIT_TEST(testConnect_SRVAndFallbackHostsFail); - CPPUNIT_TEST(testConnect_TimeoutDuringResolve); - CPPUNIT_TEST(testConnect_TimeoutDuringConnect); + //CPPUNIT_TEST(testConnect_TimeoutDuringResolve); + CPPUNIT_TEST(testConnect_TimeoutDuringConnectToOnlyCandidate); + CPPUNIT_TEST(testConnect_TimeoutDuringConnectToCandidateFallsBack); CPPUNIT_TEST(testConnect_NoTimeout); CPPUNIT_TEST(testStop_DuringSRVQuery); CPPUNIT_TEST(testStop_Timeout); @@ -209,7 +210,7 @@ class ConnectorTest : public CppUnit::TestFixture { CPPUNIT_ASSERT(!boost::dynamic_pointer_cast(error)); } - void testConnect_TimeoutDuringResolve() { + /*void testConnect_TimeoutDuringResolve() { Connector::ref testling(createConnector()); testling->setTimeoutMilliseconds(10); resolver->setIsResponsive(false); @@ -222,9 +223,9 @@ class ConnectorTest : public CppUnit::TestFixture { CPPUNIT_ASSERT_EQUAL(1, static_cast(connections.size())); CPPUNIT_ASSERT(boost::dynamic_pointer_cast(error)); CPPUNIT_ASSERT(!connections[0]); - } + }*/ - void testConnect_TimeoutDuringConnect() { + void testConnect_TimeoutDuringConnectToOnlyCandidate() { Connector::ref testling(createConnector()); testling->setTimeoutMilliseconds(10); resolver->addXMPPClientService("foo.com", host1); @@ -240,6 +241,30 @@ class ConnectorTest : public CppUnit::TestFixture { CPPUNIT_ASSERT(!boost::dynamic_pointer_cast(error)); } + void testConnect_TimeoutDuringConnectToCandidateFallsBack() { + Connector::ref testling(createConnector()); + testling->setTimeoutMilliseconds(10); + + resolver->addXMPPClientService("foo.com", "host-foo.com", 1234); + HostAddress address1("1.1.1.1"); + resolver->addAddress("host-foo.com", address1); + HostAddress address2("2.2.2.2"); + resolver->addAddress("host-foo.com", address2); + + connectionFactory->isResponsive = false; + testling->start(); + eventLoop->processEvents(); + connectionFactory->isResponsive = true; + timerFactory->setTime(10); + eventLoop->processEvents(); + + CPPUNIT_ASSERT_EQUAL(1, static_cast(connections.size())); + CPPUNIT_ASSERT(connections[0]); + CPPUNIT_ASSERT(HostAddressPort(address2, 1234) == *(connections[0]->hostAddressPort)); + CPPUNIT_ASSERT(!boost::dynamic_pointer_cast(error)); + } + + void testConnect_NoTimeout() { Connector::ref testling(createConnector()); testling->setTimeoutMilliseconds(10); -- cgit v0.10.2-6-g49f6