X-Git-Url: https://git.novaco.in/?p=novacoin.git;a=blobdiff_plain;f=src%2Fnet.cpp;h=b5249ba51af7012bc25d37e890837f9534563be7;hp=756e5cb5f3a9b1b3e89c0ac22abd08937a7faf8e;hb=5d022bcaea6cf22c90530911a801bc49120fbda2;hpb=3bdc0abd5d7e262151c62426cb3006433fb21ffc diff --git a/src/net.cpp b/src/net.cpp index 756e5cb..b5249ba 100644 --- a/src/net.cpp +++ b/src/net.cpp @@ -142,8 +142,8 @@ CAddress GetLocalAddress(const CNetAddr *paddrPeer) bool RecvLine(SOCKET hSocket, string& strLine) { - strLine = ""; - while (true) + strLine.clear(); + for ( ; ; ) { char c; int nBytes = recv(hSocket, &c, 1, 0); @@ -218,6 +218,44 @@ void SetReachable(enum Network net, bool fFlag) vfReachable[NET_IPV4] = true; } +int GetnScore(const CService& addr) +{ + LOCK(cs_mapLocalHost); + if (mapLocalHost.count(addr) == LOCAL_NONE) + return 0; + return mapLocalHost[addr].nScore; +} + + +// Is our peer's addrLocal potentially useful as an external IP source? +bool IsPeerAddrLocalGood(CNode *pnode) +{ + return fDiscover && pnode->addr.IsRoutable() && pnode->addrLocal.IsRoutable() && + !IsLimited(pnode->addrLocal.GetNetwork()); +} + +// pushes our own address to a peer +void AdvertiseLocal(CNode *pnode) +{ + if (!fNoListen && pnode->fSuccessfullyConnected) + { + CAddress addrLocal = GetLocalAddress(&pnode->addr); + // If discovery is enabled, sometimes give our peer the address it + // tells us that it sees us as in case it has a better idea of our + // address than we do. + if (IsPeerAddrLocalGood(pnode) && (!addrLocal.IsRoutable() || + GetRand((GetnScore(addrLocal) > LOCAL_MANUAL) ? 8:2) == 0)) + { + addrLocal.SetIP(pnode->addrLocal); + } + if (addrLocal.IsRoutable()) + { + printf("AdvertiseLocal: advertising address %s\n", addrLocal.ToString().c_str()); + pnode->PushAddress(addrLocal); + } + } +} + // learn a new local address bool AddLocal(const CService& addr, int nScore) { @@ -570,10 +608,9 @@ void CNode::copyStats(CNodeStats &stats) } #undef X - - - - +void Release(CNode* node) { + node->Release(); +} @@ -606,7 +643,7 @@ void ThreadSocketHandler2(void* parg) { printf("ThreadSocketHandler started\n"); size_t nPrevNodeCount = 0; - while (true) + for ( ; ; ) { // // Disconnect nodes @@ -925,8 +962,7 @@ void ThreadSocketHandler2(void* parg) } { LOCK(cs_vNodes); - BOOST_FOREACH(CNode* pnode, vNodesCopy) - pnode->Release(); + for_each(vNodesCopy.begin(), vNodesCopy.end(), Release); } Sleep(10); @@ -938,7 +974,7 @@ void ThreadSocketHandler2(void* parg) // The first name is used as information source for addrman. // The second name should resolve to a list of seed addresses. static const char *strDNSSeed[][2] = { - {"novacoin.karelia.pro", "dnsseed.novacoin.karelia.pro"}, + {"node.novacoin.karelia.pro", "dnsseed.novacoin.karelia.pro"}, {"novacoin.ru", "dnsseed.novacoin.ru"}, {"novacoin.ru", "testseed.novacoin.ru"}, {"novaco.in", "dnsseed.novaco.in"}, @@ -1154,7 +1190,7 @@ void ThreadOpenConnections2(void* parg) // Initiate network connections int64_t nStart = GetTime(); - while (true) + for ( ; ; ) { ProcessOneShot(); @@ -1225,7 +1261,7 @@ void ThreadOpenConnections2(void* parg) int64_t nANow = GetAdjustedTime(); int nTries = 0; - while (true) + for ( ; ; ) { // use an nUnkBias between 10 (no outgoing connections) and 90 (8 outgoing connections) CAddress addr = addrman.Select(10 + min(nOutbound,8)*10); @@ -1481,9 +1517,6 @@ void ThreadMessageHandler2(void* parg) StartSync(vNodesCopy); // Poll the connected nodes for messages - CNode* pnodeTrickle = NULL; - if (!vNodesCopy.empty()) - pnodeTrickle = vNodesCopy[GetRand(vNodesCopy.size())]; BOOST_FOREACH(CNode* pnode, vNodesCopy) { // Receive messages @@ -1499,7 +1532,7 @@ void ThreadMessageHandler2(void* parg) { TRY_LOCK(pnode->cs_vSend, lockSend); if (lockSend) - SendMessages(pnode, pnode == pnodeTrickle); + SendMessages(pnode); } if (fShutdown) return; @@ -1507,8 +1540,7 @@ void ThreadMessageHandler2(void* parg) { LOCK(cs_vNodes); - BOOST_FOREACH(CNode* pnode, vNodesCopy) - pnode->Release(); + for_each(vNodesCopy.begin(), vNodesCopy.end(), Release); } // Wait and allow messages to bunch up. @@ -1531,7 +1563,7 @@ void ThreadMessageHandler2(void* parg) bool BindListenPort(const CService &addrBind, string& strError) { - strError = ""; + strError.clear(); int nOne = 1; // Create socket for listening for incoming connections @@ -1760,7 +1792,7 @@ bool StopNode() if (semOutbound) for (int i=0; ipost(); - do + for ( ; ; ) { int nThreadsRunning = 0; for (int n = 0; n < THREAD_MAX; n++) @@ -1770,7 +1802,7 @@ bool StopNode() if (GetTime() - nStart > 20) break; Sleep(20); - } while(true); + }; if (vnThreadsRunning[THREAD_SOCKETHANDLER] > 0) printf("ThreadSocketHandler still running\n"); if (vnThreadsRunning[THREAD_OPENCONNECTIONS] > 0) printf("ThreadOpenConnections still running\n"); if (vnThreadsRunning[THREAD_MESSAGEHANDLER] > 0) printf("ThreadMessageHandler still running\n"); @@ -1877,3 +1909,6 @@ uint64_t CNode::GetTotalBytesSent() LOCK(cs_totalBytesSent); return nTotalBytesSent; } +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); +}