Browse Source

Avoid socket info timing issues by keeping a copy of the info handy for

cross-thread replies.
cl-refactor
Gav Wood 11 years ago
parent
commit
de44b7522c
  1. 2
      alephzero/Main.cpp
  2. 13
      libethereum/PeerNetwork.cpp
  3. 21
      libethereum/PeerNetwork.h

2
alephzero/Main.cpp

@ -67,7 +67,7 @@ void Main::refresh()
ui->address->setText(QString::fromStdString(asHex(m_client.state().address().asArray())));
ui->peers->clear();
for (PeerInfo const& i: m_client.peers())
ui->peers->addItem(QString("%3 ms - %1:%2 - %4").arg(i.endpoint.address().to_string().c_str()).arg(i.endpoint.port()).arg(chrono::duration_cast<chrono::milliseconds>(i.lastPing).count()).arg(i.clientVersion.c_str()));
ui->peers->addItem(QString("%3 ms - %1:%2 - %4").arg(i.host.c_str()).arg(i.port).arg(chrono::duration_cast<chrono::milliseconds>(i.lastPing).count()).arg(i.clientVersion.c_str()));
auto d = m_client.blockChain().details();
auto diff = BlockInfo(m_client.blockChain().block()).difficulty;

13
libethereum/PeerNetwork.cpp

@ -31,6 +31,7 @@ PeerSession::PeerSession(PeerServer* _s, bi::tcp::socket _socket, uint _rNId):
m_socket(std::move(_socket)),
m_reqNetworkId(_rNId)
{
}
PeerSession::~PeerSession()
@ -43,16 +44,19 @@ bool PeerSession::interpret(RLP const& _r)
switch (_r[0].toInt<unsigned>())
{
case Hello:
{
cout << std::setw(2) << m_socket.native_handle() << " | Hello" << endl;
m_protocolVersion = _r[1].toInt<uint>();
m_networkId = _r[2].toInt<uint>();
m_clientVersion = _r[3].toString();
auto clientVersion = _r[3].toString();
if (m_protocolVersion != 0 || m_networkId != m_reqNetworkId)
{
disconnect();
return false;
}
cout << std::setw(2) << m_socket.native_handle() << " | Client version: " << m_clientVersion << endl;
m_info = PeerInfo({clientVersion, m_socket.remote_endpoint().address().to_string(), (short)m_socket.remote_endpoint().port(), std::chrono::steady_clock::duration()});
cout << std::setw(2) << m_socket.native_handle() << " | Client version: " << clientVersion << endl;
// Grab their block chain off them.
{
@ -62,6 +66,7 @@ bool PeerSession::interpret(RLP const& _r)
sealAndSend(s);
}
break;
}
case Disconnect:
m_socket.close();
return false;
@ -73,7 +78,7 @@ bool PeerSession::interpret(RLP const& _r)
break;
}
case Pong:
m_lastPing = std::chrono::steady_clock::now() - m_ping;
m_info.lastPing = std::chrono::steady_clock::now() - m_ping;
// cout << "Latency: " << chrono::duration_cast<chrono::milliseconds>(m_lastPing).count() << " ms" << endl;
break;
case GetPeers:
@ -513,7 +518,7 @@ std::vector<PeerInfo> PeerServer::peers() const
for (auto& i: m_peers)
if (auto j = i.lock())
if (j->m_socket.is_open())
ret.push_back(PeerInfo{j->m_clientVersion, j->m_socket.remote_endpoint(), j->m_lastPing});
ret.push_back(j->m_info);
return ret;
}

21
libethereum/PeerNetwork.h

@ -53,6 +53,15 @@ enum PacketType
class PeerServer;
// TODO: include in PeerSession for ease of copying out later.
struct PeerInfo
{
std::string clientVersion;
std::string host;
short port;
std::chrono::steady_clock::duration lastPing;
};
class PeerSession: public std::enable_shared_from_this<PeerSession>
{
friend class PeerServer;
@ -81,27 +90,19 @@ private:
PeerServer* m_server;
bi::tcp::socket m_socket;
std::array<byte, 65536> m_data;
PeerInfo m_info;
bytes m_incoming;
std::string m_clientVersion;
uint m_protocolVersion;
uint m_networkId;
uint m_reqNetworkId;
std::chrono::steady_clock::time_point m_ping;
std::chrono::steady_clock::duration m_lastPing;
std::set<h256> m_knownBlocks;
std::set<h256> m_knownTransactions;
};
struct PeerInfo
{
std::string clientVersion;
bi::tcp::endpoint endpoint;
std::chrono::steady_clock::duration lastPing;
};
class PeerServer
{
friend class PeerSession;
@ -155,7 +156,7 @@ private:
h256 m_latestBlockSent;
std::set<h256> m_transactionsSent;
unsigned m_idealPeerCount;
unsigned m_idealPeerCount = 5;
};

Loading…
Cancel
Save