Added status icons and better connection handling

This commit is contained in:
szporowolik
2019-10-19 23:00:54 +02:00
parent 48f60abbfa
commit 2f7d2d07bb
14 changed files with 224 additions and 152 deletions

View File

@@ -109,24 +109,25 @@ public class DatabaseController
{
// General exception found
Console.WriteLine(a_ex.ToString());
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - package type: " + strUDPFrameType);
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + strUDPFrameInstance + " > ERROR MySQL - package type: " + strUDPFrameType);
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + strUDPFrameInstance + " > ERROR MySQL >" + a_ex.Message);
bStatus = false;
}
catch (MySqlException m_ex)
{
// MySQL exception found
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - package type: " + strUDPFrameType);
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + strUDPFrameInstance + " > ERROR MySQL - package type: " + strUDPFrameType);
switch (m_ex.Number)
{
case 1042: // Unable to connect to any of the specified MySQL hosts (Check Server,Port)
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - unable to connect");
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + strUDPFrameInstance + " > ERROR MySQL - unable to connect >" + m_ex.Message);
break;
case 0: // Access denied (Check DB name,username,password)
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - access denied");
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + strUDPFrameInstance + " > ERROR MySQL - access denied > " + m_ex.Message);
break;
default:
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - " + m_ex.Number);
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - " + m_ex.Message);
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + strUDPFrameInstance + " > ERROR MySQL > " + m_ex.Number);
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + strUDPFrameInstance + " > ERROR MySQL > " + m_ex.Message);
break;
}
bStatus = false;
@@ -134,9 +135,9 @@ public class DatabaseController
connMySQL.Close();
Console.WriteLine("Sending data to MySQL - Done");
}
catch (ArgumentException)
catch (ArgumentException x_ex)
{
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "ERROR MySQL - wrong connection parameters");
PerunHelper.LogHistoryAdd(ref Globals.arrLogHistory, "#" + strUDPFrameInstance + " > ERROR MySQL - unable to connect > " + x_ex.Message);
}

View File

@@ -10,8 +10,14 @@ internal class Globals
public static int intInstanceId = 0; // Instance ID
public static bool bStatusIconsForce = true; // Force icons reload
public static bool bdcConnection = false; // Historic db connection status
public static bool btcpcServer = false; // Historic tcp connection status
public static bool bTCPServer = false; // Historic tcp connection status
public static bool bSRSStatus = false; // Historic srs connection status
public static bool bLotATCStatus = false; // Historic lotatc connection status
public static bool bClientConnected = false; // Is TCP connection alive
public static int intMysqlErros = 0; // Error counter
public static int intGameErros = 0; // Error counter
public static int intGameErrosHistory = 0; // Error counter
public static int intSRSErros = 0; // Error counter
public static int intLotATCErros = 0; // Error counter
}

View File

@@ -26,7 +26,14 @@ class LogController
}
else
{
fileStream = new FileStream(logFilePath, FileMode.Append);
try
{
fileStream = new FileStream(logFilePath, FileMode.Append);
}
catch
{
}
}
log = new StreamWriter(fileStream);
log.WriteLine(strLog);

View File

@@ -61,56 +61,61 @@ public class TCPController
{
// Start listening
Console.WriteLine("TCP: Waiting for packet");
tcpClient = tcpServer.AcceptTcpClient(); //if a connection exists, the server will accept it
ns = tcpClient.GetStream(); //networkstream is used to send/receive messages
//ns.ReadTimeout = 100;
while (tcpClient.Connected && !bDone) //while the client is connected, we look for incoming messages
// Wait for pending connection
if (tcpServer.Pending())
{
StringBuilder CompleteMessage = new StringBuilder();
if (ns.CanRead)
Console.WriteLine("TCP: Waiting for packet");
tcpClient = tcpServer.AcceptTcpClient(); //if a connection exists, the server will accept it
ns = tcpClient.GetStream(); //networkstream is used to send/receive messages
//ns.ReadTimeout = 100;
Globals.bClientConnected = true;
while (tcpClient.Connected && !bDone) //while the client is connected, we look for incoming messages
{
byte[] ReadBuffer = new byte[1024];
CompleteMessage = new StringBuilder();
int numberOfBytesRead = 0;
StringBuilder CompleteMessage = new StringBuilder();
Globals.bClientConnected = true;
// Incoming message may be larger than the buffer size.
do
if (ns.CanRead)
{
numberOfBytesRead = ns.Read(ReadBuffer, 0, ReadBuffer.Length);
CompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(ReadBuffer, 0, numberOfBytesRead));
}
while (ns.DataAvailable && !bDone);
}
strReceivedData = CompleteMessage.ToString();
Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData);
byte[] ReadBuffer = new byte[1024];
CompleteMessage = new StringBuilder();
int numberOfBytesRead = 0;
try
{
dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame
string strRawTCPFrameType = dynamicRawTCPFrame.type;
PerunHelper.LogHistoryAdd(ref arrLogHistory, "#" + Globals.intInstanceId + "> TCP packet received, type: " + strRawTCPFrameType);
// Add to mySQL send buffer (find first empty slot)
for (int i = 0; i < arrSendBuffer.Length - 1; i++)
{
if (arrSendBuffer[i] == null)
// Incoming message may be larger than the buffer size.
do
{
arrSendBuffer[i] = strReceivedData;
break;
numberOfBytesRead = ns.Read(ReadBuffer, 0, ReadBuffer.Length);
CompleteMessage.AppendFormat("{0}", Encoding.ASCII.GetString(ReadBuffer, 0, numberOfBytesRead));
}
while (ns.DataAvailable && !bDone);
}
strReceivedData = CompleteMessage.ToString();
Console.WriteLine("Sender: {0} Payload: {1}", null, strReceivedData);
try
{
dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame
string strRawTCPFrameType = dynamicRawTCPFrame.type;
PerunHelper.LogHistoryAdd(ref arrLogHistory, "#" + Globals.intInstanceId + "> TCP packet received, type: " + strRawTCPFrameType);
// Add to mySQL send buffer (find first empty slot)
for (int i = 0; i < arrSendBuffer.Length - 1; i++)
{
if (arrSendBuffer[i] == null)
{
arrSendBuffer[i] = strReceivedData;
break;
}
}
}
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
PerunHelper.LogHistoryAdd(ref arrLogHistory, "TCP ERROR incorrect JSON");
catch (Exception e)
{
Globals.intGameErros++;
Console.WriteLine(e.ToString());
PerunHelper.LogHistoryAdd(ref arrLogHistory, "#" + Globals.intInstanceId + " > TCP ERROR incorrect JSON > " + e.Message);
}
}
}
}
tcpServer.Stop();
bStatus = false;
@@ -120,8 +125,9 @@ public class TCPController
// General exception found
if (e.HResult != -2147467259)
{
Globals.intGameErros++;
Console.WriteLine(e.ToString());
PerunHelper.LogHistoryAdd(ref arrLogHistory, "TCP error - connection closed or port in use");
PerunHelper.LogHistoryAdd(ref arrLogHistory, "#" + Globals.intInstanceId + " > TCP error - connection closed or port in use > " + e.Message);
}
}