dash/src/netfulfilledman.cpp
Wladimir J. van der Laan 4cd32e43e4
Merge #13235: Break circular dependency: init -> * -> init by extracting shutdown.h
1fabd59e7 Break circular dependency: init -> * -> init by extracting shutdown.h (Ben Woosley)
e62fdfeea Drop unused init.h includes (Ben Woosley)

Pull request description:

  Most includers just wanted to react to pending shutdown.

  This isolates access to `fRequestShutdown` and limits access to the shutdown api functions, including the new `CancelShutdown` for setting it to `false`.

Tree-SHA512: df42f75dfbba163576710e9a67cf1228531fd99d70a2f187bfba0bcc476d6749cf88180a97e66a81bb5b6c3c7f0917de7402d26039ba7b644cb7509b02f7e267
2021-07-02 00:42:18 +03:00

95 lines
2.9 KiB
C++

// Copyright (c) 2014-2020 The Dash Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <chainparams.h>
#include <netfulfilledman.h>
#include <shutdown.h>
#include <util/system.h>
CNetFulfilledRequestManager netfulfilledman;
void CNetFulfilledRequestManager::AddFulfilledRequest(const CService& addr, const std::string& strRequest)
{
LOCK(cs_mapFulfilledRequests);
CService addrSquashed = Params().AllowMultiplePorts() ? addr : CService(addr, 0);
mapFulfilledRequests[addrSquashed][strRequest] = GetTime() + Params().FulfilledRequestExpireTime();
}
bool CNetFulfilledRequestManager::HasFulfilledRequest(const CService& addr, const std::string& strRequest)
{
LOCK(cs_mapFulfilledRequests);
CService addrSquashed = Params().AllowMultiplePorts() ? addr : CService(addr, 0);
fulfilledreqmap_t::iterator it = mapFulfilledRequests.find(addrSquashed);
return it != mapFulfilledRequests.end() &&
it->second.find(strRequest) != it->second.end() &&
it->second[strRequest] > GetTime();
}
void CNetFulfilledRequestManager::RemoveFulfilledRequest(const CService& addr, const std::string& strRequest)
{
LOCK(cs_mapFulfilledRequests);
CService addrSquashed = Params().AllowMultiplePorts() ? addr : CService(addr, 0);
fulfilledreqmap_t::iterator it = mapFulfilledRequests.find(addrSquashed);
if (it != mapFulfilledRequests.end()) {
it->second.erase(strRequest);
}
}
void CNetFulfilledRequestManager::RemoveAllFulfilledRequests(const CService& addr)
{
LOCK(cs_mapFulfilledRequests);
CService addrSquashed = Params().AllowMultiplePorts() ? addr : CService(addr, 0);
fulfilledreqmap_t::iterator it = mapFulfilledRequests.find(addrSquashed);
if (it != mapFulfilledRequests.end()) {
mapFulfilledRequests.erase(it++);
}
}
void CNetFulfilledRequestManager::CheckAndRemove()
{
LOCK(cs_mapFulfilledRequests);
int64_t now = GetTime();
fulfilledreqmap_t::iterator it = mapFulfilledRequests.begin();
while(it != mapFulfilledRequests.end()) {
fulfilledreqmapentry_t::iterator it_entry = it->second.begin();
while(it_entry != it->second.end()) {
if(now > it_entry->second) {
it->second.erase(it_entry++);
} else {
++it_entry;
}
}
if(it->second.size() == 0) {
mapFulfilledRequests.erase(it++);
} else {
++it;
}
}
}
void CNetFulfilledRequestManager::Clear()
{
LOCK(cs_mapFulfilledRequests);
mapFulfilledRequests.clear();
}
std::string CNetFulfilledRequestManager::ToString() const
{
std::ostringstream info;
info << "Nodes with fulfilled requests: " << (int)mapFulfilledRequests.size();
return info.str();
}
void CNetFulfilledRequestManager::DoMaintenance()
{
if (ShutdownRequested()) return;
CheckAndRemove();
}