35
35
#include < miniupnpc/upnperrors.h>
36
36
#endif
37
37
38
- #include < boost/filesystem.hpp>
39
- #include < boost/thread.hpp>
40
38
41
39
#include < math.h>
42
40
@@ -1042,7 +1040,7 @@ void CConnman::AcceptConnection(const ListenSocket& hListenSocket) {
1042
1040
void CConnman::ThreadSocketHandler ()
1043
1041
{
1044
1042
unsigned int nPrevNodeCount = 0 ;
1045
- while (true )
1043
+ while (!interruptNet )
1046
1044
{
1047
1045
//
1048
1046
// Disconnect nodes
@@ -1180,7 +1178,8 @@ void CConnman::ThreadSocketHandler()
1180
1178
1181
1179
int nSelect = select (have_fds ? hSocketMax + 1 : 0 ,
1182
1180
&fdsetRecv, &fdsetSend, &fdsetError, &timeout);
1183
- boost::this_thread::interruption_point ();
1181
+ if (interruptNet)
1182
+ return ;
1184
1183
1185
1184
if (nSelect == SOCKET_ERROR)
1186
1185
{
@@ -1193,7 +1192,8 @@ void CConnman::ThreadSocketHandler()
1193
1192
}
1194
1193
FD_ZERO (&fdsetSend);
1195
1194
FD_ZERO (&fdsetError);
1196
- MilliSleep (timeout.tv_usec /1000 );
1195
+ if (!interruptNet.sleep_for (std::chrono::milliseconds (timeout.tv_usec /1000 )))
1196
+ return ;
1197
1197
}
1198
1198
1199
1199
//
@@ -1219,7 +1219,8 @@ void CConnman::ThreadSocketHandler()
1219
1219
}
1220
1220
BOOST_FOREACH (CNode* pnode, vNodesCopy)
1221
1221
{
1222
- boost::this_thread::interruption_point ();
1222
+ if (interruptNet)
1223
+ return ;
1223
1224
1224
1225
//
1225
1226
// Receive
@@ -1241,7 +1242,7 @@ void CConnman::ThreadSocketHandler()
1241
1242
if (!pnode->ReceiveMsgBytes (pchBuf, nBytes, notify))
1242
1243
pnode->CloseSocketDisconnect ();
1243
1244
if (notify)
1244
- messageHandlerCondition .notify_one ();
1245
+ condMsgProc .notify_one ();
1245
1246
pnode->nLastRecv = GetTime ();
1246
1247
pnode->nRecvBytes += nBytes;
1247
1248
RecordBytesRecv (nBytes);
@@ -1469,7 +1470,8 @@ void CConnman::ThreadDNSAddressSeed()
1469
1470
// less influence on the network topology, and reduces traffic to the seeds.
1470
1471
if ((addrman.size () > 0 ) &&
1471
1472
(!GetBoolArg (" -forcednsseed" , DEFAULT_FORCEDNSSEED))) {
1472
- MilliSleep (11 * 1000 );
1473
+ if (!interruptNet.sleep_for (std::chrono::seconds (11 )))
1474
+ return ;
1473
1475
1474
1476
LOCK (cs_vNodes);
1475
1477
int nRelevant = 0 ;
@@ -1580,10 +1582,12 @@ void CConnman::ThreadOpenConnections()
1580
1582
OpenNetworkConnection (addr, false , NULL , strAddr.c_str ());
1581
1583
for (int i = 0 ; i < 10 && i < nLoop; i++)
1582
1584
{
1583
- MilliSleep (500 );
1585
+ if (!interruptNet.sleep_for (std::chrono::milliseconds (500 )))
1586
+ return ;
1584
1587
}
1585
1588
}
1586
- MilliSleep (500 );
1589
+ if (!interruptNet.sleep_for (std::chrono::milliseconds (500 )))
1590
+ return ;
1587
1591
}
1588
1592
}
1589
1593
@@ -1592,14 +1596,16 @@ void CConnman::ThreadOpenConnections()
1592
1596
1593
1597
// Minimum time before next feeler connection (in microseconds).
1594
1598
int64_t nNextFeeler = PoissonNextSend (nStart*1000 *1000 , FEELER_INTERVAL);
1595
- while (true )
1599
+ while (!interruptNet )
1596
1600
{
1597
1601
ProcessOneShot ();
1598
1602
1599
- MilliSleep (500 );
1603
+ if (!interruptNet.sleep_for (std::chrono::milliseconds (500 )))
1604
+ return ;
1600
1605
1601
1606
CSemaphoreGrant grant (*semOutbound);
1602
- boost::this_thread::interruption_point ();
1607
+ if (interruptNet)
1608
+ return ;
1603
1609
1604
1610
// Add seed nodes if DNS seeds are all down (an infrastructure attack?).
1605
1611
if (addrman.size () == 0 && (GetTime () - nStart > 60 )) {
@@ -1657,7 +1663,7 @@ void CConnman::ThreadOpenConnections()
1657
1663
1658
1664
int64_t nANow = GetAdjustedTime ();
1659
1665
int nTries = 0 ;
1660
- while (true )
1666
+ while (!interruptNet )
1661
1667
{
1662
1668
CAddrInfo addr = addrman.Select (fFeeler );
1663
1669
@@ -1700,7 +1706,8 @@ void CConnman::ThreadOpenConnections()
1700
1706
if (fFeeler ) {
1701
1707
// Add small amount of random noise before connection to avoid synchronization.
1702
1708
int randsleep = GetRandInt (FEELER_SLEEP_WINDOW * 1000 );
1703
- MilliSleep (randsleep);
1709
+ if (!interruptNet.sleep_for (std::chrono::milliseconds (randsleep)))
1710
+ return ;
1704
1711
LogPrint (" net" , " Making feeler connection to %s\n " , addrConnect.ToString ());
1705
1712
}
1706
1713
@@ -1779,11 +1786,12 @@ void CConnman::ThreadOpenAddedConnections()
1779
1786
// OpenNetworkConnection can detect existing connections to that IP/port.
1780
1787
CService service (LookupNumeric (info.strAddedNode .c_str (), Params ().GetDefaultPort ()));
1781
1788
OpenNetworkConnection (CAddress (service, NODE_NONE), false , &grant, info.strAddedNode .c_str (), false );
1782
- MilliSleep (500 );
1789
+ if (!interruptNet.sleep_for (std::chrono::milliseconds (500 )))
1790
+ return ;
1783
1791
}
1784
1792
}
1785
-
1786
- MilliSleep ( 120000 ); // Retry every 2 minutes
1793
+ if (!interruptNet. sleep_for ( std::chrono::minutes ( 2 )))
1794
+ return ;
1787
1795
}
1788
1796
}
1789
1797
@@ -1793,7 +1801,9 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
1793
1801
//
1794
1802
// Initiate outbound network connection
1795
1803
//
1796
- boost::this_thread::interruption_point ();
1804
+ if (interruptNet) {
1805
+ return false ;
1806
+ }
1797
1807
if (!fNetworkActive ) {
1798
1808
return false ;
1799
1809
}
@@ -1806,7 +1816,6 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
1806
1816
return false ;
1807
1817
1808
1818
CNode* pnode = ConnectNode (addrConnect, pszDest, fCountFailure );
1809
- boost::this_thread::interruption_point ();
1810
1819
1811
1820
if (!pnode)
1812
1821
return false ;
@@ -1820,13 +1829,9 @@ bool CConnman::OpenNetworkConnection(const CAddress& addrConnect, bool fCountFai
1820
1829
return true ;
1821
1830
}
1822
1831
1823
-
1824
1832
void CConnman::ThreadMessageHandler ()
1825
1833
{
1826
- boost::mutex condition_mutex;
1827
- boost::unique_lock<boost::mutex> lock (condition_mutex);
1828
-
1829
- while (true )
1834
+ while (!flagInterruptMsgProc)
1830
1835
{
1831
1836
std::vector<CNode*> vNodesCopy;
1832
1837
{
@@ -1849,7 +1854,7 @@ void CConnman::ThreadMessageHandler()
1849
1854
TRY_LOCK (pnode->cs_vRecvMsg , lockRecv);
1850
1855
if (lockRecv)
1851
1856
{
1852
- if (!GetNodeSignals ().ProcessMessages (pnode, *this ))
1857
+ if (!GetNodeSignals ().ProcessMessages (pnode, *this , flagInterruptMsgProc ))
1853
1858
pnode->CloseSocketDisconnect ();
1854
1859
1855
1860
if (pnode->nSendSize < GetSendBufferSize ())
@@ -1861,15 +1866,17 @@ void CConnman::ThreadMessageHandler()
1861
1866
}
1862
1867
}
1863
1868
}
1864
- boost::this_thread::interruption_point ();
1869
+ if (flagInterruptMsgProc)
1870
+ return ;
1865
1871
1866
1872
// Send messages
1867
1873
{
1868
1874
TRY_LOCK (pnode->cs_vSend , lockSend);
1869
1875
if (lockSend)
1870
- GetNodeSignals ().SendMessages (pnode, *this );
1876
+ GetNodeSignals ().SendMessages (pnode, *this , flagInterruptMsgProc );
1871
1877
}
1872
- boost::this_thread::interruption_point ();
1878
+ if (flagInterruptMsgProc)
1879
+ return ;
1873
1880
}
1874
1881
1875
1882
{
@@ -1878,8 +1885,10 @@ void CConnman::ThreadMessageHandler()
1878
1885
pnode->Release ();
1879
1886
}
1880
1887
1881
- if (fSleep )
1882
- messageHandlerCondition.timed_wait (lock, boost::posix_time::microsec_clock::universal_time () + boost::posix_time::milliseconds (100 ));
1888
+ if (fSleep ) {
1889
+ std::unique_lock<std::mutex> lock (mutexMsgProc);
1890
+ condMsgProc.wait_until (lock, std::chrono::steady_clock::now () + std::chrono::milliseconds (100 ));
1891
+ }
1883
1892
}
1884
1893
}
1885
1894
@@ -2071,14 +2080,15 @@ CConnman::CConnman(uint64_t nSeed0In, uint64_t nSeed1In) : nSeed0(nSeed0In), nSe
2071
2080
nMaxOutbound = 0 ;
2072
2081
nBestHeight = 0 ;
2073
2082
clientInterface = NULL ;
2083
+ flagInterruptMsgProc = false ;
2074
2084
}
2075
2085
2076
2086
NodeId CConnman::GetNewNodeId ()
2077
2087
{
2078
2088
return nLastNodeId.fetch_add (1 , std::memory_order_relaxed);
2079
2089
}
2080
2090
2081
- bool CConnman::Start (boost::thread_group& threadGroup, CScheduler& scheduler, std::string& strNodeError, Options connOptions)
2091
+ bool CConnman::Start (CScheduler& scheduler, std::string& strNodeError, Options connOptions)
2082
2092
{
2083
2093
nTotalBytesRecv = 0 ;
2084
2094
nTotalBytesSent = 0 ;
@@ -2145,24 +2155,27 @@ bool CConnman::Start(boost::thread_group& threadGroup, CScheduler& scheduler, st
2145
2155
//
2146
2156
// Start threads
2147
2157
//
2158
+ InterruptSocks5 (false );
2159
+ interruptNet.reset ();
2160
+ flagInterruptMsgProc = false ;
2161
+
2162
+ // Send and receive from sockets, accept connections
2163
+ threadSocketHandler = std::thread (&TraceThread<std::function<void ()> >, " net" , std::function<void ()>(std::bind (&CConnman::ThreadSocketHandler, this )));
2148
2164
2149
2165
if (!GetBoolArg (" -dnsseed" , true ))
2150
2166
LogPrintf (" DNS seeding disabled\n " );
2151
2167
else
2152
- threadGroup.create_thread (boost::bind (&TraceThread<boost::function<void ()> >, " dnsseed" , boost::function<void ()>(boost::bind (&CConnman::ThreadDNSAddressSeed, this ))));
2153
-
2154
- // Send and receive from sockets, accept connections
2155
- threadGroup.create_thread (boost::bind (&TraceThread<boost::function<void ()> >, " net" , boost::function<void ()>(boost::bind (&CConnman::ThreadSocketHandler, this ))));
2168
+ threadDNSAddressSeed = std::thread (&TraceThread<std::function<void ()> >, " dnsseed" , std::function<void ()>(std::bind (&CConnman::ThreadDNSAddressSeed, this )));
2156
2169
2157
2170
// Initiate outbound connections from -addnode
2158
- threadGroup. create_thread ( boost::bind (&TraceThread<boost ::function<void ()> >, " addcon" , boost ::function<void ()>(boost ::bind (&CConnman::ThreadOpenAddedConnections, this ) )));
2171
+ threadOpenAddedConnections = std::thread (&TraceThread<std ::function<void ()> >, " addcon" , std ::function<void ()>(std ::bind (&CConnman::ThreadOpenAddedConnections, this )));
2159
2172
2160
2173
// Initiate outbound connections unless connect=0
2161
2174
if (!mapMultiArgs.count (" -connect" ) || mapMultiArgs.at (" -connect" ).size () != 1 || mapMultiArgs.at (" -connect" )[0 ] != " 0" )
2162
- threadGroup. create_thread ( boost::bind (&TraceThread<boost ::function<void ()> >, " opencon" , boost ::function<void ()>(boost ::bind (&CConnman::ThreadOpenConnections, this ) )));
2175
+ threadOpenConnections = std::thread (&TraceThread<std ::function<void ()> >, " opencon" , std ::function<void ()>(std ::bind (&CConnman::ThreadOpenConnections, this )));
2163
2176
2164
2177
// Process messages
2165
- threadGroup. create_thread ( boost::bind (&TraceThread<boost ::function<void ()> >, " msghand" , boost ::function<void ()>(boost ::bind (&CConnman::ThreadMessageHandler, this ) )));
2178
+ threadMessageHandler = std::thread (&TraceThread<std ::function<void ()> >, " msghand" , std ::function<void ()>(std ::bind (&CConnman::ThreadMessageHandler, this )));
2166
2179
2167
2180
// Dump network addresses
2168
2181
scheduler.scheduleEvery (boost::bind (&CConnman::DumpData, this ), DUMP_ADDRESSES_INTERVAL);
@@ -2185,12 +2198,34 @@ class CNetCleanup
2185
2198
}
2186
2199
instance_of_cnetcleanup;
2187
2200
2188
- void CConnman::Stop ()
2201
+ void CConnman::Interrupt ()
2189
2202
{
2190
- LogPrintf (" %s\n " ,__func__);
2203
+ {
2204
+ std::lock_guard<std::mutex> lock (mutexMsgProc);
2205
+ flagInterruptMsgProc = true ;
2206
+ }
2207
+ condMsgProc.notify_all ();
2208
+
2209
+ interruptNet ();
2210
+ InterruptSocks5 (true );
2211
+
2191
2212
if (semOutbound)
2192
2213
for (int i=0 ; i<(nMaxOutbound + nMaxFeeler); i++)
2193
2214
semOutbound->post ();
2215
+ }
2216
+
2217
+ void CConnman::Stop ()
2218
+ {
2219
+ if (threadMessageHandler.joinable ())
2220
+ threadMessageHandler.join ();
2221
+ if (threadOpenConnections.joinable ())
2222
+ threadOpenConnections.join ();
2223
+ if (threadOpenAddedConnections.joinable ())
2224
+ threadOpenAddedConnections.join ();
2225
+ if (threadDNSAddressSeed.joinable ())
2226
+ threadDNSAddressSeed.join ();
2227
+ if (threadSocketHandler.joinable ())
2228
+ threadSocketHandler.join ();
2194
2229
2195
2230
if (fAddressesInitialized )
2196
2231
{
@@ -2233,6 +2268,7 @@ void CConnman::DeleteNode(CNode* pnode)
2233
2268
2234
2269
CConnman::~CConnman ()
2235
2270
{
2271
+ Interrupt ();
2236
2272
Stop ();
2237
2273
}
2238
2274
0 commit comments