// by Diplomat #pragma warning disable 0105 using System; using System.Runtime.InteropServices; using Devolutions.IronRdp.Diplomat; #pragma warning restore 0105 namespace Devolutions.IronRdp; #nullable enable public partial class ClientConnector: IDisposable { private unsafe Raw.ClientConnector* _inner; public DynState DynState { get { return GetDynState(); } } /// /// Creates a managed ClientConnector from a raw handle. /// /// /// Safety: you should not build two managed objects using the same raw handle (may causes use-after-free and double-free). ///
/// This constructor assumes the raw struct is allocated on Rust side. /// If implemented, the custom Drop implementation on Rust side WILL run on destruction. ///
public unsafe ClientConnector(Raw.ClientConnector* handle) { _inner = handle; } /// /// /// A ClientConnector allocated on Rust side. /// public static ClientConnector New(Config config, string clientAddr) { unsafe { byte[] clientAddrBuf = DiplomatUtils.StringToUtf8(clientAddr); nuint clientAddrBufLength = (nuint)clientAddrBuf.Length; Raw.Config* configRaw; configRaw = config.AsFFI(); if (configRaw == null) { throw new ObjectDisposedException("Config"); } fixed (byte* clientAddrBufPtr = clientAddrBuf) { Raw.ConnectorFfiResultBoxClientConnectorBoxIronRdpError result = Raw.ClientConnector.New(configRaw, clientAddrBufPtr, clientAddrBufLength); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } Raw.ClientConnector* retVal = result.Ok; return new ClientConnector(retVal); } } } /// /// Must use /// /// public void WithStaticChannelRdpSnd() { unsafe { if (_inner == null) { throw new ObjectDisposedException("ClientConnector"); } Raw.ConnectorFfiResultVoidBoxIronRdpError result = Raw.ClientConnector.WithStaticChannelRdpSnd(_inner); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } } } /// /// Must use /// /// public void WithStaticChannelRdpdr(string computerName, uint smartCardDeviceId) { unsafe { if (_inner == null) { throw new ObjectDisposedException("ClientConnector"); } byte[] computerNameBuf = DiplomatUtils.StringToUtf8(computerName); nuint computerNameBufLength = (nuint)computerNameBuf.Length; fixed (byte* computerNameBufPtr = computerNameBuf) { Raw.ConnectorFfiResultVoidBoxIronRdpError result = Raw.ClientConnector.WithStaticChannelRdpdr(_inner, computerNameBufPtr, computerNameBufLength, smartCardDeviceId); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } } } } /// public void WithDynamicChannelDisplayControl() { unsafe { if (_inner == null) { throw new ObjectDisposedException("ClientConnector"); } Raw.ConnectorFfiResultVoidBoxIronRdpError result = Raw.ClientConnector.WithDynamicChannelDisplayControl(_inner); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } } } /// public bool ShouldPerformSecurityUpgrade() { unsafe { if (_inner == null) { throw new ObjectDisposedException("ClientConnector"); } Raw.ConnectorFfiResultBoolBoxIronRdpError result = Raw.ClientConnector.ShouldPerformSecurityUpgrade(_inner); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } bool retVal = result.Ok; return retVal; } } /// public void MarkSecurityUpgradeAsDone() { unsafe { if (_inner == null) { throw new ObjectDisposedException("ClientConnector"); } Raw.ConnectorFfiResultVoidBoxIronRdpError result = Raw.ClientConnector.MarkSecurityUpgradeAsDone(_inner); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } } } /// public bool ShouldPerformCredssp() { unsafe { if (_inner == null) { throw new ObjectDisposedException("ClientConnector"); } Raw.ConnectorFfiResultBoolBoxIronRdpError result = Raw.ClientConnector.ShouldPerformCredssp(_inner); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } bool retVal = result.Ok; return retVal; } } /// public void MarkCredsspAsDone() { unsafe { if (_inner == null) { throw new ObjectDisposedException("ClientConnector"); } Raw.ConnectorFfiResultVoidBoxIronRdpError result = Raw.ClientConnector.MarkCredsspAsDone(_inner); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } } } /// /// /// A Written allocated on Rust side. /// public Written Step(byte[] input, WriteBuf writeBuf) { unsafe { if (_inner == null) { throw new ObjectDisposedException("ClientConnector"); } nuint inputLength = (nuint)input.Length; Raw.WriteBuf* writeBufRaw; writeBufRaw = writeBuf.AsFFI(); if (writeBufRaw == null) { throw new ObjectDisposedException("WriteBuf"); } fixed (byte* inputPtr = input) { Raw.ConnectorFfiResultBoxWrittenBoxIronRdpError result = Raw.ClientConnector.Step(_inner, inputPtr, inputLength, writeBufRaw); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } Raw.Written* retVal = result.Ok; return new Written(retVal); } } } /// /// /// A Written allocated on Rust side. /// public Written StepNoInput(WriteBuf writeBuf) { unsafe { if (_inner == null) { throw new ObjectDisposedException("ClientConnector"); } Raw.WriteBuf* writeBufRaw; writeBufRaw = writeBuf.AsFFI(); if (writeBufRaw == null) { throw new ObjectDisposedException("WriteBuf"); } Raw.ConnectorFfiResultBoxWrittenBoxIronRdpError result = Raw.ClientConnector.StepNoInput(_inner, writeBufRaw); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } Raw.Written* retVal = result.Ok; return new Written(retVal); } } /// public void AttachStaticCliprdr(Cliprdr cliprdr) { unsafe { if (_inner == null) { throw new ObjectDisposedException("ClientConnector"); } Raw.Cliprdr* cliprdrRaw; cliprdrRaw = cliprdr.AsFFI(); if (cliprdrRaw == null) { throw new ObjectDisposedException("Cliprdr"); } Raw.ConnectorFfiResultVoidBoxIronRdpError result = Raw.ClientConnector.AttachStaticCliprdr(_inner, cliprdrRaw); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } } } /// /// /// A PduHint allocated on Rust side. /// public PduHint NextPduHint() { unsafe { if (_inner == null) { throw new ObjectDisposedException("ClientConnector"); } Raw.ConnectorFfiResultOptBoxPduHintBoxIronRdpError result = Raw.ClientConnector.NextPduHint(_inner); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } Raw.PduHint* retVal = result.Ok; if (retVal == null) { return null; } return new PduHint(retVal); } } /// /// /// A DynState allocated on Rust side. /// public DynState GetDynState() { unsafe { if (_inner == null) { throw new ObjectDisposedException("ClientConnector"); } Raw.ConnectorFfiResultBoxDynStateBoxIronRdpError result = Raw.ClientConnector.GetDynState(_inner); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } Raw.DynState* retVal = result.Ok; return new DynState(retVal); } } /// /// /// A ClientConnectorState allocated on Rust side. /// public ClientConnectorState ConsumeAndCastToClientConnectorState() { unsafe { if (_inner == null) { throw new ObjectDisposedException("ClientConnector"); } Raw.ConnectorFfiResultBoxClientConnectorStateBoxIronRdpError result = Raw.ClientConnector.ConsumeAndCastToClientConnectorState(_inner); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } Raw.ClientConnectorState* retVal = result.Ok; return new ClientConnectorState(retVal); } } /// /// Returns the underlying raw handle. /// public unsafe Raw.ClientConnector* AsFFI() { return _inner; } /// /// Destroys the underlying object immediately. /// public void Dispose() { unsafe { if (_inner == null) { return; } Raw.ClientConnector.Destroy(_inner); _inner = null; GC.SuppressFinalize(this); } } ~ClientConnector() { Dispose(); } }