v0.8.3 - hotfix for disconnections issues , further network code improvment

This commit is contained in:
szporowolik
2019-10-21 01:53:49 +02:00
parent 61e201b261
commit 67eda5d79b
3 changed files with 52 additions and 27 deletions

View File

@@ -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,8 +73,8 @@ 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,6 +105,16 @@ public class TCPController
strReceivedData = CompleteMessage.ToString();
Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData);
string pattern = @"(\<SOT\>)+?.*?(\<EOT\>)+?";
foreach (Match match in Regex.Matches(strReceivedData, pattern))
{
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
{
if (strReceivedData != "")
@@ -137,6 +149,19 @@ public class TCPController
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);
}
}
}
}
System.Threading.Thread.Sleep(500);

View File

@@ -34,7 +34,7 @@
<CreateWebPageOnPublish>true</CreateWebPageOnPublish>
<WebPage>Perun.htm</WebPage>
<ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>0.8.2.%2a</ApplicationVersion>
<ApplicationVersion>0.8.3.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust>
<CreateDesktopShortcut>true</CreateDesktopShortcut>
<PublishWizardCompleted>true</PublishWizardCompleted>

View File

@@ -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")]