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;
using System.Net.Sockets; using System.Net.Sockets;
using System.Text; using System.Text;
using System.Text.RegularExpressions;
using System.Threading; using System.Threading;
public class TCPController public class TCPController
@@ -42,6 +43,7 @@ public class TCPController
NetworkStream nsReadStream = null; NetworkStream nsReadStream = null;
TcpClient tcpClient = null; TcpClient tcpClient = null;
bool bTCPConnectionOnline = false; bool bTCPConnectionOnline = false;
string strReceiveBuffer;
// Main loop - do until diconnect button is clicked // Main loop - do until diconnect button is clicked
while (!bCloseConnection) while (!bCloseConnection)
@@ -71,9 +73,9 @@ public class TCPController
bTCPConnectionOnline = true; bTCPConnectionOnline = true;
tcpClient = tcpServer.AcceptTcpClient(); //if a connection exists, the server will accept it tcpClient = tcpServer.AcceptTcpClient(); //if a connection exists, the server will accept it
nsReadStream = tcpClient.GetStream(); //networkstream is used to send/receive messages nsReadStream = tcpClient.GetStream(); //networkstream is used to send/receive messages
nsReadStream.ReadTimeout = 6000; nsReadStream.ReadTimeout = 10000;
tcpClient.ReceiveTimeout = 6000; tcpClient.ReceiveTimeout = 10000;
while (tcpClient.Connected && !bCloseConnection && bTCPConnectionOnline) //while the client is connected, we look for incoming messages 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(); strReceivedData = CompleteMessage.ToString();
Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData); Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData);
try string pattern = @"(\<SOT\>)+?.*?(\<EOT\>)+?";
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 if (strReceivedData != "")
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)
{ {
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; if (arrMySQLSendBuffer[i] == null)
break; {
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; bTCPConnectionOnline = false;
} }
}
catch (Exception e) // Send empty byte to check if connection is still alive
{ try
Globals.intGameErros++; {
Console.WriteLine(e.ToString()); tcpClient.Client.Send(new byte[] { 0 }, 1, 0);
PerunHelper.GUILogHistoryAdd(ref arrGUILogHistory, "#" + Globals.intInstanceId + " > TCP ERROR incorrect JSON > " + e.Message); }
bTCPConnectionOnline = false; catch (SocketException e)
{
Console.WriteLine(e.ToString());
PerunHelper.GUILogHistoryAdd(ref arrGUILogHistory, "#" + Globals.intInstanceId + " > TCP ERROR cannot send > " + e.Message);
}
} }
} }
} }

View File

@@ -34,7 +34,7 @@
<CreateWebPageOnPublish>true</CreateWebPageOnPublish> <CreateWebPageOnPublish>true</CreateWebPageOnPublish>
<WebPage>Perun.htm</WebPage> <WebPage>Perun.htm</WebPage>
<ApplicationRevision>0</ApplicationRevision> <ApplicationRevision>0</ApplicationRevision>
<ApplicationVersion>0.8.2.%2a</ApplicationVersion> <ApplicationVersion>0.8.3.%2a</ApplicationVersion>
<UseApplicationTrust>false</UseApplicationTrust> <UseApplicationTrust>false</UseApplicationTrust>
<CreateDesktopShortcut>true</CreateDesktopShortcut> <CreateDesktopShortcut>true</CreateDesktopShortcut>
<PublishWizardCompleted>true</PublishWizardCompleted> <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 // 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: // przy użyciu symbolu „*”, tak jak pokazano poniżej:
// [assembly: AssemblyVersion("1.0.*")] // [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("0.8.2.0")] [assembly: AssemblyVersion("0.8.3.0")]
[assembly: AssemblyFileVersion("0.8.2.0")] [assembly: AssemblyFileVersion("0.8.3.0")]
[assembly: NeutralResourcesLanguage("en")] [assembly: NeutralResourcesLanguage("en")]