Browse Source

修改数组越界问题

yinyujing 4 years ago
parent
commit
8d5137bb3f
1 changed files with 34 additions and 27 deletions
  1. 34 27
      NB_IOT_TCP_HP_SOCKET/MainForm_TCP.cs

+ 34 - 27
NB_IOT_TCP_HP_SOCKET/MainForm_TCP.cs

@@ -455,11 +455,14 @@ namespace NB_IOT_TCP_HP_SOCKET
 
 
                 if (deveiceConnId != null && deveiceConnId.Count > 0)
                 if (deveiceConnId != null && deveiceConnId.Count > 0)
                 {
                 {
-                    KeyValuePair<string, IntPtr>[] nowDeveiceConnId = new KeyValuePair<string, IntPtr>[deveiceConnId.Count];
-                    deveiceConnId.ToArray().CopyTo(nowDeveiceConnId, 0);
-                    
+                    //KeyValuePair<string, IntPtr>[] nowDeveiceConnId = new KeyValuePair<string, IntPtr>[deveiceConnId.Count];
+                    Dictionary<string, IntPtr> nowDeveiceConnId = new Dictionary<string, IntPtr>(deveiceConnId);
+                    //deveiceConnId.ToArray().CopyTo(nowDeveiceConnId, 0);
+
+
+                    KeyValuePair<string, IntPtr>[] keyValues = nowDeveiceConnId.ToArray();
                     // 移除设备的链接关联关系
                     // 移除设备的链接关联关系
-                    foreach (KeyValuePair<string, IntPtr> conn in nowDeveiceConnId)
+                    foreach (KeyValuePair<string, IntPtr> conn in keyValues)
                     {
                     {
                         IntPtr deviceConnId = conn.Value;
                         IntPtr deviceConnId = conn.Value;
                         if (connId == deviceConnId)
                         if (connId == deviceConnId)
@@ -613,39 +616,43 @@ namespace NB_IOT_TCP_HP_SOCKET
 
 
                         List<string> disconnectId = new List<string>();
                         List<string> disconnectId = new List<string>();
 
 
-                        KeyValuePair<string, IntPtr>[] nowDeveiceConnId = new KeyValuePair<string, IntPtr>[0];
                         if (deveiceConnId != null && deveiceConnId.Count > 0)
                         if (deveiceConnId != null && deveiceConnId.Count > 0)
                         {
                         {
-                            nowDeveiceConnId = new KeyValuePair<string, IntPtr>[deveiceConnId.Count];
-                            deveiceConnId.ToArray().CopyTo(nowDeveiceConnId , 0);
-                        }
-                        // 处理设备对应的链接,如果超时则断开链接并移除设备-链接对应关系
-                        foreach (KeyValuePair<string, IntPtr> conn in nowDeveiceConnId)
-                        {
-                            IntPtr connId = conn.Value;
-                            if (allConnIds == null || Array.IndexOf(allConnIds, connId) == -1)
-                            {
-                                disconnectId.Add(conn.Key);
 
 
-                                // 删除设参任务列表
-                                if (sendTaskList.ContainsKey(connId))
-                                {
-                                    sendTaskList.Remove(connId);
-                                }
-                                continue;
-                            }
-                            uint SilenceTime = 0;
-                            if (server.GetSilencePeriod(connId, ref SilenceTime))
+                            Dictionary<string, IntPtr> nowDeveiceConnId = new Dictionary<string, IntPtr>(deveiceConnId);
+
+                            KeyValuePair<string, IntPtr>[] keyValues = nowDeveiceConnId.ToArray();
+                            //nowDeveiceConnId = new KeyValuePair<string, IntPtr>[deveiceConnId.Count];
+                            //deveiceConnId.ToArray().CopyTo(nowDeveiceConnId , 0);
+
+                            // 处理设备对应的链接,如果超时则断开链接并移除设备-链接对应关系
+                            foreach (KeyValuePair<string, IntPtr> conn in keyValues)
                             {
                             {
-                                if (SilenceTime > Convert.ToInt32(ConfigurationManager.AppSettings["ConnectionTimeOut"]))
+                                IntPtr connId = conn.Value;
+                                if (allConnIds == null || Array.IndexOf(allConnIds, connId) == -1)
                                 {
                                 {
-                                    server.Disconnect(connId);
+                                    disconnectId.Add(conn.Key);
+
                                     // 删除设参任务列表
                                     // 删除设参任务列表
                                     if (sendTaskList.ContainsKey(connId))
                                     if (sendTaskList.ContainsKey(connId))
                                     {
                                     {
                                         sendTaskList.Remove(connId);
                                         sendTaskList.Remove(connId);
                                     }
                                     }
-                                    disconnectId.Add(conn.Key);
+                                    continue;
+                                }
+                                uint SilenceTime = 0;
+                                if (server.GetSilencePeriod(connId, ref SilenceTime))
+                                {
+                                    if (SilenceTime > Convert.ToInt32(ConfigurationManager.AppSettings["ConnectionTimeOut"]))
+                                    {
+                                        server.Disconnect(connId);
+                                        // 删除设参任务列表
+                                        if (sendTaskList.ContainsKey(connId))
+                                        {
+                                            sendTaskList.Remove(connId);
+                                        }
+                                        disconnectId.Add(conn.Key);
+                                    }
                                 }
                                 }
                             }
                             }
                         }
                         }