Browse Source

Asynchronous connect. Fixes #7.

cl-refactor
Gav Wood 11 years ago
parent
commit
9e7ce2c981
  1. 3
      libethereum/BlockInfo.cpp
  2. 48
      libethereum/PeerNetwork.cpp
  3. 4
      libethereum/PeerNetwork.h
  4. 9
      test/main.cpp

3
libethereum/BlockInfo.cpp

@ -57,7 +57,8 @@ h256 BlockInfo::headerHashWithoutNonce() const
void BlockInfo::fillStream(RLPStream& _s, bool _nonce) const
{
_s.appendList(_nonce ? 9 : 8) << parentHash << sha3Uncles << coinbaseAddress << stateRoot << sha3Transactions << difficulty << timestamp << extraData;
_s.appendList(_nonce ? 9 : 8) << parentHash << sha3Uncles << coinbaseAddress << stateRoot << sha3Transactions << difficulty << timestamp;
_s.appendString(extraData);
if (_nonce)
_s << nonce;
}

48
libethereum/PeerNetwork.cpp

@ -794,7 +794,7 @@ void PeerServer::ensureAccepting()
if (m_verbosity >= 1)
cout << "Listening on local port " << m_listenPort << " (public: " << m_public << ")" << endl;
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)
try
@ -820,52 +820,28 @@ void PeerServer::ensureAccepting()
}
}
bool PeerServer::connect(string const& _addr, uint _port)
void PeerServer::connect(bi::tcp::endpoint const& _ep)
{
bi::tcp::resolver resolver(m_ioService);
if (m_verbosity >= 1)
cout << "Attempting connection to " << _addr << ":" << dec << _port << endl;
try
cout << "Attempting connection to " << _ep << endl;
bi::tcp::socket* s = new bi::tcp::socket(m_ioService);
s->async_connect(_ep, [=](boost::system::error_code const& ec)
{
bi::tcp::socket s(m_ioService);
boost::asio::connect(s, resolver.resolve({ _addr, toString(_port) }));
auto p = make_shared<PeerSession>(this, std::move(s), m_requiredNetworkId);
m_peers.push_back(p);
if (m_verbosity >= 1)
cout << "Connected." << endl;
p->start();
return true;
}
catch (exception& _e)
if (ec)
{
if (m_verbosity >= 1)
cout << "Connection refused (" << _e.what() << ")" << endl;
return false;
cout << "Connection refused to " << _ep << " (" << ec.message() << ")" << endl;
}
}
bool PeerServer::connect(bi::tcp::endpoint _ep)
{
bi::tcp::resolver resolver(m_ioService);
if (m_verbosity >= 1)
cout << "Attempting connection to " << _ep << endl;
try
else
{
bi::tcp::socket s(m_ioService);
boost::asio::connect(s, resolver.resolve(_ep));
auto p = make_shared<PeerSession>(this, std::move(s), m_requiredNetworkId);
auto p = make_shared<PeerSession>(this, std::move(*s), m_requiredNetworkId);
m_peers.push_back(p);
if (m_verbosity >= 1)
cout << "Connected." << endl;
cout << "Connected to " << p->endpoint() << endl;
p->start();
return true;
}
catch (exception& _e)
{
if (m_verbosity >= 1)
cout << "Connection refused (" << _e.what() << ")" << endl;
return false;
}
delete s;
});
}
bool PeerServer::process(BlockChain& _bc)

4
libethereum/PeerNetwork.h

@ -131,8 +131,8 @@ public:
~PeerServer();
/// Connect to a peer explicitly.
bool connect(std::string const& _addr = "127.0.0.1", uint _port = 30303);
bool connect(bi::tcp::endpoint _ep);
void connect(std::string const& _addr, uint _port = 30303) { connect(bi::tcp::endpoint(bi::address::from_string(_addr), _port)); }
void connect(bi::tcp::endpoint const& _ep);
/// Sync with the BlockChain. It might contain one of our mined blocks, we might have new candidates from the network.
/// Conduct I/O, polling, syncing, whatever.

9
test/main.cpp

@ -30,8 +30,17 @@ int stateTest();
int hexPrefixTest();
int peerTest(int argc, char** argv);
#include <BlockInfo.h>
using namespace eth;
int main(int argc, char** argv)
{
/*RLPStream s;
BlockInfo::genesis().fillStream(s, false);
std::cout << RLP(s.out()) << std::endl;
std::cout << asHex(s.out()) << std::endl;
std::cout << sha3(s.out()) << std::endl;*/
// hexPrefixTest();
// rlpTest();
trieTest();

Loading…
Cancel
Save