Packet Sniffer SDK DLL Edition

Events :: OnPacketsReceive

 Previous Next

Occurs immediately after a packet has been received by the network adapter.

Syntax:

void __stdcall OnPacketsReceive(DWORD_PTR Param, DWORD_PTR ThParam, PHANDLE hPackets, DWORD dwCount);

Parameters:

Param

[in] OnPacketsReceive event handler parameter. May be set on the QueSetOnPacketsRecv function call.

ThParam

[in] User defined thread parameter (see HNFN_THREADBEGIN).

hPackets

[in] Array of HNPacket objects handles.

dwCount

[in] The size of the array of HNPacket (see hPackets parameter). Means the quantity of objects which hPackets parameter contains.

Description:

Packets processing code in the OnPacketsReceive event handler must be as fast as possible because if the packets processing performance is low, and the network traffic is high, you run the risk of losing some packets.

Please use statistics to make sure that your application does not lose any packets.

For example, when you use a BPF filter, and if your application is in time to process all packets, then the value returned by AdpGetProcessCount is equal to the AdpGetAcceptCount one.

If you do not use a filter, you should compare AdpGetProcessCount and AdpGetRecvCount returned values. In this way you can check, whether your application is able to process the whole network traffic received by the Packet Sniffer SDK internal driver from the system.

When the network traffic is very high, it would be very appropriate to create a packets processing queue in your application, using a separate thread, and just enqueue the received packets in the OnPacketsReceive event handler, and to notify this queue about the receiving of a new packet (see HNQueue).

Also, you should take into account that OnPacketsReceive event handler is called in the context of a thread, created by Packet Sniffer SDK library.

We strongly recommend you to do nothing related to the data displaying in this event handler. If the traffic is high, such actions may lead to the hanging of the application and packets loss.

OnPacketsReceive event handler example:

//---------------------------------------------------------------------------
// Event handler OnPacketsReceive of the HNQueue object
//---------------------------------------------------------------------------
void __sdtcall OnPacketsReceive(DWORD_PTR Param, DWORD_PTR ThParam, PHANDLE hPackets, DWORD dwCount)
{                                                        
   for (DWORD i = 0; i < dwCount; i++)
   {
     if (PktGetMediumType(hPackets[i]) != atEthernet) continue;
                       
     BYTE* PacketBuffer = PktGetPacketData(hPackets[i]);
     ETHERNET_HEADER *pEth  = (ETHERNET_HEADER *)PacketBuffer;
     unsigned short EthType = htons(pEth->EthType);

     ...
   } 

   QueReturnFreeItems(hQue,hPackets,dwCount);
   //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
//---------------------------------------------------------------------------
// Event handler OnPacketsReceive of the HNQueue object
//---------------------------------------------------------------------------
procedure OnPacketsReceive(Param: Pointer; ThParam: Pointer; hPackets: Pointer; dwCount: Cardinal); stdcall;
 var
  pEth:    PETHERNET_HEADER;
  EthType: Word;       
  PacketBuffer: PBYTE; 
begin                                    
   for ... do 
   begin
     if (PktGetMediumType(hPackets^.[i]) <> atEthernet) then continue;
              
     PacketBuffer = PBYTE(PktGetPacketData(hPackets^.[i]));
     pEth  := PETHERNET_HEADER(PacketBuffer);
     EthType := htons(pEth^.EthType);
     ...
   end;
     
   QueReturnFreeItems(hQue,hPackets,dwCount);
   //^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
end;
See also:HNQueue, QueSetReceiveManyAtOnce, QueSetOnPacketRecv, AdpOpenAdapter, AdpGetThreadCount, AdpSetThreadCount, AdpGetProcessCount, AdpGetAcceptCount, AdpGetRecvCount, AdpGetOpenTime, HNPacket