From ecba45c93391066bc30c7bd602c3a7683fbb99a7 Mon Sep 17 00:00:00 2001 From: xm <1271024303@qq.com> Date: 星期五, 31 七月 2020 10:56:12 +0800 Subject: [PATCH] 临时备份 --- ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs | 73 +++++++++++++++++++++++++----------- 1 files changed, 51 insertions(+), 22 deletions(-) diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs index b3f93e1..91553e0 100755 --- a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs +++ b/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> + /// 褰撳墠鐨凷ocket鏄惁杩愯 + /// </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 } -} \ No newline at end of file +} -- Gitblit v1.8.0