42 lines
1.2 KiB
C#
42 lines
1.2 KiB
C#
namespace NucleicProtocol.Interop;
|
|||
|
|
|
||
|
|
/// Managed ownership wrapper for the shared Swift transcript projector.
|
||
|
|
public interface ITranscriptProjector : IDisposable
|
||
|
|
{
|
||
|
|
ProjectionDiff Apply(string hostMessageJson);
|
||
|
|
}
|
||
|
|
|
||
|
|
public sealed class ProtocolProjection : ITranscriptProjector
|
||
|
|
{
|
||
|
|
private readonly object gate = new();
|
||
|
|
private readonly INativeProtocol native;
|
||
|
|
private nint handle;
|
||
|
|
|
||
|
|
public ProtocolProjection(INativeProtocol? native = null)
|
||
|
|
{
|
||
|
|
this.native = native ?? PInvokeNativeProtocol.Instance;
|
||
|
|
handle = this.native.ProjectionCreate();
|
||
|
|
if (handle == 0) throw new InvalidOperationException("could not create transcript projection");
|
||
|
|
}
|
||
|
|
|
||
|
|
public ProjectionDiff Apply(string hostMessageJson)
|
||
|
|
{
|
||
|
|
lock (gate)
|
||
|
|
{
|
||
|
|
ObjectDisposedException.ThrowIf(handle == 0, this);
|
||
|
|
var value = native.ProjectionApply(handle, hostMessageJson);
|
||
|
|
return ProjectionDiff.Parse(native.CopyAndFreeString(value));
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
public void Dispose()
|
||
|
|
{
|
||
|
|
lock (gate)
|
||
|
|
{
|
||
|
|
if (handle == 0) return;
|
||
|
|
native.ProjectionFree(handle);
|
||
|
|
handle = 0;
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|