add:基于IPC的IACore在net6的实现
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
using Ink_Canvas.Controls;
|
||||
using System;
|
||||
using System.Windows;
|
||||
using System.Windows.Media;
|
||||
@@ -8,15 +7,7 @@ namespace Ink_Canvas.Helpers
|
||||
{
|
||||
internal class AnimationsHelper
|
||||
{
|
||||
private static UIElement ResolveAnimationTarget(UIElement element)
|
||||
{
|
||||
if (element is BoardMenuFrame frame)
|
||||
{
|
||||
frame.ApplyTemplate();
|
||||
return frame.AnimationTarget ?? element;
|
||||
}
|
||||
return element;
|
||||
}
|
||||
private static UIElement ResolveAnimationTarget(UIElement element) => element;
|
||||
|
||||
public static void ShowWithFadeIn(UIElement element, double duration = 0.15)
|
||||
{
|
||||
|
||||
@@ -1,24 +1,16 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.IO.Pipes;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
using System.Windows.Ink;
|
||||
using System.Windows.Media;
|
||||
|
||||
namespace Ink_Canvas.Helpers
|
||||
{
|
||||
/// <summary>
|
||||
/// 通过 Named Pipe IPC 调用 32 位辅助进程 InkCanvasForClass.IACoreHelper.exe 进行 IACore 形状识别。
|
||||
/// 单例,线程安全,辅助进程崩溃后自动重启。
|
||||
/// </summary>
|
||||
public sealed class IpcIACoreClient : IDisposable
|
||||
{
|
||||
// ── 单例 ──────────────────────────────────────────────────────────────
|
||||
private static IpcIACoreClient _instance;
|
||||
private static readonly object _instanceLock = new object();
|
||||
|
||||
@@ -34,35 +26,26 @@ namespace Ink_Canvas.Helpers
|
||||
}
|
||||
}
|
||||
|
||||
// ── 状态 ──────────────────────────────────────────────────────────────
|
||||
private Process _helperProcess;
|
||||
private readonly object _pipeLock = new object();
|
||||
private bool _disposed;
|
||||
private bool _available;
|
||||
|
||||
// 辅助进程 EXE 的路径(与主 EXE 同目录)
|
||||
private static string HelperExePath =>
|
||||
Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "InkCanvasForClass.IACoreHelper.exe");
|
||||
|
||||
// Named Pipe 名称(含主进程 PID,保证唯一)
|
||||
private string PipeName =>
|
||||
string.Format("ICC_IACoreHelper_{0}", Process.GetCurrentProcess().Id);
|
||||
|
||||
private IpcIACoreClient() { }
|
||||
|
||||
// ── 公开 API ──────────────────────────────────────────────────────────
|
||||
|
||||
/// <summary>启动辅助进程,返回是否成功。</summary>
|
||||
public bool Start()
|
||||
{
|
||||
if (_disposed) return false;
|
||||
|
||||
// 已运行则无需重启,避免 Warmup + App 两处调用导致重复启动
|
||||
if (IsAvailable) return true;
|
||||
|
||||
if (!File.Exists(HelperExePath))
|
||||
{
|
||||
LogHelper.WriteLogToFile($"[IACoreIPC] 辅助进程不存在: {HelperExePath}", LogHelper.LogType.Warning);
|
||||
_available = false;
|
||||
return false;
|
||||
}
|
||||
@@ -70,13 +53,8 @@ namespace Ink_Canvas.Helpers
|
||||
return LaunchHelper();
|
||||
}
|
||||
|
||||
/// <summary>是否就绪(辅助进程运行中)。</summary>
|
||||
public bool IsAvailable => _available && _helperProcess != null && !_helperProcess.HasExited;
|
||||
|
||||
/// <summary>
|
||||
/// 发送笔画到辅助进程进行 IACore 形状识别。
|
||||
/// 超时或失败时返回 <see cref="InkShapeRecognitionResult.Empty"/>。
|
||||
/// </summary>
|
||||
public InkShapeRecognitionResult Recognize(StrokeCollection strokes)
|
||||
{
|
||||
if (strokes == null || strokes.Count == 0)
|
||||
@@ -84,32 +62,22 @@ namespace Ink_Canvas.Helpers
|
||||
|
||||
EnsureHelperAlive();
|
||||
if (!IsAvailable)
|
||||
{
|
||||
LogHelper.WriteLogToFile($"[IACoreIPC] Recognize 跳过:辅助进程不可用(strokes={strokes.Count})", LogHelper.LogType.Warning);
|
||||
return InkShapeRecognitionResult.Empty;
|
||||
}
|
||||
|
||||
lock (_pipeLock)
|
||||
{
|
||||
try
|
||||
{
|
||||
LogHelper.WriteLogToFile($"[IACoreIPC] Recognize 请求:strokes={strokes.Count}");
|
||||
var result = SendRecognizeRequest(strokes);
|
||||
LogHelper.WriteLogToFile(
|
||||
$"[IACoreIPC] Recognize 响应:IsSuccess={result.IsSuccess}, Shape={result.ShapeName}, StrokesToRemove={result.StrokesToRemove?.Count ?? 0}");
|
||||
return result;
|
||||
return SendRecognizeRequest(strokes);
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch
|
||||
{
|
||||
LogHelper.WriteLogToFile($"[IACoreIPC] 识别失败: {ex.GetType().Name}: {ex.Message}", LogHelper.LogType.Error);
|
||||
KillHelper();
|
||||
return InkShapeRecognitionResult.Empty;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// ── 内部实现 ──────────────────────────────────────────────────────────
|
||||
|
||||
private bool LaunchHelper()
|
||||
{
|
||||
try
|
||||
@@ -133,16 +101,12 @@ namespace Ink_Canvas.Helpers
|
||||
_helperProcess.EnableRaisingEvents = true;
|
||||
_helperProcess.Exited += OnHelperExited;
|
||||
|
||||
// 等待 Pipe 就绪(最多 3 s)
|
||||
bool pipeReady = WaitForPipe(3000);
|
||||
_available = pipeReady;
|
||||
|
||||
LogHelper.WriteLogToFile($"[IACoreIPC] 辅助进程启动{(pipeReady ? "成功" : "失败(Pipe 未就绪)")},PID={_helperProcess?.Id}");
|
||||
return pipeReady;
|
||||
}
|
||||
catch (Exception ex)
|
||||
catch
|
||||
{
|
||||
LogHelper.WriteLogToFile($"[IACoreIPC] 启动辅助进程失败: {ex.Message}", LogHelper.LogType.Error);
|
||||
_available = false;
|
||||
return false;
|
||||
}
|
||||
@@ -161,7 +125,6 @@ namespace Ink_Canvas.Helpers
|
||||
using (var probe = new NamedPipeClientStream(".", PipeName, PipeDirection.InOut))
|
||||
{
|
||||
probe.Connect(200);
|
||||
// 连接成功后立即断开,下次 Recognize 会重新连接
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -183,7 +146,6 @@ namespace Ink_Canvas.Helpers
|
||||
using (var writer = new BinaryWriter(client, System.Text.Encoding.UTF8, leaveOpen: true))
|
||||
using (var reader = new BinaryReader(client, System.Text.Encoding.UTF8, leaveOpen: true))
|
||||
{
|
||||
// 序列化请求
|
||||
writer.Write(CmdRecognize);
|
||||
writer.Write(strokes.Count);
|
||||
foreach (var stroke in strokes)
|
||||
@@ -199,7 +161,6 @@ namespace Ink_Canvas.Helpers
|
||||
}
|
||||
writer.Flush();
|
||||
|
||||
// 读取响应
|
||||
bool success = reader.ReadBoolean();
|
||||
string shape = reader.ReadString();
|
||||
float cx = reader.ReadSingle();
|
||||
@@ -220,7 +181,6 @@ namespace Ink_Canvas.Helpers
|
||||
if (!success || string.IsNullOrEmpty(shape))
|
||||
return InkShapeRecognitionResult.Empty;
|
||||
|
||||
// 根据下标还原参与识别的笔画子集
|
||||
var recognized = new StrokeCollection();
|
||||
foreach (int idx in indices)
|
||||
if (idx >= 0 && idx < strokes.Count)
|
||||
@@ -240,16 +200,12 @@ namespace Ink_Canvas.Helpers
|
||||
private void EnsureHelperAlive()
|
||||
{
|
||||
if (!IsAvailable)
|
||||
{
|
||||
LogHelper.WriteLogToFile("[IACoreIPC] 辅助进程不可用,尝试重启...", LogHelper.LogType.Warning);
|
||||
LaunchHelper();
|
||||
}
|
||||
}
|
||||
|
||||
private void OnHelperExited(object sender, EventArgs e)
|
||||
{
|
||||
_available = false;
|
||||
LogHelper.WriteLogToFile("[IACoreIPC] 辅助进程意外退出", LogHelper.LogType.Warning);
|
||||
}
|
||||
|
||||
private void KillHelper()
|
||||
@@ -257,12 +213,10 @@ namespace Ink_Canvas.Helpers
|
||||
if (_helperProcess == null) return;
|
||||
try
|
||||
{
|
||||
// 先解除事件订阅,避免主动 Kill 时触发 OnHelperExited 误报
|
||||
try { _helperProcess.Exited -= OnHelperExited; } catch { }
|
||||
|
||||
if (!_helperProcess.HasExited)
|
||||
{
|
||||
// 发送优雅关闭命令
|
||||
try
|
||||
{
|
||||
using (var client = new NamedPipeClientStream(".", PipeName, PipeDirection.InOut))
|
||||
@@ -272,7 +226,7 @@ namespace Ink_Canvas.Helpers
|
||||
w.Write(CmdShutdown);
|
||||
}
|
||||
}
|
||||
catch { /* 忽略,下面直接 Kill */ }
|
||||
catch { }
|
||||
|
||||
if (!_helperProcess.WaitForExit(800))
|
||||
_helperProcess.Kill();
|
||||
@@ -294,7 +248,6 @@ namespace Ink_Canvas.Helpers
|
||||
KillHelper();
|
||||
}
|
||||
|
||||
// ── 协议常量(与 IACoreHelper/IpcProtocol.cs 保持一致) ─────────────
|
||||
private const int IpcTimeoutMs = 5000;
|
||||
private const byte CmdRecognize = 0x01;
|
||||
private const byte CmdShutdown = 0xFF;
|
||||
|
||||
Reference in New Issue
Block a user