Skip to content

Commit 05cae8a

Browse files
author
Marko Bencun
committed
range-based loops and const qualifications in net.cpp
Plus a use of std::copy() instead of manual copying.
1 parent 16e4184 commit 05cae8a

File tree

1 file changed

+37
-34
lines changed

1 file changed

+37
-34
lines changed

src/net.cpp

Lines changed: 37 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -135,11 +135,10 @@ static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn
135135
const int64_t nOneWeek = 7*24*60*60;
136136
std::vector<CAddress> vSeedsOut;
137137
vSeedsOut.reserve(vSeedsIn.size());
138-
for (std::vector<SeedSpec6>::const_iterator i(vSeedsIn.begin()); i != vSeedsIn.end(); ++i)
139-
{
138+
for (const auto& seed_in : vSeedsIn) {
140139
struct in6_addr ip;
141-
memcpy(&ip, i->addr, sizeof(ip));
142-
CAddress addr(CService(ip, i->port), NODE_NETWORK);
140+
memcpy(&ip, seed_in.addr, sizeof(ip));
141+
CAddress addr(CService(ip, seed_in.port), NODE_NETWORK);
143142
addr.nTime = GetTime() - GetRand(nOneWeek) - nOneWeek;
144143
vSeedsOut.push_back(addr);
145144
}
@@ -299,18 +298,22 @@ bool IsReachable(const CNetAddr& addr)
299298
CNode* CConnman::FindNode(const CNetAddr& ip)
300299
{
301300
LOCK(cs_vNodes);
302-
for (CNode* pnode : vNodes)
303-
if ((CNetAddr)pnode->addr == ip)
304-
return (pnode);
301+
for (CNode* pnode : vNodes) {
302+
if ((CNetAddr)pnode->addr == ip) {
303+
return pnode;
304+
}
305+
}
305306
return nullptr;
306307
}
307308

308309
CNode* CConnman::FindNode(const CSubNet& subNet)
309310
{
310311
LOCK(cs_vNodes);
311-
for (CNode* pnode : vNodes)
312-
if (subNet.Match((CNetAddr)pnode->addr))
313-
return (pnode);
312+
for (CNode* pnode : vNodes) {
313+
if (subNet.Match((CNetAddr)pnode->addr)) {
314+
return pnode;
315+
}
316+
}
314317
return nullptr;
315318
}
316319

@@ -319,7 +322,7 @@ CNode* CConnman::FindNode(const std::string& addrName)
319322
LOCK(cs_vNodes);
320323
for (CNode* pnode : vNodes) {
321324
if (pnode->GetAddrName() == addrName) {
322-
return (pnode);
325+
return pnode;
323326
}
324327
}
325328
return nullptr;
@@ -328,9 +331,11 @@ CNode* CConnman::FindNode(const std::string& addrName)
328331
CNode* CConnman::FindNode(const CService& addr)
329332
{
330333
LOCK(cs_vNodes);
331-
for (CNode* pnode : vNodes)
332-
if ((CService)pnode->addr == addr)
333-
return (pnode);
334+
for (CNode* pnode : vNodes) {
335+
if ((CService)pnode->addr == addr) {
336+
return pnode;
337+
}
338+
}
334339
return nullptr;
335340
}
336341

@@ -474,10 +479,9 @@ void CConnman::ClearBanned()
474479
bool CConnman::IsBanned(CNetAddr ip)
475480
{
476481
LOCK(cs_setBanned);
477-
for (banmap_t::iterator it = setBanned.begin(); it != setBanned.end(); it++)
478-
{
479-
CSubNet subNet = (*it).first;
480-
CBanEntry banEntry = (*it).second;
482+
for (const auto& it : setBanned) {
483+
CSubNet subNet = it.first;
484+
CBanEntry banEntry = it.second;
481485

482486
if (subNet.Match(ip) && GetTime() < banEntry.nBanUntil) {
483487
return true;
@@ -952,7 +956,7 @@ bool CConnman::AttemptToEvictConnection()
952956
{
953957
LOCK(cs_vNodes);
954958

955-
for (CNode *node : vNodes) {
959+
for (const CNode* node : vNodes) {
956960
if (node->fWhitelisted)
957961
continue;
958962
if (!node->fInbound)
@@ -1030,9 +1034,9 @@ bool CConnman::AttemptToEvictConnection()
10301034
// Disconnect from the network group with the most connections
10311035
NodeId evicted = vEvictionCandidates.front().id;
10321036
LOCK(cs_vNodes);
1033-
for(std::vector<CNode*>::const_iterator it(vNodes.begin()); it != vNodes.end(); ++it) {
1034-
if ((*it)->GetId() == evicted) {
1035-
(*it)->fDisconnect = true;
1037+
for (CNode* pnode : vNodes) {
1038+
if (pnode->GetId() == evicted) {
1039+
pnode->fDisconnect = true;
10361040
return true;
10371041
}
10381042
}
@@ -1056,9 +1060,9 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
10561060
bool whitelisted = hListenSocket.whitelisted || IsWhitelistedRange(addr);
10571061
{
10581062
LOCK(cs_vNodes);
1059-
for (CNode* pnode : vNodes)
1060-
if (pnode->fInbound)
1061-
nInbound++;
1063+
for (const CNode* pnode : vNodes) {
1064+
if (pnode->fInbound) nInbound++;
1065+
}
10621066
}
10631067

10641068
if (hSocket == INVALID_SOCKET)
@@ -1850,8 +1854,7 @@ std::vector<AddedNodeInfo> CConnman::GetAddedNodeInfo()
18501854
{
18511855
LOCK(cs_vAddedNodes);
18521856
ret.reserve(vAddedNodes.size());
1853-
for (const std::string& strAddNode : vAddedNodes)
1854-
lAddresses.push_back(strAddNode);
1857+
std::copy(vAddedNodes.cbegin(), vAddedNodes.cend(), std::back_inserter(lAddresses));
18551858
}
18561859

18571860

@@ -2488,9 +2491,8 @@ std::vector<CAddress> CConnman::GetAddresses()
24882491
bool CConnman::AddNode(const std::string& strNode)
24892492
{
24902493
LOCK(cs_vAddedNodes);
2491-
for(std::vector<std::string>::const_iterator it = vAddedNodes.begin(); it != vAddedNodes.end(); ++it) {
2492-
if (strNode == *it)
2493-
return false;
2494+
for (const std::string& it : vAddedNodes) {
2495+
if (strNode == it) return false;
24942496
}
24952497

24962498
vAddedNodes.push_back(strNode);
@@ -2516,9 +2518,11 @@ size_t CConnman::GetNodeCount(NumConnections flags)
25162518
return vNodes.size();
25172519

25182520
int nNum = 0;
2519-
for(std::vector<CNode*>::const_iterator it = vNodes.begin(); it != vNodes.end(); ++it)
2520-
if (flags & ((*it)->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT))
2521+
for (const auto& pnode : vNodes) {
2522+
if (flags & (pnode->fInbound ? CONNECTIONS_IN : CONNECTIONS_OUT)) {
25212523
nNum++;
2524+
}
2525+
}
25222526

25232527
return nNum;
25242528
}
@@ -2528,8 +2532,7 @@ void CConnman::GetNodeStats(std::vector<CNodeStats>& vstats)
25282532
vstats.clear();
25292533
LOCK(cs_vNodes);
25302534
vstats.reserve(vNodes.size());
2531-
for(std::vector<CNode*>::iterator it = vNodes.begin(); it != vNodes.end(); ++it) {
2532-
CNode* pnode = *it;
2535+
for (CNode* pnode : vNodes) {
25332536
vstats.emplace_back();
25342537
pnode->copyStats(vstats.back());
25352538
}

0 commit comments

Comments
 (0)