Change the method to stop Wi-Fi LAN advertising

PiperOrigin-RevId: 684905306
This commit is contained in:
Guogang Li
2024-10-11 11:40:34 -07:00
committed by Copybara-Service
parent ceb14e10e3
commit 37d7dc19de
2 changed files with 25 additions and 76 deletions
@@ -18,24 +18,31 @@
// Windows headers
// clang-format off
#include <windows.h> // NOLINT
#include <windns.h> // NOLINT
// clang-format on
// Standard C/C++ headers
#include <cstddef>
#include <cstdint>
#include <deque>
#include <exception>
#include <memory>
#include <optional>
#include <string>
#include <utility>
// Nearby connections headers
#include "absl/base/thread_annotations.h"
#include "absl/container/flat_hash_map.h"
#include "absl/functional/any_invocable.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "absl/time/time.h"
#include "absl/types/optional.h"
#include "internal/platform/byte_array.h"
#include "internal/platform/cancellation_flag.h"
#include "internal/platform/count_down_latch.h"
#include "internal/platform/exception.h"
#include "internal/platform/implementation/cancelable.h"
#include "internal/platform/implementation/wifi_lan.h"
#include "internal/platform/implementation/windows/scheduled_executor.h"
#include "internal/platform/input_stream.h"
@@ -250,11 +257,6 @@ class WifiLanMedium : public api::WifiLanMedium {
std::unique_ptr<api::WifiLanServerSocket> ListenForService(
int port = 0) override;
// DnsServiceDeRegister is a async process, after operation finish, callback
// will call this method to notify the waiting method StopAdvertising to
// continue.
void NotifyDnsServiceUnregistered(DWORD status);
absl::optional<std::pair<std::int32_t, std::int32_t>> GetDynamicPortRange()
override {
return absl::nullopt;
@@ -312,8 +314,6 @@ class WifiLanMedium : public api::WifiLanMedium {
DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate);
fire_and_forget Watcher_DeviceRemoved(
DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate);
static void Advertising_StopCompleted(DWORD Status, PVOID pQueryContext,
PDNS_SERVICE_INSTANCE pInstance);
// Gets error message from exception pointer
std::string GetErrorMessage(std::exception_ptr eptr);
@@ -328,13 +328,6 @@ class WifiLanMedium : public api::WifiLanMedium {
DnssdServiceInstance dnssd_service_instance_{nullptr};
DnssdRegistrationResult dnssd_regirstraion_result_{nullptr};
// Stop advertising properties
DNS_SERVICE_INSTANCE dns_service_instance_{nullptr};
DNS_SERVICE_REGISTER_REQUEST dns_service_register_request_;
std::unique_ptr<std::wstring> dns_service_instance_name_{nullptr};
std::unique_ptr<CountDownLatch> dns_service_stop_latch_;
DWORD dns_service_stop_status_;
// Discovery properties
DeviceWatcher device_watcher_{nullptr};
winrt::event_token device_watcher_added_event_token;
@@ -173,26 +173,6 @@ bool WifiLanMedium::StartAdvertising(const NsdServiceInfo& nsd_service_info) {
return false;
}
// Win32 call only can use globel function or static method in class
void WifiLanMedium::Advertising_StopCompleted(DWORD Status, PVOID pQueryContext,
PDNS_SERVICE_INSTANCE pInstance) {
LOG(INFO) << "unregister with status=" << Status;
try {
WifiLanMedium* medium = static_cast<WifiLanMedium*>(pQueryContext);
medium->NotifyDnsServiceUnregistered(Status);
} catch (...) {
LOG(ERROR) << "failed to notify the stop of DNS service instance."
<< Status;
}
}
void WifiLanMedium::NotifyDnsServiceUnregistered(DWORD status) {
if (dns_service_stop_latch_.get() != nullptr) {
dns_service_stop_status_ = status;
dns_service_stop_latch_.get()->CountDown();
}
}
bool WifiLanMedium::StopAdvertising(const NsdServiceInfo& nsd_service_info) {
// Need to use Win32 API to deregister the Dnssd instance
if (!IsAdvertising()) {
@@ -201,47 +181,7 @@ bool WifiLanMedium::StopAdvertising(const NsdServiceInfo& nsd_service_info) {
return false;
}
// Init DNS service instance
std::string instance_name = absl::StrFormat(
kMdnsInstanceNameFormat.data(), nsd_service_info.GetServiceName(),
nsd_service_info.GetServiceType());
int port = nsd_service_info.GetPort();
dns_service_instance_name_ = std::make_unique<std::wstring>(
string_utils::StringToWideString(instance_name));
dns_service_instance_.pszInstanceName =
(LPWSTR)dns_service_instance_name_->c_str();
dns_service_instance_.pszHostName = (LPWSTR)kMdnsHostName.data();
dns_service_instance_.wPort = port;
// Init DNS service register request
dns_service_register_request_.Version = DNS_QUERY_REQUEST_VERSION1;
dns_service_register_request_.InterfaceIndex =
0; // all interfaces will be considered
dns_service_register_request_.unicastEnabled = false;
dns_service_register_request_.hCredentials = nullptr;
dns_service_register_request_.pServiceInstance = &dns_service_instance_;
dns_service_register_request_.pQueryContext = this; // callback use it
dns_service_register_request_.pRegisterCompletionCallback =
WifiLanMedium::Advertising_StopCompleted;
dns_service_stop_latch_ = std::make_unique<CountDownLatch>(1);
DWORD status = DnsServiceDeRegister(&dns_service_register_request_, nullptr);
if (status != DNS_REQUEST_PENDING) {
LOG(ERROR) << "failed to stop mDNS advertising for service type ="
<< nsd_service_info.GetServiceType();
return false;
}
// Wait for stop finish
dns_service_stop_latch_.get()->Await();
dns_service_stop_latch_ = nullptr;
if (dns_service_stop_status_ != 0) {
LOG(INFO) << "failed to stop mDNS advertising for service type ="
<< nsd_service_info.GetServiceType();
return false;
}
dnssd_service_instance_ = nullptr;
LOG(INFO) << "succeeded to stop mDNS advertising for service type ="
<< nsd_service_info.GetServiceType();
@@ -635,6 +575,22 @@ fire_and_forget WifiLanMedium::Watcher_DeviceUpdated(
std::optional<NsdServiceInfo> last_nsd_service_info =
GetDiscoveredService(winrt::to_string(deviceInfoUpdate.Id()));
if (!last_nsd_service_info.has_value()) {
if (IsConnectableIpAddress(
ipaddr_4bytes_to_dotdecimal_string(nsd_service_info.GetIPAddress()),
nsd_service_info.GetPort(), kConnectTimeout)) {
// If the device is not in the discovered service list, but it is
// connectable during update, we add it to the discovered service list.
LOG(INFO) << "device added for service name "
<< nsd_service_info.GetServiceName() << ", address: "
<< ipaddr_4bytes_to_dotdecimal_string(
nsd_service_info.GetIPAddress())
<< ":" << nsd_service_info.GetPort();
UpdateDiscoveredService(winrt::to_string(deviceInfoUpdate.Id()),
nsd_service_info);
discovered_service_callback_.service_discovered_cb(nsd_service_info);
return fire_and_forget{};
}
LOG(WARNING)
<< "Don't update WIFI_LAN device due to it is not in device list.";
return fire_and_forget{};