diff options
Diffstat (limited to 'Swiften')
-rw-r--r-- | Swiften/Network/PlatformDomainNameResolver.cpp | 23 | ||||
-rw-r--r-- | Swiften/Network/PlatformDomainNameServiceQuery.cpp | 19 | ||||
-rw-r--r-- | Swiften/Network/PlatformDomainNameServiceQuery.h | 5 |
3 files changed, 44 insertions, 3 deletions
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 @@ -4,12 +4,13 @@ #include "Swiften/Network/PlatformDomainNameServiceQuery.h" #include <string> #include <vector> #include <boost/asio.hpp> #include <boost/bind.hpp> +#include <boost/thread.hpp> #include <boost/enable_shared_from_this.hpp> #include <idna.h> #include <algorithm> #include "Swiften/Base/String.h" #include "Swiften/Network/HostAddress.h" @@ -18,15 +19,30 @@ #include "Swiften/Network/DomainNameAddressQuery.h" using namespace Swift; namespace { struct AddressQuery : public DomainNameAddressQuery, public boost::enable_shared_from_this<AddressQuery>, 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 { boost::asio::ip::tcp::resolver::iterator endpointIterator = resolver.resolve(query); if (endpointIterator == boost::asio::ip::tcp::resolver::iterator()) { emitError(); @@ -39,20 +55,23 @@ namespace { shared_from_this()); } } catch (...) { emitError(); } + safeToJoin = true; } void emitError() { MainEventLoop::postEvent(boost::bind(boost::ref(onResult), HostAddress(), boost::optional<DomainNameResolveError>(DomainNameResolveError())), shared_from_this()); } boost::asio::io_service ioService; String hostname; + boost::thread* thread; + bool safeToJoin; }; String getNormalized(const String& domain) { char* output; if (idna_to_ascii_8z(domain.getUTF8Data(), &output, 0) == IDNA_SUCCESS) { String result(output); 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 @@ -29,16 +29,31 @@ 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<DomainNameServiceQuery::Result> records; #if defined(SWIFTEN_PLATFORM_WINDOWS) DNS_RECORD* responses; // FIXME: This conversion doesn't work if unicode is deffed above if (DnsQuery(service.getUTF8Data(), DNS_TYPE_SRV, DNS_QUERY_STANDARD, NULL, &responses, NULL) != ERROR_SUCCESS) { @@ -142,15 +157,17 @@ void PlatformDomainNameServiceQuery::run() { records.push_back(record); currentEntry += entryLength; answersCount--; } #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<DomainNameServiceQuery::Result>()), 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,22 +1,27 @@ #pragma once +#include <boost/thread.hpp> #include <boost/enable_shared_from_this.hpp> #include "Swiften/Network/DomainNameServiceQuery.h" #include "Swiften/EventLoop/EventOwner.h" #include "Swiften/Base/String.h" namespace Swift { class PlatformDomainNameServiceQuery : public DomainNameServiceQuery, public boost::enable_shared_from_this<PlatformDomainNameServiceQuery>, public EventOwner { public: PlatformDomainNameServiceQuery(const String& service); + ~PlatformDomainNameServiceQuery(); virtual void run(); private: + void doRun(); void emitError(); private: + boost::thread* thread; + bool safeToJoin; String service; }; } |