@@ -38,6 +38,10 @@ void ThreadDNSAddressSeed2(void* parg);
38
38
bool OpenNetworkConnection (const CAddress& addrConnect, CSemaphoreGrant *grantOutbound = NULL , const char *strDest = NULL , bool fOneShot = false );
39
39
40
40
41
+ struct LocalServiceInfo {
42
+ int nScore;
43
+ int nPort;
44
+ };
41
45
42
46
//
43
47
// Global state variables
@@ -46,7 +50,7 @@ bool fClient = false;
46
50
static bool fUseUPnP = false ;
47
51
uint64 nLocalServices = (fClient ? 0 : NODE_NETWORK);
48
52
static CCriticalSection cs_mapLocalHost;
49
- static map<CService, int > mapLocalHost;
53
+ static map<CNetAddr, LocalServiceInfo > mapLocalHost;
50
54
static bool vfReachable[NET_MAX] = {};
51
55
static bool vfLimited[NET_MAX] = {};
52
56
static CNode* pnodeLocalHost = NULL ;
@@ -98,23 +102,23 @@ bool GetLocal(CService& addr, const CNetAddr *paddrPeer)
98
102
if (fUseProxy || mapArgs.count (" -connect" ) || fNoListen )
99
103
return false ;
100
104
101
- int nBestCount = -1 ;
105
+ int nBestScore = -1 ;
102
106
int nBestReachability = -1 ;
103
107
{
104
108
LOCK (cs_mapLocalHost);
105
- for (map<CService, int >::iterator it = mapLocalHost.begin (); it != mapLocalHost.end (); it++)
109
+ for (map<CNetAddr, LocalServiceInfo >::iterator it = mapLocalHost.begin (); it != mapLocalHost.end (); it++)
106
110
{
107
- int nCount = (*it).second ;
111
+ int nScore = (*it).second . nScore ;
108
112
int nReachability = (*it).first .GetReachabilityFrom (paddrPeer);
109
- if (nReachability > nBestReachability || (nReachability == nBestReachability && nCount > nBestCount ))
113
+ if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore ))
110
114
{
111
- addr = ( *it).first ;
115
+ addr = CService (( *it).first , (*it). second . nPort ) ;
112
116
nBestReachability = nReachability;
113
- nBestCount = nCount ;
117
+ nBestScore = nScore ;
114
118
}
115
119
}
116
120
}
117
- return nBestCount >= 0 ;
121
+ return nBestScore >= 0 ;
118
122
}
119
123
120
124
// get best local address for a particular peer as a CAddress
@@ -211,7 +215,12 @@ bool AddLocal(const CService& addr, int nScore)
211
215
212
216
{
213
217
LOCK (cs_mapLocalHost);
214
- mapLocalHost[addr] = std::max (nScore, mapLocalHost[addr]) + (mapLocalHost.count (addr) ? 1 : 0 );
218
+ bool fAlready = mapLocalHost.count (addr) > 0 ;
219
+ LocalServiceInfo &info = mapLocalHost[addr];
220
+ if (!fAlready || nScore >= info.nScore ) {
221
+ info.nScore = nScore;
222
+ info.nPort = addr.GetPort () + (fAlready ? 1 : 0 );
223
+ }
215
224
enum Network net = addr.GetNetwork ();
216
225
vfReachable[net] = true ;
217
226
if (net == NET_IPV6) vfReachable[NET_IPV4] = true ;
@@ -222,11 +231,9 @@ bool AddLocal(const CService& addr, int nScore)
222
231
return true ;
223
232
}
224
233
225
- bool AddLocal (const CNetAddr& addr, int nScore, int port )
234
+ bool AddLocal (const CNetAddr & addr, int nScore)
226
235
{
227
- if (port == -1 )
228
- port = GetListenPort ();
229
- return AddLocal (CService (addr, port), nScore);
236
+ return AddLocal (CService (addr, GetListenPort ()), nScore);
230
237
}
231
238
232
239
/* * Make a particular network entirely off-limits (no automatic connects to it) */
@@ -249,7 +256,7 @@ bool SeenLocal(const CService& addr)
249
256
LOCK (cs_mapLocalHost);
250
257
if (mapLocalHost.count (addr) == 0 )
251
258
return false ;
252
- mapLocalHost[addr]++;
259
+ mapLocalHost[addr]. nScore ++;
253
260
}
254
261
255
262
AdvertizeLocal ();
@@ -1887,8 +1894,9 @@ bool StopNode()
1887
1894
fShutdown = true ;
1888
1895
nTransactionsUpdated++;
1889
1896
int64 nStart = GetTime ();
1890
- for (int i=0 ; i<MAX_OUTBOUND_CONNECTIONS; i++)
1891
- semOutbound->post ();
1897
+ if (semOutbound)
1898
+ for (int i=0 ; i<MAX_OUTBOUND_CONNECTIONS; i++)
1899
+ semOutbound->post ();
1892
1900
do
1893
1901
{
1894
1902
int nThreadsRunning = 0 ;
0 commit comments