wxr
2020-01-10 1a4b95a7ebef71838bd3eda2c22056bbf0db65ec
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
using System;
using System.Net.Sockets;
using System.Net;
using HDL_ON.UI;
 
namespace HDL_ON
{
    /// <summary>
    /// 接收处理UDP数据包
    /// </summary>
    /// <typeparam name="T"></typeparam>
    public static class BusSocket
    {
        //监听端口
        static int port;
 
        //本地Socket
        private static Socket busSocket;
 
        /// <summary>
        /// 启动Socket接收和发送功能
        /// </summary>
        /// <param name="port"></param>
        public static void Start (int port)
        {
            if (IsRunning) {
                return;
            }
 
            BusSocket.port = port;
 
            busSocket = new Socket (AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
            busSocket.EnableBroadcast = true;
            try {
                busSocket.Bind (new IPEndPoint (IPAddress.Any, port));
            }catch{
                busSocket = null;
                return;
            }
 
            //socket = new Socket (AddressFamily.InterNetwork, SocketType.Raw, ProtocolType.IP);
            //socket.Bind (new IPEndPoint (IPAddress.Parse (ip), 0)); //绑定套接字
            //socket.SetSocketOption (SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption (IPAddress.Parse ("224.0.168.188")));
            //if (SetSocketOption () == false) error_occurred = true;
 
            asyncBeginReceive ();
 
            MainPage.Log ("BusSocket启动成功!");
        }
 
        /// <summary>
        /// 停止Socket
        /// </summary>
        public static void Stop()
        {
            if (!IsRunning)
            {
                return;
            }
            try
            {
                busSocket.Close();
            }
            catch { }
            busSocket = null;
            //HDL_ON.SimpleControl.Phone.Music.A31PlayMusicPage.isExit = true;
            //SmartHome.MqttCommon.DisConnect ();
            MainPage.Log("BusSocket关闭成功!");
        }
 
        /// <summary>
        /// 当前的Socket是否运行
        /// </summary>
        public static bool IsRunning
        {
            get
            {
                return null == busSocket ? false : true;
            }
        }
 
        /// <summary>
        /// 开始异步接收数据
        /// </summary>
        private static void asyncBeginReceive()
        {
            if (!IsRunning)
            {
                return;
            }
 
            try {
 
                Packet packet = new Packet ();
 
                busSocket.BeginReceiveFrom (packet.Bytes, 0, packet.Bytes.Length, SocketFlags.None, ref packet.RemoteEndPoint, new AsyncCallback (asyncEndReceive), packet);
 
            } 
            catch (Exception e) {
                System.Threading.Thread.Sleep (1);
                asyncBeginReceive ();
            } 
 
        }
 
        /// <summary>
        /// 异步接收数据结束
        /// </summary>
        /// <param name="iar"></param>
        private static void asyncEndReceive(IAsyncResult iar)
        {
 
            if (!IsRunning)
            {
                return;
            }
 
            try
            {
                asyncBeginReceive ();
 
                Packet packet = (Packet)iar.AsyncState;
 
                int len = busSocket.EndReceiveFrom(iar, ref packet.RemoteEndPoint);
                byte[] bytes = packet.Bytes;
                packet.Bytes = new byte[len];
                System.Array.Copy(bytes, 0, packet.Bytes, 0, packet.Bytes.Length);
                packet.Manager();
            }
            catch { }
        }
 
        /// <summary>
        /// 异步发送数据
        /// </summary>
        /// <param name="tempPacket"></param>
        public static void AsyncBeginSend(Packet tempPacket)
        {
            try
            {
                if (!IsRunning) {
                    return;
                }
                tempPacket.FlagDateTime = System.DateTime.Now;
                tempPacket.HaveSendCount++;
                busSocket.BeginSendTo(tempPacket.Bytes, 0, tempPacket.Bytes.Length, SocketFlags.None, tempPacket.RemoteEndPoint, new AsyncCallback(asyncEndSend), tempPacket);
            }
            catch { 
            
            }
        }
 
        /// <summary>
        /// 异步发送数据结束
        /// </summary>
        /// <param name="iar"></param>
        private static void asyncEndSend(IAsyncResult iar)
        {
            Packet tempUDPPacketBuffer = (Packet)iar.AsyncState;
            try
            {
                int bytesSent = busSocket.EndSendTo(iar);
            }
            catch {
            
            }
        }
    }
}