From b544d5cccd76735e034c8055918a50927b437474 Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期二, 10 十一月 2026 08:29:04 +0800
Subject: [PATCH] fix: 维护模式重复进入失败与按钮灰死 + Broker 并发验证用例

---
 CanSimulator/CanBusSimulator.cs |  108 +++++++++++++++++++++++++++++++++++------------------
 1 files changed, 71 insertions(+), 37 deletions(-)

diff --git a/CanSimulator/CanBusSimulator.cs b/CanSimulator/CanBusSimulator.cs
index 3497aac..4a2deda 100644
--- a/CanSimulator/CanBusSimulator.cs
+++ b/CanSimulator/CanBusSimulator.cs
@@ -66,6 +66,12 @@
         public int SimulatedLatencyMs { get; set; } = 0;
 
         /// <summary>
+        /// 鎬荤嚎绾у簲绛斿櫒锛氬紑鍚悗锛屼换浣曡妭鐐瑰彂鍑虹殑甯т細琚師鏍峰洖鏄剧粰鍙戦�佽�呰嚜韬�
+        /// 锛堟ā鎷熷崟涓粠璁惧搴旂瓟锛屾棤闇�绗簩涓妭鐐瑰湪绾匡級銆傛潵婧愭爣璁颁负 RESPONDER銆�
+        /// </summary>
+        public bool EchoResponderToSender { get; set; } = false;
+
+        /// <summary>
         /// 宸叉敞鍐岀殑鑺傜偣
         /// </summary>
         private readonly ConcurrentDictionary<string, CanNode> _nodes = new();
@@ -233,7 +239,7 @@
             TotalFramesTransmitted++;
             LastActivityTime = DateTime.Now;
 
-            // 璺敱鍒版墍鏈夊叾浠栬妭鐐�
+            // 璺敱鍒版墍鏈夊叾浠栬妭鐐癸紱鑻ユ帴鏀惰妭鐐瑰惎鐢ㄨ嚜鍔ㄥ搷搴旓紝鐢卞叾鍥炲
             int routed = 0;
             foreach (var kvp in _nodes)
             {
@@ -244,6 +250,10 @@
                     kvp.Value.ReceiveFrame(frame);
                     routed++;
                     TotalFramesReceived++;
+
+                    // 鎺ユ敹鏂硅嚜鍔ㄥ搷搴旓細妯℃嫙浠庤澶囨敹鍒板懡浠ゅ悗绛斿锛堥潰鏉库�滆嚜鍔ㄥ搷搴斺�濊涔夛級
+                    if (kvp.Value.AutoResponseEnabled)
+                        SendAutoResponse(kvp.Value, frame);
                 }
                 catch (Exception ex)
                 {
@@ -251,56 +261,26 @@
                 }
             }
 
-            // 妫�鏌ユ簮鑺傜偣鏄惁鍚敤鑷姩鍝嶅簲妯″紡
-            if (_nodes.TryGetValue(frame.SourceNodeId, out var sourceNode) && sourceNode.AutoResponseEnabled)
+            // 鎬荤嚎绾у簲绛斿櫒锛氭妸璇ュ抚鍘熸牱鍥炴樉缁欏彂閫佽�呰嚜韬紙缁曡繃"涓嶅洖婧�"锛岀洿鎺ユ姇閫掞級
+            if (EchoResponderToSender && _nodes.TryGetValue(frame.SourceNodeId, out var senderNode))
             {
                 try
                 {
-                    // 妯℃嫙鍝嶅簲寤惰繜
-                    if (sourceNode.AutoResponseDelayMs > 0)
-                    {
-                        Thread.Sleep(sourceNode.AutoResponseDelayMs);
-                    }
-
-                    // 鍒涘缓鍝嶅簲甯э紙澶嶅埗鍘熷甯ф暟鎹紝鏉ユ簮涓烘簮鑺傜偣锛�
-                    var responseFrame = new CanFrame
+                    var echo = new CanFrame
                     {
                         FrameId = frame.FrameId,
                         Format = frame.Format,
                         Type = frame.Type,
                         Data = (byte[])frame.Data.Clone(),
-                        SourceNodeId = sourceNode.NodeId,
+                        SourceNodeId = "RESPONDER",
                         Timestamp = DateTime.Now
                     };
-
-                    // 灏嗗搷搴斿抚璺敱鍒版墍鏈夊叾浠栬妭鐐癸紙涓嶅寘鎷搷搴旀簮鑷韩锛�
-                    foreach (var kvp in _nodes)
-                    {
-                        if (kvp.Key == sourceNode.NodeId) continue;
-                        try
-                        {
-                            kvp.Value.ReceiveFrame(responseFrame);
-                            TotalFramesReceived++;
-                        }
-                        catch (Exception ex)
-                        {
-                            Log.Error($"[CanBus] 璺敱鍝嶅簲甯у埌鑺傜偣 {kvp.Key} 澶辫触: {ex.Message}");
-                        }
-                    }
-
-                    // 灏嗗搷搴斿抚閫佸叆婧愯妭鐐圭殑鎺ユ敹閭锛堣婧愯妭鐐硅兘鐪嬪埌鑷繁鐨勫搷搴旓級
-                    sourceNode.ReceiveFrame(responseFrame);
+                    senderNode.ReceiveFrame(echo);
                     TotalFramesReceived++;
-
-                    // 璁板綍鍝嶅簲甯у埌鎬荤嚎鏃ュ織
-                    _busLog.Enqueue(responseFrame);
-                    TotalFramesTransmitted++;
-
-                    Log.Debug($"[CanBus] 鑺傜偣 {sourceNode.NodeId} 鑷姩鍝嶅簲甯� {responseFrame.FrameIdHex}");
                 }
                 catch (Exception ex)
                 {
-                    Log.Error($"[CanBus] 婧愯妭鐐硅嚜鍔ㄥ搷搴斿け璐�: {ex.Message}");
+                    Log.Error($"[CanBus] 搴旂瓟鍣ㄥ洖鏄剧粰 {frame.SourceNodeId} 澶辫触: {ex.Message}");
                 }
             }
 
@@ -309,6 +289,60 @@
         }
 
         /// <summary>
+        /// 鎺ユ敹鑺傜偣鑷姩鍝嶅簲锛氭妸鏀跺埌甯у師鏍峰洖缁欏叾瀹冭妭鐐癸紙鍚彂甯ф柟锛夛紝骞惰鍏ヨ嚜韬偖绠变笌鎬荤嚎鏃ュ織
+        /// </summary>
+        private void SendAutoResponse(CanNode responder, CanFrame incoming)
+        {
+            try
+            {
+                // 妯℃嫙鍝嶅簲寤惰繜
+                if (responder.AutoResponseDelayMs > 0)
+                {
+                    Thread.Sleep(responder.AutoResponseDelayMs);
+                }
+
+                var responseFrame = new CanFrame
+                {
+                    FrameId = incoming.FrameId,
+                    Format = incoming.Format,
+                    Type = incoming.Type,
+                    Data = (byte[])incoming.Data.Clone(),
+                    SourceNodeId = responder.NodeId,
+                    Timestamp = DateTime.Now
+                };
+
+                // 鍝嶅簲甯ц矾鐢卞埌鍏跺畠鑺傜偣锛堝惈鏈�鍒濈殑鍙戝抚鏂癸級
+                foreach (var kvp in _nodes)
+                {
+                    if (kvp.Key == responder.NodeId) continue;
+                    try
+                    {
+                        kvp.Value.ReceiveFrame(responseFrame);
+                        TotalFramesReceived++;
+                    }
+                    catch (Exception ex)
+                    {
+                        Log.Error($"[CanBus] 璺敱鍝嶅簲甯у埌鑺傜偣 {kvp.Key} 澶辫触: {ex.Message}");
+                    }
+                }
+
+                // 鍝嶅簲甯ч�佸叆搴旂瓟鑺傜偣鑷韩鎺ユ敹閭锛堣皟璇曢〉鍙鑷繁鍙戝嚭鐨勫搷搴旓級
+                responder.ReceiveFrame(responseFrame);
+                TotalFramesReceived++;
+
+                // 璁板綍鍝嶅簲甯у埌鎬荤嚎鏃ュ織
+                _busLog.Enqueue(responseFrame);
+                TotalFramesTransmitted++;
+
+                Log.Debug($"[CanBus] 鎺ユ敹鑺傜偣 {responder.NodeId} 鑷姩鍝嶅簲甯� {responseFrame.FrameIdHex}");
+            }
+            catch (Exception ex)
+            {
+                Log.Error($"[CanBus] 鑺傜偣 {responder.NodeId} 鑷姩鍝嶅簲澶辫触: {ex.Message}");
+            }
+        }
+
+        /// <summary>
         /// 鐩存帴娉ㄥ叆甯у埌鎬荤嚎锛堢敤浜庢ā鎷熷閮ㄨ澶囧彂閫侊級
         /// </summary>
         public void InjectFrame(CanFrame frame)

--
Gitblit v1.9.1