wxr
2020-09-10 af1cb3ecd0f4b0589e00b28f7f9edccf39e6e12b
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HDL_ON.Entity;
 
namespace HDL_ON
{
    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, string revGatewayIP);
        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 (((System.Net.IPEndPoint)RemoteEndPoint).Port == 6688)
            {
                ReceviceAllDadaAction?.Invoke(0, 0, 0, 0, 0, 0, Bytes, (System.Net.IPEndPoint)RemoteEndPoint);
                Control.ManagerReceive(0, 0, 0, 0, 0, Bytes, (System.Net.IPEndPoint)RemoteEndPoint);
            }
            else
            {
                try
                {
                    //对于操作数据库的时间比较长的,可以创建另一个线程处理
                    if (!"HDLMIRACLE".Equals(System.Text.Encoding.ASCII.GetString(Bytes, 4, 10)))
                    {
                        return;
                    }
                    Control.controlLostCount = 0;
 
                    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 == 252 && targetDeviceID == 252) || (targetSubnetID == 0xff && targetDeviceID == 0xff)))
                    {
                        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)
                    {
                        string revIp = $"{Bytes[0]}.{Bytes[1]}.{Bytes[2]}.{Bytes[3]}";
                        if (revIp == "172.16.2.237")
                        {
 
                        }
                        ReceiveEvent(subnetID, deviceID, command, usefulBytes, revIp);
                    }
 
                    //if (command == Command.SuperGatewayAgentACK) {
                    //    SuperGateWay.SendAndReceiveDatas.Recevice (subnetID, deviceID, usefulBytes);
                    //}
                    if (ReceviceAllDadaAction != null)
                    {
                        ReceviceAllDadaAction(subnetID, deviceID, deviceType, command, targetSubnetID, targetDeviceID, usefulBytes, (System.Net.IPEndPoint)RemoteEndPoint);
                    }
                    //处理是否要重发数据
                    Control.ManagerReceive(subnetID, deviceID, command, targetSubnetID, targetDeviceID, usefulBytes, RemoteEndPoint);
                }
                catch (Exception ex)
                {
                    MainPage.Log($"packet {ex.Message} ");
                }
            }
        }
    }
}