diff options
author | John Newbery <john@johnnewbery.com> | 2020-06-21 17:23:58 -0400 |
---|---|---|
committer | John Newbery <john@johnnewbery.com> | 2021-02-15 16:02:43 +0000 |
commit | 0b43b81f69ff13dbc1e893a80950f186690b4f62 (patch) | |
tree | 72e51fb8087e770abe28d862fed5bda54566ab19 | |
parent | 1a07600b4b0d08cffc7cd5c58af33fcd1ede558e (diff) | |
download | bitcoin-0b43b81f69ff13dbc1e893a80950f186690b4f62.tar.xz |
[net processing] Move send ping message logic into function
-rw-r--r-- | src/net_processing.cpp | 65 |
1 files changed, 37 insertions, 28 deletions
diff --git a/src/net_processing.cpp b/src/net_processing.cpp index 1140eabe4c..94d9be6a9b 100644 --- a/src/net_processing.cpp +++ b/src/net_processing.cpp @@ -324,6 +324,9 @@ private: /** Send a version message to a peer */ void PushNodeVersion(CNode& pnode, int64_t nTime); + /** Send a ping message every PING_INTERVAL or if requested via RPC. */ + void MaybeSendPing(CNode& node_to); + const CChainParams& m_chainparams; CConnman& m_connman; /** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */ @@ -4292,6 +4295,39 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers() } } +void PeerManagerImpl::MaybeSendPing(CNode& node_to) +{ + const CNetMsgMaker msgMaker(node_to.GetCommonVersion()); + bool pingSend = false; + + if (node_to.fPingQueued) { + // RPC ping request by user + pingSend = true; + } + + if (node_to.nPingNonceSent == 0 && node_to.m_ping_start.load() + PING_INTERVAL < GetTime<std::chrono::microseconds>()) { + // Ping automatically sent as a latency probe & keepalive. + pingSend = true; + } + + if (pingSend) { + uint64_t nonce = 0; + while (nonce == 0) { + GetRandBytes((unsigned char*)&nonce, sizeof(nonce)); + } + node_to.fPingQueued = false; + node_to.m_ping_start = GetTime<std::chrono::microseconds>(); + if (node_to.GetCommonVersion() > BIP0031_VERSION) { + node_to.nPingNonceSent = nonce; + m_connman.PushMessage(&node_to, msgMaker.Make(NetMsgType::PING, nonce)); + } else { + // Peer is too old to support ping command with nonce, pong will never arrive. + node_to.nPingNonceSent = 0; + m_connman.PushMessage(&node_to, msgMaker.Make(NetMsgType::PING)); + } + } +} + namespace { class CompareInvMempoolOrder { @@ -4330,34 +4366,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto) // If we get here, the outgoing message serialization version is set and can't change. const CNetMsgMaker msgMaker(pto->GetCommonVersion()); - // - // Message: ping - // - bool pingSend = false; - if (pto->fPingQueued) { - // RPC ping request by user - pingSend = true; - } - if (pto->nPingNonceSent == 0 && pto->m_ping_start.load() + PING_INTERVAL < GetTime<std::chrono::microseconds>()) { - // Ping automatically sent as a latency probe & keepalive. - pingSend = true; - } - if (pingSend) { - uint64_t nonce = 0; - while (nonce == 0) { - GetRandBytes((unsigned char*)&nonce, sizeof(nonce)); - } - pto->fPingQueued = false; - pto->m_ping_start = GetTime<std::chrono::microseconds>(); - if (pto->GetCommonVersion() > BIP0031_VERSION) { - pto->nPingNonceSent = nonce; - m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::PING, nonce)); - } else { - // Peer is too old to support ping command with nonce, pong will never arrive. - pto->nPingNonceSent = 0; - m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::PING)); - } - } + MaybeSendPing(*pto); { LOCK(cs_main); |