public static async Task TryToGetLanListAsync(ItemAddCallback <ServerInformation> foundCallback, IProgress <AsyncProgressEntry> progress = null, CancellationToken cancellation = default(CancellationToken)) { var holder = progress == null ? null : new Progress(); DispatcherTimer timer = null; try { if (holder != null) { timer = new DispatcherTimer(DispatcherPriority.Background, Application.Current?.Dispatcher ?? Dispatcher.CurrentDispatcher) { Interval = TimeSpan.FromSeconds(0.1), IsEnabled = true }; timer.Tick += (sender, args) => { progress.Report(new AsyncProgressEntry($"Scanned {holder.Current} of {holder.Total}", holder.Current, holder.Total)); }; } await Task.Run(() => { TryToGetLanList(foundCallback, SettingsHolder.Online.LanPortsEnumeration.ToPortsDiapason(), holder, cancellation); }, cancellation); } finally { if (holder != null && timer != null) { timer.IsEnabled = false; } } }
public async Task <bool> LoadAsync(ItemAddCallback <ServerInformation> callback, IProgress <AsyncProgressEntry> progress, CancellationToken cancellation) { Logging.Here(); await KunosApiProvider.TryToGetLanListAsync(callback, progress, cancellation); return(!cancellation.IsCancellationRequested); }
private static void TryToGetLanList(ItemAddCallback <ServerInformation> foundCallback, IEnumerable <int> ports, [CanBeNull] Progress progress, CancellationToken cancellation) { var addresses = GetBroadcastAddresses().ToList(); // ReSharper disable PossibleMultipleEnumeration if (progress != null) { progress.Total = ports.Count() * addresses.Count; progress.Current = 0; } var entries = addresses.SelectMany(x => ports.Select(y => new { BroadcastIp = x, Port = y })); // ReSharper enable PossibleMultipleEnumeration try { Parallel.ForEach(entries, new ParallelOptions { CancellationToken = cancellation, MaxDegreeOfParallelism = (Environment.ProcessorCount - 1).Clamp(1, 4) }, (entry, ipLoopState) => { cancellation.ThrowIfCancellationRequested(); var found = BroadcastPing(entry.BroadcastIp, entry.Port); if (found != null) { try { var information = TryToGetInformationDirect(found.Ip, found.Port); if (information == null) { return; } information.IsLan = true; ActionExtension.InvokeInMainThreadAsync(() => foundCallback(information)); } catch (Exception e) { Logging.Warning(e); } } if (progress != null) { Interlocked.Increment(ref progress.Current); } }); } catch (Exception e) when(e.IsCancelled()) { } }
public static void TryToGetLanList(ItemAddCallback <ServerInformation> foundCallback) { TryToGetLanList(foundCallback, SettingsHolder.Online.LanPortsEnumeration.ToPortsDiapason(), null, default(CancellationToken)); }
public static void TryToGetLanList(ItemAddCallback <ServerInformation> foundCallback, IEnumerable <int> ports) { TryToGetLanList(foundCallback, ports, null, default(CancellationToken)); }
public static void TryToGetLanList(ItemAddCallback <ServerInformation> foundCallback) { TryToGetLanList(foundCallback, PortsDiapason.Create(SettingsHolder.Online.LanPortsEnumeration), null, default); }