diff --git a/src/net.h b/src/net.h --- a/src/net.h +++ b/src/net.h @@ -345,6 +345,7 @@ void AcceptConnection(const ListenSocket &hListenSocket); void DisconnectNodes(); void NotifyNumConnectionsChanged(); + void InactivityCheck(CNode *pnode); void ThreadSocketHandler(); void ThreadDNSAddressSeed(); diff --git a/src/net.cpp b/src/net.cpp --- a/src/net.cpp +++ b/src/net.cpp @@ -1305,6 +1305,39 @@ } } +void CConnman::InactivityCheck(CNode *pnode) { + int64_t nTime = GetSystemTimeInSeconds(); + if (nTime - pnode->nTimeConnected > 60) { + if (pnode->nLastRecv == 0 || pnode->nLastSend == 0) { + LogPrint(BCLog::NET, + "socket no message in first 60 seconds, %d %d from %d\n", + pnode->nLastRecv != 0, pnode->nLastSend != 0, + pnode->GetId()); + pnode->fDisconnect = true; + } else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL) { + LogPrintf("socket sending timeout: %is\n", + nTime - pnode->nLastSend); + pnode->fDisconnect = true; + } else if (nTime - pnode->nLastRecv > (pnode->nVersion > BIP0031_VERSION + ? TIMEOUT_INTERVAL + : 90 * 60)) { + LogPrintf("socket receive timeout: %is\n", + nTime - pnode->nLastRecv); + pnode->fDisconnect = true; + } else if (pnode->nPingNonceSent && + pnode->nPingUsecStart + TIMEOUT_INTERVAL * 1000000 < + GetTimeMicros()) { + LogPrintf("ping timeout: %fs\n", + 0.000001 * (GetTimeMicros() - pnode->nPingUsecStart)); + pnode->fDisconnect = true; + } else if (!pnode->fSuccessfullyConnected) { + LogPrint(BCLog::NET, "version handshake timeout from %d\n", + pnode->GetId()); + pnode->fDisconnect = true; + } + } +} + void CConnman::ThreadSocketHandler() { while (!interruptNet) { DisconnectNodes(); @@ -1508,41 +1541,7 @@ } } - // - // Inactivity checking - // - int64_t nTime = GetSystemTimeInSeconds(); - if (nTime - pnode->nTimeConnected > 60) { - if (pnode->nLastRecv == 0 || pnode->nLastSend == 0) { - LogPrint(BCLog::NET, - "socket no message in first 60 seconds, %d %d " - "from %d\n", - pnode->nLastRecv != 0, pnode->nLastSend != 0, - pnode->GetId()); - pnode->fDisconnect = true; - } else if (nTime - pnode->nLastSend > TIMEOUT_INTERVAL) { - LogPrintf("socket sending timeout: %is\n", - nTime - pnode->nLastSend); - pnode->fDisconnect = true; - } else if (nTime - pnode->nLastRecv > - (pnode->nVersion > BIP0031_VERSION ? TIMEOUT_INTERVAL - : 90 * 60)) { - LogPrintf("socket receive timeout: %is\n", - nTime - pnode->nLastRecv); - pnode->fDisconnect = true; - } else if (pnode->nPingNonceSent && - pnode->nPingUsecStart + TIMEOUT_INTERVAL * 1000000 < - GetTimeMicros()) { - LogPrintf("ping timeout: %fs\n", - 0.000001 * - (GetTimeMicros() - pnode->nPingUsecStart)); - pnode->fDisconnect = true; - } else if (!pnode->fSuccessfullyConnected) { - LogPrint(BCLog::NET, "version handshake timeout from %d\n", - pnode->GetId()); - pnode->fDisconnect = true; - } - } + InactivityCheck(pnode); } { LOCK(cs_vNodes);