コード例 #1
0
        // Main thread for receiving telegrams
        private void RcvThreading()
        {
            try
            {
                if (SimulateMFCS)
                {
                    RcvListener = new TcpListener(RcvIPEndPoint);
                    Log.AddLog(Log.Severity.EVENT, Name, "Communication.RcvThreading", RcvIPEndPoint.ToString());
                    RcvListener.Start();
                }


                LastReceiveTime = DateTime.Now;

                while (Thread.CurrentThread.ThreadState == ThreadState.Background)
                {
                    try
                    {
                        RcvTelegrams.RemoveAll(p => p.CommRcvStatus >= Telegram.CommRcvStatusEnum.NotifyDone);
                        if (DateTime.Now - LastReceiveTime > RcvTimeOut)
                        {
                            InitRcvSocket();
                            LastReceiveTime = DateTime.Now;
                            Log.AddLog(Log.Severity.EVENT, Name, "Communication.RcvThreading", "Timeout receiving");
                        }
                        else if (DateTime.Now - LastNotifyTime > RefreshTime)
                        {
                            LastNotifyTime = DateTime.Now;
                            Log.AddLog(Log.Severity.EVENT, Name, "Communication.RcvThreading", "Refresh() is called");
                            OnRefresh.ForEach(prop => prop?.Invoke());
                        }
                        else if (!RcvSocket.Connected)
                        {
                            InitRcvSocket();
                            ConnectRcvPartner();
                        }
                        else
                        {
                            if (RcvSocket.Available == 0)
                            {
                                Thread.Sleep(1);
                            }
                            else
                            {
                                Telegram tel     = new TelegramOnlyHeader();
                                int      numRead = 0;
                                do
                                {
                                    numRead += RcvSocket.Receive(tel.ByteBuffer, numRead, tel.ByteBuffer.Length - numRead, SocketFlags.None);
                                } while (numRead < tel.ByteBuffer.Length);
                                // Log.AddLog(Log.Severity.EVENT, Name, "Communication.RcvThreading", String.Format("Received {0} bytes", numRead));
                                tel.ReadBuffer();
                                tel.Validate(false);
                                Telegram tel1 = Activator.CreateInstance(AllTelegrams[tel.TelType].GetType()) as Telegram;
                                tel.ByteBuffer.CopyTo(tel1.ByteBuffer, 0);
                                if (tel1.DesignLength() - tel.DesignLength() > 0 && tel.TelCode == 0)
                                {
                                    numRead = 0;
                                    do
                                    {
                                        numRead += RcvSocket.Receive(tel1.ByteBuffer, tel.DesignLength() + numRead, tel1.DesignLength() - tel.DesignLength() - numRead, SocketFlags.None);
                                    } while (numRead < tel1.DesignLength() - tel.DesignLength());
                                    // Log.AddLog(Log.Severity.EVENT, Name, "Communication.RcvThreading", String.Format("Received {0} bytes", numRead));
                                }
                                tel1.ReadBuffer();
                                tel1.Validate();
                                NotifyRcv(tel1);
                                TelegramACK telACK = new TelegramACK();
                                telACK.Sequence = tel.Sequence;
                                telACK.TelCode  = (System.UInt16) 0xFFFF;
                                telACK.TelType  = tel.TelType;
                                telACK.Sender   = tel.Receiver;
                                telACK.Receiver = tel.Sender;
                                telACK.Build();
                                RcvSocket.Send(telACK.ByteBuffer);
                                Log.AddLog(Log.Severity.EVENT, Name, "Communication.RcvThreading", String.Format("ACK sended {0}", telACK.ToString()));
                                tel1.CommRcvStatus = Telegram.CommRcvStatusEnum.Ack;
                                RcvTelegrams.Add(tel1);
                                LastReceiveTime = DateTime.Now;
                                LastNotifyTime  = DateTime.Now;
                                Log.AddLog(Log.Severity.EVENT, Name, "Communication.RcvThreading", String.Format("Received finished : {0}", tel1.ToString()));
                            }
                        }
                    }
                    catch (SocketException ex)
                    {
                        Log.AddLog(Log.Severity.EXCEPTION, Name, "Communication.RcvThreading::Socket", ex.Message);
                        Thread.Sleep(1000);
                    }
                    catch (TelegramException ex)
                    {
                        Log.AddLog(Log.Severity.EXCEPTION, Name, "Communication.RcvThreading::Telegram", ex.Message);
                        Thread.Sleep(1000);
                    }
                    catch (KeyNotFoundException ex)
                    {
                        Log.AddLog(Log.Severity.EXCEPTION, Name, "Communication.RcvThreading::KeyNotFound", ex.Message);
                        Thread.Sleep(1000);
                    }
                    catch (CommunicationException ex)
                    {
                        Log.AddLog(Log.Severity.EXCEPTION, Name, "Communication.RcvThreading::Communication", ex.Message);
                        Thread.Sleep(1000);
                    }
                    catch (ThreadAbortException ex)
                    {
                        Log.AddLog(Log.Severity.EXCEPTION, Name, "Communication.RcvThreading::Communication", ex.Message);
                        return;
                    }
                    catch (Exception ex)
                    {
                        Log.AddLog(Log.Severity.EXCEPTION, Name, "Communication.SendThread::unknown", ex.Message);
                        Thread.Sleep(1000);
                    }
                }
            }
            finally
            {
                RcvSocket.Close();
                RcvSocket.Dispose();
                SendSocket.Close();
                SendSocket.Dispose();
            }
        }
コード例 #2
0
ファイル: Communicator.cs プロジェクト: II01/i2MFCSv2.TAI
        // notify about new received telegram


        // Main thread for receiving telegrams
        public override void RcvThreading()
        {
            try
            {
                LastReceiveTime = DateTime.Now;

                // initialize from this thread

                while (Thread.CurrentThread.ThreadState == ThreadState.Background)
                {
                    try
                    {
                        lock (_lockRcvTelegram)
                            RcvTelegrams.RemoveAll(p => p.CommRcvStatus >= Telegram.CommRcvStatusEnum.NotifyDone);
                        if (DateTime.Now - LastReceiveTime > RcvTimeOut)
                        {
                            InitRcvSocket();
                            LastReceiveTime = DateTime.Now;
                            Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.RcvThreading", "Timeout receiving");
                        }
                        else if (DateTime.Now - LastNotifyTime > RefreshTime)
                        {
                            LastNotifyTime = DateTime.Now;
                            Log?.AddLog(Log.Severity.EVENT, Name, "Communicator.RcvThreading", "Refresh() is called");
                            CallRefresh();
                        }
                        else if (!RcvSocket.Connected)
                        {
                            InitRcvSocket();
                            ConnectRcvPartner();
                        }
                        else
                        {
                            if (RcvSocket.Available == 0)
                            {
                                Thread.Sleep(1);
                            }
                            else
                            {
                                Telegram tel     = new TelegramOnlyHeader();
                                int      numRead = 0;
                                int      crRead  = 0;
                                do
                                {
                                    crRead   = RcvSocket.Receive(tel.ByteBuffer, numRead, tel.ByteBuffer.Length - numRead, SocketFlags.None);
                                    numRead += crRead;
                                    if (crRead == 0)
                                    {
                                        throw new TelegramException("Receive bytes is 0.");
                                    }
                                } while (numRead < tel.ByteBuffer.Length);
                                // Log?.AddLog(Log.Severity.EVENT, Name, "Communication.RcvThreading", String.Format("Received {0} bytes", numRead));
                                tel.ReadBuffer();
                                tel.Validate(false);
                                Telegram tel1 = Activator.CreateInstance(AllTelegrams[tel.TelType].GetType()) as Telegram;
                                tel.ByteBuffer.CopyTo(tel1.ByteBuffer, 0);
                                if (tel1.DesignLength() - tel.DesignLength() > 0 && tel.TelCode == 0)
                                {
                                    numRead = 0;
                                    crRead  = 0;
                                    do
                                    {
                                        crRead   = RcvSocket.Receive(tel1.ByteBuffer, tel.DesignLength() + numRead, tel1.DesignLength() - tel.DesignLength() - numRead, SocketFlags.None);
                                        numRead += crRead;
                                        if (crRead == 0)
                                        {
                                            throw new TelegramException("Receive bytes is 0.");
                                        }
                                    } while (numRead < tel1.DesignLength() - tel.DesignLength());
                                    // Log?.AddLog(Log.Severity.EVENT, Name, "Communication.RcvThreading", String.Format("Received {0} bytes", numRead));
                                }
                                tel1.ReadBuffer();
                                tel1.Validate();
                                NotifyRcv(tel1);
                                TelegramACK telACK = new TelegramACK();
                                telACK.Sequence = tel.Sequence;
                                telACK.TelCode  = (System.UInt16) 0xFFFF;
                                telACK.TelType  = tel.TelType;
                                telACK.Sender   = tel.Receiver;
                                telACK.Receiver = tel.Sender;
                                telACK.Build();
                                RcvSocket.Send(telACK.ByteBuffer);
                                tel1.CommRcvStatus = Telegram.CommRcvStatusEnum.NotifyDone;
                                LastReceiveTime    = DateTime.Now;
                                LastNotifyTime     = DateTime.Now;
                                Log?.AddLog(Log.Severity.EVENT, Name, "Communicator.RcvThreading", String.Format("Received finished : {0}", tel1.ToString()));
                            }
                        }
                    }
                    catch (SocketException ex)
                    {
                        Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.RcvThreading::SocketException", ex.Message);
                        Thread.Sleep(1000);
                    }
                    catch (TelegramException ex)
                    {
                        Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.RcvThreading::TelegramException", ex.Message);
                        Thread.Sleep(1000);
                    }
                    catch (KeyNotFoundException ex)
                    {
                        Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.RcvThreading::KeyNotFound", ex.Message);
                        Thread.Sleep(1000);
                    }
                    catch (CommunicationException ex)
                    {
                        Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.RcvThreading::CommunicationException", ex.Message);
                        Thread.Sleep(1000);
                    }
                    catch (ThreadAbortException ex)
                    {
                        Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.RcvThreading::ThreadAbortException", ex.Message);
                        return;
                    }
                    catch (ServiceCommunicatorException ex)
                    {
                        Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.RcvThreading::ServiceCommunicationException", ex.Message);
                        Thread.Sleep(3000);
                    }
                    catch (Exception ex)
                    {
                        Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.SendThread::Exception", ex.Message);
                        Thread.Sleep(1000);
                    }
                }
            }
            finally
            {
                RcvSocket.Close();
                RcvSocket.Dispose();
                SendSocket.Close();
                SendSocket.Dispose();
            }
        }