Network code improved

This commit is contained in:
szporowolik
2019-10-20 13:25:06 +02:00
parent 624347b1a0
commit 27c2163784

View File

@@ -43,7 +43,7 @@ public class TCPController
{ {
NetworkStream ns=null; NetworkStream ns=null;
TcpClient tcpClient=null; TcpClient tcpClient=null;
bool bTCPConnectionOnline = false;
while (!bDone) while (!bDone)
{ {
Console.WriteLine("TCP Listen start"); Console.WriteLine("TCP Listen start");
@@ -66,13 +66,14 @@ public class TCPController
if (tcpServer.Pending()) if (tcpServer.Pending())
{ {
Console.WriteLine("TCP: Connected"); Console.WriteLine("TCP: Connected");
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
ns = tcpClient.GetStream(); //networkstream is used to send/receive messages ns = tcpClient.GetStream(); //networkstream is used to send/receive messages
ns.ReadTimeout = 6000; ns.ReadTimeout = 6000;
tcpClient.ReceiveTimeout = 6000; tcpClient.ReceiveTimeout = 6000;
while (tcpClient.Connected && !bDone) //while the client is connected, we look for incoming messages while (tcpClient.Connected && !bDone && bTCPConnectionOnline) //while the client is connected, we look for incoming messages
{ {
StringBuilder CompleteMessage = new StringBuilder(); StringBuilder CompleteMessage = new StringBuilder();
Globals.bClientConnected = true; Globals.bClientConnected = true;
@@ -101,6 +102,8 @@ public class TCPController
Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData); Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData);
try try
{
if (strReceivedData != "")
{ {
dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame
string strRawTCPFrameType = dynamicRawTCPFrame.type; string strRawTCPFrameType = dynamicRawTCPFrame.type;
@@ -119,12 +122,17 @@ public class TCPController
} }
} }
} }
} else
{
bTCPConnectionOnline = false;
}
} }
catch (Exception e) catch (Exception e)
{ {
Globals.intGameErros++; Globals.intGameErros++;
Console.WriteLine(e.ToString()); Console.WriteLine(e.ToString());
PerunHelper.LogHistoryAdd(ref arrLogHistory, "#" + Globals.intInstanceId + " > TCP ERROR incorrect JSON > " + e.Message); PerunHelper.LogHistoryAdd(ref arrLogHistory, "#" + Globals.intInstanceId + " > TCP ERROR incorrect JSON > " + e.Message);
bTCPConnectionOnline = false;
} }
} }
} }
@@ -141,6 +149,7 @@ public class TCPController
Globals.intGameErros++; Globals.intGameErros++;
Console.WriteLine(e.ToString()); Console.WriteLine(e.ToString());
PerunHelper.LogHistoryAdd(ref arrLogHistory, "#" + Globals.intInstanceId + " > TCP error - connection closed or port in use > " + e.Message); PerunHelper.LogHistoryAdd(ref arrLogHistory, "#" + Globals.intInstanceId + " > TCP error - connection closed or port in use > " + e.Message);
bTCPConnectionOnline = false;
//} //}
} }