瀏覽代碼

Try Fix Connection Issue

Zixuan 6 天之前
父節點
當前提交
1d370d8e00
共有 3 個文件被更改,包括 72 次插入24 次删除
  1. 57 16
      MinicsConsole/Helper/Mini8DataDispatcher.cs
  2. 2 2
      MinicsConsole/Program.cs
  3. 13 6
      MinicsUI/Views/MainWindow.xaml.cs

+ 57 - 16
MinicsConsole/Helper/Mini8DataDispatcher.cs

@@ -1,16 +1,50 @@
-namespace MinicsConsole.Helper;
+using HardwareData;
+using MinicsConsole.Connector;
+using System.Diagnostics;
+using System.Net.NetworkInformation;
+using System.Threading.Channels;
 
-public class Mini8DataDispatcher(ILog log)
+namespace MinicsConsole.Helper;
+
+public class Mini8DataDispatcher
 {
+    public Mini8DataDispatcher(ILog log)
+    {
+        this.log = log;
+    }
+    private readonly ILog log;
+
     private readonly ConcurrentDictionary<string, IMini8DataNotifier> _Connectors = [];
+    private readonly ConcurrentDictionary<string, EventQueue<(string name, byte mini8, byte channel, ChannelData channelData)>> _ConnectorQueue = [];
+
+    private void QueueHandler((string name, byte mini8, byte channel, ChannelData channelData) t)
+    {
+        if (!_Connectors.TryGetValue(t.name, out IMini8DataNotifier? notifer) || notifer is null)
+            return;
+
+        try
+        {
+            notifer.ChannelInfoNotify(t.mini8, t.channel, t.channelData);
+        }
+        catch
+        {
+            log?.Warning($"DataDispatcher try send ChannelInfoNotify failed {t.name} mini8 {t.mini8} channel {t.channel}");
+        }
+    }
 
     public bool TryAddNotifier(string name, IMini8DataNotifier connector)
     {
-        return _Connectors.TryAdd(name, connector);
+        if (!_Connectors.TryAdd(name, connector))
+            return false;
+
+        _ConnectorQueue[name] = new(QueueHandler);
+
+        return true;
     }
 
     public bool TryRemoveConnector(string name)
     {
+        _ConnectorQueue.TryRemove(name, out _);
         return _Connectors.TryRemove(name, out _);
     }
 
@@ -18,20 +52,27 @@ public class Mini8DataDispatcher(ILog log)
     {
         if (channelData is null)
             return;
-        Task.Factory.StartNew(() =>
+
+        foreach (var item in _ConnectorQueue)
         {
-            Parallel.ForEach(_Connectors.Values, item =>
-            {
-                try
-                {
-                    item?.ChannelInfoNotify(mini8, channel, channelData);
-                }
-                catch
-                {
-                    log?.Warning($"DataDispatcher try send ChannelInfoNotify failed {item.Name} mini8 {mini8} channel {channel}");
-                }
-            });
-        });
+            item.Value.Enqueue((item.Key, mini8, channel, channelData));
+        }
+
+        //Parallel.ForEach(_Connectors.Values, item =>
+        //{
+        //    Task.Factory.StartNew(() =>
+        //    {
+
+        //        try
+        //        {
+        //            item?.ChannelInfoNotify(mini8, channel, channelData);
+        //        }
+        //        catch
+        //        {
+        //            log?.Warning($"DataDispatcher try send ChannelInfoNotify failed {item.Name} mini8 {mini8} channel {channel}");
+        //        }
+        //    });
+        //});
     }
 
     public void AlarmNotify(byte mini8, byte channel, float temperature)

+ 2 - 2
MinicsConsole/Program.cs

@@ -10,7 +10,6 @@ internal class Program
         Mutex mutex = new(true, "7E862400-8020-BE75-5266-B2C4BEB54076", out bool flag);
         if (!flag)
             return;
-
         _expLog.Initialize("Exceptions");
 
         AppDomain.CurrentDomain.UnhandledException += CurrentDomain_UnhandledException;
@@ -27,7 +26,8 @@ internal class Program
             options =>
             {
                 options.EnableDetailedErrors = true;
-                options.MaximumReceiveMessageSize = 262144;//256k
+                options.MaximumReceiveMessageSize = 524288;//256k
+                
                 //options.ClientTimeoutInterval = TimeSpan.FromSeconds(5);
                 //options.KeepAliveInterval = TimeSpan.FromSeconds(10);
                 //options.MaximumParallelInvocationsPerClient = 5;

+ 13 - 6
MinicsUI/Views/MainWindow.xaml.cs

@@ -61,15 +61,22 @@ public partial class MainWindow : Window
                     {
                         this.Left = 0;
                         this.Top = 0;
-                        this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
-                        this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
+                        //this.Width = System.Windows.SystemParameters.PrimaryScreenWidth;
+                        //this.Height = System.Windows.SystemParameters.PrimaryScreenHeight;
+                        this.Width = 1024;
+                        this.Height = 768;
                     }
                     else
                     {
-                        this.Left = monitor.StartPixHorizontal;
-                        this.Top = monitor.StartPixVertical;
-                        this.Width = monitor.ResolutionHorizontal;
-                        this.Height = monitor.ResolutionVertical;
+                        this.Left = 0;
+                        this.Top = 0;
+                        this.Width = 1024;
+                        this.Height = 768;
+
+                        //this.Left = monitor.StartPixHorizontal;
+                        //this.Top = monitor.StartPixVertical;
+                        //this.Width = monitor.ResolutionHorizontal;
+                        //this.Height = monitor.ResolutionVertical;
                     }
                     break;
                 default: