JAKAServer.cs
2.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
using Dolen.Communication.TCP;
using System;
using System.Collections.Generic;
namespace JAKA
{
public class JAKAServer
{
Dictionary<string, JAKABean> JAKAs;
TCPServer TCPServer;
public JAKAServer(string[] clientips, string serverIp = "0.0.0.0", int port = 5000)
{
JAKAs = new Dictionary<string, JAKABean>();
TCPServer = new TCPServer(serverIp,port);
TCPServer.RecvMsgEvent += TCPServer_RecvMsgEvent;
TCPServer.ClientChangedEvent += TCPServer_ClientChangedEvent;
foreach (string ip in clientips)
{
JAKAs.Add(ip, new JAKABean(ip));
}
}
private void TCPServer_ClientChangedEvent(List<Client> clientKey)
{
foreach (string ip in JAKAs.Keys)
{
Client client = clientKey.Find(s => s.IP.Equals(ip));
if (client!=null)
{
JAKAs[ip].RemoteEndPoint = client.EndPoint;
JAKAs[ip].RobotData.Online = true;
}
else
{
JAKAs[ip].RemoteEndPoint = "";
JAKAs[ip].RobotData.Online = false;
}
}
}
private void TCPServer_RecvMsgEvent(string clientKey, string msg)
{
string ip = clientKey.Split(':')[0];
JAKABean jAKA = JAKAs[ip];
if(jAKA != null)
{
jAKA.RobotData.RecvMsg = msg;
}
}
public void Start()
{
TCPServer.Start();
foreach (JAKABean item in JAKAs.Values)
{
item.Start();
}
}
public void Stop()
{
TCPServer.Stop();
}
public void AssociateControl(UCRobot uCRobot, JAKABean jAKABean)
{
uCRobot.Regidter(jAKABean, this);
}
public JAKABean GetJAKABean(string ip)
{
return JAKAs[ip];
}
public void SendCmd(string endpoint, string cmd)
{
string ip=endpoint.Split(':')[0];
if(JAKAs.ContainsKey(ip))
{
JAKAs[ip].RobotData.RecvMsg = "";
JAKAs[ip].RobotData.CurCmd = cmd;
TCPServer.SendMsg(endpoint, cmd);
}
}
}
}