add webSocket
This commit is contained in:
parent
bac52d315b
commit
1f97891439
1668
DroneSimulator/FormMain.Designer.cs
generated
1668
DroneSimulator/FormMain.Designer.cs
generated
File diff suppressed because it is too large
Load Diff
@ -8,201 +8,210 @@ using System.Runtime.InteropServices;
|
|||||||
using System.CodeDom;
|
using System.CodeDom;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
|
|
||||||
|
using System.Net.WebSockets;
|
||||||
|
using System.Threading;
|
||||||
|
using System.Threading.Tasks;
|
||||||
|
using System.Windows.Forms;
|
||||||
|
|
||||||
namespace DroneSimulator
|
namespace DroneSimulator
|
||||||
{
|
{
|
||||||
public partial class Form_Main : Form
|
public partial class Form_Main : Form
|
||||||
{
|
|
||||||
Screen2D screen2D = null;
|
|
||||||
|
|
||||||
NetServerClients netServerClient = new NetServerClients();
|
|
||||||
NetServerVisual netServerVisual = new NetServerVisual();
|
|
||||||
|
|
||||||
List<Drone> AllDrones = new List<Drone>();
|
|
||||||
|
|
||||||
public Form_Main()
|
|
||||||
{
|
{
|
||||||
InitializeComponent();
|
Screen2D screen2D = null;
|
||||||
}
|
|
||||||
|
|
||||||
private void ClientConnectionCallback(object o)
|
NetServerClients netServerClient = new NetServerClients();
|
||||||
{
|
NetServerVisual netServerVisual = new NetServerVisual();
|
||||||
NetServerClients.ConnectData data = (NetServerClients.ConnectData)o;
|
|
||||||
|
|
||||||
Invoke((MethodInvoker)delegate
|
List<Drone> AllDrones = new List<Drone>();
|
||||||
{
|
private ClientWebSocket _webSocket;
|
||||||
label_Clients_Num.Text = data.Count.ToString();
|
private CancellationTokenSource _cts;
|
||||||
});
|
private bool _isRunning;
|
||||||
|
public Form_Main()
|
||||||
if (data.Connect)
|
|
||||||
{
|
|
||||||
Drone drone = new Drone(data.ID);
|
|
||||||
drone.Create(1.0f, 0.5f, 1.0f);
|
|
||||||
|
|
||||||
screen2D.CreateDrone(Color.Red, data.ID);
|
|
||||||
|
|
||||||
AllDrones.Add(drone);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
foreach (Drone drone in AllDrones)
|
|
||||||
{
|
{
|
||||||
if (drone.ID != data.ID) continue;
|
InitializeComponent();
|
||||||
drone.Close();
|
|
||||||
|
|
||||||
screen2D.RemoveDrone(data.ID);
|
|
||||||
|
|
||||||
AllDrones.Remove(drone);
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void ClientReceiveCallback(object o)
|
private void ClientConnectionCallback(object o)
|
||||||
{
|
|
||||||
NetServerClients.ReceiveData data = (NetServerClients.ReceiveData)o;
|
|
||||||
|
|
||||||
Drone? drone = null;
|
|
||||||
|
|
||||||
foreach (Drone d in AllDrones)
|
|
||||||
{
|
|
||||||
if (d.ID != data.ID) continue;
|
|
||||||
drone = d;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (drone == null) return;
|
|
||||||
|
|
||||||
List<byte[]?>? send = drone.DataStream(data.Buffer, data.Size);
|
|
||||||
|
|
||||||
if (send == null) return;
|
|
||||||
try
|
|
||||||
{
|
|
||||||
foreach (byte[]? b in send)
|
|
||||||
{
|
{
|
||||||
if (b != null) data.Client?.Send(b);
|
NetServerClients.ConnectData data = (NetServerClients.ConnectData)o;
|
||||||
|
|
||||||
|
Invoke((MethodInvoker)delegate
|
||||||
|
{
|
||||||
|
label_Clients_Num.Text = data.Count.ToString();
|
||||||
|
});
|
||||||
|
|
||||||
|
if (data.Connect)
|
||||||
|
{
|
||||||
|
Drone drone = new Drone(data.ID);
|
||||||
|
drone.Create(1.0f, 0.5f, 1.0f);
|
||||||
|
|
||||||
|
screen2D.CreateDrone(Color.Red, data.ID);
|
||||||
|
|
||||||
|
AllDrones.Add(drone);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
foreach (Drone drone in AllDrones)
|
||||||
|
{
|
||||||
|
if (drone.ID != data.ID) continue;
|
||||||
|
drone.Close();
|
||||||
|
|
||||||
|
screen2D.RemoveDrone(data.ID);
|
||||||
|
|
||||||
|
AllDrones.Remove(drone);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
catch { }
|
private void ClientReceiveCallback(object o)
|
||||||
|
{
|
||||||
|
NetServerClients.ReceiveData data = (NetServerClients.ReceiveData)o;
|
||||||
|
|
||||||
|
Drone? drone = null;
|
||||||
|
|
||||||
|
foreach (Drone d in AllDrones)
|
||||||
|
{
|
||||||
|
if (d.ID != data.ID) continue;
|
||||||
|
drone = d;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (drone == null) return;
|
||||||
|
|
||||||
|
List<byte[]?>? send = drone.DataStream(data.Buffer, data.Size);
|
||||||
|
|
||||||
|
if (send == null) return;
|
||||||
|
try
|
||||||
|
{
|
||||||
|
foreach (byte[]? b in send)
|
||||||
|
{
|
||||||
|
if (b != null) data.Client?.Send(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
|
||||||
|
private void button_Client_Start_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
var done = netServerClient.StartServer((int)numericUpDown_Clients_Port.Value, (int)numericUpDown_Clients_Limit.Value, ClientConnectionCallback, ClientReceiveCallback);
|
||||||
|
switch (done)
|
||||||
|
{
|
||||||
|
case NetServerClients.ServerState.Error:
|
||||||
|
{
|
||||||
|
MessageBox.Show("Error to start clients server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case NetServerClients.ServerState.Start:
|
||||||
|
{
|
||||||
|
button_Client_Start.Text = "Stop";
|
||||||
|
button_Client_Start.BackColor = Color.LimeGreen;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case NetServerClients.ServerState.Stop:
|
||||||
|
{
|
||||||
|
label_Clients_Num.Text = "0";
|
||||||
|
button_Client_Start.Text = "Start";
|
||||||
|
button_Client_Start.BackColor = Color.Transparent;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (done != NetServerClients.ServerState.Start) return;
|
||||||
|
|
||||||
|
pictureBox_2D.Image = null;
|
||||||
|
|
||||||
|
screen2D = new Screen2D(DrawCallback);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void DrawCallback(Bitmap bmp)
|
||||||
|
{
|
||||||
|
Invoke((MethodInvoker)delegate
|
||||||
|
{
|
||||||
|
if (pictureBox_2D.Image == null) pictureBox_2D.Image = bmp;
|
||||||
|
pictureBox_2D.Refresh();
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
private void timer_Test_Tick(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
if (screen2D == null) return;
|
||||||
|
|
||||||
|
foreach (Drone d in AllDrones)
|
||||||
|
{
|
||||||
|
screen2D.Move(d.ID, d.PosXYZ, d.GetOrientation());
|
||||||
|
}
|
||||||
|
|
||||||
|
screen2D.DrawScene();
|
||||||
|
}
|
||||||
|
private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
|
||||||
|
{
|
||||||
|
foreach (Drone d in AllDrones) d.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void VisualConnectionCallback(object o)
|
||||||
|
{
|
||||||
|
NetServerVisual.ConnectData data = (NetServerVisual.ConnectData)o;
|
||||||
|
|
||||||
|
Invoke((MethodInvoker)delegate
|
||||||
|
{
|
||||||
|
label_Clients_Num.Text = data.Count.ToString();
|
||||||
|
});
|
||||||
|
|
||||||
|
if (data.Connect)
|
||||||
|
{
|
||||||
|
//---
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
//---
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void VisualReceiveCallback(object o)
|
||||||
|
{
|
||||||
|
NetServerVisual.ReceiveData data = (NetServerVisual.ReceiveData)o;
|
||||||
|
|
||||||
|
foreach (Drone d in AllDrones)
|
||||||
|
{
|
||||||
|
Drone.DataVisual v = d.GetVisual();
|
||||||
|
|
||||||
|
try { data.Client.Send(Drone.getBytes(v)); }
|
||||||
|
catch { }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private void button_Visual_Start_Click(object sender, EventArgs e)
|
||||||
|
{
|
||||||
|
var done = netServerVisual.StartServer((int)numericUpDown_Visual_Port.Value, (int)numericUpDown_Visual_Limit.Value, VisualConnectionCallback, VisualReceiveCallback);
|
||||||
|
switch (done)
|
||||||
|
{
|
||||||
|
case NetServerVisual.ServerState.Error:
|
||||||
|
{
|
||||||
|
MessageBox.Show("Error to start visual server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case NetServerVisual.ServerState.Start:
|
||||||
|
{
|
||||||
|
button_Visual_Start.Text = "Stop";
|
||||||
|
button_Visual_Start.BackColor = Color.LimeGreen;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case NetServerVisual.ServerState.Stop:
|
||||||
|
{
|
||||||
|
label_Visual_Num.Text = "0";
|
||||||
|
button_Visual_Start.Text = "Start";
|
||||||
|
button_Visual_Start.BackColor = Color.Transparent;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void button_Client_Start_Click(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
var done = netServerClient.StartServer((int)numericUpDown_Clients_Port.Value, (int)numericUpDown_Clients_Limit.Value, ClientConnectionCallback, ClientReceiveCallback);
|
|
||||||
switch (done)
|
|
||||||
{
|
|
||||||
case NetServerClients.ServerState.Error:
|
|
||||||
{
|
|
||||||
MessageBox.Show("Error to start clients server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case NetServerClients.ServerState.Start:
|
|
||||||
{
|
|
||||||
button_Client_Start.Text = "Stop";
|
|
||||||
button_Client_Start.BackColor = Color.LimeGreen;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case NetServerClients.ServerState.Stop:
|
|
||||||
{
|
|
||||||
label_Clients_Num.Text = "0";
|
|
||||||
button_Client_Start.Text = "Start";
|
|
||||||
button_Client_Start.BackColor = Color.Transparent;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (done != NetServerClients.ServerState.Start) return;
|
|
||||||
|
|
||||||
pictureBox_2D.Image = null;
|
|
||||||
|
|
||||||
screen2D = new Screen2D(DrawCallback);
|
|
||||||
}
|
|
||||||
|
|
||||||
private void exitToolStripMenuItem_Click(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void DrawCallback(Bitmap bmp)
|
|
||||||
{
|
|
||||||
Invoke((MethodInvoker)delegate
|
|
||||||
{
|
|
||||||
if (pictureBox_2D.Image == null) pictureBox_2D.Image = bmp;
|
|
||||||
pictureBox_2D.Refresh();
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void timer_Test_Tick(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
if (screen2D == null) return;
|
|
||||||
|
|
||||||
foreach (Drone d in AllDrones)
|
|
||||||
{
|
|
||||||
screen2D.Move(d.ID, d.PosXYZ, d.GetOrientation());
|
|
||||||
}
|
|
||||||
|
|
||||||
screen2D.DrawScene();
|
|
||||||
}
|
|
||||||
private void Form_Main_FormClosing(object sender, FormClosingEventArgs e)
|
|
||||||
{
|
|
||||||
foreach (Drone d in AllDrones) d.Close();
|
|
||||||
}
|
|
||||||
|
|
||||||
private void VisualConnectionCallback(object o)
|
|
||||||
{
|
|
||||||
NetServerVisual.ConnectData data = (NetServerVisual.ConnectData)o;
|
|
||||||
|
|
||||||
Invoke((MethodInvoker)delegate
|
|
||||||
{
|
|
||||||
label_Clients_Num.Text = data.Count.ToString();
|
|
||||||
});
|
|
||||||
|
|
||||||
if (data.Connect)
|
|
||||||
{
|
|
||||||
//---
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
//---
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void VisualReceiveCallback(object o)
|
|
||||||
{
|
|
||||||
NetServerVisual.ReceiveData data = (NetServerVisual.ReceiveData)o;
|
|
||||||
|
|
||||||
foreach (Drone d in AllDrones)
|
|
||||||
{
|
|
||||||
Drone.DataVisual v = d.GetVisual();
|
|
||||||
|
|
||||||
try { data.Client.Send(Drone.getBytes(v)); }
|
|
||||||
catch { }
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
private void button_Visual_Start_Click(object sender, EventArgs e)
|
|
||||||
{
|
|
||||||
var done = netServerVisual.StartServer((int)numericUpDown_Visual_Port.Value, (int)numericUpDown_Visual_Limit.Value, VisualConnectionCallback, VisualReceiveCallback);
|
|
||||||
switch (done)
|
|
||||||
{
|
|
||||||
case NetServerVisual.ServerState.Error:
|
|
||||||
{
|
|
||||||
MessageBox.Show("Error to start visual server", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case NetServerVisual.ServerState.Start:
|
|
||||||
{
|
|
||||||
button_Visual_Start.Text = "Stop";
|
|
||||||
button_Visual_Start.BackColor = Color.LimeGreen;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case NetServerVisual.ServerState.Stop:
|
|
||||||
{
|
|
||||||
label_Visual_Num.Text = "0";
|
|
||||||
button_Visual_Start.Text = "Start";
|
|
||||||
button_Visual_Start.BackColor = Color.Transparent;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user