Skip to content

Commit 0b43b81

Browse files
committed
[net processing] Move send ping message logic into function
1 parent 1a07600 commit 0b43b81

File tree

1 file changed

+37
-28
lines changed

1 file changed

+37
-28
lines changed

src/net_processing.cpp

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,9 @@ class PeerManagerImpl final : public PeerManager
324324
/** Send a version message to a peer */
325325
void PushNodeVersion(CNode& pnode, int64_t nTime);
326326

327+
/** Send a ping message every PING_INTERVAL or if requested via RPC. */
328+
void MaybeSendPing(CNode& node_to);
329+
327330
const CChainParams& m_chainparams;
328331
CConnman& m_connman;
329332
/** Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
@@ -4292,6 +4295,39 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
42924295
}
42934296
}
42944297

4298+
void PeerManagerImpl::MaybeSendPing(CNode& node_to)
4299+
{
4300+
const CNetMsgMaker msgMaker(node_to.GetCommonVersion());
4301+
bool pingSend = false;
4302+
4303+
if (node_to.fPingQueued) {
4304+
// RPC ping request by user
4305+
pingSend = true;
4306+
}
4307+
4308+
if (node_to.nPingNonceSent == 0 && node_to.m_ping_start.load() + PING_INTERVAL < GetTime<std::chrono::microseconds>()) {
4309+
// Ping automatically sent as a latency probe & keepalive.
4310+
pingSend = true;
4311+
}
4312+
4313+
if (pingSend) {
4314+
uint64_t nonce = 0;
4315+
while (nonce == 0) {
4316+
GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
4317+
}
4318+
node_to.fPingQueued = false;
4319+
node_to.m_ping_start = GetTime<std::chrono::microseconds>();
4320+
if (node_to.GetCommonVersion() > BIP0031_VERSION) {
4321+
node_to.nPingNonceSent = nonce;
4322+
m_connman.PushMessage(&node_to, msgMaker.Make(NetMsgType::PING, nonce));
4323+
} else {
4324+
// Peer is too old to support ping command with nonce, pong will never arrive.
4325+
node_to.nPingNonceSent = 0;
4326+
m_connman.PushMessage(&node_to, msgMaker.Make(NetMsgType::PING));
4327+
}
4328+
}
4329+
}
4330+
42954331
namespace {
42964332
class CompareInvMempoolOrder
42974333
{
@@ -4330,34 +4366,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
43304366
// If we get here, the outgoing message serialization version is set and can't change.
43314367
const CNetMsgMaker msgMaker(pto->GetCommonVersion());
43324368

4333-
//
4334-
// Message: ping
4335-
//
4336-
bool pingSend = false;
4337-
if (pto->fPingQueued) {
4338-
// RPC ping request by user
4339-
pingSend = true;
4340-
}
4341-
if (pto->nPingNonceSent == 0 && pto->m_ping_start.load() + PING_INTERVAL < GetTime<std::chrono::microseconds>()) {
4342-
// Ping automatically sent as a latency probe & keepalive.
4343-
pingSend = true;
4344-
}
4345-
if (pingSend) {
4346-
uint64_t nonce = 0;
4347-
while (nonce == 0) {
4348-
GetRandBytes((unsigned char*)&nonce, sizeof(nonce));
4349-
}
4350-
pto->fPingQueued = false;
4351-
pto->m_ping_start = GetTime<std::chrono::microseconds>();
4352-
if (pto->GetCommonVersion() > BIP0031_VERSION) {
4353-
pto->nPingNonceSent = nonce;
4354-
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::PING, nonce));
4355-
} else {
4356-
// Peer is too old to support ping command with nonce, pong will never arrive.
4357-
pto->nPingNonceSent = 0;
4358-
m_connman.PushMessage(pto, msgMaker.Make(NetMsgType::PING));
4359-
}
4360-
}
4369+
MaybeSendPing(*pto);
43614370

43624371
{
43634372
LOCK(cs_main);

0 commit comments

Comments
 (0)