lookup<T extends ResourceRecord> method

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

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);

  final List<int> packet = query.encode();

  if (_mDnsAddress?.type == InternetAddressType.IPv4) {
    // Send and listen on same "ANY" interface
    _incomingIPv4?.send(packet, _mDnsAddress!, selectedMDnsPort);
  } else {
    for (final RawDatagramSocket socket in _ipv6InterfaceSockets) {
      socket.send(packet, _mDnsAddress!, selectedMDnsPort);
    }
  }

  return results;
}