Skip to content

Commit

Permalink
fix issue #22 for Windows and Android (#55)
Browse files Browse the repository at this point in the history
  • Loading branch information
jnstahl authored Feb 2, 2024
1 parent 58d701a commit d3ca82b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
3 changes: 2 additions & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ android {
namespace 'com.nimroddayan.flutternsd'
}

compileSdkVersion 33

compileSdkVersion 34

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -241,10 +243,17 @@ private fun NsdServiceInfo?.toMap(): Map<String, Any?> {
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
Expand Down
10 changes: 8 additions & 2 deletions lib/flutter_nsd.dart
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,12 @@ class FlutterNsd {
final int port = call.arguments['port'];
final String name = call.arguments['name'];
final Map<String, Uint8List> txt = Map.from(call.arguments['txt']);
var nsdServiceInfo = NsdServiceInfo(hostname, port, name, txt);
List<String>? hostAddresses;
List<Object?>? 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;
}

Expand All @@ -104,11 +109,12 @@ class FlutterNsd {
/// Info class for holding discovered service
class NsdServiceInfo {
final String? hostname;
final List<String>? hostAddresses;
final int? port;
final String? name;
final Map<String, Uint8List>? 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
Expand Down
4 changes: 4 additions & 0 deletions windows/flutter_nsd_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ namespace {
std::string servicename;
std::string ipv4address;
std::string ipv6address;
flutter::EncodableList addressList;
int port;
std::map<flutter::EncodableValue, flutter::EncodableValue> txt;
};
Expand Down Expand Up @@ -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)}
Expand Down Expand Up @@ -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);

}
Expand All @@ -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)) {
Expand Down
4 changes: 2 additions & 2 deletions windows/mdns_impl.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@
#include <iphlpapi.h>
#define sleep(x) Sleep(x * 1000)

#if (_DEBUG == 0)
//#if (_DEBUG == 0)
#define printf
#endif
//#endif

#include "mdns.h"

Expand Down

0 comments on commit d3ca82b

Please sign in to comment.