2

我有一个WPF带有PcapDotNetDLL 的应用程序来测量我的机器Interface Rate

这是Model

public class Interface
{
    public PacketDevice PacketDevice { get { return livePacketDevice; } }
    private DateTime _lastTimestamp;
    private double _bitsPerSecond;
    private double _packetsPerSecond;
    private DateTime _lastTimestamp;
    private static List<Interface> _machineInterfaces; // list of all machine interfaces

    public void Start(Interface inf)
    {
        OpenAdapterForStatistics(inf.PacketDevice);
    }

    public void OpenAdapterForStatistics(PacketDevice selectedOutputDevice)
    {
        if (selectedOutputDevice != null)
        {
            using (PacketCommunicator statCommunicator = selectedOutputDevice.Open(100, PacketDeviceOpenAttributes.Promiscuous, 1000)) //open the output adapter
            {
                try
                {
                    statCommunicator.Mode = PacketCommunicatorMode.Statistics; //put the interface in statstics mode
                    statCommunicator.ReceiveStatistics(0, StatisticsHandler); //start the main loop
                }
                catch (Exception)
                { }
            }
        }
    }

    private void StatisticsHandler(PacketSampleStatistics statistics)
    {
        DateTime currentTimestamp = statistics.Timestamp; //current sample time
        DateTime previousTimestamp = _lastTimestamp; //previous sample time
        _lastTimestamp = currentTimestamp; //set _lastTimestamp for the next iteration

        if (previousTimestamp == DateTime.MinValue) //if there wasn't a previous sample than skip this iteration (it's the first iteration)
            return;

        double delayInSeconds = (currentTimestamp - previousTimestamp).TotalSeconds; //calculate the delay from the last sample
        _bitsPerSecond = statistics.AcceptedBytes * 8 / delayInSeconds; //calculate bits per second
        _packetsPerSecond = statistics.AcceptedPackets / delayInSeconds; //calculate packets per second

        if (NewPointEventHandler != null)
            NewPointEventHandler(_bitsPerSecond);
        double value = _packetsPerSecond;
    }

如您所见Start,方法开始测量Interface速率并将值放入 2 个字段中:

_bitsPerSecond_packetsPerSecond

所以在应用程序启动后我有这个字段:

List<Interface> _machineInterfaces; 

那读取了我所有的机器接口。

之后我开始我的Start方法:

    private void StartStatistics()
    {
        int index = listview.SelectedIndex; // select the selected interface from my `ListView` list.
        Interface inf = new Interface();
        ThreadStart tStarter = delegate
        {              
            inf.Start(Interface.MachineInterfaces[index]); // send the selected interface
        };
        Thread thread = new Thread(tStarter);
        thread.IsBackground = true;
        thread.Start();

        statisticsTimer.Start(); // start my timer
    }
  • 好的,现在这是我的问题:

这是我的Timer Tick Event

public RadObservableCollection<double> mbitPerSecondValue { get; private set; }

如果我BitsPerSecond Class Interface member定义为常规而不是Static它的值始终为零:

    private void statisticsTimer_Tick(object sender, EventArgs e)
    {
        int index = listview.SelectedIndex;

        double bps = Interface.MachineInterfaces[index].BitsPerSecond; // always zero !
        mbitPerSecondValue.Add(bps);
    }

如果BitsPerSecond定义为静态一切都很好:

    private void statisticsTimer_Tick(object sender, EventArgs e)
    {
        int index = listview.SelectedIndex;
        double bps = Interface.BitsPerSecond;
        mbitPerSecondValue.Add(bps);
    }

所以我的问题是为什么?

编辑

目前我改变了我的功能:

private void StartStatistics()
        {
            int index = lvAdapters.SelectedIndex;
            Interface inf = new Interface();
            ThreadStart tStarter = delegate
            {
                foreach (Interface item in Interface.MachineInterfaces)
                    item.Start();
            };

            Thread thread = new Thread(tStarter);
            thread.IsBackground = true;
            thread.Start();

            statisticsTimer.Start();
        }

我想要实现的是在我的机器上打开每个接口的统计信息,但是在第一个接口(我有 2 个)中我可以看到流量在变化(BitsPerSecond)但是在第二个接口中它总是为零(我确保生成通过此接口的一些流量,因此它不应该为零)

4

2 回答 2

2

对于第二个问题,尝试Start从不同的线程调用每个接口。我看到的唯一可疑的事情是,它可能statCommunicator.ReceiveStatistics正在阻塞线程并阻止其他接口被启动。

这应该避免这个问题:

private void StartStatistics()
{
    foreach (Interface item in Interface.MachineInterfaces)
    {
        ThreadStart tStarter = delegate
        {
            item.Start();
        };

        Thread thread = new Thread(tStarter);
        thread.IsBackground = true;
        thread.Start();
    }

    statisticsTimer.Start();
}
于 2015-07-02T10:10:40.577 回答
0

好吧,很明显为什么它在定义为时起作用static:所有实例Interface共享相同的属性,所以当你从一个地方增加它的值时,新值会自动在任何地方可用。

但作为常规的非静态属性,您必须确保从之前修改过的同一个实例中读取。而你不是。

首先,您正在创建一个新Interface的(我们称其为接口 A),然后调用它Start,并将您从 中获取的另一个Interface(我们将其称为接口 B)Interface.MachineInterfaces作为参数传递:

private void StartStatistics()
{
    ...
    Interface inf = new Interface();
    ThreadStart tStarter = delegate
    {              
        inf.Start(Interface.MachineInterfaces[index]); // send the selected interface
    };
    ...
}

Start接口 A 的方法中,您订阅了接口 B 的统计信息,但处理程序仍在接口 A 中:

public void Start(Interface inf)
{
    OpenAdapterForStatistics(inf.PacketDevice);
}

public void OpenAdapterForStatistics(PacketDevice selectedOutputDevice)
{
    ...
    statCommunicator.ReceiveStatistics(0, StatisticsHandler); //start the main loop
    ...
}

当调用接口 A 中的处理程序时,它会增加自己的_bitsPerSecond值。不是接口B,而是接口A。

private void StatisticsHandler(PacketSampleStatistics statistics)
{
    ...
    _bitsPerSecond = statistics.AcceptedBytes * 8 / delayInSeconds; //calculate bits per second
    ...
}

但最后,您正在检查BitsPerSecond接口 B 中的值,再次取自Interface.MachineInterfaces!

private void statisticsTimer_Tick(object sender, EventArgs e)
{
    ...
    double bps = Interface.MachineInterfaces[index].BitsPerSecond; // always zero !
    ...
}

-- 建议的解决方案 1 --

你为什么不让它Start使用它自己的实例,所以你不必创建一个新Interface的来使用它?

public void Start()
{
    OpenAdapterForStatistics(this.PacketDevice);
}

这样你就可以这样做:

private void StartStatistics()
{
    int index = listview.SelectedIndex; // select the selected interface from my `ListView` list.
    ThreadStart tStarter = delegate
    {              
        Interface.MachineInterfaces[index].Start(); // send the selected interface
    };
    Thread thread = new Thread(tStarter);
    thread.IsBackground = true;
    thread.Start();

    statisticsTimer.Start(); // start my timer
}

...并且您应该在 Timer Tick 回调中获得所需的输出。

-- 建议的解决方案 2 --

如果您不想Start从内部的原始接口调用Interface.MachineInterfaces,那么您必须将新接口存储在某种字典中,以便稍后访问它以从中获取BitsPerSecond

private Dictionary<Interface, Interface> InterfaceDictionary = new Dictionary<Interface, Interface>();

private void StartStatistics()
{
    int index = listview.SelectedIndex; // select the selected interface from my `ListView` list.
    Interface inf = new Interface();
    ThreadStart tStarter = delegate
    {              
        inf.Start(Interface.MachineInterfaces[index]); // send the selected interface
    };
    Thread thread = new Thread(tStarter);
    thread.IsBackground = true;
    thread.Start();

    statisticsTimer.Start(); // start my timer

    if (InterfaceDictionary.ContainsKey(Interface.MachineInterfaces[index]))
        InterfaceDictionary[Interface.MachineInterfaces[index]] = inf;
    else
        InterfaceDictionary.Add(Interface.MachineInterfaces[index], inf);
}

在您的 Timer Tick 回调中,从关联的接口中获取数据,而不是从以下接口中获取数据Interface.MachineInterfaces

private void statisticsTimer_Tick(object sender, EventArgs e)
{
    int index = listview.SelectedIndex;
    var interface = InterfaceDictionary[Interface.MachineInterfaces[index]];
    double bps = interface.BitsPerSecond;
    mbitPerSecondValue.Add(bps);
}
于 2015-07-01T15:29:06.533 回答