aboutsummaryrefslogtreecommitdiff
path: root/src/net.cpp
diff options
context:
space:
mode:
authorCory Fields <cory-nospam-@coryfields.com>2016-04-16 19:13:12 -0400
committerCory Fields <cory-nospam-@coryfields.com>2016-09-08 12:24:06 -0400
commit53347f0cb99e514815e44a56439a4a10012238f8 (patch)
treefc5baefe46ef918143904a299fd8aa5083097eda /src/net.cpp
parentc0569c7fa1e25599b3f1d6a16b15ec23052021da (diff)
net: create generic functor accessors and move vNodes to CConnman
Diffstat (limited to 'src/net.cpp')
-rw-r--r--src/net.cpp83
1 files changed, 64 insertions, 19 deletions
diff --git a/src/net.cpp b/src/net.cpp
index e1cfc565d6..f20f63e04d 100644
--- a/src/net.cpp
+++ b/src/net.cpp
@@ -87,8 +87,6 @@ uint64_t nLocalHostNonce = 0;
int nMaxConnections = DEFAULT_MAX_PEER_CONNECTIONS;
std::string strSubVersion;
-std::vector<CNode*> vNodes;
-CCriticalSection cs_vNodes;
limitedmap<uint256, int64_t> mapAlreadyAskedFor(MAX_INV_SZ);
NodeId nLastNodeId = 0;
@@ -315,7 +313,7 @@ uint64_t CNode::nMaxOutboundTotalBytesSentInCycle = 0;
uint64_t CNode::nMaxOutboundTimeframe = 60*60*24; //1 day
uint64_t CNode::nMaxOutboundCycleStartTime = 0;
-CNode* FindNode(const CNetAddr& ip)
+CNode* CConnman::FindNode(const CNetAddr& ip)
{
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes)
@@ -324,7 +322,7 @@ CNode* FindNode(const CNetAddr& ip)
return NULL;
}
-CNode* FindNode(const CSubNet& subNet)
+CNode* CConnman::FindNode(const CSubNet& subNet)
{
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes)
@@ -333,7 +331,7 @@ CNode* FindNode(const CSubNet& subNet)
return NULL;
}
-CNode* FindNode(const std::string& addrName)
+CNode* CConnman::FindNode(const std::string& addrName)
{
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes)
@@ -342,7 +340,7 @@ CNode* FindNode(const std::string& addrName)
return NULL;
}
-CNode* FindNode(const CService& addr)
+CNode* CConnman::FindNode(const CService& addr)
{
LOCK(cs_vNodes);
BOOST_FOREACH(CNode* pnode, vNodes)
@@ -351,16 +349,6 @@ CNode* FindNode(const CService& addr)
return NULL;
}
-//TODO: This is used in only one place in main, and should be removed
-CNode* FindNode(const NodeId nodeid)
-{
- LOCK(cs_vNodes);
- BOOST_FOREACH(CNode* pnode, vNodes)
- if (pnode->GetId() == nodeid)
- return (pnode);
- return NULL;
-}
-
CNode* CConnman::ConnectNode(CAddress addrConnect, const char *pszDest, bool fCountFailure)
{
if (pszDest == NULL) {
@@ -899,7 +887,8 @@ static bool CompareNodeTXTime(const NodeEvictionCandidate &a, const NodeEviction
* to forge. In order to partition a node the attacker must be
* simultaneously better at all of them than honest peers.
*/
-static bool AttemptToEvictConnection() {
+bool CConnman::AttemptToEvictConnection()
+{
std::vector<NodeEvictionCandidate> vEvictionCandidates;
{
LOCK(cs_vNodes);
@@ -2320,7 +2309,6 @@ bool CConnman::DisconnectNode(const std::string& strNode)
}
return false;
}
-
bool CConnman::DisconnectNode(NodeId id)
{
LOCK(cs_vNodes);
@@ -2333,7 +2321,7 @@ bool CConnman::DisconnectNode(NodeId id)
return false;
}
-void RelayTransaction(const CTransaction& tx)
+void CConnman::RelayTransaction(const CTransaction& tx)
{
CInv inv(MSG_TX, tx.GetHash());
LOCK(cs_vNodes);
@@ -2671,6 +2659,63 @@ void CNode::EndMessage(const char* pszCommand) UNLOCK_FUNCTION(cs_vSend)
LEAVE_CRITICAL_SECTION(cs_vSend);
}
+bool CConnman::ForNode(NodeId id, std::function<bool(CNode* pnode)> func)
+{
+ CNode* found = nullptr;
+ LOCK(cs_vNodes);
+ for (auto&& pnode : vNodes) {
+ if(pnode->id == id) {
+ found = pnode;
+ break;
+ }
+ }
+ return found != nullptr && func(found);
+}
+
+bool CConnman::ForEachNode(std::function<bool(CNode* pnode)> func)
+{
+ LOCK(cs_vNodes);
+ for (auto&& node : vNodes)
+ if(!func(node))
+ return false;
+ return true;
+}
+
+bool CConnman::ForEachNode(std::function<bool(const CNode* pnode)> func) const
+{
+ LOCK(cs_vNodes);
+ for (const auto& node : vNodes)
+ if(!func(node))
+ return false;
+ return true;
+}
+
+bool CConnman::ForEachNodeThen(std::function<bool(CNode* pnode)> pre, std::function<void()> post)
+{
+ bool ret = true;
+ LOCK(cs_vNodes);
+ for (auto&& node : vNodes)
+ if(!pre(node)) {
+ ret = false;
+ break;
+ }
+ post();
+ return ret;
+}
+
+bool CConnman::ForEachNodeThen(std::function<bool(const CNode* pnode)> pre, std::function<void()> post) const
+{
+ bool ret = true;
+ LOCK(cs_vNodes);
+ for (const auto& node : vNodes)
+ if(!pre(node)) {
+ ret = false;
+ break;
+ }
+ post();
+ return ret;
+}
+
int64_t PoissonNextSend(int64_t nNow, int average_interval_seconds) {
return nNow + (int64_t)(log1p(GetRand(1ULL << 48) * -0.0000000000000035527136788 /* -1/2^48 */) * average_interval_seconds * -1000000.0 + 0.5);
}