From e7e514d95e190a3a7d466740a4c3a1dfa9833ccc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Remko=20Tron=C3=A7on?= Date: Thu, 3 Dec 2009 00:09:55 +0100 Subject: Do domain resolving in a separate thread. diff --git a/Swiften/Network/PlatformDomainNameResolver.cpp b/Swiften/Network/PlatformDomainNameResolver.cpp index e30615b..3636cd6 100644 --- a/Swiften/Network/PlatformDomainNameResolver.cpp +++ b/Swiften/Network/PlatformDomainNameResolver.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -21,9 +22,24 @@ using namespace Swift; namespace { struct AddressQuery : public DomainNameAddressQuery, public boost::enable_shared_from_this, public EventOwner { - AddressQuery(const String& host) : hostname(host) {} + AddressQuery(const String& host) : hostname(host), thread(NULL), safeToJoin(false) {} - virtual void run() { + ~AddressQuery() { + if (safeToJoin) { + thread->join(); + } + else { + // FIXME: UGLYYYYY + } + delete thread; + } + + void run() { + safeToJoin = false; + thread = new boost::thread(boost::bind(&AddressQuery::doRun, shared_from_this())); + } + + void doRun() { boost::asio::ip::tcp::resolver resolver(ioService); boost::asio::ip::tcp::resolver::query query(hostname.getUTF8String(), "5222"); try { @@ -42,6 +58,7 @@ namespace { catch (...) { emitError(); } + safeToJoin = true; } void emitError() { @@ -50,6 +67,8 @@ namespace { boost::asio::io_service ioService; String hostname; + boost::thread* thread; + bool safeToJoin; }; String getNormalized(const String& domain) { diff --git a/Swiften/Network/PlatformDomainNameServiceQuery.cpp b/Swiften/Network/PlatformDomainNameServiceQuery.cpp index eeb9fd6..12afbb7 100644 --- a/Swiften/Network/PlatformDomainNameServiceQuery.cpp +++ b/Swiften/Network/PlatformDomainNameServiceQuery.cpp @@ -32,10 +32,25 @@ namespace { namespace Swift { -PlatformDomainNameServiceQuery::PlatformDomainNameServiceQuery(const String& service) : service(service) { +PlatformDomainNameServiceQuery::PlatformDomainNameServiceQuery(const String& service) : thread(NULL), service(service), safeToJoin(true) { +} + +PlatformDomainNameServiceQuery::~PlatformDomainNameServiceQuery() { + if (safeToJoin) { + thread->join(); + } + else { + // FIXME: UGLYYYYY + } + delete thread; } void PlatformDomainNameServiceQuery::run() { + safeToJoin = false; + thread = new boost::thread(boost::bind(&PlatformDomainNameServiceQuery::doRun, shared_from_this())); +} + +void PlatformDomainNameServiceQuery::doRun() { std::vector records; #if defined(SWIFTEN_PLATFORM_WINDOWS) @@ -145,11 +160,13 @@ void PlatformDomainNameServiceQuery::run() { } #endif + safeToJoin = true; std::sort(records.begin(), records.end(), SRVRecordPriorityComparator()); MainEventLoop::postEvent(boost::bind(boost::ref(onResult), records)); } void PlatformDomainNameServiceQuery::emitError() { + safeToJoin = true; MainEventLoop::postEvent(boost::bind(boost::ref(onResult), std::vector()), shared_from_this()); } diff --git a/Swiften/Network/PlatformDomainNameServiceQuery.h b/Swiften/Network/PlatformDomainNameServiceQuery.h index 58257af..1b1c1b5 100644 --- a/Swiften/Network/PlatformDomainNameServiceQuery.h +++ b/Swiften/Network/PlatformDomainNameServiceQuery.h @@ -1,5 +1,6 @@ #pragma once +#include #include #include "Swiften/Network/DomainNameServiceQuery.h" @@ -10,13 +11,17 @@ namespace Swift { class PlatformDomainNameServiceQuery : public DomainNameServiceQuery, public boost::enable_shared_from_this, public EventOwner { public: PlatformDomainNameServiceQuery(const String& service); + ~PlatformDomainNameServiceQuery(); virtual void run(); private: + void doRun(); void emitError(); private: + boost::thread* thread; + bool safeToJoin; String service; }; } -- cgit v0.10.2-6-g49f6