Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix issue #22 for Windows and Android #55

Merged
merged 2 commits into from
Feb 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 10 additions & 5 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -19,30 +19,35 @@ group 'com.nimroddayan.flutternsd'
version '1.0-SNAPSHOT'

buildscript {
ext.kotlin_version = '1.4.0'
ext.kotlin_version = '1.7.10'
repositories {
google()
jcenter()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:4.0.1'
classpath 'com.android.tools.build:gradle:7.3.1'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}

rootProject.allprojects {
repositories {
google()
jcenter()
mavenCentral()
}
}

apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 29
// Conditional for compatibility with AGP <4.2.
if (project.android.hasProperty("namespace")) {
namespace 'com.nimroddayan.flutternsd'
}

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 @@ -109,6 +110,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 @@ -158,6 +160,7 @@ namespace {
MdnsResult& packet = packets[base];
packet.dnsname = _hostname;
packet.ipv4address = _address;
packet.addressList.push_back(flutter::EncodableValue(_address));
process(base, last);

}
Expand All @@ -167,6 +170,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