quickIcmpScanAsync method

Future<List<Host>> quickIcmpScanAsync(
  1. String subnet, {
  2. int firstIP = 1,
  3. int lastIP = 255,
  4. Duration timeout = const Duration(seconds: 1),
})

Discovers network devices in the given subnet.

Works the same as quickIcmpScanSync, but uses isolate to avoid blocking the main thread.

Implementation

Future<List<Host>> quickIcmpScanAsync(
  String subnet, {
  int firstIP = 1,
  int lastIP = 255,
  Duration timeout = const Duration(seconds: 1),
}) {
  final completer = Completer<List<Host>>();
  final receivePort = ReceivePort();
  final isolateArgs = [
    subnet,
    firstIP,
    lastIP,
    timeout.inSeconds,
    receivePort.sendPort,
  ];

  final hosts = <Host>[];

  receivePort.listen((msg) {
    if (msg is List) {
      final address = msg.elementAt(0) as String;
      final pingTime = msg.elementAtOrNull(1) as int?;

      final host = Host(
        internetAddress: InternetAddress(address),
        pingTime: pingTime != null ? Duration(microseconds: pingTime) : null,
      );

      hosts.add(host);
    } else if (msg == 'Complete') {
      completer.complete(hosts);
    }
  });

  Isolate.spawn(
    _quickIcmpScan,
    isolateArgs,
    debugName: 'QuickScanThread',
  );

  return completer.future;
}