Browse Source

Comments for socket in acceptor. Remove start arg from Host constructor; it is not used and conflicts with restoreNodes being used to set node credentials.

cl-refactor
subtly 10 years ago
parent
commit
5566f335e8
  1. 26
      libp2p/Host.cpp
  2. 2
      libp2p/Host.h

26
libp2p/Host.cpp

@ -45,7 +45,7 @@ void HostNodeTableHandler::processEvent(NodeId const& _n, NodeTableEventType con
m_host.onNodeTableEvent(_n, _e);
}
Host::Host(std::string const& _clientVersion, NetworkPreferences const& _n, bool _start):
Host::Host(std::string const& _clientVersion, NetworkPreferences const& _n):
Worker("p2p", 0),
m_clientVersion(_clientVersion),
m_netPrefs(_n),
@ -60,8 +60,6 @@ Host::Host(std::string const& _clientVersion, NetworkPreferences const& _n, bool
clog(NetNote) << "IP Address: " << address << " = " << (isPrivateAddress(address) ? "[LOCAL]" : "[PEER]");
clog(NetNote) << "Id:" << id();
if (_start)
start();
}
Host::~Host()
@ -300,16 +298,34 @@ void Host::runAcceptor()
{
clog(NetConnect) << "Listening on local port " << m_listenPort << " (public: " << m_tcpPublic << ")";
m_accepting = true;
// socket is created outside of acceptor-callback
// An allocated socket is necessary as asio can use the socket
// until the callback succeeds or fails.
//
// Until callback succeeds or fails, we can't dealloc it.
//
// Callback is guaranteed to be called via asio or when
// m_tcp4Acceptor->stop() is called by Host.
//
// All exceptions are caught so they don't halt asio and so the
// socket is deleted.
//
// It's possible for an accepted connection to return an error in which
// case the socket may be open and must be closed to prevent asio from
// processing socket events after socket is deallocated.
bi::tcp::socket* s = new bi::tcp::socket(m_ioService);
bi::tcp::socket *s = new bi::tcp::socket(m_ioService);
m_tcp4Acceptor.async_accept(*s, [=](boost::system::error_code ec)
{
// if no error code, doHandshake takes ownership
bool success = false;
if (!ec)
{
try
{
// incoming connection so we don't yet know nodeid
// doHandshake takes ownersihp of *s via std::move
// incoming connection; we don't yet know nodeid
doHandshake(s, NodeId());
success = true;
}

2
libp2p/Host.h

@ -125,7 +125,7 @@ class Host: public Worker
public:
/// Start server, listening for connections on the given port.
Host(std::string const& _clientVersion, NetworkPreferences const& _n = NetworkPreferences(), bool _start = false);
Host(std::string const& _clientVersion, NetworkPreferences const& _n = NetworkPreferences());
/// Will block on network process events.
virtual ~Host();

Loading…
Cancel
Save