Esempio n. 1
0
void connection_queue::try_connect(connection_queue::mutex_t::scoped_lock& l)
{
    INVARIANT_CHECK;

#ifdef TORRENT_CONNECTION_LOGGING
    m_log << log_time() << " " << free_slots() << std::endl;
#endif
    // if this is enabled, UPnP connections will be blocked when shutting down
//		if (m_abort) return;

    if (m_num_connecting >= m_half_open_limit
            && m_half_open_limit > 0) return;

    if (m_queue.empty())
    {
        error_code ec;
        m_timer.cancel(ec);
        return;
    }

    // all entries are connecting, no need to look for new ones
    if (m_queue.size() == m_num_connecting)
        return;

    std::list<entry>::iterator i = std::find_if(m_queue.begin()
                                   , m_queue.end(), boost::bind(&entry::connecting, _1) == false);

    std::list<entry> to_connect;

    while (i != m_queue.end())
    {
        TORRENT_ASSERT(i->connecting == false);
        ptime expire = time_now_hires() + i->timeout;
        if (m_num_connecting == 0)
        {
#if defined TORRENT_ASIO_DEBUGGING
            add_outstanding_async("connection_queue::on_timeout");
#endif
            error_code ec;
            m_timer.expires_at(expire, ec);
            m_timer.async_wait(boost::bind(&connection_queue::on_timeout, this, _1));
            ++m_num_timers;
        }
        i->connecting = true;
        ++m_num_connecting;
        i->expires = expire;

        INVARIANT_CHECK;

        to_connect.push_back(*i);

#ifdef TORRENT_CONNECTION_LOGGING
        m_log << log_time() << " " << free_slots() << std::endl;
#endif

        if (m_num_connecting >= m_half_open_limit
                && m_half_open_limit > 0) break;
        if (m_num_connecting == m_queue.size()) break;
        i = std::find_if(i, m_queue.end(), boost::bind(&entry::connecting, _1) == false);
    }

    l.unlock();

    while (!to_connect.empty())
    {
        entry& ent = to_connect.front();
        TORRENT_TRY {
            ent.on_connect(ent.ticket);
        } TORRENT_CATCH(std::exception&) {}
        to_connect.pop_front();
    }

}
Esempio n. 2
0
	void connection_queue::try_connect(connection_queue::mutex_t::scoped_lock& l)
	{
		INVARIANT_CHECK;

#if BOOST_VERSION >= 103700
		TORRENT_ASSERT(l.owns_lock());
#endif

#ifdef TORRENT_CONNECTION_LOGGING
		m_log << log_time() << " " << free_slots() << std::endl;
#endif
		// if this is enabled, UPnP connections will be blocked when shutting down
//		if (m_abort) return;

		if (m_num_connecting >= m_half_open_limit
			&& m_half_open_limit > 0) return;
	
		if (m_queue.empty())
		{
			error_code ec;
			m_timer.cancel(ec);
			return;
		}

		std::list<entry>::iterator i = std::find_if(m_queue.begin()
			, m_queue.end(), boost::bind(&entry::connecting, _1) == false);

		std::list<entry> to_connect;

		while (i != m_queue.end())
		{
			TORRENT_ASSERT(i->connecting == false);
			ptime expire = time_now_hires() + i->timeout;
			if (m_num_connecting == 0)
			{
				error_code ec;
				m_timer.expires_at(expire, ec);
				m_timer.async_wait(boost::bind(&connection_queue::on_timeout, this, _1));
			}
			i->connecting = true;
			++m_num_connecting;
			i->expires = expire;

			INVARIANT_CHECK;

			to_connect.push_back(*i);

#ifdef TORRENT_CONNECTION_LOGGING
			m_log << log_time() << " " << free_slots() << std::endl;
#endif

			if (m_num_connecting >= m_half_open_limit
				&& m_half_open_limit > 0) break;
			i = std::find_if(i, m_queue.end(), boost::bind(&entry::connecting, _1) == false);
		}

		l.unlock();

		while (!to_connect.empty())
		{
			entry& ent = to_connect.front();
#ifndef BOOST_NO_EXCEPTIONS
			try {
#endif
				ent.on_connect(ent.ticket);
#ifndef BOOST_NO_EXCEPTIONS
			} catch (std::exception&) {}
#endif
			to_connect.pop_front();
		}

	}