NetworkAlienClient.cs
9.77 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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Sockets;
using System.Text;
using System.Threading;
using HslCommunication.Core.IMessage;
namespace HslCommunication.Core.Net
{
/// <summary>
/// 异形客户端的基类,提供了基础的异形操作
/// </summary>
public class NetworkAlienClient : NetworkServerBase
{
#region Constructor
/// <summary>
/// 默认的无参构造方法
/// </summary>
public NetworkAlienClient( )
{
password = new byte[6];
alreadyLock = new SimpleHybirdLock( );
alreadyOnline = new List<AlienSession>( );
trustOnline = new List<string>( );
trustLock = new SimpleHybirdLock( );
ThreadCheckStart( );
}
#endregion
#region NetworkServerBase Override
/// <summary>
/// 当接收到了新的请求的时候执行的操作
/// </summary>
/// <param name="socket">异步对象</param>
/// <param name="endPoint">终结点</param>
protected override void ThreadPoolLogin( Socket socket, IPEndPoint endPoint )
{
// 注册包
// 0x48 0x73 0x6E 0x00 0x17 0x31 0x32 0x33 0x34 0x35 0x36 0x37 0x38 0x39 0x30 0x31 0x00 0x00 0x00 0x00 0x00 0x00 0xC0 0xA8 0x00 0x01 0x17 0x10
// +------------+ +--+ +--+ +----------------------------------------------------+ +---------------------------+ +-----------------+ +-------+
// + 固定消息头 +备用 长度 DTU码 12345678901 (唯一标识) 登录密码(不受信的排除) Ip:192.168.0.1 端口10000
// +------------+ +--+ +--+ +----------------------------------------------------+ +---------------------------+ +-----------------+
// 返回
// 0x48 0x73 0x6E 0x00 0x01 0x00
// +------------+ +--+ +--+ +--+
// 固定消息头 备用 长度 结果代码
// 结果代码
// 0x00: 登录成功
// 0x01: DTU重复登录
// 0x02: DTU禁止登录
// 0x03: 密码验证失败
OperateResult<byte[]> check = ReceiveByMessage( socket, 5000, new AlienMessage( ) );
if (!check.IsSuccess) return;
if (check.Content[4] != 0x17 || check.Content.Length != 0x1C)
{
socket?.Close( );
LogNet?.WriteWarn( ToString( ), "Length Check Failed" );
return;
}
// 密码验证
bool isPasswrodRight = true;
for (int i = 0; i < password.Length; i++)
{
if (check.Content[16 + i] != password[i])
{
isPasswrodRight = false;
break;
}
}
string dtu = Encoding.ASCII.GetString( check.Content, 5, 11 ).Trim( );
// 密码失败的情况
if (!isPasswrodRight)
{
OperateResult send = Send( socket, GetResponse( StatusPasswodWrong ) );
if (send.IsSuccess) socket?.Close( );
LogNet?.WriteWarn( ToString( ), "Login Password Wrong, Id:" + dtu );
return;
}
AlienSession session = new AlienSession( )
{
DTU = dtu,
Socket = socket,
};
// 检测是否禁止登录
if (!IsClientPermission( session ))
{
OperateResult send = Send( socket, GetResponse( StatusLoginForbidden ) );
if (send.IsSuccess) socket?.Close( );
LogNet?.WriteWarn( ToString( ), "Login Forbidden, Id:" + session.DTU );
return;
}
// 检测是否重复登录,不重复的话,也就是意味着登录成功了
if (IsClientOnline( session ))
{
OperateResult send = Send( socket, GetResponse( StatusLoginRepeat ) );
if (send.IsSuccess) socket?.Close( );
LogNet?.WriteWarn( ToString( ), "Login Repeat, Id:" + session.DTU );
return;
}
else
{
OperateResult send = Send( socket, GetResponse( StatusOk ) );
if (!send.IsSuccess) return;
}
// 触发上线消息
OnClientConnected?.Invoke( this, session );
}
#endregion
#region Client Event
/// <summary>
/// 当有服务器连接上来的时候触发
/// </summary>
public event Action<NetworkAlienClient, AlienSession> OnClientConnected = null;
#endregion
#region Private Method
/// <summary>
/// 获取返回的命令信息
/// </summary>
/// <param name="status">状态</param>
/// <returns>回发的指令信息</returns>
private byte[] GetResponse(byte status)
{
return new byte[]
{
0x48,0x73,0x6E,0x00,0x01,status
};
}
/// <summary>
/// 状态登录成功
/// </summary>
private const byte StatusOk = 0x00;
/// <summary>
/// 重复登录
/// </summary>
private const byte StatusLoginRepeat = 0x01;
/// <summary>
/// 禁止登录
/// </summary>
private const byte StatusLoginForbidden = 0x02;
/// <summary>
/// 密码错误
/// </summary>
private const byte StatusPasswodWrong = 0x03;
/// <summary>
/// 检测当前的DTU是否在线
/// </summary>
/// <param name="session"></param>
/// <returns></returns>
private bool IsClientOnline( AlienSession session )
{
bool result = false;
alreadyLock.Enter( );
for (int i = 0; i < alreadyOnline.Count; i++)
{
if (alreadyOnline[i].DTU == session.DTU)
{
result = true;
break;
}
}
if (!result)
{
alreadyOnline.Add( session );
}
alreadyLock.Leave( );
return result;
}
/// <summary>
/// 检测当前的dtu是否允许登录
/// </summary>
/// <param name="session"></param>
/// <returns></returns>
private bool IsClientPermission( AlienSession session )
{
bool result = false;
trustLock.Enter( );
if (trustOnline.Count == 0)
{
result = true;
}
else
{
for (int i = 0; i < trustOnline.Count; i++)
{
if (trustOnline[i] == session.DTU)
{
result = true;
break;
}
}
}
trustLock.Leave( );
return result;
}
#endregion
#region Public Method
/// <summary>
/// 设置密码,长度为6
/// </summary>
/// <param name="password"></param>
public void SetPassword(byte[] password)
{
if(password?.Length == 6)
{
password.CopyTo( this.password, 0 );
}
}
/// <summary>
/// 设置可信任的客户端列表
/// </summary>
/// <param name="clients"></param>
public void SetTrustClients(string[] clients)
{
trustLock.Enter( );
trustOnline = new List<string>( clients );
trustLock.Leave( );
}
/// <summary>
/// 退出异形客户端
/// </summary>
/// <param name="session">异形客户端的会话</param>
public void AlienSessionLoginOut( AlienSession session )
{
alreadyLock.Enter( );
alreadyOnline.Remove( session );
alreadyLock.Leave( );
}
#endregion
#region Thread Check Client
private void ThreadCheckStart()
{
threadCheck = new Thread(new ThreadStart( ThreadCheckAlienClient ));
threadCheck.IsBackground = true;
threadCheck.Priority = ThreadPriority.AboveNormal;
threadCheck.Start( );
}
private void ThreadCheckAlienClient( )
{
Thread.Sleep( 1000 );
while (true)
{
Thread.Sleep( 1000 );
alreadyLock.Enter( );
for (int i = alreadyOnline.Count - 1; i >= 0; i--)
{
if (!alreadyOnline[i].IsStatusOk)
{
alreadyOnline.RemoveAt( i );
}
}
alreadyLock.Leave( );
}
}
#endregion
#region Private Member
private byte[] password; // 密码设置
private List<AlienSession> alreadyOnline; // 所有在线信息
private SimpleHybirdLock alreadyLock; // 列表的同步锁
private List<string> trustOnline; // 禁止登录的客户端信息
private SimpleHybirdLock trustLock; // 禁止登录的锁
private Thread threadCheck; // 后台检测在线情况的
#endregion
#region Object Override
/// <summary>
/// 获取本对象的字符串表示形式
/// </summary>
/// <returns></returns>
public override string ToString( )
{
return "NetworkAlienBase";
}
#endregion
}
}