@@ -324,6 +324,9 @@ class PeerManagerImpl final : public PeerManager
324
324
/* * Send a version message to a peer */
325
325
void PushNodeVersion (CNode& pnode, int64_t nTime);
326
326
327
+ /* * Send a ping message every PING_INTERVAL or if requested via RPC. */
328
+ void MaybeSendPing (CNode& node_to);
329
+
327
330
const CChainParams& m_chainparams;
328
331
CConnman& m_connman;
329
332
/* * Pointer to this node's banman. May be nullptr - check existence before dereferencing. */
@@ -4292,6 +4295,39 @@ void PeerManagerImpl::CheckForStaleTipAndEvictPeers()
4292
4295
}
4293
4296
}
4294
4297
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
+
4295
4331
namespace {
4296
4332
class CompareInvMempoolOrder
4297
4333
{
@@ -4330,34 +4366,7 @@ bool PeerManagerImpl::SendMessages(CNode* pto)
4330
4366
// If we get here, the outgoing message serialization version is set and can't change.
4331
4367
const CNetMsgMaker msgMaker (pto->GetCommonVersion ());
4332
4368
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);
4361
4370
4362
4371
{
4363
4372
LOCK (cs_main);
0 commit comments