public override string ToString() { return(PartitionId.PadRight(3, ' ') + " " + LocalPort.ToString().PadRight(5 + 5, ' ') + " " + ForeignPort.ToString().PadRight(5 + 5, ' ') + " " + ForeignIp.ToString().PadRight(15, ' ') + " " + InboundIBBK.ToString().PadRight(20, ' ') + " " + InboundByte.ToString().PadRight(20, ' ') + " " + InboundIBBKDup.ToString().PadRight(20, ' ') + " " + InboundByteDup.ToString().PadRight(20, ' ') + " " + EfficiencyFormatter(InboundEfficiency).PadRight(6 + 7, ' ') + " " + OutboundIBBK.ToString().PadRight(20, ' ') + " " + OutboundByte.ToString().PadRight(20, ' ') + " " + OutboundIBBKDup.ToString().PadRight(20, ' ') + " " + OutboundByteDup.ToString().PadRight(20, ' ') + " " + EfficiencyFormatter(OutboundEfficiency).PadRight(6 + 8, ' ') + " " + RcvdSocket.ToString().PadRight(20, ' ') + " " + MaxRecvWindow.ToString().PadRight(10 + 3, ' ') + " " + RecvWindowClosed.ToString().PadRight(20, ' ') + " " + SendSocket.ToString().PadRight(20, ' ') + " " + MaxSendWindow.ToString().PadRight(10 + 3, ' ') + " " + Retransmitted.ToString().PadRight(20, ' ') + " " + InRetransmitMode.ToString().PadRight(20, ' ') + " " + HighestDepth.ToString().PadRight(20, ' ') + " " + Sws.ToString().PadRight(20, ' ') + " " + ProtocolFormatter(Protocol, FlagAA).PadRight(8, ' ') + " " + LocalCallFormatter(localCall).PadRight(3 + 4, ' ') + " " + StateFormatter(state).PadRight(12, ' ') + " " + StartTime.ToString().PadRight(22, ' ') + " " + EndTime.ToString().PadRight(22, ' ')); }
public Form1() { InitializeComponent(); //Создание API для общения с сервером SendSocket sendSocket = new SendSocket("127.0.0.1", 8001); api = new ConveyorApi(sendSocket); TryConnect(); }
public PluginClient(int sendPort, int receivePort) { _sendSocket = new SendSocket(HostName, sendPort); _receiveSocket = new ReceiveSocket(HostName, receivePort); _sendSocket.Connection += connected => SocketConnectionHandler(_sendSocket, connected); _receiveSocket.Connection += connected => SocketConnectionHandler(_receiveSocket, connected); _receiveSocket.MessageReceived += ReceiveSocketOnMessageReceived; _eventProcessingThread = new ProcessingThread("PluginClient Event Processing thread", EventProcessingIteration); }
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..?"); }
public PushPullSocketPortSpecs() { Context1 = NetMQContext.Create(); Context2 = NetMQContext.Create(); RcvdSignal = new ManualResetEvent(false); ClientFiber = PoolFiber.StartNew(); Pull = new PullSocket <string>(Context1, "tcp://localhost:6002", x => Encoding.Unicode.GetString(x), Channel); Push = new SendSocket <string>(Context2, "tcp://localhost:6002", s => Encoding.Unicode.GetBytes(s), ZmqSocketType.Push, false); }
public PushPullSocketPortSpecs() { Context1 = NetMQContext.Create(); Context2 = NetMQContext.Create(); RcvdSignal = new ManualResetEvent(false); ClientFiber = PoolFiber.StartNew(); Pull = new PullSocket<string>(Context1, "tcp://localhost:6002", x => Encoding.Unicode.GetString(x), Channel); Push = new SendSocket<string>(Context2, "tcp://localhost:6002", s => Encoding.Unicode.GetBytes(s), ZmqSocketType.Push, false); }
public PubSubSpecs() { ClientFiber = PoolFiber.StartNew(); Context1 = NetMQContext.Create(); Send = new SendSocket <string>(Context1, "tcp://localhost:6001", s => Encoding.Unicode.GetBytes(s)); Context2 = NetMQContext.Create(); RcvdSignal = new ManualResetEvent(false); Subscriber = new SubscribeSocket <string>(Context2, "tcp://localhost:6001", x => Encoding.Unicode.GetString(x), Channel); Subscriber.SubscribeAll(); }
public PubSubSpecs() { ClientFiber = PoolFiber.StartNew(); Context1 = NetMQContext.Create(); Send = new SendSocket<string>(Context1, "tcp://localhost:6001", s => Encoding.Unicode.GetBytes(s)); Context2 = NetMQContext.Create(); RcvdSignal = new ManualResetEvent(false); Subscriber = new SubscribeSocket<string>(Context2, "tcp://localhost:6001", x => Encoding.Unicode.GetString(x), Channel); Subscriber.SubscribeAll(); }
public void ErrorConnect() { SendSocket SendSocket = new SendSocket(address3, port3); try { SendSocket.SendMessage(new byte[] { 1, 2 }, 2); } catch { return; } Assert.Fail(); }
public void TestReadTask() { SendSocket sSock = new SendSocket(); sSock.Connect(localhost, recvPort, sendPort); RecvSocket rSock = new RecvSocket(); rSock.Connect(recvPort); rSock.BeginRead(); for (int i = 0; i < 10; ++i) { System.Threading.Thread.Sleep(1000); SendString(sSock, String.Format("{0} : Hello World", i)); } }
public void TestStopRead() { SendSocket sSock = new SendSocket(); sSock.Connect(localhost, recvPort, sendPort); RecvSocket rSock = new RecvSocket(); rSock.Connect(recvPort); rSock.BeginRead(); for (int i = 1; i < 11; ++i) { System.Threading.Thread.Sleep(1000); // kill read half way through if (i % 5 == 0) { rSock.EndRead(); } SendString(sSock, String.Format("{0} : Hello World", i)); } }
// 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); } }
// 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(); } }
public void CreateSendSocket() { SendSocket sendSocket = new SendSocket(address, port); }
// sends a string lol private void SendString(SendSocket sendSock, string message) { sendSock.Send(message); }
/// <summary> /// Создание новго АПИ для работы с конвеером /// </summary> /// <param name="SendSocket">Сокет, через который происходит связь с сервером</param> public ConveyorApi(SendSocket SendSocket) { sendSocket = SendSocket; InitialCommand(); }
// 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(); } }