Exemplo n.º 1
0
 protected override void Run()
 {
     while (this.running)
     {
         try
         {
             if (this.mQueueConvert.Count >= 1)
             {
                 SECSBlock mHSMSItem = null;
                 lock (this.syncObject)
                 {
                     mHSMSItem = this.mQueueConvert.Dequeue();
                 }
                 if (mHSMSItem != null)
                 {
                     this.Parse(mHSMSItem);
                 }
                 else
                 {
                     this.logger.Debug("Parser#Run Invoked. block ==null");
                 }
                 continue;
             }
         }
         catch (Exception exception)
         {
             this.logger.Error("Parser#Run", exception);
             if (this.OnParseError != null)
             {
                 this.OnParseError(string.Format("{0}: {1}", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ParseError), exception.Message));
             }
         }
         Thread.Sleep(50);
     }
 }
Exemplo n.º 2
0
 public void QueueEnque(SECSBlock aHSMSItem)
 {
     lock (this.syncObject)
     {
         this.mQueueConvert.Enqueue(aHSMSItem);
     }
 }
Exemplo n.º 3
0
 public void Parse(SECSBlock mHSMSItem)
 {
     try
     {
         SECSDecoding decoding = new SECSDecoding();
         string       str      = ByteStringBuilder.ToLogString(mHSMSItem.Header);
         string       str2     = ByteStringBuilder.ToLogString(mHSMSItem.DataItem);
         SECSMessage  msg      = decoding.Byte_TO_SecsMessage(mHSMSItem.Header);
         msg.Root   = decoding.Byte_TO_SecsItem(mHSMSItem.DataItem);
         msg.Header = mHSMSItem.Header;
         //this.logger.Info(string.Format("[RECV] S{0}F{1} {2} System Bytes={3} {4} {5}", new object[]
         //{
         //	msg.Stream,
         //	msg.Function,
         //	msg.WBit ? "W" : "",
         //	msg.SystemBytes,
         //	str,
         //	str2
         //}));
         //this.logger.Warn("[RECV] " + SecsItem2Str.GetSecsMessageStr(msg));
         if (this.OnReceived != null)
         {
             this.OnReceived(msg);
         }
     }
     catch (Exception exception)
     {
         this.logger.Error("Parser#Parse", exception);
         if (this.OnParseError != null)
         {
             this.OnParseError(string.Format("{0}: {1}", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ParseError), exception.Message));
         }
     }
 }
Exemplo n.º 4
0
 protected override void Run()
 {
     while (this.running)
     {
         byte[] bs = new byte[4];
         try
         {
             this.ReadLength(bs);
             int aLength = (int)Byte2SecsValue.GetInt(bs);
             if (aLength > 0)
             {
                 SECSBlock block = this.ByteToBlock(this.ReadBody(aLength));
                 if (this.OnReadCompleted != null)
                 {
                     this.OnReadCompleted(block);
                 }
             }
             else
             {
                 Thread.Sleep(50);
             }
         }
         catch (OutOfMemoryException exception)
         {
             GC.Collect();
             GC.WaitForPendingFinalizers();
             this.logger.Error("Reader#Run", exception);
             if (this.OnReadError != null)
             {
                 this.OnReadError(string.Format("{0}: Out Of Memory.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ReadError)));
             }
             this.Disconnect(exception.Message);
         }
         catch (IOException exception2)
         {
             this.logger.Error("Reader#Run", exception2);
             if (this.OnReadError != null)
             {
                 this.OnReadError(string.Format("{0}: Socket Error.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ReadError)));
             }
             this.Disconnect(exception2.Message);
         }
         catch (Exception exception3)
         {
             this.logger.Error("Reader#Run", exception3);
             if (this.OnReadError != null)
             {
                 this.OnReadError(string.Format("{0}: {1}.", SECSErrorsMessage.GetSECSErrorMessage(SECSErrors.ReadError), exception3.Message));
             }
             this.Disconnect(exception3.Message);
         }
     }
 }
Exemplo n.º 5
0
        public SECSBlock ByteToBlock(byte[] aBytes)
        {
            SECSBlock block = new SECSBlock
            {
                Length   = aBytes.Length,
                Header   = new byte[10],
                DataItem = new byte[aBytes.Length - 10]
            };

            Array.Copy(aBytes, 0, block.Header, 0, block.Header.Length);
            Array.Copy(aBytes, 10, block.DataItem, 0, block.DataItem.Length);
            string str = ByteStringBuilder.ToLogString(block.Header);

            this.logger.Debug(string.Format("Reader#ByteToBlock Header: {0}", str));
            string str2 = ByteStringBuilder.ToLogString(block.DataItem);

            this.logger.Debug(string.Format("Reader#ByteToBlock Data: {0}", str2));
            return(block);
        }
Exemplo n.º 6
0
        private void OnReadHsms(SECSBlock aHSMSItem)
        {
            try
            {
                if (this.NowStatus == eHSMS_PORT_STATUS.TERMINATE)
                {
                    this.logger.Error("HSMSPort::OnReadHsms=> Port TERMINATE.");
                    return;
                }
            }
            catch (Exception exception)
            {
                this.logger.Error("HSMSPort::OnReadHsms", exception);
            }
            byte[] aBytes = new byte[4];
            Array.Copy(aHSMSItem.Header, 6, aBytes, 0, aBytes.Length);
            long mSystemBytes = Byte2SecsValue.GetLong(aBytes);
            byte num          = aHSMSItem.Header[5];

            if (num == 0)
            {
                this.logger.Debug("HSMSPort::OnReadHsms Not control message.");
                aHSMSItem.IsControlMsg = false;
                this.mHsmsParser.QueueEnque(aHSMSItem);
                return;
            }
            this.logger.Debug("HSMSPort::OnReadHsms control message.");
            lock (this.syncObject)
            {
                aHSMSItem.IsControlMsg = true;
                switch (num)
                {
                case 1:
                    this.mHsmsTimer.StopT7Timer();
                    if (this.NowStatus != eHSMS_PORT_STATUS.SELECT)
                    {
                        this.mHsmsWriter.WriteControlMessage(mSystemBytes, 0, eControlMessage.SELECT_RSP, 255, 255);
                        this.FireSelected();
                    }
                    else
                    {
                        this.mHsmsWriter.WriteControlMessage(mSystemBytes, 1, eControlMessage.SELECT_RSP, 255, 255);
                    }
                    break;

                case 2:
                    this.mHsmsTimer.StopT6Timer();
                    this.FireSelected(aHSMSItem.Header[3]);
                    break;

                case 3:
                    this.UpdateStatus(eHSMS_PORT_STATUS.CONNECT);
                    this.mHsmsWriter.WriteControlMessage(mSystemBytes, 0, eControlMessage.DESELECT_RSP, 255, 255);
                    break;

                case 5:
                    this.mHsmsWriter.WriteControlMessage(mSystemBytes, 0, eControlMessage.LINKTEST_RSP, 255, 255);
                    break;

                case 6:
                    this.mHsmsTimer.StopT6Timer();
                    break;

                case 7:
                    this.TraceRejectReqCode(aHSMSItem.Header[3]);
                    break;

                case 9:
                    this.OnDisconnect("Separate.");
                    break;
                }
            }
        }
Exemplo n.º 7
0
        private Queue <SECSBlock> GetSECS1Bytes(byte[] encodingBytes)
        {
            Queue <SECSBlock> queue = new Queue <SECSBlock>();
            int num2 = (encodingBytes.Length - 14) / 244;
            int data;

            for (int i = 0; i < num2; i = i++)
            {
                data = 0;
                SECSBlock block = new SECSBlock
                {
                    Length       = 254,
                    IsControlMsg = false
                };
                byte[] buffer = new byte[10];
                Array.Copy(encodingBytes, 4, buffer, 0, 10);
                byte[] buffer2 = new byte[254];
                Array.Copy(encodingBytes, 10 + i * 244, buffer2, 0, 254);
                buffer[5]      = (byte)(i + 1);
                block.Header   = buffer;
                block.DataItem = buffer2;
                for (int j = 0; j < buffer.Length; j++)
                {
                    data += (int)(buffer[j] & 255);
                }
                for (int k = 0; k < buffer2.Length; k++)
                {
                    data += (int)(buffer2[k] & 255);
                }
                byte[] buffer3 = SecsValue2Byte.GetIntBytes(data, 4);
                block.CheckSum = new byte[4];
                Array.Copy(buffer3, 2, block.CheckSum, 0, 2);
                queue.Enqueue(block);
            }
            data = 0;
            int       num3   = encodingBytes.Length - 14 - num2 * 244;
            SECSBlock block2 = new SECSBlock();

            byte[] buffer4 = new byte[10];
            Array.Copy(encodingBytes, 4, buffer4, 0, 10);
            buffer4[4] = 128;
            buffer4[5] = (byte)(num2 + 1);
            byte[] buffer5 = new byte[encodingBytes.Length - 14 - num2 * 244];
            Array.Copy(encodingBytes, 14 + num2 * 244, buffer5, 0, buffer5.Length);
            block2.Length       = num3 + 10;
            block2.IsControlMsg = false;
            block2.Header       = buffer4;
            block2.DataItem     = buffer5;
            for (int l = 0; l < buffer4.Length; l++)
            {
                data += (int)(buffer4[l] & 255);
            }
            for (int m = 0; m < buffer5.Length; m++)
            {
                data += (int)(buffer5[m] & 255);
            }
            byte[] intBytes = SecsValue2Byte.GetIntBytes(data, 4);
            block2.CheckSum = new byte[2];
            Array.Copy(intBytes, 2, block2.CheckSum, 0, 2);
            queue.Enqueue(block2);
            return(queue);
        }
Exemplo n.º 8
0
 private void RcvdData(byte data)
 {
     if (this.secs1.PortStatus == eSECS1_PORT_STATUS.PortRcvd)
     {
         lock (this.syncRcvd)
         {
             this.rcvdBlock.Add(data);
             if (this.rcvdBlockLength == 0)
             {
                 this.rcvdBlockLength = (int)this.rcvdBlock[0];
                 this.secs1.StopTimer(eTimeout.T2);
                 if (this.rcvdBlockLength < 10 || this.rcvdBlockLength > 254)
                 {
                     this.Logger.Debug(string.Format("RECV Invalid Length Bytes: {0}.", this.rcvdBlockLength));
                     this.rcvdBlockLength = 0;
                     this.rcvdBlock.Clear();
                     this.secs1.UpdatePortStatus(eSECS1_PORT_STATUS.PortRcvd, eSECS1_PORT_STATUS.PortCmpl);
                     this.secs1.StartTimer(eTimeout.T1);
                     goto IL_3E2;
                 }
             }
             else
             {
                 this.secs1.StopTimer(eTimeout.T1);
             }
             byte[] buffer = this.rcvdBlock.ToArray();
             if (this.rcvdBlockLength == this.rcvdBlock.Count - 3)
             {
                 byte[] buffer2 = new byte[10];
                 Array.Copy(buffer, 1, buffer2, 0, 10);
                 byte[] buffer3 = new byte[2];
                 Array.Copy(buffer, buffer.Length - 2, buffer3, 0, 2);
                 byte[] buffer4 = new byte[buffer.Length - 13];
                 Array.Copy(buffer, buffer2.Length + 1, buffer4, 0, buffer4.Length);
                 //this.Logger.Info(string.Format("[RECV] HDR = {0}\nDATA = {1}\nCSUM = {2}", SecsItem2Str.GetBinaryStr(buffer2), SecsItem2Str.GetBinaryStr(buffer4), SecsItem2Str.GetBinaryStr(buffer3)));
                 int num  = (int)buffer3[0] << 8 | (int)buffer3[1];
                 int num2 = (int)(buffer2[4] & 128);
                 int num3 = 0;
                 for (int i = 1; i < this.rcvdBlock.Count - 2; i++)
                 {
                     num3 += (int)this.rcvdBlock[i];
                 }
                 if (num != num3)
                 {
                     this.secs1.UpdatePortStatus(this.secs1.PortStatus, eSECS1_PORT_STATUS.PortIdle);
                     this.secs1.SendNAK();
                     this.Logger.Debug("Checksum not match, Reply to send NAK.");
                 }
                 else
                 {
                     if (this.rcvdBlocks.Count > 0)
                     {
                         this.secs1.StopTimer(eTimeout.T4);
                     }
                     this.secs1.UpdatePortStatus(this.secs1.PortStatus, eSECS1_PORT_STATUS.PortIdle);
                     this.secs1.SendACK();
                     if (num2 == 0)
                     {
                         SECSBlock block = new SECSBlock
                         {
                             Header   = buffer2,
                             DataItem = buffer4,
                             CheckSum = buffer3
                         };
                         this.rcvdBlocks.Add(block);
                         this.Logger.Debug("RECV One of Multi-Block Ok, Send ACK.");
                         this.secs1.StartTimer(eTimeout.T4);
                     }
                     else if (this.rcvdBlocks.Count == 0)
                     {
                         this.Logger.Debug("RECV Single-Block Message Ok, Send ACK.");
                         SECSDecoding decoding = new SECSDecoding();
                         SECSMessage  msg      = decoding.Byte_TO_SecsMessage(buffer2);
                         SECSItem     item     = decoding.Byte_TO_SecsItem(buffer4);
                         if (item != null)
                         {
                             msg.Root = item;
                         }
                         this.eventExecutor.NofityRECV(msg);
                     }
                     else
                     {
                         this.Logger.Debug("RECV Multi-Block Message Ok, Send ACK.");
                         SECSDecoding decoding2 = new SECSDecoding();
                         SECSMessage  message2  = decoding2.Byte_TO_SecsMessage(buffer2);
                         List <byte>  list      = new List <byte>();
                         for (int j = 0; j < this.rcvdBlocks.Count; j++)
                         {
                             list.AddRange(this.rcvdBlocks[j].DataItem);
                         }
                         this.rcvdBlocks.Clear();
                         list.AddRange(buffer4);
                         byte[]   aDataByte = list.ToArray();
                         SECSItem item2     = decoding2.Byte_TO_SecsItem(aDataByte);
                         if (item2 != null)
                         {
                             message2.Root = item2;
                         }
                         this.eventExecutor.NofityRECV(message2);
                     }
                 }
             }
             else
             {
                 this.secs1.StartTimer(eTimeout.T1);
             }
             IL_3E2 :;
         }
         return;
     }
     if (this.secs1.PortStatus == eSECS1_PORT_STATUS.PortCmpl)
     {
         this.secs1.StopTimer(eTimeout.T1);
         this.Logger.Info(string.Format("PortCmpl, Invalid data {0}", data));
         this.secs1.StartTimer(eTimeout.T1);
         return;
     }
     this.Logger.Info(string.Format("Invalid Status - {0}, Invalid data: {1}", this.secs1.PortStatus, data));
 }