WIP - main form change

This commit is contained in:
szporowolik
2019-09-12 16:00:53 +02:00
parent 4900ef4888
commit 52ecfda6c5
7 changed files with 112 additions and 285 deletions

View File

@@ -11,37 +11,22 @@ namespace Perun_v1
/// </summary>
// Mutex to block multiple instances
static Mutex mutex = new Mutex(true, "{5a710507-92e9-4664-9b91-918b8b82f107}");
//static Mutex mutex = new Mutex(true, "{5a710507-92e9-4664-9b91-918b8b82f107}");
[STAThread]
static void Main()
{
// Check if no other instances running
if (mutex.WaitOne(TimeSpan.Zero, true))
{
// Only instance
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
// Only instance
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.ApplicationExit += new EventHandler(Application_ApplicationExit);
void Application_ApplicationExit(object sender, EventArgs e)
{
Properties.Settings.Default.Save(); // We will save settings on exit
}
Application.Run(new form_Main()); // Run main form
mutex.ReleaseMutex(); // Release mutex
}
else
void Application_ApplicationExit(object sender, EventArgs e)
{
// Multiple instance - send our Win32 message to make the currently running instance jump to front
NativeMethods.PostMessage(
(IntPtr)NativeMethods.HWND_BROADCAST,
NativeMethods.WM_SHOWME,
IntPtr.Zero,
IntPtr.Zero);
MessageBox.Show("Only one Perun instance is allowed.", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Information);
Properties.Settings.Default.Save(); // We will save settings on exit
}
Application.Run(new form_Main()); // Run main form
}
}
}

View File

@@ -29,7 +29,7 @@ internal class TCPController
{
// FInish listening
TCPController.boolDone = true;
TCPController.tcpServer.Close();
TCPController.tcpServer.Stop();
TCPController.tcpServer = null;
for (int i = 0; i < arrSendBuffer.Length - 1; i++)
@@ -57,27 +57,36 @@ internal class TCPController
while (!TCPController.boolDone)
{
// Start listening
Console.WriteLine("TCP: Waiting for packet");
arrReceiveByteArray = udpListener.Receive(ref ipendpointGroupEP);
strReceivedData = Encoding.ASCII.GetString(arrReceiveByteArray, 0, arrReceiveByteArray.Length);
Console.WriteLine("Sender: {0} Payload: {1}", ipendpointGroupEP.ToString(), strReceivedData);
TcpClient tcpClient = tcpServer.AcceptTcpClient(); //if a connection exists, the server will accept it
NetworkStream ns = tcpClient.GetStream(); //networkstream is used to send/receive messages
// Add to log history and rotate
dynamic dynamicRawUDPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame
string strRawUDPFrameType = dynamicRawUDPFrame.type;
PerunHelper.LogHistoryAdd(ref arrLogHistory, "UDP packet received, type: " + strRawUDPFrameType);
// Add to mySQL send buffer (find first empty slot)
for (int i = 0; i < arrSendBuffer.Length - 1; i++)
while (tcpClient.Connected && !TCPController.boolDone) //while the client is connected, we look for incoming messages
{
if (arrSendBuffer[i] == null)
arrReceiveByteArray = new byte[1024]; //the messages arrive as byte array
ns.Read(arrReceiveByteArray, 0, arrReceiveByteArray.Length); //the same networkstream reads the message sent by the client
strReceivedData = Encoding.ASCII.GetString(arrReceiveByteArray, 0, arrReceiveByteArray.Length);
Console.WriteLine("Sender: {0} Payload: {1}", null , strReceivedData);
dynamic dynamicRawTCPFrame = JsonConvert.DeserializeObject(strReceivedData); // Deserialize received frame
string strRawTCPFrameType = dynamicRawTCPFrame.type;
PerunHelper.LogHistoryAdd(ref arrLogHistory, "TCP packet received, type: " + strRawTCPFrameType);
// Add to mySQL send buffer (find first empty slot)
for (int i = 0; i < arrSendBuffer.Length - 1; i++)
{
arrSendBuffer[i] = strReceivedData;
break;
if (arrSendBuffer[i] == null)
{
arrSendBuffer[i] = strReceivedData;
break;
}
}
}
}
udpListener.Close(); // Close port
tcpServer.Stop();
}
catch (Exception e)
{
@@ -88,6 +97,6 @@ internal class TCPController
PerunHelper.LogHistoryAdd(ref arrLogHistory, "ERROR UDP - port may be in use");
}
}
Console.WriteLine("UDP listen stop");
Console.WriteLine("TCP listen stop");
}
}