Packet Sniffer SDK DLL Edition

HNTcpSession wrapper for C#

 Previous Next

File: HNTcpSession.cs

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Diagnostics;
using System.Text;
using System.Runtime.InteropServices;

namespace microOLAP.PSSDK
{
    /// <summary>
    /// Summary description for HNTcpSession.
    /// </summary>
    public partial class HNTCPSession : Component
    {
        #region DLL exports

        /// <summary>
        /// Returns the TCP session state.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetState")]
        private static extern int TcpSesGetState(IntPtr hTcpSes);

        /// <summary>
        /// Returns the reason why the TCP session has been closed.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetCloseReason")]
        private static extern int TcpSesGetCloseReason(IntPtr hTcpSes);

        /// <summary>
        /// Returns the TCP session creation time.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetCreateTime")]
        private static extern uint TcpSesGetCreateTime(IntPtr hTcpSes);

        /// <summary>
        /// Returns the time of receiving of the last packet participated in TCP connection.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetLastPacketTime")]
        private static extern uint TcpSesGetLastPacketTime(IntPtr hTcpSes);

        /// <summary>
        /// Returns IP address of the client participating in the connection.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetClientIP")]
        private static extern uint TcpSesGetClientIP(IntPtr hTcpSes);

        /// <summary>
        /// Returns port number of the client participating in the connection.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetClientPort")]
        private static extern ushort TcpSesGetClientPort(IntPtr hTcpSes);

        /// <summary>
        /// Returns IP address of the server participating in the connection.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetServerIP")]
        private static extern uint TcpSesGetServerIP(IntPtr hTcpSes);

        /// <summary>
        /// Returns port number of the server participating in the connection.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetServerPort")]
        private static extern ushort TcpSesGetServerPort(IntPtr hTcpSes);

        /// <summary>
        /// Returns the number of packets sent by client.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <param name="HiValue"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetClientPacketsCount")]
#if WIN64
        private static extern ulong TcpSesGetClientPacketsCount(IntPtr hTcpSes);
#else
        private static extern uint TcpSesGetClientPacketsCount(IntPtr hTcpSes, ref uint HiValue);
#endif

        /// <summary>
        /// Returns the number of packets sent by server.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <param name="HiValue"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetServerPacketsCount")]
#if WIN64
        private static extern ulong TcpSesGetServerPacketsCount(IntPtr hTcpSes);
#else
        private static extern uint TcpSesGetServerPacketsCount(IntPtr hTcpSes, ref uint HiValue);
#endif

        /// <summary>
        /// Returns the size of data sent by client.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <param name="HiValue"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetClientDataSize")]
#if WIN64
        private static extern ulong TcpSesGetClientDataSize(IntPtr hTcpSes);
#else
        private static extern uint TcpSesGetClientDataSize(IntPtr hTcpSes, ref uint HiValue);
#endif

        /// <summary>
        /// Returns the size of data sent by server.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <param name="HiValue"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetServerDataSize")]
#if WIN64
        private static extern ulong TcpSesGetServerDataSize(IntPtr hTcpSes);
#else
        private static extern uint TcpSesGetServerDataSize(IntPtr hTcpSes, ref uint HiValue);
#endif

        /// <summary>
        /// Returns user-defined value associated with TCP session.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetUserData")]
        private static extern IntPtr TcpSesGetUserData(IntPtr hTcpSes);

        /// <summary>
        /// Sets user-defined value associated with TCP session.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <param name="UserData"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesSetUserData")]
        private static extern IntPtr TcpSesSetUserData(IntPtr hTcpSes, IntPtr UserData);

        /// <summary>
        /// Swap client/server properties.
        /// </summary>
        /// <param name="hTcpSes"></param>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesSwapClientServer")]
        private static extern void TcpSesSwapClientServer(IntPtr hTcpSes);

        /// <summary>
        /// Returns the TCP session type.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetType")]
        private static extern int TcpSesGetType(IntPtr hTcpSes);

        /// <summary>
        /// Returns IPv6 address of the client participating in the connection.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <param name="pIpAddr"></param>
        /// <param name="BufferSize"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetClientIPv6")]
        private static extern int TcpSesGetClientIPv6(IntPtr hTcpSes, byte[] IpAddr, uint BufferSize);

        /// <summary>
        /// Returns IPv6 address of the server participating in the connection.
        /// </summary>
        /// <param name="hTcpSes"></param>
        /// <param name="pIpAddr"></param>
        /// <param name="BufferSize"></param>
        /// <returns></returns>
        [DllImport("pssdk.dll", EntryPoint = "TcpSesGetServerIPv6")]
        private static extern int TcpSesGetServerIPv6(IntPtr hTcpSes, byte[] IpAddr, uint BufferSize);

        #endregion

        /// <summary>
        /// Handle of the HNTcpSession object.
        /// </summary>
        private IntPtr hTcpSes = IntPtr.Zero;

        /// <summary>
        /// Contructor of the HNTcpSession component object.
        /// </summary>
        public HNTCPSession()
        {
            InitializeComponent();
        }

        #region HNTcpSession properties

        /// <summary>
        /// Checks if the HNTcpSession object has been created correctly.
        /// </summary>
        [Browsable(false)]
        public bool IsValid
        {
            get
            {
                return (hTcpSes != IntPtr.Zero);
            }
        }

        /// <summary>
        /// Sets/Returns a TCP session handle.
        /// </summary>
        [Browsable(false)]
        public IntPtr Handle
        {
            get
            {
                return hTcpSes;
            }
            set
            {
                hTcpSes = value;
            }
        }

        /// <summary>
        /// Returns the TCP session state.
        /// </summary>
        [Browsable(false)]
        public HNTcpState State
        {
            get
            {
                return (HNTcpState)TcpSesGetState(hTcpSes);
            }
        }

        /// <summary>
        /// Returns the reason why the TCP session has been closed.
        /// </summary>
        [Browsable(false)]
        public HNTcpCloseReason CloseReason
        {
            get
            {
                return (HNTcpCloseReason)TcpSesGetCloseReason(hTcpSes);
            }
        }

        /// <summary>
        /// Returns the TCP session creation time.
        /// </summary>
        [Browsable(false)]
        public uint CreateTime
        {
            get
            {
                return TcpSesGetCreateTime(hTcpSes);
            }
        }

        /// <summary>
        /// Returns the time of receiving of the last packet participated in TCP connection.
        /// </summary>
        [Browsable(false)]
        public uint LastPacketTime
        {
            get
            {
                return TcpSesGetLastPacketTime(hTcpSes);
            }
        }

        /// <summary>
        /// Returns IP address of the client participating in the connection.
        /// </summary>
        [Browsable(false)]
        public uint ClientIP
        {
            get
            {
                return TcpSesGetClientIP(hTcpSes);
            }
        }

        /// <summary>
        /// Returns port number of the client participating in the connection.
        /// </summary>
        [Browsable(false)]
        public ushort ClientPort
        {
            get
            {
                return TcpSesGetClientPort(hTcpSes);
            }
        }

        /// <summary>
        /// Returns IP address of the server participating in the connection.
        /// </summary>
        [Browsable(false)]
        public uint ServerIP
        {
            get
            {
                return TcpSesGetServerIP(hTcpSes);
            }
        }

        /// <summary>
        /// Returns port number of the server participating in the connection.
        /// </summary>
        [Browsable(false)]
        public ushort ServerPort
        {
            get
            {
                return TcpSesGetServerPort(hTcpSes);
            }
        }

        /// <summary>
        /// Sets/Returns user-defined value associated with TCP session.
        /// </summary>
        [Browsable(false)]
        public IntPtr UserData
        {
            get
            {
                return TcpSesGetUserData(hTcpSes);
            }
            set
            {
                TcpSesSetUserData(hTcpSes, value);
            }
        }

        /// <summary>
        /// Returns the number of packets sent by client.
        /// </summary>
        [Browsable(false)]
        public ulong ClientPacketsCount
        {
            get
            {
#if WIN64
                return TcpSesGetClientPacketsCount(hTcpSes);
#else
                uint LowValue, HighValue = 0;
                LowValue = TcpSesGetClientPacketsCount(hTcpSes, ref HighValue);
                return ((((ulong)HighValue) << 32) + LowValue);
#endif
            }
        }

        /// <summary>
        /// Returns the number of packets sent by server.
        /// </summary>
        [Browsable(false)]
        public ulong ServerPacketsCount
        {
            get
            {
#if WIN64
                return TcpSesGetServerPacketsCount(hTcpSes);
#else
                uint LowValue, HighValue = 0;
                LowValue = TcpSesGetServerPacketsCount(hTcpSes, ref HighValue);
                return ((((ulong)HighValue) << 32) + LowValue);
#endif
            }
        }

        /// <summary>
        /// Returns the size of data sent by client.
        /// </summary>
        [Browsable(false)]
        public ulong ClientDataSize
        {
            get
            {
#if WIN64
                return TcpSesGetClientDataSize(hTcpSes);
#else
                uint LowValue, HighValue = 0;
                LowValue = TcpSesGetClientDataSize(hTcpSes, ref HighValue);
                return ((((ulong)HighValue) << 32) + LowValue);
#endif
            }
        }

        /// <summary>
        /// Returns the size of data sent by server.
        /// </summary>
        [Browsable(false)]
        public ulong ServerDataSize
        {
            get
            {
#if WIN64
                return TcpSesGetServerDataSize(hTcpSes);
#else
                uint LowValue, HighValue = 0;
                LowValue = TcpSesGetServerDataSize(hTcpSes, ref HighValue);
                return ((((ulong)HighValue) << 32) + LowValue);
#endif
            }
        }

        /// <summary>
        /// Returns the TCP session type.
        /// </summary>
        public HNTcpSessionType SessionType
        {
            get 
            { 
                return (HNTcpSessionType)TcpSesGetType(hTcpSes); 
            }
        }

        #endregion

        #region HNTCPSession methods

        /// <summary>
        /// Swap client/server properties.
        /// </summary>
        public void SwapClientServer()
        {
            TcpSesSwapClientServer(hTcpSes);
        }

        /// <summary>
        /// Returns IPv6 address of the client participating in the connection.
        /// </summary>
        /// <param name="pIpAddr"></param>
        /// <param name="BufferSize"></param>
        public PSSDKRES GetClientIPv6(byte[] IpAddr, uint BufferSize)
        {
            return (PSSDKRES)TcpSesGetClientIPv6(hTcpSes, IpAddr, BufferSize);
        }

        /// <summary>
        /// Returns IPv6 address of the server participating in the connection.
        /// </summary>
        /// <param name="pIpAddr"></param>
        /// <param name="BufferSize"></param>
        public PSSDKRES GetServerIPv6(byte[] IpAddr, uint BufferSize)
        { 
            return (PSSDKRES)TcpSesGetServerIPv6(hTcpSes,IpAddr,BufferSize);
        }

        #endregion
    }
}
See also:HNTcpSession, All wrappers in one ZIP archive