2007-05-05 02:29:33 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
Copyright (c) 2007, 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 <boost/bind.hpp>
|
2011-02-25 18:00:36 +01:00
|
|
|
#include "libtorrent/config.hpp"
|
2007-05-05 02:29:33 +02:00
|
|
|
#include "libtorrent/invariant_check.hpp"
|
|
|
|
#include "libtorrent/connection_queue.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/io_service.hpp"
|
2009-09-16 05:46:36 +02:00
|
|
|
#include "libtorrent/error_code.hpp"
|
2009-11-23 09:38:50 +01:00
|
|
|
#include "libtorrent/error.hpp"
|
2007-05-05 02:29:33 +02:00
|
|
|
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
#include "libtorrent/debug.hpp"
|
|
|
|
#endif
|
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
namespace libtorrent
|
|
|
|
{
|
|
|
|
|
|
|
|
connection_queue::connection_queue(io_service& ios): m_next_ticket(0)
|
|
|
|
, m_num_connecting(0)
|
|
|
|
, m_half_open_limit(0)
|
2008-10-30 18:13:10 +01:00
|
|
|
, m_abort(false)
|
2007-05-05 02:29:33 +02:00
|
|
|
, m_timer(ios)
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-07-15 21:00:41 +02:00
|
|
|
, m_in_timeout_function(false)
|
|
|
|
#endif
|
2008-03-16 11:49:47 +01:00
|
|
|
{
|
|
|
|
#ifdef TORRENT_CONNECTION_LOGGING
|
|
|
|
m_log.open("connection_queue.log");
|
|
|
|
#endif
|
|
|
|
}
|
2007-05-05 02:29:33 +02:00
|
|
|
|
2008-03-16 11:49:47 +01:00
|
|
|
int connection_queue::free_slots() const
|
|
|
|
{
|
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
2008-03-21 06:41:56 +01:00
|
|
|
return m_half_open_limit == 0 ? (std::numeric_limits<int>::max)()
|
2008-03-16 11:49:47 +01:00
|
|
|
: m_half_open_limit - m_queue.size();
|
|
|
|
}
|
2007-05-05 02:29:33 +02:00
|
|
|
|
|
|
|
void connection_queue::enqueue(boost::function<void(int)> const& on_connect
|
|
|
|
, boost::function<void()> const& on_timeout
|
2008-03-12 08:44:27 +01:00
|
|
|
, time_duration timeout, int priority)
|
2007-05-05 02:29:33 +02:00
|
|
|
{
|
2007-09-08 18:27:43 +02:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2008-03-12 08:44:27 +01:00
|
|
|
TORRENT_ASSERT(priority >= 0);
|
2011-09-17 23:15:42 +02:00
|
|
|
TORRENT_ASSERT(priority < 3);
|
2008-03-12 08:44:27 +01:00
|
|
|
|
|
|
|
entry* e = 0;
|
|
|
|
|
|
|
|
switch (priority)
|
|
|
|
{
|
|
|
|
case 0:
|
|
|
|
m_queue.push_back(entry());
|
|
|
|
e = &m_queue.back();
|
|
|
|
break;
|
|
|
|
case 1:
|
2011-09-17 23:15:42 +02:00
|
|
|
case 2:
|
2008-03-12 08:44:27 +01:00
|
|
|
m_queue.push_front(entry());
|
|
|
|
e = &m_queue.front();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
e->priority = priority;
|
|
|
|
e->on_connect = on_connect;
|
|
|
|
e->on_timeout = on_timeout;
|
|
|
|
e->ticket = m_next_ticket;
|
|
|
|
e->timeout = timeout;
|
2007-05-05 02:29:33 +02:00
|
|
|
++m_next_ticket;
|
2009-05-23 23:36:09 +02:00
|
|
|
|
|
|
|
if (m_num_connecting < m_half_open_limit
|
|
|
|
|| m_half_open_limit == 0)
|
|
|
|
m_timer.get_io_service().post(boost::bind(
|
|
|
|
&connection_queue::on_try_connect, this));
|
2007-05-05 02:29:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void connection_queue::done(int ticket)
|
|
|
|
{
|
2007-09-08 18:27:43 +02:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
|
|
|
std::list<entry>::iterator i = std::find_if(m_queue.begin()
|
|
|
|
, m_queue.end(), boost::bind(&entry::ticket, _1) == ticket);
|
|
|
|
if (i == m_queue.end())
|
|
|
|
{
|
|
|
|
// this might not be here in case on_timeout calls remove
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (i->connecting) --m_num_connecting;
|
|
|
|
m_queue.erase(i);
|
2009-05-23 23:36:09 +02:00
|
|
|
|
|
|
|
if (m_num_connecting < m_half_open_limit
|
|
|
|
|| m_half_open_limit == 0)
|
|
|
|
m_timer.get_io_service().post(boost::bind(
|
|
|
|
&connection_queue::on_try_connect, this));
|
2007-05-05 02:29:33 +02:00
|
|
|
}
|
|
|
|
|
2007-10-26 09:14:19 +02:00
|
|
|
void connection_queue::close()
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2008-10-30 18:13:10 +01:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
2012-01-20 09:04:10 +01:00
|
|
|
if (m_num_connecting == 0) m_timer.cancel(ec);
|
2008-10-30 18:13:10 +01:00
|
|
|
m_abort = true;
|
|
|
|
|
2012-02-21 07:47:48 +01:00
|
|
|
std::list<entry> tmp;
|
|
|
|
tmp.swap(m_queue);
|
|
|
|
m_num_connecting = 0;
|
|
|
|
|
|
|
|
// we don't want to call the timeout callback while we're locked
|
|
|
|
// since that is a recipie for dead-locks
|
|
|
|
l.unlock();
|
|
|
|
|
|
|
|
while (!tmp.empty())
|
2008-10-30 18:13:10 +01:00
|
|
|
{
|
2012-02-21 07:47:48 +01:00
|
|
|
entry& e = tmp.front();
|
2011-09-17 23:15:42 +02:00
|
|
|
if (e.priority > 1)
|
|
|
|
{
|
2012-02-21 07:47:48 +01:00
|
|
|
mutex_t::scoped_lock ll(m_mutex);
|
|
|
|
if (e.connecting) ++m_num_connecting;
|
|
|
|
m_queue.push_back(e);
|
|
|
|
tmp.pop_front();
|
2011-09-17 23:15:42 +02:00
|
|
|
continue;
|
|
|
|
}
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
2009-05-23 23:36:09 +02:00
|
|
|
e.on_timeout();
|
2011-02-25 18:00:36 +01:00
|
|
|
} TORRENT_CATCH(std::exception&) {}
|
2012-02-21 07:47:48 +01:00
|
|
|
tmp.pop_front();
|
2008-10-30 18:13:10 +01:00
|
|
|
}
|
2007-10-26 09:14:19 +02:00
|
|
|
}
|
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
void connection_queue::limit(int limit)
|
2008-03-16 11:49:47 +01:00
|
|
|
{
|
|
|
|
TORRENT_ASSERT(limit >= 0);
|
|
|
|
m_half_open_limit = limit;
|
|
|
|
}
|
2007-05-05 02:29:33 +02:00
|
|
|
|
|
|
|
int connection_queue::limit() const
|
|
|
|
{ return m_half_open_limit; }
|
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-05-05 02:29:33 +02:00
|
|
|
|
|
|
|
void connection_queue::check_invariant() const
|
|
|
|
{
|
|
|
|
int num_connecting = 0;
|
|
|
|
for (std::list<entry>::const_iterator i = m_queue.begin();
|
|
|
|
i != m_queue.end(); ++i)
|
|
|
|
{
|
|
|
|
if (i->connecting) ++num_connecting;
|
2010-06-18 06:43:20 +02:00
|
|
|
else TORRENT_ASSERT(i->expires == max_time());
|
2007-05-05 02:29:33 +02:00
|
|
|
}
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(num_connecting == m_num_connecting);
|
2007-05-05 02:29:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
2009-05-23 23:36:09 +02:00
|
|
|
void connection_queue::try_connect(connection_queue::mutex_t::scoped_lock& l)
|
2007-05-05 02:29:33 +02:00
|
|
|
{
|
|
|
|
INVARIANT_CHECK;
|
2009-06-03 09:22:43 +02:00
|
|
|
|
2008-03-16 11:49:47 +01:00
|
|
|
#ifdef TORRENT_CONNECTION_LOGGING
|
|
|
|
m_log << log_time() << " " << free_slots() << std::endl;
|
|
|
|
#endif
|
2010-09-30 10:57:12 +02:00
|
|
|
// if this is enabled, UPnP connections will be blocked when shutting down
|
|
|
|
// if (m_abort) return;
|
2008-03-16 11:49:47 +01:00
|
|
|
|
2008-03-19 20:27:28 +01:00
|
|
|
if (m_num_connecting >= m_half_open_limit
|
|
|
|
&& m_half_open_limit > 0) return;
|
2007-11-02 01:27:53 +01:00
|
|
|
|
|
|
|
if (m_queue.empty())
|
|
|
|
{
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-12-29 23:45:42 +01:00
|
|
|
m_timer.cancel(ec);
|
2007-11-02 01:27:53 +01:00
|
|
|
return;
|
|
|
|
}
|
2007-05-05 02:29:33 +02:00
|
|
|
|
|
|
|
std::list<entry>::iterator i = std::find_if(m_queue.begin()
|
|
|
|
, m_queue.end(), boost::bind(&entry::connecting, _1) == false);
|
2009-04-26 04:46:10 +02:00
|
|
|
|
|
|
|
std::list<entry> to_connect;
|
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
while (i != m_queue.end())
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
TORRENT_ASSERT(i->connecting == false);
|
2010-06-18 06:43:20 +02:00
|
|
|
ptime expire = time_now_hires() + i->timeout;
|
2007-05-05 02:29:33 +02:00
|
|
|
if (m_num_connecting == 0)
|
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("connection_queue::on_timeout");
|
|
|
|
#endif
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-12-29 23:45:42 +01:00
|
|
|
m_timer.expires_at(expire, ec);
|
2007-05-05 02:29:33 +02:00
|
|
|
m_timer.async_wait(boost::bind(&connection_queue::on_timeout, this, _1));
|
|
|
|
}
|
|
|
|
i->connecting = true;
|
|
|
|
++m_num_connecting;
|
|
|
|
i->expires = expire;
|
2007-05-17 04:55:44 +02:00
|
|
|
|
|
|
|
INVARIANT_CHECK;
|
|
|
|
|
2009-04-26 04:46:10 +02:00
|
|
|
to_connect.push_back(*i);
|
2007-05-05 02:29:33 +02:00
|
|
|
|
2008-03-16 11:49:47 +01:00
|
|
|
#ifdef TORRENT_CONNECTION_LOGGING
|
|
|
|
m_log << log_time() << " " << free_slots() << std::endl;
|
|
|
|
#endif
|
|
|
|
|
2008-03-19 20:27:28 +01:00
|
|
|
if (m_num_connecting >= m_half_open_limit
|
|
|
|
&& m_half_open_limit > 0) break;
|
2007-05-05 02:29:33 +02:00
|
|
|
i = std::find_if(i, m_queue.end(), boost::bind(&entry::connecting, _1) == false);
|
|
|
|
}
|
2009-04-26 04:46:10 +02:00
|
|
|
|
2009-05-23 23:36:09 +02:00
|
|
|
l.unlock();
|
|
|
|
|
2009-04-26 04:46:10 +02:00
|
|
|
while (!to_connect.empty())
|
|
|
|
{
|
|
|
|
entry& ent = to_connect.front();
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
2009-04-26 04:46:10 +02:00
|
|
|
ent.on_connect(ent.ticket);
|
2011-02-25 18:00:36 +01:00
|
|
|
} TORRENT_CATCH(std::exception&) {}
|
2009-04-26 04:46:10 +02:00
|
|
|
to_connect.pop_front();
|
|
|
|
}
|
2009-05-23 23:36:09 +02:00
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
}
|
|
|
|
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-07-15 21:00:41 +02:00
|
|
|
struct function_guard
|
|
|
|
{
|
2007-10-05 02:30:00 +02:00
|
|
|
function_guard(bool& v): val(v) { TORRENT_ASSERT(!val); val = true; }
|
2007-07-15 21:00:41 +02:00
|
|
|
~function_guard() { val = false; }
|
|
|
|
|
|
|
|
bool& val;
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2008-05-03 18:05:42 +02:00
|
|
|
void connection_queue::on_timeout(error_code const& e)
|
2007-05-05 02:29:33 +02:00
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
complete_async("connection_queue::on_timeout");
|
|
|
|
#endif
|
2007-09-08 18:27:43 +02:00
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
INVARIANT_CHECK;
|
2008-11-29 22:33:21 +01:00
|
|
|
#ifdef TORRENT_DEBUG
|
2007-07-15 21:00:41 +02:00
|
|
|
function_guard guard_(m_in_timeout_function);
|
|
|
|
#endif
|
2007-05-05 02:29:33 +02:00
|
|
|
|
2009-11-23 09:38:50 +01:00
|
|
|
TORRENT_ASSERT(!e || e == error::operation_aborted);
|
2007-05-05 02:29:33 +02:00
|
|
|
if (e) return;
|
|
|
|
|
|
|
|
ptime next_expire = max_time();
|
2010-06-18 06:43:20 +02:00
|
|
|
ptime now = time_now_hires() + milliseconds(100);
|
2007-09-10 01:03:49 +02:00
|
|
|
std::list<entry> timed_out;
|
2007-05-05 02:29:33 +02:00
|
|
|
for (std::list<entry>::iterator i = m_queue.begin();
|
2007-07-27 19:41:42 +02:00
|
|
|
!m_queue.empty() && i != m_queue.end();)
|
2007-05-05 02:29:33 +02:00
|
|
|
{
|
|
|
|
if (i->connecting && i->expires < now)
|
|
|
|
{
|
2007-09-10 01:03:49 +02:00
|
|
|
std::list<entry>::iterator j = i;
|
|
|
|
++i;
|
|
|
|
timed_out.splice(timed_out.end(), m_queue, j, i);
|
2007-05-05 02:29:33 +02:00
|
|
|
--m_num_connecting;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (i->expires < next_expire)
|
|
|
|
next_expire = i->expires;
|
|
|
|
++i;
|
|
|
|
}
|
2007-09-10 01:03:49 +02:00
|
|
|
|
|
|
|
// we don't want to call the timeout callback while we're locked
|
|
|
|
// since that is a recepie for dead-locks
|
|
|
|
l.unlock();
|
|
|
|
|
|
|
|
for (std::list<entry>::iterator i = timed_out.begin()
|
|
|
|
, end(timed_out.end()); i != end; ++i)
|
|
|
|
{
|
2011-05-15 00:25:49 +02:00
|
|
|
TORRENT_ASSERT(i->connecting);
|
|
|
|
TORRENT_ASSERT(i->ticket != -1);
|
2011-02-25 18:00:36 +01:00
|
|
|
TORRENT_TRY {
|
2009-05-23 23:36:09 +02:00
|
|
|
i->on_timeout();
|
2011-02-25 18:00:36 +01:00
|
|
|
} TORRENT_CATCH(std::exception&) {}
|
2007-09-10 01:03:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
l.lock();
|
|
|
|
|
2007-05-05 02:29:33 +02:00
|
|
|
if (next_expire < max_time())
|
|
|
|
{
|
2010-11-28 02:47:30 +01:00
|
|
|
#if defined TORRENT_ASIO_DEBUGGING
|
|
|
|
add_outstanding_async("connection_queue::on_timeout");
|
|
|
|
#endif
|
2008-05-03 18:05:42 +02:00
|
|
|
error_code ec;
|
2007-12-29 23:45:42 +01:00
|
|
|
m_timer.expires_at(next_expire, ec);
|
2007-05-05 02:29:33 +02:00
|
|
|
m_timer.async_wait(boost::bind(&connection_queue::on_timeout, this, _1));
|
|
|
|
}
|
2009-05-23 23:36:09 +02:00
|
|
|
try_connect(l);
|
2007-05-05 02:29:33 +02:00
|
|
|
}
|
|
|
|
|
2009-05-23 23:36:09 +02:00
|
|
|
void connection_queue::on_try_connect()
|
|
|
|
{
|
|
|
|
mutex_t::scoped_lock l(m_mutex);
|
|
|
|
try_connect(l);
|
|
|
|
}
|
2007-05-05 02:29:33 +02:00
|
|
|
}
|
|
|
|
|