【问题标题】:How to speed up packet processing?如何加快数据包处理?
【发布时间】:2013-01-09 13:05:21
【问题描述】:

我正在使用 pcap.net 库在 C#.net 中开发通话录音应用程序。对于数据包捕获,我使用的是 Wireshark 的Dumpcap.exe。数据包文件在 5 秒内创建。要读取每个数据包文件,我所做的是

   OfflinePacketDevice selectedDevice = new OfflinePacketDevice(filename);
            using (PacketCommunicator communicator =
           selectedDevice.Open(65536,                                  // portion of the packet to capture
                // 65536 guarantees that the whole packet will be captured on all the link layers
                               PacketDeviceOpenAttributes.Promiscuous, // promiscuous mode
                               0))                                  // read timeout
            {

                communicator.ReceivePackets(0, DispatcherHandler);

DispatcherHandler 方法中,我正在处理每个数据包。 DispatcherHandler 调用每个文件需要 0 秒。

我在以 same 方法处理 RTP 数据包数据包时出现延迟..

为了识别 rtp 数据包,我使用了 有序字典,键为 ipadrress+portnumber。所以我需要在每个rtp数据包到来时检查这个键是否存在于字典中。此任务在处理每个转储文件时变慢。

if (objPortIPDict.Contains(ip.Source.ToString().Replace(".", "") + port))
{
 // here i write the rtp payload to a file
}

【问题讨论】:

    标签: c# dump


    【解决方案1】:

    我读到了一些奇怪的东西:

    1) 为什么在Dictionary 中使用Contains

    objPortIPDict.Contains(ip.Source.ToString().Replace(".", "") + port)
    

    如果objPortIPDict 是字典,则使用ContainsKey

    2) 从第一个派生。如果这是一个Dictionary,它的ContainsKeyO(1) 的执行时间,所以不受字典本身数据量的影响。

    是的,它可能会受到影响,如果数据量变得如此很大,整个应用程序会变慢,但就当前应用程序状态域而言,选择时间将始终保持不变。

    【讨论】:

    • 没错。这是 O(1),因为 Dictionary 使用哈希码,当然计算为 O(1)。如果字典变得如此满以至于有很多哈希码冲突,它将变得比 O(1) 慢很多 - 但在此之前你可能会耗尽内存(至少对于 32 位程序)。
    • objPortIPDict 是 OrderedDictionary。因为插入的键不是数字,它的格式类似于 "10.10.10.50,4000"。搜索key会变慢吗?
    • 如果你使用 ContainsKey,不会。
    • Contains 确定 System.Collections.Specialized.OrderedDictionary 集合是否包含特定键。
    猜你喜欢
    • 2015-12-31
    • 1970-01-01
    • 1970-01-01
    • 2014-02-19
    • 1970-01-01
    • 1970-01-01
    • 2010-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多