listen port refactoring

This commit is contained in:
Marcos Pinto 2007-09-10 20:52:42 +00:00
parent c2b9a5eaaa
commit c6cb4a20c8
1 changed files with 14 additions and 14 deletions

View File

@ -676,13 +676,15 @@ namespace detail
m_listen_socket->set_option(socket_acceptor::reuse_address(true)); m_listen_socket->set_option(socket_acceptor::reuse_address(true));
m_listen_socket->bind(m_listen_interface); m_listen_socket->bind(m_listen_interface);
m_listen_socket->listen(); m_listen_socket->listen();
m_listen_interface = m_listen_socket->local_endpoint();
m_external_listen_port = m_listen_interface.port(); m_external_listen_port = m_listen_interface.port();
break; break;
} }
catch (asio::system_error& e) catch (asio::system_error& e)
{ {
// TODO: make sure this is correct // TODO: make sure this is correct
if (e.code() == asio::error::host_not_found) if (e.code() == asio::error::host_not_found
|| m_listen_interface.port() == 0)
{ {
if (m_alerts.should_post(alert::fatal)) if (m_alerts.should_post(alert::fatal))
{ {
@ -727,14 +729,18 @@ namespace detail
} }
} }
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
if (m_listen_socket) if (m_listen_socket)
{ {
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
(*m_logger) << "listening on port: " << m_listen_interface.port() (*m_logger) << "listening on port: " << m_listen_interface.port()
<< " external port: " << m_external_listen_port << "\n"; << " external port: " << m_external_listen_port << "\n";
}
#endif #endif
if (m_listen_socket) async_accept(); async_accept();
if (m_natpmp.get())
m_natpmp->set_mappings(m_listen_interface.port(), 0);
if (m_upnp.get())
m_upnp->set_mappings(m_listen_interface.port(), 0);
}
} }
void session_impl::async_accept() void session_impl::async_accept()
@ -760,7 +766,6 @@ namespace detail
if (m_abort) return; if (m_abort) return;
async_accept();
if (e) if (e)
{ {
#if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING) #if defined(TORRENT_VERBOSE_LOGGING) || defined(TORRENT_LOGGING)
@ -769,8 +774,12 @@ namespace detail
(*m_logger) << msg << "\n"; (*m_logger) << msg << "\n";
#endif #endif
assert(m_listen_socket.unique()); assert(m_listen_socket.unique());
// try any random port
m_listen_interface.port(0);
open_listen_port();
return; return;
} }
async_accept();
// we got a connection request! // we got a connection request!
m_incoming_connection = true; m_incoming_connection = true;
@ -1269,10 +1278,6 @@ namespace detail
{ {
session_impl::mutex_t::scoped_lock l(m_mutex); session_impl::mutex_t::scoped_lock l(m_mutex);
open_listen_port(); open_listen_port();
if (m_natpmp.get())
m_natpmp->set_mappings(m_listen_interface.port(), 0);
if (m_upnp.get())
m_upnp->set_mappings(m_listen_interface.port(), 0);
} }
ptime timer = time_now(); ptime timer = time_now();
@ -1696,11 +1701,6 @@ namespace detail
bool new_listen_address = m_listen_interface.address() != new_interface.address(); bool new_listen_address = m_listen_interface.address() != new_interface.address();
if (m_natpmp.get())
m_natpmp->set_mappings(m_listen_interface.port(), 0);
if (m_upnp.get())
m_upnp->set_mappings(m_listen_interface.port(), 0);
#ifndef TORRENT_DISABLE_DHT #ifndef TORRENT_DISABLE_DHT
if ((new_listen_address || m_dht_same_port) && m_dht) if ((new_listen_address || m_dht_same_port) && m_dht)
{ {