Exemplo n.º 1
0
        public void TestServerLogger()
        {
            SendSocket s = new SendSocket();

            s.Connect(serverIp, serverPort, localPort);
            s.Send("HELLO WORLD!");
            s.Send("CAN YOU HEAR ME?");

            System.Threading.Thread.Sleep(4000);

            s.Send("HELLO..?");
        }
Exemplo n.º 2
0
        public bool SendMessage(string message, out string response)
        {
            if (!IsConnected)
            {
                throw new InvalidOperationException("Cannot send a message, when not open");
            }

            if (message == null)
            {
                throw new ArgumentNullException(nameof(message));
            }

            if (message.Contains("\n"))
            {
                throw new ArgumentException("Must not contain newline characters", nameof(message));
            }

            lock (_sendLock)
            {
                ClearQueue(_inputQueue);

                // Send message
                if (!_sendSocket.Send(message))
                {
                    response = null;
                    return(false);
                }

                // Wait for response
                var success = _inputQueue.TryTake(out response, SocketBase.SocketTimeout);
                if (!success)
                {
                    Log.Warning("Sent message '{Message}' but did not get any response within timeout of {SocketTimeout}", message, SocketBase.SocketTimeout);
                }

                return(success);
            }
        }
Exemplo n.º 3
0
        // Main thread for sending telegrams
        private void SendThreading()
        {
            TelegramACK telAck = null;

            try
            {
                if (SimulateMFCS)
                {
                    SendListener = new TcpListener(SendIPEndPoint);
                    Log.AddLog(Log.Severity.EVENT, Name, "Communication.SendThreading", SendIPEndPoint.ToString());
                    SendListener.Start();
                }

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

                LastSendTime = DateTime.Now;

                while (Thread.CurrentThread.ThreadState == ThreadState.Background)
                {
                    try
                    {
                        Telegram tel = null;
                        lock (_lockSendTelegram)
                            tel = SendTelegrams.FirstOrDefault(prop => prop.CommSendStatus < Telegram.CommSendStatusEnum.Ack);
                        if (DateTime.Now - LastSendTime > SendTimeOut)
                        {
                            InitSendSocket();
                            if (tel != null)
                            {
                                tel.CommSendStatus = Telegram.CommSendStatusEnum.None;
                            }
                            LastSendTime = DateTime.Now;
                            Retry        = 0;
                            Log.AddLog(Log.Severity.EVENT, Name, "Communication.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();
                                    AddSendTelegram(t);
                                    Log.AddLog(Log.Severity.EVENT, Name, "Communication.SendThread", String.Format("Adding KeepALife telegram"));
                                }
                            }
                        }
                        else if (!SendSocket.Connected)
                        {
                            InitSendSocket();
                            ConnectSendPartner();
                        }
                        else if (tel != null)
                        {
                            switch (tel.CommSendStatus)
                            {
                            case (Telegram.CommSendStatusEnum.None):
                                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, "Communication.SendThread", String.Format("Sended {0}", tel.ToString()));
                                tel.CommSendStatus = Telegram.CommSendStatusEnum.WaitACK;
                                telAck             = new TelegramACK();
                                SendTime           = DateTime.Now;
                                break;

                            case (Telegram.CommSendStatusEnum.WaitACK):
                                int numRead = 0;
                                do
                                {
                                    numRead += SendSocket.Receive(telAck.ByteBuffer, numRead, telAck.ByteBuffer.Length - numRead, SocketFlags.None);
                                } 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, "Communication.SendThreading", String.Format("Send Finished : {0}", tel.ToString()));
                                    NotifySend(tel);
                                }
                                else
                                {
                                    //                                      tel.CommSendStatus = Telegram.CommSendStatusEnum.None;
                                    Retry++;
                                    Log.AddLog(Log.Severity.EVENT, Name, "Communication.SendThreading", String.Format("Retry increased - {0}", Retry));
                                }
                                break;

                            default:
                                break;
                            }
                        }
                        else
                        {
                            Thread.Sleep(100);
                        }
                    }
                    catch (SocketException ex)
                    {
                        Log.AddLog(Log.Severity.EXCEPTION, Name, "Communication.SendThread::SocketException", ex.Message);
                        Thread.Sleep(1000);
                    }
                }
                catch (TelegramException ex)
                {
                    Log.AddLog(Log.Severity.EXCEPTION, Name, "Communication.SendThread::TelegramException", ex.Message);
                    Thread.Sleep(1000);
                }
                catch (ThreadAbortException ex)
                {
                    Log.AddLog(Log.Severity.EXCEPTION, Name, "Communication.RcvThreading::Communication", ex.Message);
                    return;
                }
                catch (CommunicationException ex)
                {
                    Log.AddLog(Log.Severity.EXCEPTION, Name, "Communication.SendThread::CommunicationException", ex.Message);
                    Thread.Sleep(1000);
                }
            }
Exemplo n.º 4
0
 // sends a string lol
 private void SendString(SendSocket sendSock, string message)
 {
     sendSock.Send(message);
 }