From df7155f061c2efb96f82c3941d0499ae202dc384 Mon Sep 17 00:00:00 2001 From: szporowolik Date: Thu, 12 Sep 2019 17:48:41 +0200 Subject: [PATCH] Switched to TCP --- .../Perun_v1/01_Classes/TCPController.cs | 87 ++++++++++--------- 02_Windows_App/Perun_v1/02_Forms/form_Main.cs | 34 +++++--- 2 files changed, 65 insertions(+), 56 deletions(-) diff --git a/02_Windows_App/Perun_v1/01_Classes/TCPController.cs b/02_Windows_App/Perun_v1/01_Classes/TCPController.cs index 92ecbf9..ef411e5 100644 --- a/02_Windows_App/Perun_v1/01_Classes/TCPController.cs +++ b/02_Windows_App/Perun_v1/01_Classes/TCPController.cs @@ -41,62 +41,65 @@ internal class TCPController public static void StartListen() { - Console.WriteLine("UDP Listen start"); - try + Console.WriteLine("TCP Listen start"); + while (!TCPController.boolDone) { - // Start listening to UDP - - tcpServer = new TcpListener(IPAddress.Any, intListenPort); - - string strReceivedData; - byte[] arrReceiveByteArray; - - // Start the main loop - TCPController.boolDone = false; - tcpServer.Start(); - while (!TCPController.boolDone) + try { - // Start listening - - Console.WriteLine("TCP: Waiting for packet"); - TcpClient tcpClient = tcpServer.AcceptTcpClient(); //if a connection exists, the server will accept it - NetworkStream ns = tcpClient.GetStream(); //networkstream is used to send/receive messages + // Start listening to UDP - while (tcpClient.Connected && !TCPController.boolDone) //while the client is connected, we look for incoming messages + tcpServer = new TcpListener(IPAddress.Any, intListenPort); + + string strReceivedData; + byte[] arrReceiveByteArray; + + // Start the main loop + TCPController.boolDone = false; + tcpServer.Start(); + while (!TCPController.boolDone) { - arrReceiveByteArray = new byte[1024]; //the messages arrive as byte array - ns.Read(arrReceiveByteArray, 0, arrReceiveByteArray.Length); //the same networkstream reads the message sent by the client - strReceivedData = Encoding.ASCII.GetString(arrReceiveByteArray, 0, arrReceiveByteArray.Length); - Console.WriteLine("Sender: {0} Payload: {1}", null , strReceivedData); + // Start listening - dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame - string strRawTCPFrameType = dynamicRawTCPFrame.type; + Console.WriteLine("TCP: Waiting for packet"); + TcpClient tcpClient = tcpServer.AcceptTcpClient(); //if a connection exists, the server will accept it + NetworkStream ns = tcpClient.GetStream(); //networkstream is used to send/receive messages - PerunHelper.LogHistoryAdd(ref arrLogHistory, "TCP packet received, type: " + strRawTCPFrameType); - - // Add to mySQL send buffer (find first empty slot) - for (int i = 0; i < arrSendBuffer.Length - 1; i++) + while (tcpClient.Connected && !TCPController.boolDone) //while the client is connected, we look for incoming messages { - if (arrSendBuffer[i] == null) + arrReceiveByteArray = new byte[1024]; //the messages arrive as byte array + ns.Read(arrReceiveByteArray, 0, arrReceiveByteArray.Length); //the same networkstream reads the message sent by the client + strReceivedData = Encoding.ASCII.GetString(arrReceiveByteArray, 0, arrReceiveByteArray.Length); + Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData); + + dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame + string strRawTCPFrameType = dynamicRawTCPFrame.type; + + PerunHelper.LogHistoryAdd(ref arrLogHistory, "TCP packet received, type: " + strRawTCPFrameType); + + // Add to mySQL send buffer (find first empty slot) + for (int i = 0; i < arrSendBuffer.Length - 1; i++) { - arrSendBuffer[i] = strReceivedData; - break; + if (arrSendBuffer[i] == null) + { + arrSendBuffer[i] = strReceivedData; + break; + } } } - } + } + tcpServer.Stop(); } - tcpServer.Stop(); - } - catch (Exception e) - { - // General exception found - if (e.HResult != -2147467259) + catch (Exception e) { - Console.WriteLine(e.ToString()); - PerunHelper.LogHistoryAdd(ref arrLogHistory, "ERROR UDP - port may be in use"); + // General exception found + if (e.HResult != -2147467259) + { + Console.WriteLine(e.ToString()); + PerunHelper.LogHistoryAdd(ref arrLogHistory, "ERROR TCP - port may be in use"); + } } + //Console.WriteLine("TCP listen stop"); } - Console.WriteLine("TCP listen stop"); } } diff --git a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs index 2df3fca..fbaee8e 100644 --- a/02_Windows_App/Perun_v1/02_Forms/form_Main.cs +++ b/02_Windows_App/Perun_v1/02_Forms/form_Main.cs @@ -109,10 +109,15 @@ namespace Perun_v1 private void con_Button_Listen_ON_Click(object sender, EventArgs e) { // Start listening - UDPController.Create(48620, ref Globals.arrLogHistory, ref arrSendBuffer); - UDPController.thrUDPListener = new Thread(UDPController.StartListen); - UDPController.thrUDPListener.Start(); - UDPController.thrUDPListener.Name = "UDPThread"; + //UDPController.Create(48620, ref Globals.arrLogHistory, ref arrSendBuffer); + //UDPController.thrUDPListener = new Thread(UDPController.StartListen); + //UDPController.thrUDPListener.Start(); + //UDPController.thrUDPListener.Name = "UDPThread"; + + TCPController.Create(48620, ref Globals.arrLogHistory, ref arrSendBuffer); + TCPController.thrTCPListener = new Thread(TCPController.StartListen); + TCPController.thrTCPListener.Start(); + TCPController.thrTCPListener.Name = "TCPThread"; form_Main_DisableControls(); // Disable controlls form_Main_SaveSettings(); // Save settings @@ -131,7 +136,8 @@ namespace Perun_v1 // Stop listening try { - UDPController.StopListen(); + //UDPController.StopListen(); + TCPController.StopListen(); } catch (Exception ex) { @@ -334,26 +340,26 @@ namespace Perun_v1 if (raw_lotatc.Count > 0) { strSRSJson = JsonConvert.SerializeObject(raw_lotatc); - strSRSJson = "{'type':'100','instance':'1','payload':'" + strSRSJson + "'}"; + strSRSJson = "{'type':'100','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':'" + strSRSJson + "'}"; } else { - strSRSJson = "{'type':'100','instance':'1','payload':{'ignore':'false'}}"; // No SRS clients connected + strSRSJson = "{'type':'100','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':{'ignore':'false'}}"; // No SRS clients connected } boolSRSdefault = false; - PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#1 > SRS data loaded"); + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Int32.Parse(con_txt_dcs_instance.Text) + " > SRS data loaded"); } catch { - PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#1 > SRS data ERROR"); + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Int32.Parse(con_txt_dcs_instance.Text) + " > SRS data ERROR"); } } if (boolSRSdefault) { - strSRSJson = "{'type':'100','instance':'1','payload':{'ignore':'true'}}"; + strSRSJson = "{'type':'100','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':{'ignore':'true'}}"; } DatabaseController.SendToMySql(strSRSJson); @@ -365,20 +371,20 @@ namespace Perun_v1 strLotATCJson = System.IO.File.ReadAllText(con_txt_3rd_lotatc.Text); dynamic raw_srs = JsonConvert.DeserializeObject(strLotATCJson); - strLotATCJson = "{'type':'101','instance':'1','payload':'" + strLotATCJson + "'}"; + strLotATCJson = "{'type':'101','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':'" + strLotATCJson + "'}"; boolLotATCdefault = false; - PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#1 > LotATC data loaded"); + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Int32.Parse(con_txt_dcs_instance.Text) + " > LotATC data loaded"); } catch { - PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#1 > LotATC data ERROR"); + PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + Int32.Parse(con_txt_dcs_instance.Text) + " > LotATC data ERROR"); } } if (boolLotATCdefault) { - strLotATCJson = "{'type':'101','instance':'1','payload':{'ignore':'true'}}"; // No LotATC controller connected + strLotATCJson = "{'type':'101','instance':'" + Int32.Parse(con_txt_dcs_instance.Text) + "','payload':{'ignore':'true'}}"; // No LotATC controller connected } DatabaseController.SendToMySql(strLotATCJson); }