premiere-libtorrent/src/resolver.cpp

130 lines
3.6 KiB
C++
Raw Normal View History

2014-07-06 21:18:00 +02:00
/*
Copyright (c) 2013, Arvid Norberg
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
* Redistributions of source code must retain the above copyright
notice, this list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in
the documentation and/or other materials provided with the distribution.
* Neither the name of the author nor the names of its
contributors may be used to endorse or promote products derived
from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
POSSIBILITY OF SUCH DAMAGE.
*/
#include "libtorrent/resolver.hpp"
#include <boost/bind.hpp>
#include "libtorrent/debug.hpp"
namespace libtorrent
{
resolver::resolver(io_service& ios)
: m_ios(ios)
, m_resolver(ios)
, m_critical_resolver(ios)
2014-07-06 21:18:00 +02:00
, m_max_size(700)
2014-08-16 22:55:44 +02:00
, m_timeout(seconds(1200))
2014-07-06 21:18:00 +02:00
{}
void resolver::on_lookup(error_code const& ec, tcp::resolver::iterator i
, resolver_interface::callback_t h, std::string hostname)
{
#if defined TORRENT_ASIO_DEBUGGING
complete_async("resolver::on_lookup");
#endif
if (ec)
{
std::vector<address> empty;
h(ec, empty);
return;
}
dns_cache_entry& ce = m_cache[hostname];
2014-08-16 22:55:44 +02:00
ptime now = time_now();
2014-07-06 21:18:00 +02:00
ce.last_seen = now;
ce.addresses.clear();
while (i != tcp::resolver::iterator())
{
ce.addresses.push_back(i->endpoint().address());
++i;
}
h(ec, ce.addresses);
// if m_cache grows too big, weed out the
// oldest entries
if (m_cache.size() > m_max_size)
{
cache_t::iterator oldest = m_cache.begin();
for (cache_t::iterator i = m_cache.begin();
i != m_cache.end(); ++i)
{
2014-09-01 09:26:29 +02:00
if (i->second.last_seen < oldest->second.last_seen)
oldest = i;
2014-07-06 21:18:00 +02:00
}
// remove the oldest entry
m_cache.erase(oldest);
}
}
void resolver::async_resolve(std::string const& host, int flags
, resolver_interface::callback_t const& h)
{
cache_t::iterator i = m_cache.find(host);
if (i != m_cache.end())
{
// keep cache entries valid for m_timeout seconds
if ((flags & resolver_interface::prefer_cache)
2014-08-16 22:55:44 +02:00
|| i->second.last_seen + m_timeout >= time_now())
2014-07-06 21:18:00 +02:00
{
error_code ec;
m_ios.post(boost::bind(h, ec, i->second.addresses));
return;
}
}
// the port is ignored
tcp::resolver::query q(host, "80");
#if defined TORRENT_ASIO_DEBUGGING
add_outstanding_async("resolver::on_lookup");
#endif
if (flags & resolver_interface::abort_on_shutdown)
{
m_resolver.async_resolve(q, boost::bind(&resolver::on_lookup, this, _1, _2
, h, host));
}
else
{
m_critical_resolver.async_resolve(q, boost::bind(&resolver::on_lookup, this, _1, _2
, h, host));
}
}
void resolver::abort()
{
m_resolver.cancel();
2014-07-06 21:18:00 +02:00
}
}