⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dddialog.cpp

📁 Network Stats using PSSDK
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    lvItem.iItem = ItemIndex;
    lvItem.pszText  = "0";
    lvItem.iSubItem = 1;            // count
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 2;            // size
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 3;            // %
    CountersList.SetItem(&lvItem);
    lvItem.pszText  = "";
    lvItem.iSubItem = 4;            // count speed
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 5;            // size speed
    CountersList.SetItem(&lvItem);

    // UDP
    lvItem.iItem += 1;
    lvItem.iSubItem = 0;
    lvItem.pszText  = "     UDP";
    ItemIndex = CountersList.InsertItem(&lvItem);
    lvItem.iItem = ItemIndex;
    lvItem.pszText  = "0";
    lvItem.iSubItem = 1;            // count
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 2;            // size
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 3;            // %
    CountersList.SetItem(&lvItem);
    lvItem.pszText  = "";
    lvItem.iSubItem = 4;            // count speed
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 5;            // size speed
    CountersList.SetItem(&lvItem);

    // ICMP
    lvItem.iItem += 1;
    lvItem.iSubItem = 0;
    lvItem.pszText  = "     ICMP";
    ItemIndex = CountersList.InsertItem(&lvItem);
    lvItem.iItem = ItemIndex;
    lvItem.pszText  = "0";
    lvItem.iSubItem = 1;            // count
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 2;            // size
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 3;            // %
    CountersList.SetItem(&lvItem);
    lvItem.pszText  = "";
    lvItem.iSubItem = 4;            // count speed
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 5;            // size speed
    CountersList.SetItem(&lvItem);

    // ARP
    lvItem.iItem += 1;
    lvItem.iSubItem = 0;
    lvItem.pszText  = "ARP";
    ItemIndex = CountersList.InsertItem(&lvItem);
    lvItem.iItem = ItemIndex;
    lvItem.pszText  = "0";
    lvItem.iSubItem = 1;            // count
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 2;            // size
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 3;            // %
    CountersList.SetItem(&lvItem);
    lvItem.pszText  = "";
    lvItem.iSubItem = 4;            // count speed
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 5;            // size speed
    CountersList.SetItem(&lvItem);

    // Other
    lvItem.iItem += 1;
    lvItem.iSubItem = 0;
    lvItem.pszText  = "Other";
    ItemIndex = CountersList.InsertItem(&lvItem);
    lvItem.iItem = ItemIndex;
    lvItem.pszText  = "0";
    lvItem.iSubItem = 1;            // count
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 2;            // size
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 3;            // %
    CountersList.SetItem(&lvItem);
    lvItem.pszText  = "";
    lvItem.iSubItem = 4;            // count speed
    CountersList.SetItem(&lvItem);
    lvItem.iSubItem = 5;            // size speed
    CountersList.SetItem(&lvItem);

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Clear counters
    ClearCounters();

    UpdateData(false);

    DWORD ExtendedStyle = CountersList.GetExtendedStyle();
    ExtendedStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES;
    CountersList.SetExtendedStyle(ExtendedStyle);

    return TRUE;  // return TRUE  unless you set the focus to a control
}

//----------------------------------------------------------------------------------
// OnConfigChange - a handler for the OnConfigChange event of the HNPSManager object
//----------------------------------------------------------------------------------
void CDDDialog::OnConfigChange(HANDLE hCfg, DWORD ChangeType) 
{
    CHNAdapterConfig AdpCfg;
    AdpCfg = hCfg;

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Handles the event on adding new configurations 
    // found by the manager
    if(ChangeType == acCreate)
    {
        //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // Adding to the list only adapters of the "Ethernet" type
        if(AdpCfg.Get_AdapterType() == atEthernet)
        {                 
            int ItemIndex = cbNetCardList.AddString(AdpCfg.Get_AdapterDescription());
            cbNetCardList.SetItemData(ItemIndex,(DWORD_PTR)hCfg);
        }
    }
}

//----------------------------------------------------------------------------------
// OnPacketReceive - a handler for the OnPacketReceive event of the HNQueue object
//----------------------------------------------------------------------------------
void CDDDialog::OnPacketReceive(DWORD_PTR ThParam, HANDLE hPacket, LPVOID pPacketData, DWORD IncPacketSize) 
{
    CHNPacket HNPacket;

    HNPacket = hPacket;   
    // increase capture counters
    dwCapTotal_Count = dwCapTotal_Count + 1;
    dwCapTotal_Bytes = dwCapTotal_Bytes + DWORD(HNPacket.Get_PacketSize());

    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Process new packet.
    ProcessPacket(hPacket);

    //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Return the released packet to the list of free packets.
    // Add it to the end of the free packet list.
    HNQueue.Return_FreeItem(hPacket);
}

//-----------------------------------------------------------------------------
// OnAboutBtn - the "About" button is pressed
//-----------------------------------------------------------------------------
void CDDDialog::OnAboutBtn() 
{
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Show the "About library ..." dialog
    HNPSManager.About();
}

//-----------------------------------------------------------------------------
// OnManagerInitBtn - the manager initialization button is pressed
//-----------------------------------------------------------------------------
void CDDDialog::OnManagerInitBtn() 
{
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Clear the adapter list in ComboBox
    cbNetCardList.ResetContent();

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Initialize the PSSDK library
    long Res = HNPSManager.Initialize();
    if(Res != HNERR_OK) 
    { 
        HNGetErrorBox(Res);
    }

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Show the first adapter in ComboBox
    cbNetCardList.SetCurSel(0);

    MgrInitBtn.EnableWindow(false);
}

//-----------------------------------------------------------------------------
// OnOpenBtn - the adapter open button is pressed
//-----------------------------------------------------------------------------
void CDDDialog::OnOpenBtn() 
{
    DWORD Res;
    CHNAdapterConfig AdpCfg;

    UpdateData();

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Define the configuration selected in ComboBox by the user and receive 
    // its handler
    int ItemIndex = cbNetCardList.GetCurSel();
    if(ItemIndex == CB_ERR) return;

    HANDLE hCfg = (HANDLE)cbNetCardList.GetItemData(ItemIndex);
    AdpCfg = hCfg;

    HNQueue.Set_ItemsCount(atoi(PktPoolCountEdit));
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //See function OnInitDialog
    //HNQueue.Set_MaxPacketSize(AdpCfg.Get_MaxPacketSize());

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Open the network adapter
    if (HNQueue.AllocItems() != HNERR_OK)
    {
        Stop();
        MessageBox("Error allocate queue's packets","Error");
        return;
    }

    PoolMem_Label = "";
    PoolMem_Label =  PoolMem_Label + "= " + GetRate((DWORD)HNQueue.Get_AllocatedSize()) + " of memory";
    UpdateData(false);

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Set the selected configuration to the adapter
    HNAdapter.Set_Config(hCfg);

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Open the network adapter
    Res = HNAdapter.OpenAdapter();
    if(Res != HNERR_OK) 
    {
        Stop();
        HNGetErrorBox(Res);
        return; 
    }

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Run the packet handling thread
    if (HNQueue.Start() != HNERR_OK)
    {
        Stop();
        MessageBox("Error create queue's thread","Error");
        return;
    }

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Run the statistics timer, firing one time per second
    SetTimer(HN_TIMER_ID,1000,NULL);

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Set the MAC filter for receiving all packets.
    // Packets actually start getting to the OnPacketReceive event handler 
    // only after setting the MAC filter to any value different from "0".
    HNAdapter.Set_MacFilter(mfAll);

    OpenBtn.EnableWindow(false);
    CloseBtn.EnableWindow(true);
    WndPktCount.EnableWindow(false);
}

//---------------------------------------------------------------------------
// OnCloseBtn - then adapter close button is pressed
//---------------------------------------------------------------------------
void CDDDialog::OnCloseBtn()
{
    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Stop the packet handling thread
    Stop();
}

//---------------------------------------------------------------------------
// Stop - stop receiving packets
//---------------------------------------------------------------------------
void CDDDialog::Stop() 
{
    if (HNAdapter.IsOpened())
    {
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // Set the MAC filter to "0" - now we do not receive packets
        HNAdapter.Set_MacFilter(0);

        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // Stop the timer statistics
        KillTimer(HN_TIMER_ID);   

        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // Refresh the statistics synchronously for the last time
        GetStat(false);

        ShowCounters();

        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // Close the adapter
        HNAdapter.CloseAdapter();
    }

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    //Queue thread stopped;
    if (HNQueue.IsStarted())
        HNQueue.Stop();

    if (HNQueue.Get_AllocatedSize() > 0)
        HNQueue.FreeItems();

    PoolMem_Label = "";
    PoolMem_Label =  PoolMem_Label + "= " + GetRate((DWORD)HNQueue.Get_AllocatedSize()) + " of memory";
    UpdateData(false);

    OpenBtn.EnableWindow(true);
    CloseBtn.EnableWindow(false);
    WndPktCount.EnableWindow(true);
}

//---------------------------------------------------------------------------
// OnTimer - WM_TIMER message handler
//---------------------------------------------------------------------------
void CDDDialog::OnTimer(UINT_PTR nIDEvent) 
{
    CDialog::OnTimer(nIDEvent);

    if(nIDEvent != HN_TIMER_ID) return;

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Refresh the statistics asynchronously
    GetStat(true);

    //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
    // Show counters
    ShowCounters();
}

//---------------------------------------------------------------------------
// GetStat - receive the adapter statistics synchronously and asynchronously 
//---------------------------------------------------------------------------
void CDDDialog::GetStat(BOOL IsAsync) 
{
    ULARGE_INTEGER Stat;

    if(IsAsync)
    {
        //++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
        // Refresh the adapter statistics asynchronously 
        HNAdapter.Get_AsyncStatistics(stNdisTranWithoutErr);
        HNAdapter.Get_AsyncStatistics(stNdisRecvWithoutErr);
        HNAdapter.Get_AsyncStatistics(stNdisTranWithErr);
        HNAdapter.Get_AsyncStatistics(stNdisRecvWithErr);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -