// 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 WriteBuf: IDisposable { private unsafe Raw.WriteBuf* _inner; public VecU8 Filled { get { return GetFilled(); } } /// /// Creates a managed WriteBuf 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 WriteBuf(Raw.WriteBuf* handle) { _inner = handle; } /// /// A WriteBuf allocated on Rust side. /// public static WriteBuf New() { unsafe { Raw.WriteBuf* retVal = Raw.WriteBuf.New(); return new WriteBuf(retVal); } } public void Clear() { unsafe { if (_inner == null) { throw new ObjectDisposedException("WriteBuf"); } Raw.WriteBuf.Clear(_inner); } } /// public void ReadIntoBuf(byte[] buf) { unsafe { if (_inner == null) { throw new ObjectDisposedException("WriteBuf"); } nuint bufLength = (nuint)buf.Length; fixed (byte* bufPtr = buf) { Raw.PduFfiResultVoidBoxIronRdpError result = Raw.WriteBuf.ReadIntoBuf(_inner, bufPtr, bufLength); if (!result.isOk) { throw new IronRdpException(new IronRdpError(result.Err)); } } } } /// /// A VecU8 allocated on Rust side. /// public VecU8 GetFilled() { unsafe { if (_inner == null) { throw new ObjectDisposedException("WriteBuf"); } Raw.VecU8* retVal = Raw.WriteBuf.GetFilled(_inner); return new VecU8(retVal); } } /// /// Returns the underlying raw handle. /// public unsafe Raw.WriteBuf* AsFFI() { return _inner; } /// /// Destroys the underlying object immediately. /// public void Dispose() { unsafe { if (_inner == null) { return; } Raw.WriteBuf.Destroy(_inner); _inner = null; GC.SuppressFinalize(this); } } ~WriteBuf() { Dispose(); } }