Catch exceptions when retrieve Hotspot IP address

PiperOrigin-RevId: 477363744
This commit is contained in:
hai007
2022-09-27 23:11:03 -07:00
committed by Copybara-Service
parent f1a6cdc988
commit f65e32ad32
@@ -14,6 +14,7 @@
#include <windows.h>
#include <exception>
#include <functional>
#include <memory>
#include <string>
@@ -215,21 +216,32 @@ std::vector<std::string> WifiHotspotServerSocket::GetIpAddresses() const {
}
std::string WifiHotspotServerSocket::GetHotspotIpAddresses() const {
for (int i = 0; i < kMaxRetries; i++) {
auto host_names = NetworkInformation::GetHostNames();
for (auto host_name : host_names) {
if (host_name.IPInformation() != nullptr &&
host_name.IPInformation().NetworkAdapter() != nullptr &&
host_name.Type() == HostNameType::Ipv4) {
std::string ipv4_s = winrt::to_string(host_name.ToString());
if (HasEnding(ipv4_s, ".1")) {
// TODO(b/228541380): replace when we find a better way to identifying
// the hotspot address
NEARBY_LOGS(INFO) << "Found Hotspot IP: " << ipv4_s;
return ipv4_s;
try {
for (int i = 0; i < kMaxRetries; i++) {
auto host_names = NetworkInformation::GetHostNames();
for (auto host_name : host_names) {
if (host_name.IPInformation() != nullptr &&
host_name.IPInformation().NetworkAdapter() != nullptr &&
host_name.Type() == HostNameType::Ipv4) {
std::string ipv4_s = winrt::to_string(host_name.ToString());
if (HasEnding(ipv4_s, ".1")) {
// TODO(b/228541380): replace when we find a better way to
// identifying the hotspot address
NEARBY_LOGS(INFO) << "Found Hotspot IP: " << ipv4_s;
return ipv4_s;
}
}
}
}
} catch (std::exception exception) {
NEARBY_LOGS(ERROR) << __func__ << ": Exception to GetHotspotIpAddresses: "
<< exception.what();
return {};
} catch (const winrt::hresult_error &ex) {
NEARBY_LOGS(ERROR) << __func__
<< ": Exception to GetHotspotIpAddresses: " << ex.code()
<< ": " << winrt::to_string(ex.message());
return {};
}
return {};
}