From 55986db3774419a410f0c4c9b9a832d50b484040 Mon Sep 17 00:00:00 2001 From: subtly Date: Sun, 29 Mar 2015 22:11:10 +0200 Subject: [PATCH] synchronous resolver --- libp2p/Network.cpp | 10 +++++++--- libp2p/Network.h | 2 +- libwebthree/WebThree.h | 9 +++------ 3 files changed, 11 insertions(+), 10 deletions(-) diff --git a/libp2p/Network.cpp b/libp2p/Network.cpp index 42208ad60..61028d458 100644 --- a/libp2p/Network.cpp +++ b/libp2p/Network.cpp @@ -208,8 +208,10 @@ bi::tcp::endpoint Network::traverseNAT(std::set const& _ifAddresses return upnpep; } -bi::tcp::endpoint Network::resolveHost(ba::io_service& _ioService, string const& _addr) +bi::tcp::endpoint Network::resolveHost(string const& _addr) { + static boost::asio::io_service s_resolverIoService; + vector split; boost::split(split, _addr, boost::is_any_of(":")); unsigned port = split.size() > 1 ? stoi(split[1]) : c_defaultIPPort; @@ -223,9 +225,11 @@ bi::tcp::endpoint Network::resolveHost(ba::io_service& _ioService, string const& { boost::system::error_code ec; // resolve returns an iterator (host can resolve to multiple addresses) - bi::tcp::resolver r(_ioService); + bi::tcp::resolver r(s_resolverIoService); auto it = r.resolve({split[0], toString(port)}, ec); - if (!ec) + if (ec) + clog(NetWarn) << "Error resolving host address " << _addr << ":" << ec.message(); + else ep = *it; } return ep; diff --git a/libp2p/Network.h b/libp2p/Network.h index 5f5ebc868..4259511b5 100644 --- a/libp2p/Network.h +++ b/libp2p/Network.h @@ -71,7 +71,7 @@ public: static bi::tcp::endpoint traverseNAT(std::set const& _ifAddresses, unsigned short _listenPort, bi::address& o_upnpifaddr); /// Resolve "host:port" string as TCP endpoint. Returns unspecified endpoint on failure. - static bi::tcp::endpoint resolveHost(ba::io_service& _ioService, std::string const& _host); + static bi::tcp::endpoint resolveHost(std::string const& _host); }; } diff --git a/libwebthree/WebThree.h b/libwebthree/WebThree.h index adb691753..a0e5cc666 100644 --- a/libwebthree/WebThree.h +++ b/libwebthree/WebThree.h @@ -145,7 +145,7 @@ public: virtual void addNode(p2p::NodeId const& _node, bi::tcp::endpoint const& _hostEndpoint) override; /// Add node to connect to. - void addNode(p2p::NodeId const& _node, std::string const& _hostString) { addNode(_node, resolveHost(_hostString)); } + void addNode(p2p::NodeId const& _node, std::string const& _hostString) { addNode(_node, p2p::Network::resolveHost(_hostString)); } /// Add node to connect to. void addNode(bi::tcp::endpoint const& _endpoint) { addNode(p2p::NodeId(), _endpoint); } @@ -157,11 +157,8 @@ public: void requirePeer(p2p::NodeId const& _node, bi::tcp::endpoint const& _endpoint) override; /// Require connection to peer. - void requirePeer(p2p::NodeId const& _node, std::string const& _hostString) { requirePeer(_node, resolveHost(_hostString)); } - - /// Resolve "host[:port]" string as TCP endpoint. Returns unspecified endpoint on failure. - bi::tcp::endpoint resolveHost(std::string const& _host) { return haveNetwork() ? p2p::Host::resolveHost(m_net, _host) : bi::tcp::endpoint(); } - + void requirePeer(p2p::NodeId const& _node, std::string const& _hostString) { requirePeer(_node, p2p::Network::resolveHost(_hostString)); } + /// Save peers dev::bytes saveNetwork() override;