From f33f5d084991d407f5a290548c1a135ff6bb52f1 Mon Sep 17 00:00:00 2001 From: Naveed Jooma Date: Wed, 11 Dec 2024 17:41:42 -0500 Subject: [PATCH 1/2] Start mdns after setting listeners --- lib/src/rpc/dial.dart | 33 ++++++++++++++++++++++----------- 1 file changed, 22 insertions(+), 11 deletions(-) diff --git a/lib/src/rpc/dial.dart b/lib/src/rpc/dial.dart index 03c2a4372db..2d095841fcc 100644 --- a/lib/src/rpc/dial.dart +++ b/lib/src/rpc/dial.dart @@ -151,6 +151,7 @@ Future dial(String address, DialOptions? options, String Func } mdnsSW.stop(); _logger.d('STATS: mDNS discovery took ${mdnsSW.elapsed}'); + throw Error(); } bool disableWebRtc = opts.webRtcOptions?.disable ?? false; @@ -175,26 +176,36 @@ Future _searchMdns(String address) async { const type = '_rpc._tcp'; final discovery = BonsoirDiscovery(type: type); await discovery.ready; - await discovery.start(); - // The duration of timeout was arbitrarily decided. - // 1 second seemed enough to allow the device to scan - // the local network for matches before moving on. - // The balance we are striking here is long enough to - // reliably scan the local network, but short enough to not - // noticeably lengthen the connection flow for the user. - - const timeout = Duration(seconds: 1); - await for (final event in discovery.eventStream!.timeout(timeout)) { + String? localAddress; + discovery.eventStream!.listen((event) { if (event.type == BonsoirDiscoveryEventType.discoveryServiceFound) { unawaited(event.service!.resolve(discovery.serviceResolver)); } else if (event.type == BonsoirDiscoveryEventType.discoveryServiceResolved) { final service = event.service! as ResolvedBonsoirService; if (service.name == targetName && service.host != null) { final host = service.host!.substring(0, service.host!.length - 1); - return ('$host:${service.port}'); + localAddress = '$host:${service.port}'; } } + }); + + await discovery.start(); + final startTime = DateTime.now(); + + // The duration of timeout was arbitrarily decided. + // 2 seconds seemed enough to allow the device to scan + // the local network for matches before moving on. + // The balance we are striking here is long enough to + // reliably scan the local network, but short enough to not + // noticeably lengthen the connection flow for the user. + const timeout = Duration(seconds: 2); + while (DateTime.now().difference(startTime) < timeout) { + await Future.delayed(const Duration(microseconds: 100)); + if (localAddress != null) { + await discovery.stop(); + return localAddress!; + } } await discovery.stop(); From 8f54a641ea475d445acf02ba46eb08d837ba46d8 Mon Sep 17 00:00:00 2001 From: Naveed Jooma Date: Wed, 11 Dec 2024 20:45:27 -0500 Subject: [PATCH 2/2] Remove forced mDNS --- lib/src/rpc/dial.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/src/rpc/dial.dart b/lib/src/rpc/dial.dart index 2d095841fcc..4c25f5b060a 100644 --- a/lib/src/rpc/dial.dart +++ b/lib/src/rpc/dial.dart @@ -151,7 +151,6 @@ Future dial(String address, DialOptions? options, String Func } mdnsSW.stop(); _logger.d('STATS: mDNS discovery took ${mdnsSW.elapsed}'); - throw Error(); } bool disableWebRtc = opts.webRtcOptions?.disable ?? false;