From 9d1660270272180cd71147d6ddd53d1382643d2e Mon Sep 17 00:00:00 2001
From: wenxiangchao <wenxiangchao@ihengxin.com.cn>
Date: 星期一, 02 十一月 2026 18:00:14 +0800
Subject: [PATCH] merge: 并入远程 Initial commit 作为历史根节点

---
 ConnectionUtil/PipeClient2.cs |  130 +++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 130 insertions(+), 0 deletions(-)

diff --git a/ConnectionUtil/PipeClient2.cs b/ConnectionUtil/PipeClient2.cs
new file mode 100644
index 0000000..348cea8
--- /dev/null
+++ b/ConnectionUtil/PipeClient2.cs
@@ -0,0 +1,130 @@
+锘縰sing System;
+using System.IO;
+using System.IO.Pipes;
+using System.Threading;
+
+namespace ConnectionUtil
+{
+    public class PipeClient2 : IDisposable
+    {
+        private readonly string _pipeName;
+        private readonly string _serverName;
+        private readonly CancellationTokenSource _cancellationTokenSource;
+        private readonly ReaderWriterLockSlim _lock;
+        private NamedPipeClientStream _pipeClient;
+        private bool _isDisposed;
+        private bool _isConnected;
+        private Thread _receiveThread;
+
+        // 浜嬩欢瀹氫箟
+        public event EventHandler<PipeDataEventArgs> OnData;
+        public event EventHandler OnConnected;
+        public event EventHandler OnDisconnected;
+
+        public PipeClient2(string pipeName, string serverName = ".")
+        {
+            _pipeName = pipeName;
+            _serverName = serverName;
+            _cancellationTokenSource = new CancellationTokenSource();
+            _lock = new ReaderWriterLockSlim();
+        }
+
+        public void Connect(int timeoutMs = 5000)
+        {
+            if (_isConnected)
+                return;
+
+            try
+            {
+                // 鍔犱笂 PipeOptions.Asynchronous 鍙互寮傛鎺ュ彈鏈嶅姟鍣ㄥ彂閫佺殑鏁版嵁锛屼絾鏄緢褰卞搷鎬ц兘
+                _pipeClient = new NamedPipeClientStream(_serverName, _pipeName, PipeDirection.InOut);
+
+                // 鍒涘缓瓒呮椂鍙栨秷浠ょ墝
+                using (var timeoutCts = new CancellationTokenSource(timeoutMs))
+                {
+                    _pipeClient.Connect(timeoutMs);
+                    _pipeClient.ReadMode = PipeTransmissionMode.Message;
+                    _isConnected = true;
+
+                    // 瑙﹀彂杩炴帴鎴愬姛浜嬩欢
+                    OnConnected?.Invoke(this, EventArgs.Empty);
+
+                    // 寮�濮嬫帴鏀舵暟鎹�
+                    //_receiveThread = new Thread(ReceiveData);
+                    //_receiveThread.Start();
+                }
+            }
+            catch (Exception ex)
+            {
+                throw;
+            }
+        }
+
+        private void ReceiveData()
+        {
+            try
+            {
+                while (_isConnected && !_cancellationTokenSource.Token.IsCancellationRequested)
+                {
+                    string message = _pipeClient.ReadMessage();
+                    if (string.IsNullOrEmpty(message))
+                        break;
+                    // 瑙﹀彂鏁版嵁鎺ユ敹浜嬩欢
+                    OnData?.Invoke(this, new PipeDataEventArgs("", message));
+                }
+            }
+            finally
+            {
+                _isConnected = false;
+                // 瑙﹀彂鏂紑杩炴帴浜嬩欢
+                OnDisconnected?.Invoke(this, EventArgs.Empty);
+            }
+        }
+
+        public bool SendMessage(string message, out string response, int timeoutMs = 5000)
+        {
+            response = null;
+            if (!_isConnected)
+                return false;
+            try
+            {
+
+                _lock.EnterWriteLock();
+                try
+                {
+                    _pipeClient.WriteMessage(message);
+                }
+                finally
+                {
+                    _lock.ExitWriteLock();
+                }
+                response = _pipeClient.ReadMessage();
+                return true;
+            }
+            catch (Exception ex)
+            {
+                response = ex.Message;
+                return false;
+            }
+        }
+
+        public void Disconnect()
+        {
+            _isConnected = false;
+            _cancellationTokenSource.Cancel();
+            _receiveThread?.Join(1000); // 绛夊緟鎺ユ敹绾跨▼缁撴潫
+        }
+
+        public void Dispose()
+        {
+            if (!_isDisposed)
+            {
+                Disconnect();
+                _pipeClient?.Dispose();
+                _cancellationTokenSource.Dispose();
+                _lock.Dispose();
+                _isDisposed = true;
+            }
+        }
+    }
+}
\ No newline at end of file

--
Gitblit v1.9.1