ReadAll.cs
6.3 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Asa.RFID
{
public class ReadAll
{
public delegate void ReceivedEvent(string ip, string id);
public event ReceivedEvent Received;
public delegate void ReceiveBufferEvent(string ip, byte[] buffer);
public event ReceiveBufferEvent ReceiveBuffer;
public delegate void LogEvent(string ip, string log);
public event LogEvent Log;
private bool _loop;
private Socket _server; //服务端
private List<Client> _client; //所有客户端
private Thread tListenClient; //监听客户端连接
private Dictionary<string, string> _id; //RFID编号
private const int CLIENT_SLEEP = 10;
public ReadAll()
{
_id = new Dictionary<string, string>();
}
public void Start(int port = 13000)
{
IPEndPoint localEP = new IPEndPoint(IPAddress.Any, port);
_server = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
_server.Bind(localEP);
_server.Listen(100);
_loop = true;
_client = new List<Client>();
tListenClient = new Thread(new ThreadStart(ListenClient));
tListenClient.Start();
}
public void Stop()
{
_loop = false;
for (int i = 0; i < _client.Count; i++)
{
_client[i].Loop = false;
_client[i].IsConn = false;
_client[i].Socket.Close();
}
_id.Clear();
_server.Close();
_client = null;
}
public string Read(string ip)
{
if (_id.TryGetValue(ip, out string value))
return value;
else
return "000";
}
public void Clear(string ip)
{
_id[ip] = "000";
}
private void ListenClient()
{
while (_loop)
{
try
{
Socket socket = _server.Accept(); //这边会暂停,不需要sleep
IPEndPoint ep = (IPEndPoint)socket.RemoteEndPoint;
Thread listen = new Thread(new ParameterizedThreadStart(ListenNet));
string ip = ep.Address.ToString();
//新的客户端
Client client = new Client
{
IP = ip,
Loop = true,
IsConn = true,
Socket = socket,
ListenNet = listen,
};
//重连后关闭旧连接
int idx = _client.FindIndex(s => s.IP.Equals(ip));
if (idx > -1)
{
_id[_client[idx].IP] = "000";
_client[idx].IsConn = false;
_client[idx].Loop = false;
_client[idx].Socket.Close();
Log?.Invoke(_client[idx].IP, "offline");
_client.RemoveAt(idx);
}
//添加到数组
if (!_id.ContainsKey(client.IP))
_id.Add(client.IP, "000");
_client.Add(client);
Log?.Invoke(client.IP, "online");
listen.Start(_client.Count - 1);
}
catch (SocketException)
{
//关闭连接,退出阻塞Accept
}
catch (Exception ex)
{
}
}
}
private void ListenNet(object obj)
{
const int LENGTH = 8; //实际使用8字节
Client client = _client[(int)obj];
List<byte> receive = new List<byte>();
byte[] buff = new byte[client.Socket.ReceiveBufferSize];
while (client.Loop)
{
Thread.Sleep(CLIENT_SLEEP);
try
{
if (!client.Loop) break;
if (client.Socket.Available > 0)
{
int count = client.Socket.Receive(buff);
byte[] temp = new byte[count];
Array.Copy(buff, 0, temp, 0, count);
receive.AddRange(temp);
Task.Run(() => ReceiveBuffer?.Invoke(client.IP, temp));
int idx = receive.FindIndex(n => n == 0x5A);
if (idx == -1)
{
receive.Clear();
continue;
}
int len = idx + LENGTH + 1; //一个系统自带的0
if (len > receive.Count)
continue;
if (receive[len - 3] == 0x4A) //一个系统的0,两个校验位
{
byte[] arr = new byte[LENGTH];
receive.CopyTo(idx, arr, 0, 4);
receive.CopyTo(idx + 5, arr, 4, 4);
Task.Run(() => TriggerEvent(client.IP, arr));
}
receive.RemoveRange(0, len);
}
}
catch (Exception ex)
{
//client.Loop = false;
}
}
}
private void TriggerEvent(string ip, byte[] buff)
{
string s;
if (buff[1] == 0)
s = "000";
else
s = (char)buff[1] + buff[2].ToString();
if (!_id[ip].Equals(s))
{
_id[ip] = s;
Log?.Invoke(ip, s);
Received?.Invoke(ip, s);
}
}
private class Client
{
public bool Loop;
public string IP;
public bool IsConn;
public Socket Socket;
public Thread ListenNet;
}
}
}