/// <summary> /// Starts processes to capture network data for all available devices /// </summary> protected void StartNetworkDataCapture() { captureResetEvent.Set(); var devices = CaptureDeviceList.Instance; foreach (var device in devices) { if (isAdvancedLogEnabled) { LogProvider.Log.Info(this, $"Found device: {device.Name}, {device.Description}."); } try { device.Open(DeviceMode.Normal); var captureDevice = new CaptureDevice(device) { IsOpened = true }; captureDevices.Push(captureDevice); } catch (Exception e) { LogProvider.Log.Error(this, $"Failed to open device {device.Name}.", e); } } captureDevices.ForEach(captureDevice => Task.Run(() => CaptureData(captureDevice))); }
/// <summary> /// Captures network data of the specified device /// </summary> /// <param name="device">Device to capture</param> protected void CaptureData(CaptureDevice captureDevice) { if (captureResetEvent.IsSet) { captureResetEvent.Reset(); } while (true) { if (cancellationTokenSource.IsCancellationRequested) { try { captureDevice.Device.Close(); captureDevice.IsOpened = false; } catch (Exception e) { LogProvider.Log.Error(this, $"Device {captureDevice.Device.Name} has not been closed.", e); } if (!captureResetEvent.IsSet && captureDevices.All(x => !x.IsOpened)) { captureResetEvent.Set(); } return; } try { var nextPacket = captureDevice.Device.GetNextPacket(); if (nextPacket != null) { ParsePacket(nextPacket); } else { Task.Delay(NoDataDelay).Wait(); } } catch (Exception e) { LogProvider.Log.Error(this, $"Data has not been captured from {captureDevice.Device.Name}.", e); } } }