Example #1
0
        // 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;
            }
        }
Example #2
0
        // Main thread for sending telegrams
        public async Task <Telegram> Send(Telegram tel)
        {
            try
            {
                if (!Connected)
                {
                    await MyConnect().ConfigureAwait(false);
                }

                TelegramACK telAck = null;
                LastSendTime = DateTime.Now;
                tel.Sequence = Sequence;
                tel.Build();
                // Log?.AddLog(Log.Severity.EVENT, Name, "Communication.SendThread", String.Format("Start sending {0}", tel.ToString()));
                await MyWrite(tel.ByteBuffer, 0, tel.Length).ConfigureAwait(false);

                Log.AddLog(Log.Severity.EVENT, Name, $"Socket written {tel.ToString()}");
                tel.CommSendStatus = Telegram.CommSendStatusEnum.WaitACK;
                telAck             = new TelegramACK();

                await MyRead(telAck.ByteBuffer, 0, telAck.DesignLength()).ConfigureAwait(false);

                telAck.ReadBuffer();
                telAck.Validate();
                if (telAck.Sequence != tel.Sequence)
                {
                    Log.AddLog(Log.Severity.EXCEPTION, Name, $"Sequence does not correspond");
                    throw new TelegramException("Sequence does not correspond");
                }
                Sequence = (Sequence < 99) ? (short)(Sequence + 1) : (short)0;
                Log.AddLog(Log.Severity.EVENT, Name, $"Send finished : {tel.ToString()}");
                return(tel);
            }
            catch (Exception ex)
            {
                Log.AddLog(Log.Severity.EXCEPTION, Name, ex.Message);
                throw;
            }
        }
Example #3
0
        // Main thread for sending telegrams
        public override void SendThreading()
        {
            TelegramACK telAck = null;

            try
            {
                lock (_lockSendTelegram)
                {
                    SendTelegrams.RemoveAll(p => p.CommSendStatus >= Telegram.CommSendStatusEnum.Ack);
                }

                LastSendTime = DateTime.Now;
                Telegram tel    = null;
                Telegram telNew = null;

                while (Thread.CurrentThread.ThreadState == ThreadState.Background)
                {
                    try
                    {
                        if (tel == null)
                        {
                            lock (_lockSendTelegram)
                            {
                                tel = SendTelegrams.FirstOrDefault(prop => prop.CommSendStatus < Telegram.CommSendStatusEnum.Ack);
                                CurrentSendTelegram = tel;
                            }
                        }
                        if (DateTime.Now - LastSendTime > SendTimeOut)
                        {
                            InitSendSocket();
                            if (tel != null)
                            {
                                tel.CommSendStatus = Telegram.CommSendStatusEnum.None;
                            }
                            LastSendTime = DateTime.Now;
                            Retry        = 0;
                            Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.SendThread", "Send timeout, SendSocket reinitialized!");
                        }
                        else if (DateTime.Now - LastSendTime > KeepALifeTime && tel == null)
                        {
                            Telegram tRes = null;
                            lock (_lockSendTelegram)
                                tRes = SendTelegrams.FirstOrDefault(prop => prop.CommSendStatus < Telegram.CommSendStatusEnum.Ack);
                            if (tRes == null)
                            {
                                if (KeepALifeTelegram != null)
                                {
                                    Telegram t = Activator.CreateInstance(KeepALifeTelegram.GetType()) as Telegram;
                                    t.Sender   = MFCS_ID;
                                    t.Receiver = PLC_ID;
                                    t.Build();
                                    tel = t;
                                    Log?.AddLog(Log.Severity.EVENT, Name, "Communicator.SendThread", String.Format("Adding KeepALife telegram"));
                                }
                            }
                        }
                        else if (!SendSocket.Connected)
                        {
                            InitSendSocket();
                            ConnectSendPartner();
                        }
                        else if (tel != null)
                        {
                            switch (tel.CommSendStatus)
                            {
                            case (Telegram.CommSendStatusEnum.None):
                                CurrentSendTelegram = tel;
                                tel.Sequence        = Sequence;
                                tel.Build();
                                // Log?.AddLog(Log.Severity.EVENT, Name, "Communication.SendThread", String.Format("Start sending {0}", tel.ToString()));
                                SendSocket.Send(tel.ByteBuffer, tel.Length, SocketFlags.None);
                                Log?.AddLog(Log.Severity.EVENT, Name, "Communicator.SendThread", String.Format("Socket written {0}", tel.ToString()));
                                tel.CommSendStatus = Telegram.CommSendStatusEnum.WaitACK;
                                telAck             = new TelegramACK();
                                SendTime           = DateTime.Now;
                                break;

                            case (Telegram.CommSendStatusEnum.WaitACK):
                                int numRead = 0;
                                int crRead  = 0;
                                do
                                {
                                    crRead = SendSocket.Receive(telAck.ByteBuffer, numRead, telAck.ByteBuffer.Length - numRead, SocketFlags.None);
                                    if (crRead == 0)
                                    {
                                        throw new TelegramException("Receive bytes is 0.");
                                    }
                                    numRead += crRead;
                                } while (numRead < telAck.ByteBuffer.Length);
                                telAck.ReadBuffer();
                                Log?.AddLog(Log.Severity.EVENT, Name, "Communication.SendThread", String.Format("Received ACK {0}", telAck.ToString()));
                                if (telAck.Validate() && telAck.Sequence == tel.Sequence)
                                {
                                    tel.CommSendStatus = Telegram.CommSendStatusEnum.Ack;
                                    LastSendTime       = DateTime.Now;
                                    Retry = 0;
                                    if (Sequence < 99)
                                    {
                                        Sequence++;
                                    }
                                    else
                                    {
                                        Sequence = 0;
                                    }
                                    Log?.AddLog(Log.Severity.EVENT, Name, "Communicator.SendThreading", String.Format("Send finished : {0}", tel.ToString()));
                                    NotifySend(tel);
                                    lock (_lockSendTelegram)
                                        telNew = SendTelegrams.FirstOrDefault(prop => prop.CommSendStatus < Telegram.CommSendStatusEnum.Ack);
                                    tel = null;
                                }
                                else
                                {
                                    //                                      tel.CommSendStatus = Telegram.CommSendStatusEnum.None;
                                    Retry++;
                                    Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.SendThreading", String.Format("Retry increased - {0}", Retry));
                                }
                                break;

                            default:
                                break;
                            }
                        }
                        else
                        {
                            WaitHandle.WaitAny(new WaitHandle[] { _newSendTelegram }, 100, true);
                        }
                    }
                    catch (SocketException ex)
                    {
                        Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.SendThread::SocketException", ex.Message);
                        Thread.Sleep(1000);
                    }
                }
                catch (TelegramException ex)
                {
                    Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.SendThread::TelegramException", ex.Message);
                    Thread.Sleep(1000);
                }
                catch (ThreadAbortException ex)
                {
                    Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.RcvThreading::ThreadAbortException", ex.Message);
                    return;
                }
                catch (CommunicationException ex)
                {
                    Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.SendThread::CommunicationException", ex.Message);
                    Thread.Sleep(1000);
                }
                catch (Exception ex)
                {
                    Log?.AddLog(Log.Severity.EXCEPTION, Name, "Communicator.SendThread::Exception", ex.Message);
                    Thread.Sleep(1000);
                }
            }
Example #4
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();
            }
        }