diff options
author | Patrick Strateman <patrick.strateman@gmail.com> | 2015-08-20 17:29:04 -0700 |
---|---|---|
committer | Patrick Strateman <patrick.strateman@gmail.com> | 2015-08-22 15:38:24 -0700 |
commit | 1317cd1928afbae14fedb39c8d23589a32fe2951 (patch) | |
tree | d118302220f1907d931c0ede0bff57e50769cd57 /src | |
parent | df239374224e6585d5b6ba37a39282d0fc647173 (diff) |
RAII wrapper for CNode*
Diffstat (limited to 'src')
-rw-r--r-- | src/net.cpp | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/src/net.cpp b/src/net.cpp index d8d2783c4b..709c652430 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -775,12 +775,23 @@ void SocketSendData(CNode *pnode) static list<CNode*> vNodesDisconnected; -static bool ReverseCompareNodeMinPingTime(CNode *a, CNode *b) +class CNodeRef { +public: + CNodeRef(CNode *pnode) : _pnode(pnode) {_pnode->AddRef();} + ~CNodeRef() {_pnode->Release();} + + CNode& operator *() const {return *_pnode;}; + CNode* operator ->() const {return _pnode;}; +private: + CNode *_pnode; +}; + +static bool ReverseCompareNodeMinPingTime(const CNodeRef &a, const CNodeRef &b) { return a->nMinPingUsecTime > b->nMinPingUsecTime; } -static bool ReverseCompareNodeTimeConnected(CNode *a, CNode *b) +static bool ReverseCompareNodeTimeConnected(const CNodeRef &a, const CNodeRef &b) { return a->nTimeConnected > b->nTimeConnected; } @@ -795,7 +806,7 @@ public: GetRandBytes(vchSecretKey.data(), vchSecretKey.size()); } - bool operator()(CNode *a, CNode *b) + bool operator()(const CNodeRef &a, const CNodeRef &b) { std::vector<unsigned char> vchGroupA, vchGroupB; CSHA256 hashA, hashB; @@ -818,7 +829,7 @@ public: }; static bool AttemptToEvictConnection(bool fPreferNewConnection) { - std::vector<CNode*> vEvictionCandidates; + std::vector<CNodeRef> vEvictionCandidates; { LOCK(cs_vNodes); @@ -831,7 +842,7 @@ static bool AttemptToEvictConnection(bool fPreferNewConnection) { continue; if (node->addr.IsLocal()) continue; - vEvictionCandidates.push_back(node); + vEvictionCandidates.push_back(CNodeRef(node)); } } @@ -859,8 +870,8 @@ static bool AttemptToEvictConnection(bool fPreferNewConnection) { // Identify CNetAddr with the most connections CNetAddr naMostConnections; unsigned int nMostConnections = 0; - std::map<CNetAddr, std::vector<CNode*> > mapAddrCounts; - BOOST_FOREACH(CNode *node, vEvictionCandidates) { + std::map<CNetAddr, std::vector<CNodeRef> > mapAddrCounts; + BOOST_FOREACH(const CNodeRef &node, vEvictionCandidates) { mapAddrCounts[node->addr].push_back(node); if (mapAddrCounts[node->addr].size() > nMostConnections) { |