Browse Source

range-based loops and const qualifications in net.cpp

Plus a use of std::copy() instead of manual copying.
0.16
Marko Bencun 8 years ago
parent
commit
05cae8aefd
  1. 71
      src/net.cpp

71
src/net.cpp

@ -135,11 +135,10 @@ static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn
const int64_t nOneWeek = 7*24*60*60; const int64_t nOneWeek = 7*24*60*60;
std::vector<CAddress> vSeedsOut; std::vector<CAddress> vSeedsOut;
vSeedsOut.reserve(vSeedsIn.size()); vSeedsOut.reserve(vSeedsIn.size());
for (std::vector<SeedSpec6>::const_iterator i(vSeedsIn.begin()); i != vSeedsIn.end(); ++i) for (const auto& seed_in : vSeedsIn) {
{
struct in6_addr ip; struct in6_addr ip;
memcpy(&ip, i->addr, sizeof(ip)); memcpy(&ip, seed_in.addr, sizeof(ip));
CAddress addr(CService(ip, i->port), NODE_NETWORK); CAddress addr(CService(ip, seed_in.port), NODE_NETWORK);
addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek; addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek;
vSeedsOut.push_back(addr); vSeedsOut.push_back(addr);
} }
@ -299,18 +298,22 @@ bool IsReachable(const CNetAddr& addr)
CNode* CConnman::FindNode(const CNetAddr& ip) CNode* CConnman::FindNode(const CNetAddr& ip)
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (CNode* pnode : vNodes) for (CNode* pnode : vNodes) {
if ((CNetAddr)pnode->addr == ip) if ((CNetAddr)pnode->addr == ip) {
return (pnode); return pnode;
}
}
return nullptr; return nullptr;
} }
CNode* CConnman::FindNode(const CSubNet& subNet) CNode* CConnman::FindNode(const CSubNet& subNet)
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (CNode* pnode : vNodes) for (CNode* pnode : vNodes) {
if (subNet.Match((CNetAddr)pnode->addr)) if (subNet.Match((CNetAddr)pnode->addr)) {
return (pnode); return pnode;
}
}
return nullptr; return nullptr;
} }
@ -319,7 +322,7 @@ CNode* CConnman::FindNode(const std::string& addrName)
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (CNode* pnode : vNodes) { for (CNode* pnode : vNodes) {
if (pnode->GetAddrName() == addrName) { if (pnode->GetAddrName() == addrName) {
return (pnode); return pnode;
} }
} }
return nullptr; return nullptr;
@ -328,9 +331,11 @@ CNode* CConnman::FindNode(const std::string& addrName)
CNode* CConnman::FindNode(const CService& addr) CNode* CConnman::FindNode(const CService& addr)
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (CNode* pnode : vNodes) for (CNode* pnode : vNodes) {
if ((CService)pnode->addr == addr) if ((CService)pnode->addr == addr) {
return (pnode); return pnode;
}
}
return nullptr; return nullptr;
} }
@ -474,10 +479,9 @@ void CConnman::ClearBanned()
bool CConnman::IsBanned(CNetAddr ip) bool CConnman::IsBanned(CNetAddr ip)
{ {
LOCK(cs_setBanned); LOCK(cs_setBanned);
for (banmap_t::iterator it = setBanned.begin(); it != setBanned.end(); it++) for (const auto& it : setBanned) {
{ CSubNet subNet = it.first;
CSubNet subNet = (*it).first; CBanEntry banEntry = it.second;
CBanEntry banEntry = (*it).second;
if (subNet.Match(ip) && GetTime() < banEntry.nBanUntil) { if (subNet.Match(ip) && GetTime() < banEntry.nBanUntil) {
return true; return true;
@ -952,7 +956,7 @@ bool CConnman::AttemptToEvictConnection()
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (CNode *node : vNodes) { for (const CNode* node : vNodes) {
if (node->fWhitelisted) if (node->fWhitelisted)
continue; continue;
if (!node->fInbound) if (!node->fInbound)
@ -1030,9 +1034,9 @@ bool CConnman::AttemptToEvictConnection()
// Disconnect from the network group with the most connections // Disconnect from the network group with the most connections
NodeId evicted = vEvictionCandidates.front().id; NodeId evicted = vEvictionCandidates.front().id;
LOCK(cs_vNodes); LOCK(cs_vNodes);
for(std::vector<CNode*>::const_iterator it(vNodes.begin()); it != vNodes.end(); ++it) { for (CNode* pnode : vNodes) {
if ((*it)->GetId() == evicted) { if (pnode->GetId() == evicted) {
(*it)->fDisconnect = true; pnode->fDisconnect = true;
return true; return true;
} }
} }
@ -1056,9 +1060,9 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
bool whitelisted = hListenSocket.whitelisted || IsWhitelistedRange(addr); bool whitelisted = hListenSocket.whitelisted || IsWhitelistedRange(addr);
{ {
LOCK(cs_vNodes); LOCK(cs_vNodes);
for (CNode* pnode : vNodes) for (const CNode* pnode : vNodes) {
if (pnode->fInbound) if (pnode->fInbound) nInbound++;
nInbound++; }
} }
if (hSocket == INVALID_SOCKET) if (hSocket == INVALID_SOCKET)
@ -1850,8 +1854,7 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
{ {
LOCK(cs_vAddedNodes); LOCK(cs_vAddedNodes);
ret.reserve(vAddedNodes.size()); ret.reserve(vAddedNodes.size());
for (const std::string& strAddNode : vAddedNodes) std::copy(vAddedNodes.cbegin(), vAddedNodes.cend(), std::back_inserter(lAddresses));
lAddresses.push_back(strAddNode);
} }
@ -2488,9 +2491,8 @@ std::vector<CAddress> CConnman::GetAddresses()
bool CConnman::AddNode(const std::string& strNode) bool CConnman::AddNode(const std::string& strNode)
{ {
LOCK(cs_vAddedNodes); LOCK(cs_vAddedNodes);
for(std::vector<std::string>::const_iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) { for (const std::string& it : vAddedNodes) {
if (strNode == *it) if (strNode == it) return false;
return false;
} }
vAddedNodes.push_back(strNode); vAddedNodes.push_back(strNode);
@ -2516,9 +2518,11 @@ size_t CConnman::GetNodeCount(NumConnections flags)
return vNodes.size(); return vNodes.size();
int nNum = 0; int nNum = 0;
for(std::vector<CNode*>::const_iterator it = vNodes.begin(); it != vNodes.end(); ++it) for (const auto& pnode : vNodes) {
if (flags & ((*it)->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT)) if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT)) {
nNum++; nNum++;
}
}
return nNum; return nNum;
} }
@ -2528,8 +2532,7 @@ void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
vstats.clear(); vstats.clear();
LOCK(cs_vNodes); LOCK(cs_vNodes);
vstats.reserve(vNodes.size()); vstats.reserve(vNodes.size());
for(std::vector<CNode*>::iterator it = vNodes.begin(); it != vNodes.end(); ++it) { for (CNode* pnode : vNodes) {
CNode* pnode = *it;
vstats.emplace_back(); vstats.emplace_back();
pnode->copyStats(vstats.back()); pnode->copyStats(vstats.back());
} }

Loading…
Cancel
Save