Bitcoin Forum
December 17, 2018, 06:44:55 AM *
News: Latest Bitcoin Core release: 0.17.0 [Torrent].
 
   Home   Help Search Login Register More  
Pages: [1]
  Print  
Author Topic: how ThreadOpenConnections work?  (Read 44 times)
zhengjianxun
Newbie
*
Offline Offline

Activity: 9
Merit: 0


View Profile
August 14, 2018, 10:33:59 AM
 #1

in bitcoin 0.10 and the net.cpp, i don't koow how ThreadOpenConnectionswork,because the red code is a inite loop , so why it can excute the next code

void ThreadOpenConnections()
{
    // Connect to specific addresses
    if (mapArgs.count("-connect") && mapMultiArgs["-connect"].size() > 0)
    {
        for (int64_t nLoop = 0;; nLoop++)
        {
            ProcessOneShot();
            BOOST_FOREACH(string strAddr, mapMultiArgs["-connect"])
            {
                CAddress addr;
                OpenNetworkConnection(addr, NULL, strAddr.c_str());
                for (int i = 0; i < 10 && i < nLoop; i++)
                {
                    MilliSleep(500);
                }
            }
            MilliSleep(500);
        }
    }


    // Initiate network connections
    int64_t nStart = GetTime();
    while (true)
    {
        ProcessOneShot();

        MilliSleep(500);

        CSemaphoreGrant grant(*semOutbound);
        boost::this_thread::interruption_point();

        // Add seed nodes if DNS seeds are all down (an infrastructure attack?).
        if (addrman.size() == 0 && (GetTime() - nStart > 60)) {
            static bool done = false;
            if (!done) {
                LogPrintf("Adding fixed seed nodes as DNS doesn't seem to be available.\n");
                addrman.Add(Params().FixedSeeds(), CNetAddr("127.0.0.1"));
                done = true;
            }
        }

        //
        // Choose an address to connect to based on most recently seen
        //
        CAddress addrConnect;

        // Only connect out to one peer per network group (/16 for IPv4).
        // Do this here so we don't have to critsect vNodes inside mapAddresses critsect.
        int nOutbound = 0;
        set<vector<unsigned char> > setConnected;
        {
            LOCK(cs_vNodes);
            BOOST_FOREACH(CNode* pnode, vNodes) {
                if (!pnode->fInbound) {
                    setConnected.insert(pnode->addr.GetGroup());
                    nOutbound++;
                }
            }
        }

        int64_t nANow = GetAdjustedTime();

        int nTries = 0;
        while (true)
        {
            CAddress addr = addrman.Select();

            // if we selected an invalid address, restart
            if (!addr.IsValid() || setConnected.count(addr.GetGroup()) || IsLocal(addr))
                break;

            // If we didn't find an appropriate destination after trying 100 addresses fetched from addrman,
            // stop this loop, and let the outer loop run again (which sleeps, adds seed nodes, recalculates
            // already-connected network ranges, ...) before trying new addrman addresses.
            nTries++;
            if (nTries > 100)
                break;

            if (IsLimited(addr))
                continue;

            // only consider very recently tried nodes after 30 failed attempts
            if (nANow - addr.nLastTry < 600 && nTries < 30)
                continue;

            // do not allow non-default ports, unless after 50 invalid addresses selected already
            if (addr.GetPort() != Params().GetDefaultPort() && nTries < 50)
                continue;

            addrConnect = addr;
            break;
        }

        if (addrConnect.IsValid())
            OpenNetworkConnection(addrConnect, &grant);
    }
}
1545029095
Hero Member
*
Offline Offline

Posts: 1545029095

View Profile Personal Message (Offline)

Ignore
1545029095
Reply with quote  #2

1545029095
Report to moderator
1545029095
Hero Member
*
Offline Offline

Posts: 1545029095

View Profile Personal Message (Offline)

Ignore
1545029095
Reply with quote  #2

1545029095
Report to moderator
1545029095
Hero Member
*
Offline Offline

Posts: 1545029095

View Profile Personal Message (Offline)

Ignore
1545029095
Reply with quote  #2

1545029095
Report to moderator
PLAY NOW
Advertised sites are not endorsed by the Bitcoin Forum. They may be unsafe, untrustworthy, or illegal in your jurisdiction. Advertise here.
1545029095
Hero Member
*
Offline Offline

Posts: 1545029095

View Profile Personal Message (Offline)

Ignore
1545029095
Reply with quote  #2

1545029095
Report to moderator
1545029095
Hero Member
*
Offline Offline

Posts: 1545029095

View Profile Personal Message (Offline)

Ignore
1545029095
Reply with quote  #2

1545029095
Report to moderator
1545029095
Hero Member
*
Offline Offline

Posts: 1545029095

View Profile Personal Message (Offline)

Ignore
1545029095
Reply with quote  #2

1545029095
Report to moderator
gmaxwell
Moderator
Legendary
*
qt
Offline Offline

Activity: 2590
Merit: 1757



View Profile
August 14, 2018, 10:23:23 PM
Merited by Foxpup (2)
 #2

You are looking at very old and unsupported code, no one should be running any thing that old anymore.

In any case, the infinite loop is wrapped in an if.  The lower code doesn't run if -connect is in use, by design.

Bitcoin will not be compromised
Pages: [1]
  Print  
 
Jump to:  

Sponsored by , a Bitcoin-accepting VPN.
Powered by MySQL Powered by PHP Powered by SMF 1.1.19 | SMF © 2006-2009, Simple Machines Valid XHTML 1.0! Valid CSS!