mirror of
https://github.com/DaKerboul/perun.git
synced 2026-08-09 18:05:40 +02:00
Network code improvment
This commit is contained in:
@@ -35,8 +35,15 @@ class LogController
|
|||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
try
|
||||||
|
{
|
||||||
log = new StreamWriter(fileStream);
|
log = new StreamWriter(fileStream);
|
||||||
log.WriteLine(strLog);
|
log.WriteLine(strLog);
|
||||||
log.Close();
|
log.Close();
|
||||||
}
|
}
|
||||||
|
catch
|
||||||
|
{
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -60,15 +60,18 @@ public class TCPController
|
|||||||
while (!bDone)
|
while (!bDone)
|
||||||
{
|
{
|
||||||
// Start listening
|
// Start listening
|
||||||
|
Console.WriteLine("TCP: Waiting for connection");
|
||||||
|
Globals.bClientConnected = false;
|
||||||
// Wait for pending connection
|
// Wait for pending connection
|
||||||
if (tcpServer.Pending())
|
if (tcpServer.Pending())
|
||||||
{
|
{
|
||||||
Console.WriteLine("TCP: Waiting for packet");
|
Console.WriteLine("TCP: Connected");
|
||||||
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 = 100;
|
ns.ReadTimeout = 6000;
|
||||||
Globals.bClientConnected = true;
|
tcpClient.ReceiveTimeout = 6000;
|
||||||
|
|
||||||
|
|
||||||
while (tcpClient.Connected && !bDone) //while the client is connected, we look for incoming messages
|
while (tcpClient.Connected && !bDone) //while the client is connected, we look for incoming messages
|
||||||
{
|
{
|
||||||
StringBuilder CompleteMessage = new StringBuilder();
|
StringBuilder CompleteMessage = new StringBuilder();
|
||||||
@@ -76,6 +79,7 @@ public class TCPController
|
|||||||
|
|
||||||
if (ns.CanRead)
|
if (ns.CanRead)
|
||||||
{
|
{
|
||||||
|
Console.WriteLine("TCP: Can read");
|
||||||
byte[] ReadBuffer = new byte[1024];
|
byte[] ReadBuffer = new byte[1024];
|
||||||
CompleteMessage = new StringBuilder();
|
CompleteMessage = new StringBuilder();
|
||||||
int numberOfBytesRead = 0;
|
int numberOfBytesRead = 0;
|
||||||
@@ -83,10 +87,15 @@ public class TCPController
|
|||||||
// Incoming message may be larger than the buffer size.
|
// Incoming message may be larger than the buffer size.
|
||||||
do
|
do
|
||||||
{
|
{
|
||||||
|
Console.WriteLine("TCP: Read");
|
||||||
numberOfBytesRead = ns.Read(ReadBuffer, 0, ReadBuffer.Length);
|
numberOfBytesRead = ns.Read(ReadBuffer, 0, ReadBuffer.Length);
|
||||||
CompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(ReadBuffer, 0, numberOfBytesRead));
|
CompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(ReadBuffer, 0, numberOfBytesRead));
|
||||||
|
if (numberOfBytesRead == 0)
|
||||||
|
{
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
while (ns.DataAvailable && !bDone);
|
}
|
||||||
|
while (ns.DataAvailable && ns.CanRead && tcpClient.Connected);
|
||||||
}
|
}
|
||||||
strReceivedData = CompleteMessage.ToString();
|
strReceivedData = CompleteMessage.ToString();
|
||||||
Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData);
|
Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData);
|
||||||
@@ -99,6 +108,8 @@ public class TCPController
|
|||||||
PerunHelper.LogHistoryAdd(ref arrLogHistory, "#" + Globals.intInstanceId + "> TCP packet received, type: " + strRawTCPFrameType);
|
PerunHelper.LogHistoryAdd(ref arrLogHistory, "#" + Globals.intInstanceId + "> TCP packet received, type: " + strRawTCPFrameType);
|
||||||
|
|
||||||
// Add to mySQL send buffer (find first empty slot)
|
// Add to mySQL send buffer (find first empty slot)
|
||||||
|
if (Int32.Parse(strRawTCPFrameType) != 0)
|
||||||
|
{
|
||||||
for (int i = 0; i < arrSendBuffer.Length - 1; i++)
|
for (int i = 0; i < arrSendBuffer.Length - 1; i++)
|
||||||
{
|
{
|
||||||
if (arrSendBuffer[i] == null)
|
if (arrSendBuffer[i] == null)
|
||||||
@@ -108,6 +119,7 @@ public class TCPController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
Globals.intGameErros++;
|
Globals.intGameErros++;
|
||||||
@@ -116,6 +128,7 @@ public class TCPController
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
System.Threading.Thread.Sleep(500);
|
||||||
}
|
}
|
||||||
tcpServer.Stop();
|
tcpServer.Stop();
|
||||||
bStatus = false;
|
bStatus = false;
|
||||||
@@ -123,12 +136,12 @@ public class TCPController
|
|||||||
catch (Exception e)
|
catch (Exception e)
|
||||||
{
|
{
|
||||||
// General exception found
|
// General exception found
|
||||||
if (e.HResult != -2147467259)
|
//if (e.HResult != -2147467259)
|
||||||
{
|
//{
|
||||||
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);
|
||||||
}
|
//}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user