@@ -145,11 +145,12 @@ uint16_t GetListenPort()
145
145
return static_cast <uint16_t >(gArgs .GetIntArg (" -port" , Params ().GetDefaultPort ()));
146
146
}
147
147
148
- // find ' best' local address for a particular peer
149
- [[nodiscard]] static bool GetLocal (CService& addr, const CNode& peer)
148
+ // Determine the " best" local address for a particular peer.
149
+ [[nodiscard]] static std::optional<CService> GetLocal (const CNode& peer)
150
150
{
151
- if (!fListen ) return false ;
151
+ if (!fListen ) return std::nullopt ;
152
152
153
+ std::optional<CService> addr;
153
154
int nBestScore = -1 ;
154
155
int nBestReachability = -1 ;
155
156
{
@@ -165,13 +166,13 @@ uint16_t GetListenPort()
165
166
const int nScore{local_service_info.nScore };
166
167
const int nReachability{local_addr.GetReachabilityFrom (peer.addr )};
167
168
if (nReachability > nBestReachability || (nReachability == nBestReachability && nScore > nBestScore)) {
168
- addr = CService{local_addr, local_service_info.nPort };
169
+ addr. emplace ( CService{local_addr, local_service_info.nPort }) ;
169
170
nBestReachability = nReachability;
170
171
nBestScore = nScore;
171
172
}
172
173
}
173
174
}
174
- return nBestScore >= 0 ;
175
+ return addr ;
175
176
}
176
177
177
178
// ! Convert the serialized seeds into usable address objects.
@@ -196,17 +197,13 @@ static std::vector<CAddress> ConvertSeeds(const std::vector<uint8_t> &vSeedsIn)
196
197
return vSeedsOut;
197
198
}
198
199
199
- // get best local address for a particular peer as a CAddress
200
- // Otherwise , return the unroutable 0.0.0.0 but filled in with
200
+ // Determine the " best" local address for a particular peer.
201
+ // If none , return the unroutable 0.0.0.0 but filled in with
201
202
// the normal parameters, since the IP may be changed to a useful
202
203
// one by discovery.
203
204
CService GetLocalAddress (const CNode& peer)
204
205
{
205
- CService addr;
206
- if (GetLocal (addr, peer)) {
207
- return addr;
208
- }
209
- return CService{CNetAddr (), GetListenPort ()};
206
+ return GetLocal (peer).value_or (CService{CNetAddr (), GetListenPort ()});
210
207
}
211
208
212
209
static int GetnScore (const CService& addr)
0 commit comments