示例#1
0
void upnp::resend_request(error_code const& e)
{
	if (e) return;

	mutex_t::scoped_lock l(m_mutex);

	if (m_closing) return;

	if (m_retry_count < 12
		&& (m_devices.empty() || m_retry_count < 4))
	{
		discover_device_impl();
		return;
	}

	if (m_devices.empty())
	{
		disable("no UPnP router found (no response)");
		return;
	}
	
	for (std::set<rootdevice>::iterator i = m_devices.begin()
		, end(m_devices.end()); i != end; ++i)
	{
		if (i->control_url.empty() && !i->upnp_connection && !i->disabled)
		{
			// we don't have a WANIP or WANPPP url for this device,
			// ask for it
			rootdevice& d = const_cast<rootdevice&>(*i);
			TORRENT_ASSERT(d.magic == 1337);
#ifndef BOOST_NO_EXCEPTIONS
			try
			{
#endif
				char msg[200];
				snprintf(msg, sizeof(msg), "connecting to: %s", d.url.c_str());
				log(msg);
				if (d.upnp_connection) d.upnp_connection->close();
				d.upnp_connection.reset(new http_connection(m_io_service
					, m_cc, bind(&upnp::on_upnp_xml, self(), _1, _2
					, boost::ref(d), _5)));
				d.upnp_connection->get(d.url, seconds(30), 1);
#ifndef BOOST_NO_EXCEPTIONS
			}
			catch (std::exception& e)
			{
				(void)e;
				char msg[200];
				snprintf(msg, sizeof(msg), "connection failed to: %s %s", d.url.c_str(), e.what());
				log(msg);
				d.disabled = true;
			}
#endif
		}
	}
}