diff options
author | Remko Tronçon <git@el-tramo.be> | 2010-12-18 18:50:50 (GMT) |
---|---|---|
committer | Remko Tronçon <git@el-tramo.be> | 2010-12-18 18:50:50 (GMT) |
commit | 312a114c7e204cfe4cfe961509ab9b24ccde7860 (patch) | |
tree | 7649aba8b50de49f5259709ee3d34b035d83b33b /Swiften/Network/PlatformDomainNameAddressQuery.cpp | |
parent | 790dd3e97c6634f6a256f2e072507b9d5f29348b (diff) | |
download | swift-contrib-312a114c7e204cfe4cfe961509ab9b24ccde7860.zip swift-contrib-312a114c7e204cfe4cfe961509ab9b24ccde7860.tar.bz2 |
Move all domain name resolve queries into one thread.
This avoids reentrancy problems on some platform DNS calls.
Resolves: #443
Diffstat (limited to 'Swiften/Network/PlatformDomainNameAddressQuery.cpp')
-rw-r--r-- | Swiften/Network/PlatformDomainNameAddressQuery.cpp | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/Swiften/Network/PlatformDomainNameAddressQuery.cpp b/Swiften/Network/PlatformDomainNameAddressQuery.cpp new file mode 100644 index 0000000..2a8574d --- /dev/null +++ b/Swiften/Network/PlatformDomainNameAddressQuery.cpp @@ -0,0 +1,56 @@ +/* + * Copyright (c) 2010 Remko Tronçon + * Licensed under the GNU General Public License v3. + * See Documentation/Licenses/GPLv3.txt for more information. + */ + +#include <Swiften/Network/PlatformDomainNameAddressQuery.h> + +#include <Swiften/Network/PlatformDomainNameResolver.h> +#include <Swiften/EventLoop/EventLoop.h> + +namespace Swift { + +PlatformDomainNameAddressQuery::PlatformDomainNameAddressQuery(const String& host, EventLoop* eventLoop, PlatformDomainNameResolver* resolver) : PlatformDomainNameQuery(resolver), hostname(host), eventLoop(eventLoop) { +} + +void PlatformDomainNameAddressQuery::run() { + getResolver()->addQueryToQueue(shared_from_this()); +} + +void PlatformDomainNameAddressQuery::runBlocking() { + //std::cout << "PlatformDomainNameResolver::doRun()" << std::endl; + boost::asio::ip::tcp::resolver resolver(ioService); + boost::asio::ip::tcp::resolver::query query(hostname.getUTF8String(), "5222"); + try { + //std::cout << "PlatformDomainNameResolver::doRun(): Resolving" << std::endl; + boost::asio::ip::tcp::resolver::iterator endpointIterator = resolver.resolve(query); + //std::cout << "PlatformDomainNameResolver::doRun(): Resolved" << std::endl; + if (endpointIterator == boost::asio::ip::tcp::resolver::iterator()) { + //std::cout << "PlatformDomainNameResolver::doRun(): Error 1" << std::endl; + emitError(); + } + else { + std::vector<HostAddress> results; + for ( ; endpointIterator != boost::asio::ip::tcp::resolver::iterator(); ++endpointIterator) { + boost::asio::ip::address address = (*endpointIterator).endpoint().address(); + results.push_back(address.is_v4() ? HostAddress(&address.to_v4().to_bytes()[0], 4) : HostAddress(&address.to_v6().to_bytes()[0], 16)); + } + + //std::cout << "PlatformDomainNameResolver::doRun(): Success" << std::endl; + eventLoop->postEvent( + boost::bind(boost::ref(onResult), results, boost::optional<DomainNameResolveError>()), + shared_from_this()); + } + } + catch (...) { + //std::cout << "PlatformDomainNameResolver::doRun(): Error 2" << std::endl; + emitError(); + } +} + +void PlatformDomainNameAddressQuery::emitError() { + eventLoop->postEvent(boost::bind(boost::ref(onResult), std::vector<HostAddress>(), boost::optional<DomainNameResolveError>(DomainNameResolveError())), shared_from_this()); +} + +} |