|
@ -62,8 +62,8 @@ Host::Host(std::string const& _clientVersion, NetworkPreferences const& _n, bool |
|
|
m_clientVersion(_clientVersion), |
|
|
m_clientVersion(_clientVersion), |
|
|
m_netPrefs(_n), |
|
|
m_netPrefs(_n), |
|
|
m_ioService(new ba::io_service), |
|
|
m_ioService(new ba::io_service), |
|
|
m_acceptor(*m_ioService), |
|
|
m_acceptor(new bi::tcp::acceptor(*m_ioService)), |
|
|
m_socket(*m_ioService), |
|
|
m_socket(new bi::tcp::socket(*m_ioService)), |
|
|
m_key(KeyPair::create()) |
|
|
m_key(KeyPair::create()) |
|
|
{ |
|
|
{ |
|
|
populateAddresses(); |
|
|
populateAddresses(); |
|
@ -91,11 +91,11 @@ void Host::start() |
|
|
bi::tcp::endpoint endpoint(bi::tcp::v4(), i ? 0 : m_netPrefs.listenPort); |
|
|
bi::tcp::endpoint endpoint(bi::tcp::v4(), i ? 0 : m_netPrefs.listenPort); |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
m_acceptor.open(endpoint.protocol()); |
|
|
m_acceptor->open(endpoint.protocol()); |
|
|
m_acceptor.set_option(ba::socket_base::reuse_address(true)); |
|
|
m_acceptor->set_option(ba::socket_base::reuse_address(true)); |
|
|
m_acceptor.bind(endpoint); |
|
|
m_acceptor->bind(endpoint); |
|
|
m_acceptor.listen(); |
|
|
m_acceptor->listen(); |
|
|
m_listenPort = i ? m_acceptor.local_endpoint().port() : m_netPrefs.listenPort; |
|
|
m_listenPort = i ? m_acceptor->local_endpoint().port() : m_netPrefs.listenPort; |
|
|
break; |
|
|
break; |
|
|
} |
|
|
} |
|
|
catch (...) |
|
|
catch (...) |
|
@ -105,7 +105,7 @@ void Host::start() |
|
|
cwarn << "Couldn't start accepting connections on host. Something very wrong with network?\n" << boost::current_exception_diagnostic_information(); |
|
|
cwarn << "Couldn't start accepting connections on host. Something very wrong with network?\n" << boost::current_exception_diagnostic_information(); |
|
|
return; |
|
|
return; |
|
|
} |
|
|
} |
|
|
m_acceptor.close(); |
|
|
m_acceptor->close(); |
|
|
continue; |
|
|
continue; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -118,20 +118,24 @@ void Host::start() |
|
|
|
|
|
|
|
|
void Host::stop() |
|
|
void Host::stop() |
|
|
{ |
|
|
{ |
|
|
|
|
|
// if there's no ioService, it means we've had quit() called - bomb out - we're not allowed in here.
|
|
|
|
|
|
if (!m_ioService) |
|
|
|
|
|
return; |
|
|
|
|
|
|
|
|
for (auto const& h: m_capabilities) |
|
|
for (auto const& h: m_capabilities) |
|
|
h.second->onStopping(); |
|
|
h.second->onStopping(); |
|
|
|
|
|
|
|
|
stopWorking(); |
|
|
stopWorking(); |
|
|
|
|
|
|
|
|
if (m_acceptor.is_open()) |
|
|
if (m_acceptor->is_open()) |
|
|
{ |
|
|
{ |
|
|
if (m_accepting) |
|
|
if (m_accepting) |
|
|
m_acceptor.cancel(); |
|
|
m_acceptor->cancel(); |
|
|
m_acceptor.close(); |
|
|
m_acceptor->close(); |
|
|
m_accepting = false; |
|
|
m_accepting = false; |
|
|
} |
|
|
} |
|
|
if (m_socket.is_open()) |
|
|
if (m_socket->is_open()) |
|
|
m_socket.close(); |
|
|
m_socket->close(); |
|
|
disconnectPeers(); |
|
|
disconnectPeers(); |
|
|
|
|
|
|
|
|
if (!!m_ioService) |
|
|
if (!!m_ioService) |
|
@ -147,6 +151,8 @@ void Host::quit() |
|
|
// such tasks may involve socket reads from Capabilities that maintain references
|
|
|
// such tasks may involve socket reads from Capabilities that maintain references
|
|
|
// to resources we're about to free.
|
|
|
// to resources we're about to free.
|
|
|
stop(); |
|
|
stop(); |
|
|
|
|
|
m_acceptor.reset(); |
|
|
|
|
|
m_socket.reset(); |
|
|
m_ioService.reset(); |
|
|
m_ioService.reset(); |
|
|
// m_acceptor & m_socket are DANGEROUS now.
|
|
|
// m_acceptor & m_socket are DANGEROUS now.
|
|
|
} |
|
|
} |
|
@ -463,18 +469,18 @@ void Host::ensureAccepting() |
|
|
{ |
|
|
{ |
|
|
clog(NetConnect) << "Listening on local port " << m_listenPort << " (public: " << m_public << ")"; |
|
|
clog(NetConnect) << "Listening on local port " << m_listenPort << " (public: " << m_public << ")"; |
|
|
m_accepting = true; |
|
|
m_accepting = true; |
|
|
m_acceptor.async_accept(m_socket, [=](boost::system::error_code ec) |
|
|
m_acceptor->async_accept(*m_socket, [=](boost::system::error_code ec) |
|
|
{ |
|
|
{ |
|
|
if (!ec) |
|
|
if (!ec) |
|
|
{ |
|
|
{ |
|
|
try |
|
|
try |
|
|
{ |
|
|
{ |
|
|
try { |
|
|
try { |
|
|
clog(NetConnect) << "Accepted connection from " << m_socket.remote_endpoint(); |
|
|
clog(NetConnect) << "Accepted connection from " << m_socket->remote_endpoint(); |
|
|
} catch (...){} |
|
|
} catch (...){} |
|
|
bi::address remoteAddress = m_socket.remote_endpoint().address(); |
|
|
bi::address remoteAddress = m_socket->remote_endpoint().address(); |
|
|
// Port defaults to 0 - we let the hello tell us which port the peer listens to
|
|
|
// Port defaults to 0 - we let the hello tell us which port the peer listens to
|
|
|
auto p = std::make_shared<Session>(this, std::move(m_socket), bi::tcp::endpoint(remoteAddress, 0)); |
|
|
auto p = std::make_shared<Session>(this, std::move(*m_socket), bi::tcp::endpoint(remoteAddress, 0)); |
|
|
p->start(); |
|
|
p->start(); |
|
|
} |
|
|
} |
|
|
catch (Exception const& _e) |
|
|
catch (Exception const& _e) |
|
|