Skip to content

Commit 1317cd1

Browse files
committed
RAII wrapper for CNode*
1 parent df23937 commit 1317cd1

File tree

1 file changed

+18
-7
lines changed

1 file changed

+18
-7
lines changed

src/net.cpp

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -775,12 +775,23 @@ void SocketSendData(CNode *pnode)
775775

776776
static list<CNode*> vNodesDisconnected;
777777

778-
static bool ReverseCompareNodeMinPingTime(CNode *a, CNode *b)
778+
class CNodeRef {
779+
public:
780+
CNodeRef(CNode *pnode) : _pnode(pnode) {_pnode->AddRef();}
781+
~CNodeRef() {_pnode->Release();}
782+
783+
CNode& operator *() const {return *_pnode;};
784+
CNode* operator ->() const {return _pnode;};
785+
private:
786+
CNode *_pnode;
787+
};
788+
789+
static bool ReverseCompareNodeMinPingTime(const CNodeRef &a, const CNodeRef &b)
779790
{
780791
return a->nMinPingUsecTime > b->nMinPingUsecTime;
781792
}
782793

783-
static bool ReverseCompareNodeTimeConnected(CNode *a, CNode *b)
794+
static bool ReverseCompareNodeTimeConnected(const CNodeRef &a, const CNodeRef &b)
784795
{
785796
return a->nTimeConnected > b->nTimeConnected;
786797
}
@@ -795,7 +806,7 @@ class CompareNetGroupKeyed
795806
GetRandBytes(vchSecretKey.data(), vchSecretKey.size());
796807
}
797808

798-
bool operator()(CNode *a, CNode *b)
809+
bool operator()(const CNodeRef &a, const CNodeRef &b)
799810
{
800811
std::vector<unsigned char> vchGroupA, vchGroupB;
801812
CSHA256 hashA, hashB;
@@ -818,7 +829,7 @@ class CompareNetGroupKeyed
818829
};
819830

820831
static bool AttemptToEvictConnection(bool fPreferNewConnection) {
821-
std::vector<CNode*> vEvictionCandidates;
832+
std::vector<CNodeRef> vEvictionCandidates;
822833
{
823834
LOCK(cs_vNodes);
824835

@@ -831,7 +842,7 @@ static bool AttemptToEvictConnection(bool fPreferNewConnection) {
831842
continue;
832843
if (node->addr.IsLocal())
833844
continue;
834-
vEvictionCandidates.push_back(node);
845+
vEvictionCandidates.push_back(CNodeRef(node));
835846
}
836847
}
837848

@@ -859,8 +870,8 @@ static bool AttemptToEvictConnection(bool fPreferNewConnection) {
859870
// Identify CNetAddr with the most connections
860871
CNetAddr naMostConnections;
861872
unsigned int nMostConnections = 0;
862-
std::map<CNetAddr, std::vector<CNode*> > mapAddrCounts;
863-
BOOST_FOREACH(CNode *node, vEvictionCandidates) {
873+
std::map<CNetAddr, std::vector<CNodeRef> > mapAddrCounts;
874+
BOOST_FOREACH(const CNodeRef &node, vEvictionCandidates) {
864875
mapAddrCounts[node->addr].push_back(node);
865876

866877
if (mapAddrCounts[node->addr].size() > nMostConnections) {

0 commit comments

Comments
 (0)