From c7df85937f73fb347ee0b19e9c052d2d00a6df6c Mon Sep 17 00:00:00 2001 From: 黄学彪 <hxb@hdlchina.com.cn> Date: 星期五, 18 九月 2020 13:58:19 +0800 Subject: [PATCH] 新版本 --- ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs | 80 ++++++++++++++-------------------------- 1 files changed, 28 insertions(+), 52 deletions(-) diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs index 91553e0..ca6fd17 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) { //骞挎挱鍥炲缃戝叧鐨勫熀鏈俊鎭鐞� @@ -205,6 +205,11 @@ else if (IsSearchingGateway) { listReceiveIP.Add(ipAddress); + } + //2020.08.07杩藉姞:鍗充娇绌虹殑浣忓畢id涔熷缓绔嬮摼鎺�,瀵瑰簲鎵嬪姩鎭㈠缃戝叧鍑哄巶璁剧疆鏃�,鍥犱负娌℃湁瑙g粦,浣嗘槸浣忓畢id涓虹┖鐨勯棶棰� + else if (Shared.Phone.UserCenter.HdlGatewayLogic.Current.HomeIdIsEmpty(homeID) == true) + { + listReceiveIP.Add(ipAddress); } } //缃戝叧鍒楄〃瀛樺偍澶勭悊 @@ -284,7 +289,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(); @@ -352,7 +364,7 @@ //鏈�鍚庡啀鏂紑mqtt杩炴帴 for (int i = 0; i < list.Count; i++) { - list[i].DisConnectLocalMqttClient("1"); + list[i]?.DisConnectLocalMqttClient("1"); } list.Clear(); oldHomeID = Shared.Common.Config.Instance.HomeId; @@ -390,24 +402,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 +424,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 +440,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 +455,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