@@ -140,10 +140,11 @@ static std::vector<CAddress> convertSeed6(const std::vector<SeedSpec6> &vSeedsIn
140140 const int64_t nOneWeek = 7 *24 *60 *60 ;
141141 std::vector<CAddress> vSeedsOut;
142142 vSeedsOut.reserve (vSeedsIn.size ());
143- for (const auto & seed_in : vSeedsIn) {
143+ for (std::vector<SeedSpec6>::const_iterator i (vSeedsIn.begin ()); i != vSeedsIn.end (); ++i)
144+ {
144145 struct in6_addr ip;
145- memcpy (&ip, seed_in. addr , sizeof (ip));
146- CAddress addr (CService (ip, seed_in. port ), NODE_NETWORK);
146+ memcpy (&ip, i-> addr , sizeof (ip));
147+ CAddress addr (CService (ip, i-> port ), NODE_NETWORK);
147148 addr.nTime = GetTime () - GetRand (nOneWeek) - nOneWeek;
148149 vSeedsOut.push_back (addr);
149150 }
@@ -1706,15 +1707,46 @@ void CConnman::ProcessOneShot()
17061707 }
17071708}
17081709
1709- void CConnman::ThreadOpenConnections (const std::vector<std::string> connect)
1710+ bool CConnman::GetTryNewOutboundPeer ()
1711+ {
1712+ return m_try_another_outbound_peer;
1713+ }
1714+
1715+ void CConnman::SetTryNewOutboundPeer (bool flag)
1716+ {
1717+ m_try_another_outbound_peer = flag;
1718+ LogPrint (BCLog::NET, " net: setting try another outbound peer=%s\n " , flag ? " true" : " false" );
1719+ }
1720+
1721+ // Return the number of peers we have over our outbound connection limit
1722+ // Exclude peers that are marked for disconnect, or are going to be
1723+ // disconnected soon (eg one-shots and feelers)
1724+ // Also exclude peers that haven't finished initial connection handshake yet
1725+ // (so that we don't decide we're over our desired connection limit, and then
1726+ // evict some peer that has finished the handshake)
1727+ int CConnman::GetExtraOutboundCount ()
1728+ {
1729+ int nOutbound = 0 ;
1730+ {
1731+ LOCK (cs_vNodes);
1732+ for (CNode* pnode : vNodes) {
1733+ if (!pnode->fInbound && !pnode->m_manual_connection && !pnode->fFeeler && !pnode->fDisconnect && !pnode->fOneShot && pnode->fSuccessfullyConnected ) {
1734+ ++nOutbound;
1735+ }
1736+ }
1737+ }
1738+ return std::max (nOutbound - nMaxOutbound, 0 );
1739+ }
1740+
1741+ void CConnman::ThreadOpenConnections ()
17101742{
17111743 // Connect to specific addresses
1712- if (!connect. empty ( ))
1744+ if (gArgs . IsArgSet ( " -connect " ))
17131745 {
17141746 for (int64_t nLoop = 0 ;; nLoop++)
17151747 {
17161748 ProcessOneShot ();
1717- for (const std::string& strAddr : connect)
1749+ for (const std::string& strAddr : gArgs . GetArgs ( " - connect" ) )
17181750 {
17191751 CAddress addr (CService (), NODE_NONE);
17201752 OpenNetworkConnection (addr, false , nullptr , strAddr.c_str (), false , false , true );
@@ -1796,7 +1828,8 @@ void CConnman::ThreadOpenConnections(const std::vector<std::string> connect)
17961828 // * Only make a feeler connection once every few minutes.
17971829 //
17981830 bool fFeeler = false ;
1799- if (nOutbound >= nMaxOutbound) {
1831+
1832+ if (nOutbound >= nMaxOutbound && !GetTryNewOutboundPeer ()) {
18001833 int64_t nTime = GetTimeMicros (); // The current time right now (in microseconds).
18011834 if (nTime > nNextFeeler) {
18021835 nNextFeeler = PoissonNextSend (nTime, FEELER_INTERVAL);
@@ -2224,6 +2257,7 @@ CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSe
22242257 semOutbound = nullptr ;
22252258 semAddnode = nullptr ;
22262259 flagInterruptMsgProc = false ;
2260+ SetTryNewOutboundPeer (false );
22272261
22282262 Options connOptions;
22292263 Init (connOptions);
@@ -2373,7 +2407,7 @@ bool CConnman::Start(CScheduler& scheduler, const Options& connOptions)
23732407 return false ;
23742408 }
23752409 if (connOptions.m_use_addrman_outgoing || !connOptions.m_specified_outgoing .empty ())
2376- threadOpenConnections = std::thread (&TraceThread<std::function<void ()> >, " opencon" , std::function<void ()>(std::bind (&CConnman::ThreadOpenConnections, this , connOptions. m_specified_outgoing )));
2410+ threadOpenConnections = std::thread (&TraceThread<std::function<void ()> >, " opencon" , std::function<void ()>(std::bind (&CConnman::ThreadOpenConnections, this )));
23772411
23782412 // Process messages
23792413 threadMessageHandler = std::thread (&TraceThread<std::function<void ()> >, " msghand" , std::function<void ()>(std::bind (&CConnman::ThreadMessageHandler, this )));
0 commit comments