Ejemplo n.º 1
0
        /// <summary>
        /// Callback function for capture.
        /// </summary>
        /// <param name="hCapEngine"></param>
        /// <param name="adapterIndex"></param>
        /// <param name="callerContext"></param>
        /// <param name="hRawFrame"></param>
        private void CaptureCallBack(IntPtr hCapEngine, uint adapterIndex, IntPtr callerContext, IntPtr hRawFrame)
        {
            if (callerContext != IntPtr.Zero)
            {
                uint errno;

                unsafe
                {
                    uint frameLen = 0;

                    errno = NetmonAPI.NmGetRawFrameLength(hRawFrame, out frameLen);
                    if (errno != 0)
                    {
                        return;
                    }

                    byte[] frameBuf = new byte[CapturedFrameSize];

                    fixed(byte *pBuf = frameBuf)
                    {
                        if (frameLen >= CapturedFrameSize)
                        {
                            NM_TIME pTime = new NM_TIME();
                            //Get the TimeStamp of the frame for building the new shortened frame
                            errno = NetmonAPI.NmGetFrameTimeStampEx(hRawFrame, ref pTime);
                            if (errno != 0)
                            {
                                return;
                            }

                            uint captureSize = 0;
                            //use NmGetPartiaRawlFrame() to get the wanted length of the raw frame,
                            errno = NetmonAPI.NmGetPartialRawFrame(
                                hRawFrame,
                                0, //offset
                                CapturedFrameSize,
                                pBuf,
                                out captureSize
                                );
                            if (errno != 0)
                            {
                                return;
                            }

                            IntPtr hPartialRawFrame;
                            errno = NetmonAPI.NmBuildRawFrameFromBufferEx(
                                (IntPtr)pBuf,
                                CapturedFrameSize,
                                0, //media type, optional
                                ref pTime,
                                out hPartialRawFrame
                                );
                            if (errno != 0)
                            {
                                return;
                            }

                            NetmonAPI.NmAddFrame(callerContext, hPartialRawFrame);
                        }
                        else
                        {
                            NetmonAPI.NmAddFrame(callerContext, hRawFrame);
                        }
                    }
                }
            }
        }