From 0087dd7734e71dfcfd1bb54db394ad7855021ffd Mon Sep 17 00:00:00 2001
From: 黄学彪 <hxb@hdlchina.com.cn>
Date: 星期五, 24 七月 2020 10:18:08 +0800
Subject: [PATCH] 新版本

---
 ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs |   73 +++++++++++-------------------------
 1 files changed, 22 insertions(+), 51 deletions(-)

diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
index 91553e0..b3f93e1 100755
--- a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
+++ b/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>
-            /// 褰撳墠鐨凷ocket鏄惁杩愯
-            /// </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
     }
-}
+}
\ No newline at end of file

--
Gitblit v1.8.0