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/PipeClient.cs | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++
1 files changed, 100 insertions(+), 0 deletions(-)
diff --git a/ConnectionUtil/PipeClient.cs b/ConnectionUtil/PipeClient.cs
new file mode 100644
index 0000000..68474fd
--- /dev/null
+++ b/ConnectionUtil/PipeClient.cs
@@ -0,0 +1,100 @@
+锘縰sing System;
+using System.IO;
+using System.IO.Pipes;
+using System.Threading;
+using System.Threading.Tasks;
+
+namespace ConnectionUtil
+{
+ public class PipeClient : IDisposable
+ {
+ private readonly string _pipeName;
+ private readonly string _serverName;
+ private readonly CancellationTokenSource _cancellationTokenSource;
+ private NamedPipeClientStream _pipeClient;
+ private bool _isDisposed;
+ private bool _isConnected;
+ private readonly object _sendLock = new object();
+
+ // 浜嬩欢瀹氫箟
+ public event EventHandler<PipeDataEventArgs> OnData;
+ public event EventHandler OnConnected;
+ public event EventHandler OnDisconnected;
+
+ public PipeClient(string pipeName, string serverName = ".")
+ {
+ _pipeName = pipeName;
+ _serverName = serverName;
+ _cancellationTokenSource = new CancellationTokenSource();
+ }
+
+ public void Connect(int timeoutMs = 5000)
+ {
+ if (_isConnected)
+ return;
+ _pipeClient = new NamedPipeClientStream(_pipeName);
+
+ _pipeClient.Connect(timeoutMs);
+ _pipeClient.ReadMode = PipeTransmissionMode.Message;
+ _isConnected = true;
+ Console.WriteLine("杩炴帴鎴愬姛锛�");
+
+ // 瑙﹀彂杩炴帴鎴愬姛浜嬩欢
+ OnConnected?.Invoke(this, EventArgs.Empty);
+ }
+
+ public bool SendMessage(string message, out string response, int timeoutMs = 5000)
+ {
+ if (!_isConnected)
+ {
+ response = "瀹㈡埛绔湭杩炴帴鍒版湇鍔″櫒";
+ return false;
+ }
+ response = null;
+ lock (_sendLock)
+ {
+ try
+ {
+ // 鍙戦�佹秷鎭�
+ _pipeClient.WriteMessage(message);
+ return true;
+ while (_isConnected && !_cancellationTokenSource.Token.IsCancellationRequested)
+ {
+ try
+ {
+ response = _pipeClient.ReadMessage();
+ break;
+ }
+ catch (Exception ex)
+ {
+ Console.WriteLine($"鎺ユ敹鏁版嵁鏃跺彂鐢熼敊璇�: {ex.Message}");
+ break;
+ }
+ }
+ return true;
+ }
+ catch (Exception ex)
+ {
+ return false;
+ }
+ }
+ }
+
+ public void Disconnect()
+ {
+ _isConnected = false;
+ _cancellationTokenSource.Cancel();
+ }
+
+ public void Dispose()
+ {
+ if (!_isDisposed)
+ {
+ Disconnect();
+ _pipeClient?.Dispose();
+ _cancellationTokenSource.Dispose();
+ _isDisposed = true;
+ }
+ }
+ }
+}
\ No newline at end of file
--
Gitblit v1.9.1