private bool PacketCallBackFromReader( int readerIndex, Byte[ ] PacketBuffer )
{
uint myHandle = this._theReaderID._handle;
Debug.Assert( readerIndex == myHandle );
String errorMessage = null;
PacketData.PacketBase packet = null;
//!! TODO: fix output SysLogger.WriteToLog = ation != Library;
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, readerIndex, Name );
Debug.Assert( envelope.PacketType == type );
}
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;
}