RFID.RFIDInterface.LakeChabotReader.MyCallback C# (CSharp) Method

MyCallback() public method

public MyCallback ( UInt32 bufferLength, IntPtr pBuffer, IntPtr context ) : Int32
bufferLength UInt32
pBuffer IntPtr
context IntPtr
return Int32
        public Int32 MyCallback
        (
            UInt32 bufferLength,
            IntPtr pBuffer,
            IntPtr context
        )
        {
            Byte[ ] packetBuffer = new Byte[ bufferLength ];

            Marshal.Copy( pBuffer, packetBuffer, 0, ( Int32 ) bufferLength );

            String errorMessage = null;
            PacketData.PacketBase packet = null;

            PacketData.PacketType type = PacketData.ParsePacket
            (
                packetBuffer,
                out packet,
                out errorMessage
            );

            SysLogger.WriteToLog = true;

            PacketData.PacketWrapper envelope = null;
            long elapsedSesionTime = ElapsedMilliseconds;

            if ( packet == null || type == PacketData.PacketType.U_N_D_F_I_N_E_D || errorMessage != null )
            {
                BadPacket badPacket = new BadPacket( );
#pragma warning disable 420 // reference to a volatile field is valid for Interlocked call
                badPacket.badPacketSequence = System.Threading.Interlocked.Increment( ref _commonBadIndex );
#pragma warning restore 420
                badPacket.packetTime = DateTime.UtcNow;

                badPacket.rawPacketData = packetBuffer.Clone( ) as byte[ ];
                badPacket.errorMessage = errorMessage;

                using ( MemoryStream data = new MemoryStream( 256 ) )
                {
                    badPacket.WriteTo( data );
                    envelope = new PacketData.PacketWrapper( new PacketData.CommandPsuedoPacket( "BadPacket", data.ToArray( ) ), PacketData.PacketType.U_N_D_F_I_N_E_D );
                }

                envelope.IsPseudoPacket = true;
                envelope.PacketNumber = badPacket.PacketSequence;
                envelope.Timestamp = badPacket.packetTime;
                envelope.ReaderIndex = ( int ) _theReaderID.Handle;
                envelope.ReaderName = _theReaderID.Name;
                envelope.CommandNumber = _processedInventoryIndex;
                envelope.ElapsedTimeMs = elapsedSesionTime;
            }
            else
            {
                envelope = new PacketData.PacketWrapper
                (
                    packet,
                    type,
                    packetBuffer,
                    _commonRequestIndex,
                    elapsedSesionTime,
                    ( int ) this.ReaderHandle,
                    Name
                );
                Debug.Assert( envelope.PacketType == type );
            }

            if ( VirtualReaderQueue != null )
            {
                lock ( VirtualReaderQueue )
                {
                    VirtualReaderQueue.Enqueue( envelope );
                }
            }

            lock ( PacketQueue )
            {
                PacketQueue.Enqueue( envelope );
            }

#pragma warning disable 420 // reference to a volatile field is valid for Interlocked call
            int queueSize = Interlocked.Increment( ref _queueCount );
#pragma warning restore 420

           
            _maxQueueSize = queueSize > _maxQueueSize ? queueSize : _maxQueueSize;
            if (queueSize > MAX_QUEUE_SIZE)
            {
                int loopCount = 0;
                while (queueSize > TARGET_QUEUE_SIZE && loopCount < 1000)
                {
                    loopCount++;
                    QueueEvent.Set();

                    Thread.Sleep(QUEUE_SLEEP_MS);
                    queueSize = _queueCount;
                }
                // Write an informational entry to the event log.    
                SysLogger.LogMessage(String.Format("Queue Size = {0}\nMax Queue Size = {1}\nSleep Count = {2}\nPacket Count = {3}", queueSize, _maxQueueSize, loopCount, ProcessedPacketCount));
            }
            return FunctionController.GetActionRequest( ) != FunctionControl.RequestedAction.Abort ? 0 : 1;
        }