lookup<T extends ResourceRecord> method

Stream<T> lookup<T extends ResourceRecord>(
  1. ResourceRecordQuery query, {
  2. Duration timeout = const Duration(seconds: 5),
})

Lookup a ResourceRecord, potentially from the cache.

The type parameter must be a valid ResourceRecordType. The fullyQualifiedName parameter is the name of the service to lookup, and must not be null. The timeout parameter specifies how long the internal cache should hold on to the record. The multicast parameter specifies whether the query should be sent as unicast (QU) or multicast (QM).

Some publishers have been observed to not respond to unicast requests properly, so the default is true.

Implementation

Stream<T> lookup<T extends ResourceRecord>(
  ResourceRecordQuery query, {
  Duration timeout = const Duration(seconds: 5),
}) {
  final int? selectedMDnsPort = _mDnsPort;
  if (!_started || selectedMDnsPort == null) {
    throw StateError('mDNS client must be started before calling lookup.');
  }
  // Look for entries in the cache.
  final List<T> cached = <T>[];
  _cache.lookup<T>(
      query.fullyQualifiedName, query.resourceRecordType, cached);
  if (cached.isNotEmpty) {
    final StreamController<T> controller = StreamController<T>();
    cached.forEach(controller.add);
    controller.close();
    return controller.stream;
  }

  // Add the pending request before sending the query.
  final Stream<T> results = _resolver.addPendingRequest<T>(
      query.resourceRecordType, query.fullyQualifiedName, timeout);

  // Send the request on all interfaces.
  final List<int> packet = query.encode();
  for (final RawDatagramSocket socket in _sockets) {
    socket.send(packet, _mDnsAddress!, selectedMDnsPort);
  }
  return results;
}