From d3ca82bd861904e16aa0ee9dcb0a03b0439a68a2 Mon Sep 17 00:00:00 2001 From: Johannes Stahl Date: Fri, 2 Feb 2024 10:39:14 -0800 Subject: [PATCH] fix issue #22 for Windows and Android (#55) --- android/build.gradle | 3 ++- .../com/nimroddayan/flutternsd/FlutterNsdPlugin.kt | 9 +++++++++ lib/flutter_nsd.dart | 10 ++++++++-- windows/flutter_nsd_plugin.cpp | 4 ++++ windows/mdns_impl.c | 4 ++-- 5 files changed, 25 insertions(+), 5 deletions(-) diff --git a/android/build.gradle b/android/build.gradle index 9ca75da..fe7874b 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -47,7 +47,8 @@ android { namespace 'com.nimroddayan.flutternsd' } - compileSdkVersion 33 + + compileSdkVersion 34 sourceSets { main.java.srcDirs += 'src/main/kotlin' diff --git a/android/src/main/kotlin/com/nimroddayan/flutternsd/FlutterNsdPlugin.kt b/android/src/main/kotlin/com/nimroddayan/flutternsd/FlutterNsdPlugin.kt index 8a7c889..3e05682 100644 --- a/android/src/main/kotlin/com/nimroddayan/flutternsd/FlutterNsdPlugin.kt +++ b/android/src/main/kotlin/com/nimroddayan/flutternsd/FlutterNsdPlugin.kt @@ -19,6 +19,8 @@ package com.nimroddayan.flutternsd import android.net.nsd.NsdManager import android.net.nsd.NsdServiceInfo +import android.os.Build +import android.os.Build.VERSION_CODES import android.os.Handler import android.os.Looper import androidx.annotation.NonNull @@ -241,10 +243,17 @@ private fun NsdServiceInfo?.toMap(): Map { val port = this?.port val name = this?.serviceName val txt = this?.attributes + val hostAddresses = if(Build.VERSION.SDK_INT > VERSION_CODES.UPSIDE_DOWN_CAKE) { + this?.hostAddresses?.map { it.hostAddress } + } else { + listOf(this?.host?.hostAddress) + } + Timber.v("Resolved service: $name-$hostname:$port $txt") return mapOf( "hostname" to hostname, + "hostAddresses" to hostAddresses, "port" to port, "name" to name, "txt" to txt diff --git a/lib/flutter_nsd.dart b/lib/flutter_nsd.dart index 20ff19d..42e2ac9 100644 --- a/lib/flutter_nsd.dart +++ b/lib/flutter_nsd.dart @@ -91,7 +91,12 @@ class FlutterNsd { final int port = call.arguments['port']; final String name = call.arguments['name']; final Map txt = Map.from(call.arguments['txt']); - var nsdServiceInfo = NsdServiceInfo(hostname, port, name, txt); + List? hostAddresses; + List? rawAddresses = call.arguments['hostAddresses']; + if (rawAddresses != null) { + hostAddresses = rawAddresses.where((e) => e != null).map((e) => e.toString()).toList(); + } + var nsdServiceInfo = NsdServiceInfo(hostname, port, name, txt, hostAddresses: hostAddresses); return nsdServiceInfo; } @@ -104,11 +109,12 @@ class FlutterNsd { /// Info class for holding discovered service class NsdServiceInfo { final String? hostname; + final List? hostAddresses; final int? port; final String? name; final Map? txt; - NsdServiceInfo(this.hostname, this.port, this.name, this.txt); + NsdServiceInfo(this.hostname, this.port, this.name, this.txt, {this.hostAddresses}); } /// List of possible error codes of NsdError diff --git a/windows/flutter_nsd_plugin.cpp b/windows/flutter_nsd_plugin.cpp index 9d837d8..a6b36a4 100644 --- a/windows/flutter_nsd_plugin.cpp +++ b/windows/flutter_nsd_plugin.cpp @@ -37,6 +37,7 @@ namespace { std::string servicename; std::string ipv4address; std::string ipv6address; + flutter::EncodableList addressList; int port; std::map txt; }; @@ -130,6 +131,7 @@ namespace { {flutter::EncodableValue("hostname"), flutter::EncodableValue(packet.hostname)}, {flutter::EncodableValue("ipv4address"), flutter::EncodableValue(packet.ipv4address)}, {flutter::EncodableValue("ipv6address"), flutter::EncodableValue(packet.ipv6address)}, + {flutter::EncodableValue("hostAddresses"), packet.addressList}, {flutter::EncodableValue("port"), flutter::EncodableValue(packet.port)}, {flutter::EncodableValue("name"), flutter::EncodableValue(name)}, {flutter::EncodableValue("txt"), flutter::EncodableValue(packet.txt)} @@ -179,6 +181,7 @@ namespace { MdnsResult& packet = packets[base]; packet.dnsname = _hostname; packet.ipv4address = _address; + packet.addressList.push_back(flutter::EncodableValue(_address)); process(base, last); } @@ -188,6 +191,7 @@ namespace { MdnsResult& packet = packets[base]; packet.dnsname = _hostname; packet.ipv6address = _address; + packet.addressList.push_back(flutter::EncodableValue(_address)); process(base, last); } void MdnsRequest::callbackTXT(const void* base, boolean last, STRING_ARG_DECL(key), STRING_ARG_DECL(value)) { diff --git a/windows/mdns_impl.c b/windows/mdns_impl.c index 8a3648f..e2f8406 100644 --- a/windows/mdns_impl.c +++ b/windows/mdns_impl.c @@ -7,9 +7,9 @@ #include #define sleep(x) Sleep(x * 1000) -#if (_DEBUG == 0) +//#if (_DEBUG == 0) #define printf -#endif +//#endif #include "mdns.h"