xm
2020-07-31 ecba45c93391066bc30c7bd602c3a7683fbb99a7
ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -160,13 +160,13 @@
                {
                    try
                    {
                        if (FindGateWaySocket.udpClient == null || FindGateWaySocket.udpClient.Available <= 0)
                        if (FindGateWaySocket.busSocket == null || FindGateWaySocket.busSocket.Available <= 0)
                        {
                            System.Threading.Thread.Sleep(500);
                            continue;
                        }
                        var ipEndPoint = new System.Net.IPEndPoint(0, 0);
                        var bytes = FindGateWaySocket.udpClient.Receive(ref ipEndPoint);
                        }
                        var bytes = new byte[1024];
                        var len = FindGateWaySocket.busSocket.Receive(bytes, bytes.Length, System.Net.Sockets.SocketFlags.None);
                        if (bytes[43] == 0xA2)
                        {
                            //广播回复网关的基本信息处理
@@ -284,14 +284,7 @@
                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        //调试用
                        if (Shared.Phone.UserCenter.UserCenterResourse.HideOption.WriteSocketReceiveDataToFile == 1)
                        {
                            Shared.Phone.UserCenter.HdlLogLogic.Current.WriteLog(3, "Receive异常\r\n" + ex.Message);
                        }
                    }
                    catch { }
                }
            })
            { IsBackground = true }.Start();
@@ -397,19 +390,24 @@
        public static class FindGateWaySocket
        {
            //本地Socket
            public static UdpClient udpClient;
            public static Socket busSocket;
            /// <summary>
            /// 启动Socket接收和发送功能
            /// </summary>
            /// <param name="port"></param>
            public static void Start()
            public static void Start(int port = 7624)
            {
                if (udpClient!=null)
                if (IsRunning)
                {
                    return;
                }
                udpClient = new UdpClient { EnableBroadcast = true };
                //定义网络类型,数据连接类型和网络协议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));
            }
            /// <summary>
@@ -419,12 +417,23 @@
            {
                try
                {
                    udpClient?.Close();
                    busSocket?.Close();
                }
                catch { }
                udpClient = null;
                busSocket = null;
                Console.WriteLine("BusSocket关闭成功!");
            }
            /// <summary>
            /// 当前的Socket是否运行
            /// </summary>
            public static bool IsRunning
            {
                get
                {
                    return busSocket == null ? false : true;
                }
            }
            /// <summary>
@@ -435,11 +444,11 @@
            {
                try
                {
                    if (udpClient == null)
                    if (IsRunning)
                    {
                        return;
                        busSocket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, iPEndPoint, new AsyncCallback(asyncEndSend), null);
                    }
                    udpClient.Send(bytes, bytes.Length, iPEndPoint);
                }
                catch (Exception ex)
                {
@@ -450,8 +459,28 @@
                    }
                }
            }
            /// <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
    }
}
}