From 67eda5d79b9a2ba31e539c22eed2864ec75f2c2b Mon Sep 17 00:00:00 2001 From: szporowolik Date: Mon, 21 Oct 2019 01:53:49 +0200 Subject: [PATCH] v0.8.3 - hotfix for disconnections issues , further network code improvment --- .../Perun_v1/01_Classes/TCPController.cs | 73 +++++++++++++------ 02_Windows_App/Perun_v1/Perun_v1.csproj | 2 +- .../Perun_v1/Properties/AssemblyInfo.cs | 4 +- 3 files changed, 52 insertions(+), 27 deletions(-) diff --git a/02_Windows_App/Perun_v1/01_Classes/TCPController.cs b/02_Windows_App/Perun_v1/01_Classes/TCPController.cs index fd5f725..4e8ce88 100644 --- a/02_Windows_App/Perun_v1/01_Classes/TCPController.cs +++ b/02_Windows_App/Perun_v1/01_Classes/TCPController.cs @@ -4,6 +4,7 @@ using System; using System.Net; using System.Net.Sockets; using System.Text; +using System.Text.RegularExpressions; using System.Threading; public class TCPController @@ -42,6 +43,7 @@ public class TCPController NetworkStream nsReadStream = null; TcpClient tcpClient = null; bool bTCPConnectionOnline = false; + string strReceiveBuffer; // Main loop - do until diconnect button is clicked while (!bCloseConnection) @@ -71,9 +73,9 @@ public class TCPController bTCPConnectionOnline = true; tcpClient = tcpServer.AcceptTcpClient(); //if a connection exists, the server will accept it nsReadStream = tcpClient.GetStream(); //networkstream is used to send/receive messages - nsReadStream.ReadTimeout = 6000; - tcpClient.ReceiveTimeout = 6000; - + nsReadStream.ReadTimeout = 10000; + tcpClient.ReceiveTimeout = 10000; + while (tcpClient.Connected && !bCloseConnection && bTCPConnectionOnline) //while the client is connected, we look for incoming messages { @@ -103,39 +105,62 @@ public class TCPController strReceivedData = CompleteMessage.ToString(); Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData); - try + string pattern = @"(\)+?.*?(\)+?"; + + foreach (Match match in Regex.Matches(strReceivedData, pattern)) { - if (strReceivedData != "") + strReceiveBuffer = match.Value; + + Console.WriteLine("Found '{0}' at position {1} end {2}",match.Value, match.Index,match.Length); + Console.WriteLine("Prepared {0}", strReceiveBuffer.Substring(5, match.Length-10)); + + strReceivedData = strReceiveBuffer.Substring(5, match.Length - 10); + try { - dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame - string strRawTCPFrameType = dynamicRawTCPFrame.type; - - PerunHelper.GUILogHistoryAdd(ref arrGUILogHistory, "#" + Globals.intInstanceId + "> TCP packet received, type: " + strRawTCPFrameType); - - // Add to mySQL send buffer (find first empty slot) - if (Int32.Parse(strRawTCPFrameType) != 0) + if (strReceivedData != "") { - for (int i = 0; i < arrMySQLSendBuffer.Length - 1; i++) + dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame + string strRawTCPFrameType = dynamicRawTCPFrame.type; + + PerunHelper.GUILogHistoryAdd(ref arrGUILogHistory, "#" + Globals.intInstanceId + "> TCP packet received, type: " + strRawTCPFrameType); + + // Add to mySQL send buffer (find first empty slot) + if (Int32.Parse(strRawTCPFrameType) != 0) { - if (arrMySQLSendBuffer[i] == null) + for (int i = 0; i < arrMySQLSendBuffer.Length - 1; i++) { - arrMySQLSendBuffer[i] = strReceivedData; - break; + if (arrMySQLSendBuffer[i] == null) + { + arrMySQLSendBuffer[i] = strReceivedData; + break; + } } } } + else + { + bTCPConnectionOnline = false; + } } - else + catch (Exception e) { + Globals.intGameErros++; + Console.WriteLine(e.ToString()); + PerunHelper.GUILogHistoryAdd(ref arrGUILogHistory, "#" + Globals.intInstanceId + " > TCP ERROR incorrect JSON > " + e.Message); bTCPConnectionOnline = false; } - } - catch (Exception e) - { - Globals.intGameErros++; - Console.WriteLine(e.ToString()); - PerunHelper.GUILogHistoryAdd(ref arrGUILogHistory, "#" + Globals.intInstanceId + " > TCP ERROR incorrect JSON > " + e.Message); - bTCPConnectionOnline = false; + + // Send empty byte to check if connection is still alive + try + { + tcpClient.Client.Send(new byte[] { 0 }, 1, 0); + } + catch (SocketException e) + { + Console.WriteLine(e.ToString()); + PerunHelper.GUILogHistoryAdd(ref arrGUILogHistory, "#" + Globals.intInstanceId + " > TCP ERROR cannot send > " + e.Message); + } + } } } diff --git a/02_Windows_App/Perun_v1/Perun_v1.csproj b/02_Windows_App/Perun_v1/Perun_v1.csproj index 346de2f..61e91ee 100644 --- a/02_Windows_App/Perun_v1/Perun_v1.csproj +++ b/02_Windows_App/Perun_v1/Perun_v1.csproj @@ -34,7 +34,7 @@ true Perun.htm 0 - 0.8.2.%2a + 0.8.3.%2a false true true diff --git a/02_Windows_App/Perun_v1/Properties/AssemblyInfo.cs b/02_Windows_App/Perun_v1/Properties/AssemblyInfo.cs index bfcf35b..c0d428c 100644 --- a/02_Windows_App/Perun_v1/Properties/AssemblyInfo.cs +++ b/02_Windows_App/Perun_v1/Properties/AssemblyInfo.cs @@ -33,7 +33,7 @@ using System.Runtime.InteropServices; // Możesz określić wszystkie wartości lub użyć domyślnych numerów kompilacji i poprawki // przy użyciu symbolu „*”, tak jak pokazano poniżej: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.8.2.0")] -[assembly: AssemblyFileVersion("0.8.2.0")] +[assembly: AssemblyVersion("0.8.3.0")] +[assembly: AssemblyFileVersion("0.8.3.0")] [assembly: NeutralResourcesLanguage("en")]