init: rough companion app stub
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:multicast_dns/multicast_dns.dart';
|
||||
|
||||
import '../../../domain/models/server_config.dart';
|
||||
|
||||
class MdnsDiscovery {
|
||||
static const String serviceType = '_localiserd._tcp';
|
||||
|
||||
MDnsClient? _client;
|
||||
|
||||
Stream<ServerConfig> discover() async* {
|
||||
_client?.stop();
|
||||
final client = MDnsClient();
|
||||
_client = client;
|
||||
await client.start();
|
||||
try {
|
||||
await for (final ptr in client.lookup<PtrResourceRecord>(
|
||||
ResourceRecordQuery.serverPointer(serviceType),
|
||||
)) {
|
||||
await for (final srv in client.lookup<SrvResourceRecord>(
|
||||
ResourceRecordQuery.service(ptr.domainName),
|
||||
)) {
|
||||
await for (final ip in client.lookup<IPAddressResourceRecord>(
|
||||
ResourceRecordQuery.addressIPv4(srv.target),
|
||||
)) {
|
||||
yield ServerConfig(host: ip.address.address, port: srv.port);
|
||||
}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
client.stop();
|
||||
if (_client == client) _client = null;
|
||||
}
|
||||
}
|
||||
|
||||
Future<void> stop() async {
|
||||
_client?.stop();
|
||||
_client = null;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user