added Server/CVocaluxeServer.cs
This commit is contained in:
1 parent
29212e4c81
commit
7f71853672
10 files changed
+199
-10
No files matched your search
@@ -25,6 +25,8 @@ namespace ClientServerLib
|
||||
private List<SRequest> requests;
|
||||
private List<SRequest> 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 { }
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
{
|
||||
|
||||
+37
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user