From 136b9e2fc48249a5ff89874f1080ba94130e7a9e Mon Sep 17 00:00:00 2001
From: xm <1271024303@qq.com>
Date: 星期二, 19 五月 2020 11:12:29 +0800
Subject: [PATCH] Merge branch 'dev-tzy' into dev-2020xm

---
 ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs |   50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 files changed, 48 insertions(+), 2 deletions(-)

diff --git a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
index 04c6a57..ec0ce95 100755
--- a/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
+++ b/ZigbeeApp/Shared/Phone/ZigBee/Common/Application.cs
@@ -333,7 +333,11 @@
         public static class FindGateWaySocket
         {
             //鏈湴Socket
-            public static System.Net.Sockets.Socket busSocket;
+            public static Socket busSocket;
+            /// <summary>
+            /// 璁℃椂鍣�
+            /// </summary>
+            private static int timeCount = 0;
 
             /// <summary>
             /// 鍚姩Socket鎺ユ敹鍜屽彂閫佸姛鑳�
@@ -379,7 +383,7 @@
             {
                 get
                 {
-                    return null == busSocket ? false : true;
+                    return busSocket == null ? false : true;
                 }
             }
 
@@ -391,12 +395,54 @@
             {
                 try
                 {
+                    //妫�娴嬭繛鎺ョ姸鎬�
+                    CheckConnectiton();
+
                     Start(7624);
                     busSocket.BeginSendTo(bytes, 0, bytes.Length, SocketFlags.None, iPEndPoint, new AsyncCallback(asyncEndSend), null);
                 }
                 catch { }
             }
 
+            /// <summary>
+            /// 妫�娴嬭繛鎺ョ姸鎬�
+            /// </summary>
+            private static void CheckConnectiton()
+            {
+                if (busSocket == null) { return; }
+                timeCount++;
+                if (timeCount < 20)
+                {
+                    //姣�10绉掓娴嬩竴娆�
+                    return;
+                }
+                timeCount = 0;
+
+                bool blockingState = busSocket.Blocking;
+                try
+                {
+                    byte[] tmp = new byte[1];
+
+                    busSocket.Blocking = false;
+                    busSocket.Send(tmp, 0, 0);
+                    //tcp杩樺湪杩炴帴鐫�
+                    busSocket.Blocking = blockingState;
+                }
+                catch (SocketException e)
+                {
+                    // 10035 == WSAEWOULDBLOCK
+                    if (e.NativeErrorCode.Equals(10035))
+                    {
+                        //Still Connected, but the Send would block
+                    }
+                    else
+                    {
+                        //tcp宸茬粡鏂紑浜嗚繛鎺�
+                        Stop();
+                    }
+                }
+            }
+
             /// <summary>
             /// 寮傛鍙戦�佹暟鎹粨鏉�
             /// </summary>

--
Gitblit v1.8.0