HDL Home App 第二版本 旧平台金堂用 正在使用
黄学彪
2020-07-24 0087dd7734e71dfcfd1bb54db394ad7855021ffd
ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -160,13 +160,13 @@
                {
                    try
                    {
                        if (FindGateWaySocket.busSocket == null || FindGateWaySocket.busSocket.Available <= 0)
                        if (FindGateWaySocket.udpClient == null || FindGateWaySocket.udpClient.Available <= 0)
                        {
                            System.Threading.Thread.Sleep(500);
                            continue;
                        }
                        var bytes = new byte[1024];
                        var len = FindGateWaySocket.busSocket.Receive(bytes, bytes.Length, System.Net.Sockets.SocketFlags.None);
                        }
                        var ipEndPoint = new System.Net.IPEndPoint(0, 0);
                        var bytes = FindGateWaySocket.udpClient.Receive(ref ipEndPoint);
                        if (bytes[43] == 0xA2)
                        {
                            //广播回复网关的基本信息处理
@@ -284,7 +284,14 @@
                            }
                        }
                    }
                    catch { }
                    catch (Exception ex)
                    {
                        //调试用
                        if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
                        {
                            Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "Receive异常\r\n" + ex.Message);
                        }
                    }
                }
            })
            { IsBackground = true }.Start();
@@ -390,24 +397,19 @@
        public static class FindGateWaySocket
        {
            //本地Socket
            public static Socket busSocket;
            public static UdpClient udpClient;
            /// <summary>
            /// 启动Socket接收和发送功能
            /// </summary>
            /// <param name="port"></param>
            public static void Start(int port = 7624)
            public static void Start()
            {
                if (IsRunning)
                if (udpClient!=null)
                {
                    return;
                }
                //定义网络类型,数据连接类型和网络协议UDP
                busSocket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp);
                busSocket.EnableBroadcast = true;
                busSocket.ReceiveTimeout = 1000;
                busSocket.SendTimeout = 1000;
                busSocket.Bind(new IPEndPoint(IPAddress.Any, port));
                udpClient = new UdpClient { EnableBroadcast = true };
            }
            /// <summary>
@@ -417,23 +419,12 @@
            {
                try
                {
                    busSocket?.Close();
                    udpClient?.Close();
                }
                catch { }
                busSocket = null;
                udpClient = null;
                Console.WriteLine("BusSocket关闭成功!");
            }
            /// <summary>
            /// 当前的Socket是否运行
            /// </summary>
            public static bool IsRunning
            {
                get
                {
                    return busSocket == null ? false : true;
                }
            }
            /// <summary>
@@ -444,11 +435,11 @@
            {
                try
                {
                    if (IsRunning)
                    if (udpClient == null)
                    {
                        busSocket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, iPEndPoint, new AsyncCallback(asyncEndSend), null);
                        return;
                    }
                    udpClient.Send(bytes, bytes.Length, iPEndPoint);
                }
                catch (Exception ex)
                {
@@ -459,28 +450,8 @@
                    }
                }
            }
            /// <summary>
            /// 异步发送数据结束
            /// </summary>
            /// <param name="iar"></param>
            private static void asyncEndSend(IAsyncResult iar)
            {
                try
                {
                    int bytesSent = busSocket.EndSendTo(iar);
                }
                catch (Exception ex)
                {
                    //调试用
                    if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
                    {
                        Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "异步发送结束异常(asyncEndSend)\r\n" + ex.Message);
                    }
                }
            }
        }
        #endregion
    }
}
}