Skip to content

Commit

Permalink
Start mdns after setting listeners
Browse files Browse the repository at this point in the history
  • Loading branch information
njooma committed Dec 11, 2024
1 parent 6cc4426 commit f33f5d0
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions lib/src/rpc/dial.dart
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ Future<ClientChannelBase> 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;
Expand All @@ -175,26 +176,36 @@ Future<String> _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();
Expand Down

0 comments on commit f33f5d0

Please sign in to comment.