// Main thread for receiving telegrams
        public async Task <Telegram> Receive()
        {
            try
            {
                if (!Connected)
                {
                    await MyConnect().ConfigureAwait(false);
                }

                // Read header
                Telegram tel     = new TelegramOnlyHeader();
                int      numRead = 0;
                do
                {
                    numRead += await MyRead(tel.ByteBuffer, numRead, tel.ByteBuffer.Length - numRead).ConfigureAwait(false);
                } while (numRead < tel.ByteBuffer.Length);

                // Read body
                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 += await MyRead(tel1.ByteBuffer, tel.DesignLength() + numRead, tel1.DesignLength() - tel.DesignLength() - numRead).ConfigureAwait(false);
                    } while (numRead < tel1.DesignLength() - tel.DesignLength());
                }

                // Prepare ACK telegram
                tel1.ReadBuffer();
                tel1.Validate();
                TelegramACK telACK = new TelegramACK {
                    Sequence = tel.Sequence, TelCode = 0xFFFF, TelType = tel.TelType, Sender = tel.Receiver, Receiver = tel.Sender
                };
                telACK.Build();

                // Send ACK telegram
                await MyWrite(telACK.ByteBuffer, 0, telACK.ByteBuffer.Length).ConfigureAwait(false);

                tel1.CommRcvStatus = Telegram.CommRcvStatusEnum.NotifyDone;
                LastReceiveTime    = DateTime.Now;
                Log.AddLog(Log.Severity.EVENT, Name, String.Format("Received finished : {0}", tel1.ToString()));
                return(tel1);
            }
            catch (Exception ex)
            {
                Log.AddLog(Log.Severity.EXCEPTION, Name, ex.Message);
                throw;
            }
        }
Exemple #2
0
        // 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();
            }
        }