namespace LanMountainDesktop.PluginIsolation.Ipc;
///
/// 进程内 IPC 传输。将 的调度委托直接绑定到
/// 的处理入口,不经过命名管道。
/// 用于不支持子进程/命名管道的平台(如 Android),插件契约与序列化路径保持不变:
/// 请求仍会经过 JsonElement 序列化/反序列化边界,与管道传输行为一致。
///
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);
};
}
///
/// 绑定后的服务端。
///
public PluginIpcServer Server { get; }
///
/// 绑定后的客户端。请求/通知将直接分发到 。
///
public PluginIpcClient Client => _client;
///
/// 将客户端与服务端以进程内直通方式连接。
///
/// 插件侧客户端
/// 宿主侧服务端
/// 传输句柄;Dispose 后客户端调度将抛出 。
public static InProcPluginIpcTransport Connect(PluginIpcClient client, PluginIpcServer server)
{
ArgumentNullException.ThrowIfNull(client);
ArgumentNullException.ThrowIfNull(server);
return new InProcPluginIpcTransport(client, server);
}
///
/// 断开传输。之后客户端的请求/通知将失败。
///
public void Dispose()
{
if (_disposed)
{
return;
}
_disposed = true;
_client.RequestDispatcher = null;
_client.NotificationDispatcher = null;
}
private void ThrowIfDisposed()
{
ObjectDisposedException.ThrowIf(_disposed, this);
}
}