using System;
|
using System.Collections.Generic;
|
using System.Linq;
|
using System.Text;
|
using Shared.SimpleControl;
|
|
namespace Shared
|
{
|
public class Packet
|
{
|
/// <summary>
|
/// 缓冲区大小
|
/// </summary>
|
public const int Size = 1024+200;
|
|
/// <summary>
|
/// 接收到的数据
|
/// </summary>
|
public byte [] Bytes;
|
|
/// <summary>
|
/// 远程的套接字
|
/// </summary>
|
public System.Net.EndPoint RemoteEndPoint;
|
|
public Packet ()
|
{
|
this.Bytes = new byte [Size];
|
|
RemoteEndPoint = (System.Net.EndPoint)new System.Net.IPEndPoint (System.Net.IPAddress.Any, 0);
|
}
|
public Packet (byte [] data, System.Net.EndPoint remoteEndPoint)
|
{
|
this.Bytes = data;
|
this.RemoteEndPoint = remoteEndPoint;
|
}
|
|
|
/// <summary>
|
/// 记录已经发送数据出去的时间
|
/// </summary>
|
public System.DateTime FlagDateTime;
|
|
|
/// <summary>
|
/// 已经发送了多少数
|
/// </summary>
|
public int HaveSendCount;
|
|
public delegate void DelegateReceive (byte subnetID, byte deviceID, Command command, byte [] usefullBytes);
|
public static event DelegateReceive ReceiveEvent;
|
/// <summary>
|
/// 接收到的所有数据
|
/// </summary>
|
public static Action<byte, byte, int, Command, byte, byte, byte [], System.Net.IPEndPoint> ReceviceAllDadaAction;
|
/// <summary>
|
/// 处理接收到的数据
|
/// </summary>
|
public virtual void Manager ()
|
{
|
if (CommonPage.needEncryptionDetection) {
|
//加密检测
|
EncryptionDetection ();
|
}
|
|
//var mes2 = System.Text.Encoding.ASCII.GetString (Bytes, 0, Bytes.Length);
|
//Utlis.WriteLine ($"============>GetBytes :{mes2}");
|
|
//对于操作数据库的时间比较长的,可以创建另一个线程处理
|
if (!"HDLMIRACLE".Equals (System.Text.Encoding.ASCII.GetString (Bytes, 4, 10))) {
|
return;
|
}
|
|
byte subnetID = this.Bytes [17]; //源子网号
|
byte deviceID = this.Bytes [18]; //源设备号
|
|
//源设备类型
|
int deviceType = this.Bytes [19] * 256 + this.Bytes [20];
|
|
Command command = (Command)(Bytes [21] * 256 + Bytes [22]); //操作码控制命令
|
|
byte targetSubnetID = this.Bytes [23];
|
byte targetDeviceID = this.Bytes [24];
|
|
//不是要接收的指令就返回
|
if (!((targetSubnetID == Global.LocalSubnetID && targetDeviceID == Global.LocalDeviceID) || (targetSubnetID == 0xff && targetDeviceID == 0xff))) {
|
//Utlis.WriteLine ($"============>targetSubnetID return");
|
return;
|
}
|
byte [] usefulBytes = null;
|
if (this.Bytes [16] == 0xFF) {
|
usefulBytes = new byte [Bytes.Length - 16 - 11];
|
System.Array.Copy (Bytes, 27, usefulBytes, 0, usefulBytes.Length);
|
} else {
|
//有用的附加数据
|
usefulBytes = new byte [this.Bytes [16] - 11];
|
Array.Copy (Bytes, 25, usefulBytes, 0, usefulBytes.Length);
|
}
|
|
if (ReceiveEvent != null) {
|
try {
|
ReceiveEvent (subnetID, deviceID, command, usefulBytes);
|
} catch (Exception ex) {
|
Utlis.WriteLine (ex.Message);
|
}
|
}
|
|
if (ReceviceAllDadaAction != null) {
|
ReceviceAllDadaAction (subnetID, deviceID, deviceType, command, targetSubnetID, targetDeviceID, usefulBytes, (System.Net.IPEndPoint)RemoteEndPoint);
|
}
|
//处理是否要重发数据
|
Control.ManagerReceive (subnetID, deviceID, command, targetSubnetID, targetDeviceID, usefulBytes, RemoteEndPoint);
|
|
}
|
|
public static string byteToHex16 (byte b)
|
{
|
string s = Convert.ToString (b, 16).ToUpper ();
|
if (s.Length <= 1) {
|
return "0" + s;
|
}
|
return s;//
|
}
|
|
/// <summary>
|
/// 是否本地
|
/// </summary>
|
public bool IsLocal = true;
|
|
/// <summary>
|
/// 本地通讯时 加密检测
|
/// </summary>
|
void EncryptionDetection () {
|
if (IsLocal) {//本地通讯
|
|
if (UserConfig.Instance.IsLocalEncrypt) {
|
if (Bytes.Length == 28) {
|
var mes = System.Text.Encoding.ASCII.GetString (Bytes, 14, 14);
|
if ("Password error" == mes) {
|
//网关返回,加密密码key不对
|
UserConfig.Instance.EncryptedPasswordCorrect = false;
|
return;
|
}
|
}
|
var messageGet = Shared.Securitys.EncryptionService.AesDecryptPayload (Bytes, UserConfig.Instance.LocalEncryptKey);
|
Bytes = messageGet;
|
|
|
} else {
|
if (Bytes.Length == 20) {
|
var mes = System.Text.Encoding.ASCII.GetString (Bytes, 14, 6);
|
if ("Locked" == mes) {
|
//网关已加密,自动切换为加密
|
UserConfig.Instance.IsLocalEncrypt = true;
|
}
|
}
|
}
|
|
}
|
}
|
|
|
}
|
}
|