mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-15 15:16:12 -04:00
nearbyconnections : Adds accessor of ServiceType for NsdServiceInfo.
PiperOrigin-RevId: 404003948
This commit is contained in:
committed by
Copybara-Service
parent
b6ea14f741
commit
296274bc48
@@ -220,7 +220,7 @@ class WifiLanNsd {
|
||||
|
||||
// A pair of IP Address and Port. A remote device can use this information
|
||||
// to connect to us. This is non-null while IsAccepting is true.
|
||||
std::pair<std::string, int> GetServiceAddress();
|
||||
std::pair<std::string, int> GetCredentials();
|
||||
|
||||
// DnsServiceDeRegister is a async process, after operation finish, callback
|
||||
// will call this method to notify the waiting method StopAdvertising to
|
||||
@@ -263,9 +263,9 @@ class WifiLanNsd {
|
||||
// Private methods
|
||||
//
|
||||
|
||||
// Generates prefered listening port. If cannot bind to this port,
|
||||
// Generates preferred listening port. If cannot bind to this port,
|
||||
// NSD will assign a random port for the service.
|
||||
// TODO: Windows firewall may break the solution, need to futher solution to
|
||||
// TODO: Windows firewall may break the solution, need to further solution to
|
||||
// resolve the potential issue
|
||||
uint16 GenerateSocketPort(const std::string& service_id);
|
||||
|
||||
@@ -384,7 +384,7 @@ class WifiLanMedium : public api::WifiLanMedium {
|
||||
int port) override;
|
||||
|
||||
// returns advertising service address
|
||||
std::pair<std::string, int> GetServiceAddress(
|
||||
std::pair<std::string, int> GetCredentials(
|
||||
const std::string& service_id) override;
|
||||
|
||||
// for internal to clean closed connection.
|
||||
|
||||
@@ -160,14 +160,15 @@ std::unique_ptr<api::WifiLanSocket> WifiLanMedium::Connect(
|
||||
api::WifiLanService& wifi_lan_service, const std::string& service_id,
|
||||
CancellationFlag* cancellation_flag) {
|
||||
try {
|
||||
auto address = wifi_lan_service.GetServiceInfo().GetServiceAddress();
|
||||
if (address.first.empty() || address.second == 0) {
|
||||
std::string ip_address = wifi_lan_service.GetServiceInfo().GetIPAddress();
|
||||
int port = wifi_lan_service.GetServiceInfo().GetPort();
|
||||
if (ip_address.empty() || port == 0) {
|
||||
NEARBY_LOGS(ERROR) << "no valid service address and port to connect.";
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
HostName host_name{string_to_wstring(address.first)};
|
||||
winrt::hstring service_name{winrt::to_hstring(address.second)};
|
||||
HostName host_name{string_to_wstring(ip_address)};
|
||||
winrt::hstring service_name{winrt::to_hstring(port)};
|
||||
|
||||
StreamSocket socket{};
|
||||
|
||||
@@ -232,7 +233,7 @@ api::WifiLanService* WifiLanMedium::GetRemoteService(
|
||||
}
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLanMedium::GetServiceAddress(
|
||||
std::pair<std::string, int> WifiLanMedium::GetCredentials(
|
||||
const std::string& service_id) {
|
||||
try {
|
||||
WifiLanNsd* nsd = GetNsd(service_id, true);
|
||||
@@ -242,7 +243,7 @@ std::pair<std::string, int> WifiLanMedium::GetServiceAddress(
|
||||
return std::pair<std::string, int>{"", 0};
|
||||
}
|
||||
|
||||
return nsd->GetServiceAddress();
|
||||
return nsd->GetCredentials();
|
||||
} catch (...) {
|
||||
NEARBY_LOGS(ERROR) << "failed to get service address due to "
|
||||
<< GetErrorMessage(std::current_exception());
|
||||
|
||||
@@ -43,7 +43,7 @@ bool WifiLanNsd::StartAcceptingConnections(
|
||||
|
||||
if (ip_addresses_.empty()) {
|
||||
NEARBY_LOGS(WARNING) << "failed to start accepting connection without IP "
|
||||
"addreses configured on computer.";
|
||||
"addresses configured on computer.";
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -121,7 +121,7 @@ bool WifiLanNsd::StartAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (nsd_service_info.GetServiceInfoName().empty()) {
|
||||
if (nsd_service_info.GetServiceName().empty()) {
|
||||
NEARBY_LOGS(ERROR) << "cannot start advertising without service name.";
|
||||
return false;
|
||||
}
|
||||
@@ -131,13 +131,14 @@ bool WifiLanNsd::StartAdvertising(const NsdServiceInfo& nsd_service_info) {
|
||||
std::stoi(stream_socket_listener_.Information().LocalPort().c_str());
|
||||
NsdServiceInfo new_nsd_servcie_info = nsd_service_info;
|
||||
// TODO: need feature enhancement to support multiple network interfaces
|
||||
new_nsd_servcie_info.SetServiceAddress(ip_addresses_[0], port);
|
||||
new_nsd_servcie_info.SetIPAddress(ip_addresses_[0]);
|
||||
new_nsd_servcie_info.SetPort(port);
|
||||
wifi_lan_service_ = WifiLanService(new_nsd_servcie_info);
|
||||
wifi_lan_service_.SetMedium(medium_);
|
||||
|
||||
std::string instance_name = absl::StrFormat(
|
||||
MDNS_INSTANCE_NAME_FORMAT.data(),
|
||||
wifi_lan_service_.GetServiceInfo().GetServiceInfoName(), service_type_);
|
||||
wifi_lan_service_.GetServiceInfo().GetServiceName(), service_type_);
|
||||
|
||||
NEARBY_LOGS(INFO) << "mDNS instance name is " << instance_name;
|
||||
|
||||
@@ -212,8 +213,8 @@ bool WifiLanNsd::StopAdvertising() {
|
||||
// Init DNS service instance
|
||||
std::string instance_name = absl::StrFormat(
|
||||
MDNS_INSTANCE_NAME_FORMAT.data(),
|
||||
wifi_lan_service_.GetServiceInfo().GetServiceInfoName(), service_type_);
|
||||
int port = wifi_lan_service_.GetServiceInfo().GetServiceAddress().second;
|
||||
wifi_lan_service_.GetServiceInfo().GetServiceName(), service_type_);
|
||||
int port = wifi_lan_service_.GetServiceInfo().GetPort();
|
||||
dns_service_instance_name_ =
|
||||
std::make_unique<std::wstring>(string_to_wstring(instance_name));
|
||||
|
||||
@@ -311,14 +312,15 @@ bool WifiLanNsd::StopDiscovery() {
|
||||
return true;
|
||||
}
|
||||
|
||||
std::pair<std::string, int> WifiLanNsd::GetServiceAddress() {
|
||||
std::pair<std::string, int> WifiLanNsd::GetCredentials() {
|
||||
if (!IsAdvertising()) {
|
||||
// no advertising is running
|
||||
NEARBY_LOGS(WARNING) << "no advertising for service id " << service_id_;
|
||||
return std::pair<std::string, int>{"", 0};
|
||||
}
|
||||
|
||||
return wifi_lan_service_.GetServiceInfo().GetServiceAddress();
|
||||
return std::make_pair(wifi_lan_service_.GetServiceInfo().GetIPAddress(),
|
||||
wifi_lan_service_.GetServiceInfo().GetPort());
|
||||
}
|
||||
|
||||
std::vector<std::string> WifiLanNsd::GetIpAddresses() {
|
||||
@@ -361,7 +363,7 @@ NsdServiceInfo WifiLanNsd::GetNsdServiceInformation(
|
||||
<< "no service name information in device information.";
|
||||
return nsd_service_info;
|
||||
}
|
||||
nsd_service_info.SetServiceInfoName(
|
||||
nsd_service_info.SetServiceName(
|
||||
InspectableReader::ReadString(inspectable));
|
||||
|
||||
// IP Address information
|
||||
@@ -387,7 +389,8 @@ NsdServiceInfo WifiLanNsd::GetNsdServiceInformation(
|
||||
}
|
||||
|
||||
int port = InspectableReader::ReadUint16(inspectable);
|
||||
nsd_service_info.SetServiceAddress(ip_address, port);
|
||||
nsd_service_info.SetIPAddress(ip_address);
|
||||
nsd_service_info.SetPort(port);
|
||||
|
||||
// read text record
|
||||
inspectable = properties.TryLookup(L"System.Devices.Dnssd.TextAttributes");
|
||||
@@ -418,7 +421,7 @@ fire_and_forget WifiLanNsd::Watcher_DeviceAdded(DeviceWatcher sender,
|
||||
DeviceInformation deviceInfo) {
|
||||
NEARBY_LOGS(INFO) << "device added for service " << service_id_;
|
||||
|
||||
// need to read IP address and port informaiton from deviceInfo
|
||||
// need to read IP address and port information from deviceInfo
|
||||
NsdServiceInfo nsd_service_info =
|
||||
GetNsdServiceInformation(deviceInfo.Properties());
|
||||
|
||||
@@ -447,7 +450,7 @@ fire_and_forget WifiLanNsd::Watcher_DeviceUpdated(
|
||||
fire_and_forget WifiLanNsd::Watcher_DeviceRemoved(
|
||||
DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) {
|
||||
NEARBY_LOGS(INFO) << "device removed for service " << service_id_;
|
||||
// need to read IP address and port informaiton from deviceInfo
|
||||
// need to read IP address and port information from deviceInfo
|
||||
NsdServiceInfo nsd_service_info =
|
||||
GetNsdServiceInformation(deviceInfoUpdate.Properties());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user