bool RecvLine(SOCKET hSocket, string& strLine)
{
- strLine = "";
- while (true)
+ strLine.clear();
+ for ( ; ; )
{
char c;
int nBytes = recv(hSocket, &c, 1, 0);
if (pnode->fSuccessfullyConnected)
{
CAddress addrLocal = GetLocalAddress(&pnode->addr);
- if (addrLocal.IsRoutable() && (CService)addrLocal != (CService)pnode->addrLocal)
+ if (addrLocal.IsRoutable() && (CService)addrLocal != pnode->addrLocal)
{
pnode->PushAddress(addrLocal);
pnode->addrLocal = addrLocal;
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)
{
}
#undef X
-
-
-
-
+void Release(CNode* node) {
+ node->Release();
+}
{
printf("ThreadSocketHandler started\n");
size_t nPrevNodeCount = 0;
- while (true)
+ for ( ; ; )
{
//
// Disconnect nodes
}
{
LOCK(cs_vNodes);
- BOOST_FOREACH(CNode* pnode, vNodesCopy)
- pnode->Release();
+ for_each(vNodesCopy.begin(), vNodesCopy.end(), Release);
}
Sleep(10);
// Initiate network connections
int64_t nStart = GetTime();
- while (true)
+ for ( ; ; )
{
ProcessOneShot();
// it'll get a pile of addresses with newer timestamps.
// Seed nodes are given a random 'last seen time' of between one and two
// weeks ago.
- const int64_t nOneWeek = 7*24*60*60;
struct in_addr ip;
memcpy(&ip, &pnSeed[i], sizeof(ip));
CAddress addr(CService(ip, GetDefaultPort()));
std::vector<CAddress> vAdd;
for (unsigned int i = 0; i < ARRAYLEN(pchTorSeed); i++)
{
- const int64_t nOneWeek = 7*24*60*60;
CAddress addr(CService(pchTorSeed[i], GetDefaultPort()));
addr.nTime = GetTime()-GetRand(nOneWeek)-nOneWeek;
vAdd.push_back(addr);
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);
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
{
TRY_LOCK(pnode->cs_vSend, lockSend);
if (lockSend)
- SendMessages(pnode, pnode == pnodeTrickle);
+ SendMessages(pnode);
}
if (fShutdown)
return;
{
LOCK(cs_vNodes);
- BOOST_FOREACH(CNode* pnode, vNodesCopy)
- pnode->Release();
+ for_each(vNodesCopy.begin(), vNodesCopy.end(), Release);
}
// Wait and allow messages to bunch up.
bool BindListenPort(const CService &addrBind, string& strError)
{
- strError = "";
+ strError.clear();
int nOne = 1;
// Create socket for listening for incoming connections
if (semOutbound)
for (int i=0; i<MAX_OUTBOUND_CONNECTIONS; i++)
semOutbound->post();
- do
+ for ( ; ; )
{
int nThreadsRunning = 0;
for (int n = 0; n < THREAD_MAX; n++)
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");
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);
+}