/// <summary> /// Try connecting to <paramref name="ep"/> and invokes <code>OnResult</code> event if success. /// </summary> protected async void Scan(IPEndPoint ep) { try { using (Socket client = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp)) { client.BeginConnect(ep, null, null); await Task.Delay(Timeout, TokenSource.Token).ConfigureAwait(false); if (client.Connected) { OnResult?.BeginInvoke(this, ep, null, null); } } } catch { } finally { SemaphoreParallelism.Release(); } }
/// <summary> /// Scanning loop. /// </summary> protected async void Loop() { while (!IsStopped) { try { await SemaphoreScanList.WaitAsync(TokenSource.Token).ConfigureAwait(false); IPEndPoint ep; lock (ScanList) { ep = ScanList.First.Value; ScanList.RemoveFirst(); } await SemaphoreParallelism.WaitAsync(TokenSource.Token).ConfigureAwait(false); Scan(ep); } catch { } } }