diff --git a/.gitignore b/.gitignore index 3fb7c02c..0e90224b 100644 --- a/.gitignore +++ b/.gitignore @@ -42,3 +42,8 @@ Output/Songs /ClientServerLib/ClientServerLib/ClientServerLib.csproj.user /ClientServerLib/ClientServerLib/bin/Debug/ClientServerLib.dll /ClientServerLib/ClientTest/bin/Debug/ClientServerLib.dll +/ClientServerLib/ClientServerLib/bin/ClientServerLib.dll +/ClientServerLib/ClientTest/bin/Debug/ClientTest.exe +/ClientServerLib/ClientTest/bin/Debug/ClientTest.vshost.exe +/ClientServerLib/ClientTest/bin/Release/ClientTest.vshost.exe +/ClientServerLib/ClientTest/bin/Release/ClientTest.vshost.exe.manifest diff --git a/ClientServerLib/ClientServerLib/CClient.cs b/ClientServerLib/ClientServerLib/CClient.cs index 162b2955..41347cf3 100644 --- a/ClientServerLib/ClientServerLib/CClient.cs +++ b/ClientServerLib/ClientServerLib/CClient.cs @@ -25,6 +25,8 @@ namespace ClientServerLib private List requests; private List responses; private OnConnectionChanged onConnectionChanged; + private OnSend onSend; + private OnReceived onReceived; public bool Connected { @@ -51,12 +53,17 @@ namespace ClientServerLib clientThread.Start(); } - public void Connect(string IP = "127.0.0.1", int Port = 3000, OnConnectionChanged OnConnectionChanged = null) + public void Connect(string IP = "127.0.0.1", int Port = 3000, + OnConnectionChanged OnConnectionChanged = null, + OnSend OnSend = null, + OnReceived OnReceived = null) { if (!doConnect) { - if (OnConnectionChanged != null) - onConnectionChanged = OnConnectionChanged; + onConnectionChanged = OnConnectionChanged; + onSend = OnSend; + onReceived = OnReceived; + ip = IP; port = Port; doConnect = true; @@ -114,7 +121,7 @@ namespace ClientServerLib connected = true; RaiseOnConnectionChanged(); } - catch + catch (Exception e) { connected = false; RaiseOnConnectionChanged(); @@ -134,6 +141,7 @@ namespace ClientServerLib try { connection.TcpClient.Close(); + connection = new CConnection(new TcpClient(), -1); } catch { @@ -184,7 +192,10 @@ namespace ClientServerLib byte[] serverParam = new byte[bytesRead]; Array.Copy(data, serverParam, bytesRead); + RaiseOnReceived(serverParam); + byte[] clientResponse = connection.CreateClientKey(serverParam); + RaiseOnSend(clientResponse); clientStream.Write(clientResponse, 0, clientResponse.Length); clientStream.Flush(); } @@ -206,7 +217,8 @@ namespace ClientServerLib { try { - responses[0].Callback(responses[0].Response); + if (responses[0].Callback != null) + responses[0].Callback(responses[0].Response); } finally { @@ -227,6 +239,7 @@ namespace ClientServerLib NetworkStream clientStream = connection.TcpClient.GetStream(); + RaiseOnSend(request.Command); byte[] encrypted = connection.Encrypt(request.Command); clientStream.Write(encrypted, 0, encrypted.Length); @@ -257,6 +270,7 @@ namespace ClientServerLib encrypted = new byte[bytesRead]; Array.Copy(data, encrypted, bytesRead); response.Response = connection.Decrypt(encrypted); + RaiseOnReceived(response.Response); return response; } @@ -272,5 +286,29 @@ namespace ClientServerLib } catch {} } + + private void RaiseOnSend(byte[] Message) + { + if (onSend == null) + return; + + try + { + onSend(Message); + } + catch { } + } + + private void RaiseOnReceived(byte[] Message) + { + if (onReceived == null) + return; + + try + { + onReceived(Message); + } + catch { } + } } } diff --git a/ClientServerLib/ClientServerLib/Structs.cs b/ClientServerLib/ClientServerLib/Structs.cs index d3f3c41f..9e1309ed 100644 --- a/ClientServerLib/ClientServerLib/Structs.cs +++ b/ClientServerLib/ClientServerLib/Structs.cs @@ -7,7 +7,10 @@ namespace ClientServerLib { public delegate void ResponseCallback(byte[] Response); public delegate byte[] HandleRequest(int ConnectionID, byte[] Message); + public delegate void OnConnectionChanged(bool Connected); + public delegate void OnSend(byte[] Message); + public delegate void OnReceived(byte[] Message); public struct SRequest { diff --git a/ClientServerLib/ClientTest/ClientTest.Designer.cs b/ClientServerLib/ClientTest/ClientTest.Designer.cs index a0dc68e9..c04db48e 100644 --- a/ClientServerLib/ClientTest/ClientTest.Designer.cs +++ b/ClientServerLib/ClientTest/ClientTest.Designer.cs @@ -41,6 +41,9 @@ this.lbDateSending = new System.Windows.Forms.Label(); this.lbDataReceiving = new System.Windows.Forms.Label(); this.tbDataReceiving = new System.Windows.Forms.TextBox(); + this.tbPassword = new System.Windows.Forms.TextBox(); + this.lbPassword = new System.Windows.Forms.Label(); + this.btLogin = new System.Windows.Forms.Button(); this.statusStrip1.SuspendLayout(); this.panel1.SuspendLayout(); this.SuspendLayout(); @@ -114,6 +117,9 @@ // // panel1 // + this.panel1.Controls.Add(this.btLogin); + this.panel1.Controls.Add(this.lbPassword); + this.panel1.Controls.Add(this.tbPassword); this.panel1.Controls.Add(this.lbServerIP); this.panel1.Controls.Add(this.btConnect); this.panel1.Controls.Add(this.tbServerIP); @@ -162,6 +168,34 @@ this.tbDataReceiving.Size = new System.Drawing.Size(804, 120); this.tbDataReceiving.TabIndex = 9; // + // tbPassword + // + this.tbPassword.Location = new System.Drawing.Point(85, 113); + this.tbPassword.Name = "tbPassword"; + this.tbPassword.Size = new System.Drawing.Size(86, 20); + this.tbPassword.TabIndex = 6; + this.tbPassword.Text = "vocaluxe"; + // + // lbPassword + // + this.lbPassword.AutoSize = true; + this.lbPassword.Location = new System.Drawing.Point(3, 116); + this.lbPassword.Name = "lbPassword"; + this.lbPassword.Size = new System.Drawing.Size(56, 13); + this.lbPassword.TabIndex = 7; + this.lbPassword.Text = "Password:"; + // + // btLogin + // + this.btLogin.Enabled = false; + this.btLogin.Location = new System.Drawing.Point(6, 139); + this.btLogin.Name = "btLogin"; + this.btLogin.Size = new System.Drawing.Size(165, 23); + this.btLogin.TabIndex = 8; + this.btLogin.Text = "Login"; + this.btLogin.UseVisualStyleBackColor = true; + this.btLogin.Click += new System.EventHandler(this.btLogin_Click); + // // ClientTest // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F); @@ -199,6 +233,9 @@ private System.Windows.Forms.Label lbDateSending; private System.Windows.Forms.Label lbDataReceiving; private System.Windows.Forms.TextBox tbDataReceiving; + private System.Windows.Forms.Button btLogin; + private System.Windows.Forms.Label lbPassword; + private System.Windows.Forms.TextBox tbPassword; } } diff --git a/ClientServerLib/ClientTest/ClientTest.cs b/ClientServerLib/ClientTest/ClientTest.cs index 4fc0f9ba..3ed470ee 100644 --- a/ClientServerLib/ClientTest/ClientTest.cs +++ b/ClientServerLib/ClientTest/ClientTest.cs @@ -37,6 +37,11 @@ namespace ClientTest Disconnect(); } + private void btLogin_Click(object sender, EventArgs e) + { + + } + private void Connect() { int port = 3000; @@ -54,7 +59,7 @@ namespace ClientTest } lbConnectionStatusText.Text = "Connecting..."; - client.Connect(tbServerIP.Text, port, OnConnectionChanged); + client.Connect(tbServerIP.Text, port, OnConnectionChanged, OnSend, OnReceived); } private void Disconnect() @@ -65,12 +70,59 @@ namespace ClientTest private void OnConnectionChanged(bool Connected) { if (Connected) - lbConnectionStatusText.Text = "Connected"; + { + this.Invoke((MethodInvoker)delegate + { + lbConnectionStatusText.Text = "Connected"; + btConnect.Text = "Disconnect"; + btLogin.Enabled = true; + }); + } else { - client.Disconnect(); - lbConnectionStatusText.Text = "Disconnected"; + this.Invoke((MethodInvoker)delegate + { + client.Disconnect(); + lbConnectionStatusText.Text = "Disconnected"; + btConnect.Text = "Connect"; + btLogin.Text = "Login"; + btLogin.Enabled = false; + }); } } + + private void OnSend(byte[] Message) + { + if (Message == null) + return; + + string text = String.Empty; + foreach (byte b in Message) + { + text += b.ToString() + " "; + } + + this.Invoke((MethodInvoker)delegate + { + tbDataSending.Text = text; + }); + } + + private void OnReceived(byte[] Message) + { + if (Message == null) + return; + + string text = String.Empty; + foreach (byte b in Message) + { + text += b.ToString() + " "; + } + + this.Invoke((MethodInvoker)delegate + { + tbDataReceiving.Text = text; + }); + } } } diff --git a/Output/ClientServerLib.dll b/Output/ClientServerLib.dll new file mode 100644 index 00000000..b762cd23 Binary files /dev/null and b/Output/ClientServerLib.dll differ diff --git a/Vocaluxe/Base/CConfig.cs b/Vocaluxe/Base/CConfig.cs index baf4b93c..73c65f55 100644 --- a/Vocaluxe/Base/CConfig.cs +++ b/Vocaluxe/Base/CConfig.cs @@ -112,6 +112,10 @@ namespace Vocaluxe.Base public static EWebcamLib WebcamLib = EWebcamLib.OpenCV; public static SWebcamConfig WebcamConfig; + // Server + public static EOffOn Server = EOffOn.TR_CONFIG_ON; + public static int ServerPort = 3000; + //Lists to save parameters and values private static readonly List _Params = new List(); private static readonly List _Values = new List(); diff --git a/Vocaluxe/Base/Server/CVocaluxeServer.cs b/Vocaluxe/Base/Server/CVocaluxeServer.cs new file mode 100644 index 00000000..b4fd8e2a --- /dev/null +++ b/Vocaluxe/Base/Server/CVocaluxeServer.cs @@ -0,0 +1,36 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; + +using ClientServerLib; +using VocaluxeLib.Menu; + +namespace Vocaluxe.Base.Server +{ + static class CVocaluxeServer + { + private static CServer _Server; + + public static void Init() + { + _Server = new CServer(RequestHandler, CConfig.ServerPort); + } + + public static void Start() + { + if (CConfig.Server == EOffOn.TR_CONFIG_ON) + _Server.Start(); + } + + public static void Close() + { + _Server.Stop(); + } + + private static byte[] RequestHandler(int ConnectionID, byte[] Message) + { + return null; + } + } +} diff --git a/Vocaluxe/Program.cs b/Vocaluxe/Program.cs index 5f204724..ea384764 100644 --- a/Vocaluxe/Program.cs +++ b/Vocaluxe/Program.cs @@ -26,6 +26,7 @@ using System.Windows.Forms; using Vocaluxe.Base; using System.Runtime.ExceptionServices; using Vocaluxe.Base.Font; +using Vocaluxe.Base.Server; namespace Vocaluxe { @@ -183,6 +184,13 @@ namespace Vocaluxe Application.DoEvents(); + // Init Server + CLog.StartBenchmark(0, "Init Server"); + CVocaluxeServer.Init(); + CLog.StopBenchmark(0, "Init Server"); + + Application.DoEvents(); + // Init Input CLog.StartBenchmark(0, "Init Input"); CController.Init(); @@ -210,6 +218,7 @@ namespace Vocaluxe // Start Main Loop _SplashScreen.Close(); + CVocaluxeServer.Start(); CDraw.MainLoop(); } @@ -219,7 +228,7 @@ namespace Vocaluxe // Unloading try { - CLog.CloseAll(); + CVocaluxeServer.Close(); CController.Close(); CSound.RecordCloseAll(); CSound.CloseAllStreams(); @@ -227,6 +236,7 @@ namespace Vocaluxe CDraw.Unload(); CDataBase.CloseConnections(); CWebcam.Close(); + CLog.CloseAll(); } catch (Exception) {} Environment.Exit(Environment.ExitCode); diff --git a/Vocaluxe/Vocaluxe.csproj b/Vocaluxe/Vocaluxe.csproj index 46d4c11c..0c75048d 100644 --- a/Vocaluxe/Vocaluxe.csproj +++ b/Vocaluxe/Vocaluxe.csproj @@ -152,6 +152,9 @@ ..\Output\AForge.Video.DirectShow.dll False + + ..\ClientServerLib\ClientServerLib\bin\ClientServerLib.dll + ..\Output\Community.CsharpSqlite.dll @@ -197,6 +200,7 @@ + Form