LabVIEW and pcap.net

Nov 9, 2011 at 10:12 PM

Just wanted to say that the work being done here is really appreciated.  Pcap.net is a great tool.

I'd really like to be able to call pcap.net from LabVIEW.  LabVIEW is kind of common in the test and test automation circles and I'd really like to be able to use pcap.net for all my traffic generation and reception tools moving forward.

LabVIEW however doesn't support delegates in the raw form - such as you might pass in a method argument.  And as a result I lose the callback for ReceivePackets.
LabVIEW does however support .NET events.  Would it be possible to allow the client to register to the delegate via an event?

Thanks

Coordinator
Nov 11, 2011 at 6:46 PM

Hi ThatGuy,

 

I don't know LabVIEW, but it looks like you can easily create some class that implements events that are being called using delegates.

If you want, you can give some more details on an interface you would like to have and I can think if this should be incorporated in Pcap.Net.

 

I hope this helps,

 

Boaz.

Nov 23, 2011 at 9:08 PM

This is what one of our .Net developers wrote to get me around this issue.  I'm not sure it's something many of your users would benefit from, but I appreciate your willingness to consider incorporating something of this nature. 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using PcapDotNet.Core;
using PcapDotNet.Packets;

namespace PcapDotNet.EventWrapper
{
    /// <summary>
    /// Class used to wrap the ReceivePackets method in order to use Event instead of delegate.
    /// LabVIEW does not support delegate, therefore may use this wrapper to receive packets.
    /// </summary>
    public class EventWrapper
    {
        /// <summary>
        /// The Packet event to subscribe before calling the method
        /// </summary>
        public event HandlePacket HandlePacketWrapper;

        /// <summary>
        /// The Statistics event to subscribe before calling the method
        /// </summary>
        public event HandleStatistics HandleStatisticsWrapper;

        /// <summary>
        /// ReceivePackets method wrapper on top of the PacketCommunicator class using Event (HandlePacketWrapper) instead of callbacks
        /// </summary>
        /// <param name="packetCommunicator">The object on which the packets should be received</param>
        /// <param name="count">See doc of PacketCommunicator.ReceivePackets</param>
        /// <returns>See doc of PacketCommunicator.ReceivePackets</returns>
        public PacketCommunicatorReceiveResult ReceivePacketsViaEvents(PacketCommunicator packetCommunicator, int count)
        {
            return packetCommunicator.ReceivePackets(count, OnHandlePacketWrapper);
        }

        /// <summary>
        /// ReceiveSomePackets method wrapper on top of the PacketCommunicator class using Event (HandlePacketWrapper) instead of callbacks
        /// </summary>
        /// <param name="packetCommunicator">The object on which the packets should be received</param>
        /// <param name="countGot">See doc of PacketCommunicator.ReceiveSomePackets</param>
        /// <param name="maxPackets">See doc of PacketCommunicator.ReceiveSomePackets</param>
        /// <returns>See doc of PacketCommunicator.ReceiveSomePackets</returns>
        public PacketCommunicatorReceiveResult ReceiveSomePacketsViaEvents(PacketCommunicator packetCommunicator, out int countGot, int maxPackets)
        {
            return packetCommunicator.ReceiveSomePackets(out countGot, maxPackets, OnHandlePacketWrapper);
        }

        /// <summary>
        /// ReceiveStatistics method wrapper on top of the PacketCommunicator class using Event (HandleStatisticsWrapper) instead of callbacks
        /// </summary>
        /// <param name="packetCommunicator">The object on which the packets should be received</param>
        /// <param name="count">See doc of PacketCommunicator.ReceiveStatistics</param>
        /// <returns>See doc of PacketCommunicator.ReceiveStatistics</returns>
        public PacketCommunicatorReceiveResult ReceiveStatisticsViaEvents(PacketCommunicator packetCommunicator, int count)
        {
            return packetCommunicator.ReceiveStatistics(count, OnHandleStatisticsWrapper);
        }

        /// <summary>
        /// Firing the Packet event if the event is set.
        /// </summary>
        /// <param name="packet">The packet received</param>
        private void OnHandlePacketWrapper(Packet packet)
        {
            if (HandlePacketWrapper != null)
            {
                HandlePacketWrapper(packet);
            }
        }

        /// <summary>
        /// Firing the Statistics event if the event is set.
        /// </summary>
        /// <param name="stats">The packet sample statistics received</param>
        private void OnHandleStatisticsWrapper(PacketSampleStatistics stats)
        {
            if (HandleStatisticsWrapper != null)
            {
                HandleStatisticsWrapper(stats);
            }
        }
    }
}