mirror of
https://github.com/wwiinnddyy/LanMountainDesktop.git
synced 2026-08-18 15:33:33 +08:00
feat: PluginIsolation 新增进程内 IPC 传输(InProcPluginIpcTransport)
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
namespace LanMountainDesktop.PluginIsolation.Ipc;
|
||||
|
||||
/// <summary>
|
||||
/// 进程内 IPC 传输。将 <see cref="PluginIpcClient"/> 的调度委托直接绑定到
|
||||
/// <see cref="PluginIpcServer"/> 的处理入口,不经过命名管道。
|
||||
/// 用于不支持子进程/命名管道的平台(如 Android),插件契约与序列化路径保持不变:
|
||||
/// 请求仍会经过 JsonElement 序列化/反序列化边界,与管道传输行为一致。
|
||||
/// </summary>
|
||||
public sealed class InProcPluginIpcTransport : IDisposable
|
||||
{
|
||||
private readonly PluginIpcClient _client;
|
||||
private volatile bool _disposed;
|
||||
|
||||
private InProcPluginIpcTransport(PluginIpcClient client, PluginIpcServer server)
|
||||
{
|
||||
_client = client;
|
||||
Server = server;
|
||||
|
||||
client.RequestDispatcher = (route, payload, cancellationToken) =>
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
return server.HandleRequestAsync(route, payload, cancellationToken);
|
||||
};
|
||||
|
||||
client.NotificationDispatcher = (route, payload, cancellationToken) =>
|
||||
{
|
||||
ThrowIfDisposed();
|
||||
return server.HandleNotificationAsync(route, payload, cancellationToken);
|
||||
};
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 绑定后的服务端。
|
||||
/// </summary>
|
||||
public PluginIpcServer Server { get; }
|
||||
|
||||
/// <summary>
|
||||
/// 绑定后的客户端。请求/通知将直接分发到 <see cref="Server"/>。
|
||||
/// </summary>
|
||||
public PluginIpcClient Client => _client;
|
||||
|
||||
/// <summary>
|
||||
/// 将客户端与服务端以进程内直通方式连接。
|
||||
/// </summary>
|
||||
/// <param name="client">插件侧客户端</param>
|
||||
/// <param name="server">宿主侧服务端</param>
|
||||
/// <returns>传输句柄;Dispose 后客户端调度将抛出 <see cref="ObjectDisposedException"/>。</returns>
|
||||
public static InProcPluginIpcTransport Connect(PluginIpcClient client, PluginIpcServer server)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(client);
|
||||
ArgumentNullException.ThrowIfNull(server);
|
||||
return new InProcPluginIpcTransport(client, server);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 断开传输。之后客户端的请求/通知将失败。
|
||||
/// </summary>
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_disposed = true;
|
||||
_client.RequestDispatcher = null;
|
||||
_client.NotificationDispatcher = null;
|
||||
}
|
||||
|
||||
private void ThrowIfDisposed()
|
||||
{
|
||||
ObjectDisposedException.ThrowIf(_disposed, this);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user