using System;
using ZeroDDS.Core;
namespace ZeroDDS.Domain;
public static class DomainParticipantFactory
{
public static IntPtr GetInstance() => Native.DpfGetInstance();
public static DomainParticipant CreateParticipant(uint domainId)
{
var f = Native.DpfGetInstance();
var h = Native.DpfCreateParticipant(f, domainId, IntPtr.Zero);
if (h == IntPtr.Zero) throw new DdsError("DomainParticipantFactory::CreateParticipant");
return new DomainParticipant(h, f);
}
public static DomainParticipant CreateParticipant(uint domainId,
ZeroDDS.Qos.DomainParticipantQos qos)
{
var f = Native.DpfGetInstance();
using var scope = new ZeroDDS.QosBridge.NativeQosScope();
var native = ZeroDDS.QosBridge.QosBridge.ToNative(qos, scope);
IntPtr h;
unsafe { h = Native.DpfCreateParticipant(f, domainId, (IntPtr)(&native)); }
if (h == IntPtr.Zero) throw new DdsError("DomainParticipantFactory::CreateParticipant with QoS");
return new DomainParticipant(h, f);
}
}
public sealed class DomainParticipant : IDisposable
{
private IntPtr _handle;
private readonly IntPtr _factory;
private bool _disposed;
internal DomainParticipant(IntPtr handle, IntPtr factory)
{
_handle = handle;
_factory = factory;
}
public IntPtr Handle => _handle;
public uint DomainId
{
get
{
EnsureAlive();
return Native.DpGetDomainId(_handle);
}
}
public void AssertLiveliness()
{
EnsureAlive();
StatusCheck.Check(Native.DpAssertLiveliness(_handle), "DomainParticipant::AssertLiveliness");
}
public bool ContainsEntity(InstanceHandle h)
{
EnsureAlive();
return Native.DpContainsEntity(_handle, h.Value) != 0;
}
public void IgnoreParticipant(InstanceHandle h)
{
EnsureAlive();
StatusCheck.Check(Native.DpIgnoreParticipant(_handle, h.Value),
"DomainParticipant::IgnoreParticipant");
}
public void DeleteContainedEntities()
{
EnsureAlive();
StatusCheck.Check(Native.DpDeleteContainedEntities(_handle),
"DomainParticipant::DeleteContainedEntities");
}
public void Dispose()
{
if (_disposed) return;
_disposed = true;
if (_handle != IntPtr.Zero)
{
Native.DpDeleteContainedEntities(_handle);
Native.DpfDeleteParticipant(_factory, _handle);
_handle = IntPtr.Zero;
}
GC.SuppressFinalize(this);
}
~DomainParticipant() { Dispose(); }
internal void EnsureAlive()
{
if (_disposed || _handle == IntPtr.Zero)
throw new AlreadyClosedException("DomainParticipant disposed");
}
}