14 Commits

Author SHA1 Message Date
Richard Markiewicz 4982320bff perf(ffi): size PDUs from the header and add buffered, sync and cancellable Framed reads (#2289)
`Framed.ReadPdu` copied the whole buffer into a new array on every
`FindSize` call. An active-stage PDU announces its length in its first
bytes (four for TPKT, at most three for fast-path), so `Framed` now
probes only the first four buffered bytes into a reusable array;
`ReadByHint` keeps the full copy because connection-time PDUs are few
and small.

New members, alongside the existing ones:

- `ReadPdu(CancellationToken)`, and `CancellationToken` overloads of
`ReadExact` and both `ReadByHint`.
- `TryReadBufferedPdu(out (Action, byte[]))`: takes the next PDU when it
is already buffered, without reading the stream, so a consumer can flush
rendering only when nothing more is pending.
- `ReadPduSync()`: a blocking read for a dedicated read thread.

A cancelled read stops waiting but leaves the stream read in flight; the
next read, asynchronous or synchronous, finishes it first, so bytes are
neither lost nor read twice. Every read reports a stream that ends
before the PDU is complete as `IronRdpLibException(EndOfFile)`, where
`ReadExact` and `ReadByHint` threw a plain `Exception`.

`Connection.Connect`,
`RDCleanPathConnection.ConnectRDCleanPath`/`ConnectVmConnectRDCleanPath`
and `Connection.SingleSequenceStep` take an optional `CancellationToken`
and pass it to the hint reads, the TCP connect and the TLS handshake.

Consumers:

- The Avalonia example cancels the connection sequence and the read loop
through one token on window close or session end, and uses
`TryReadBufferedPdu` to present the bitmap only once no further PDU is
buffered, so a burst of updates draws once.
- `ConnectExample` uses the cancellable read with a timeout instead of
`Task.WhenAny`, which left a read pending.
- Devolutions/IronSessions `RdpSession` uses `TryReadBufferedPdu` to
flush rendering only when the socket is quiet,
`ReadPdu(CancellationToken)` for disconnect and timeout, the cancellable
`ReadByHint` during the connection sequence, and `ReadPduSync` for a
dedicated read thread on Mono (Android/iOS), where awaiting every read
hopped pool threads. `ReadPduSync` has no consumer in this repository on
purpose: the examples are async end to end.

`ffi/INTENT.md` records the probe width, the `ReadByHint` split, the EOF
policy and the read regime.

Smoke tests cover header-only sizing for TPKT and both fast-path length
encodings, several PDUs in one read, a partial PDU left buffered, the
sync read across split reads, EOF inside a PDU body on every read path,
and a cancelled read whose in-flight stream read the next read finishes.
2026-10-28 20:35:17 -04:00
irvingouj@Devolutions d28a214c94 fix(ffi)!: return owned data, not borrowed views (#2167)
`DecodedImage`, `DecodedPointer` and `ActiveStageOutput` own the
framebuffer, the pointer bitmap and response payloads, which are large
native allocations the garbage collector does not see. That is the first
condition in `ffi/INTENT.md` for being `IDisposable`. They handed out
views that borrow from them, which the new .NET generator refuses for
disposable types, and every caller copied the bytes into a managed array
right away anyway.

- `ActiveStageOutput.ResponseFrame`/`WindowingOrders` and
`DecodedPointer.Data` return an owned `VecU8`, which has the same
`Size`/`Fill` members as the old view.
- `DecodedImage.Data` is replaced by `DataSize` and `FillData(byte[])`.
They copy the framebuffer once into the caller's buffer, and a larger
buffer is fine.

The three types are now `IDisposable`, and `ffi/README.md` lists them.
The Avalonia example disposes the image it replaces on reactivation, and
both examples dispose each output and response frame they read.

BREAKING CHANGE: `DecodedImage.Data` is removed, use `DataSize` and
`FillData`. `ResponseFrame`, `WindowingOrders` and `DecodedPointer.Data`
now return `VecU8` instead of `BytesSlice`.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-authored-by: Marc-André Moreau <mamoreau@devolutions.net>
Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com>
2026-10-27 11:22:16 -04:00
irvingouj@Devolutions e3affd83c5 build(ffi)!: move to Diplomat 0.16.1-devo.2 (#2165)
The .NET bindings were generated by Diplomat 0.7 from a personal fork,
years behind upstream. Move to the protected `Devolutions/diplomat` tag
that IronVNC and picky-rs already use, with the same xtask pinning and
install flow.

Rust side:
- pin `diplomat` and `diplomat-runtime` to tag `0.16.1-devo.2` and drop
the `[patch.crates-io]` entry for the old fork; its zero-length slice
fix is upstream now, since slice parameters go through `DiplomatSlice`,
which maps a null pointer to an empty slice
- `opaque_mut` on the 20 types that are mutated through `&mut`, named
lifetimes on borrowed returns, `DiplomatWriteable` renamed to
`DiplomatWrite`, and no explicit `Clone`/`Copy` on bridge enums
- move `dotnet-interop-conf.toml` to the `lib_name` + `[dotnet]` layout

Tooling: xtask installs `diplomat-tool` from the tag with Rust 1.96.0,
because the tag does not build with the workspace toolchain. It records
the source and version so a tool from another tag is never reused,
checks that the tool and the ffi crate use the same tag, and calls the
tool by absolute path. The `ffi` CI job caches those records with the
binary, so a cache hit does not reinstall the tool.

The library and examples use properties instead of the removed
`GetX()`/`SetX()` methods.

`ffi/README.md` gains a migration section with these steps.

BREAKING CHANGE: for consumers of `Devolutions.IronRdp`:
- `GetX()`/`SetX()` methods that duplicated a property are removed
- the `&mut` getters on `ClientConnectorState` and
`CredsspSequenceInitResult` are methods only
- `IronRdpErrorKind.IO` and `WrongOS` are now `Io` and `WrongOs`
- `Position`, `MultitransportRequest` and `NetworkCharacteristics` are
structs with public fields instead of classes with properties
- the `Devolutions.IronRdp.Raw` namespace is internal, and the public
`AsFFI()` methods and raw-pointer constructors are removed
- `DiplomatWriteable`, `DiplomatOpaqueException` and the
`IronRdpError.ToDisplay(DiplomatWriteable)` overload are removed
- `IronRdpException` is no longer `partial`
- only the ten types listed in `ffi/README.md` are `IDisposable`; every
other type, such as `Config`, `InclusiveRectangle` and
`ClientConnector`, is released by its finalizer and no longer has
`Dispose()`
- overlapping calls on one native object now throw, such as calling
`ActiveStage` from the PDU loop and an input handler at once, or using a
`CredsspSequence` while its `CredsspProcessGenerator` is alive

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>

Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-26 13:12:48 -04:00
irvingouj@Devolutions d3e0cb17e1 feat(ffi): expose RDCleanPath (#1014)
Add RDCleanPath support for Devolutions.IronRDP .NET package
2025-10-30 16:38:41 +00:00
Benoît Cortier 712da42ded fix: inject socket local address for the client addr (#759)
We used to inject the resolved target server address, but that is not
what is expected. Server typically ignores this field so this was not a
problem up until now.
2025-04-21 09:08:50 +00:00
Benoît CORTIER 1bb6895422 docs(ffi): fix compilation of examples 2025-03-25 12:26:28 +01:00
irvingouj @ Devolutions 590221469a chore(ffi):allow Devolutions.IronRdp to be build and packed with Nuget with the dll on Windows (#457)
* Allow Devolutions.IronRdp to be build and packed with Nuget alone with the dll

* review fix
2024-05-03 15:29:04 -04:00
irvingouj @ Devolutions 0ec5be5dc4 feat(ffi): API for enabling tracing logs (#452) 2024-05-03 02:44:00 -04:00
irvingouj @ Devolutions a232b4ee0f feat(ffi): add a client example using Avalonia (#443) 2024-04-24 10:09:34 -04:00
Benoît CORTIER c0031d34eb fix(dotnet): use ImageSharp to also support Linux 2024-04-09 11:35:48 -04:00
Benoît CORTIER a18fb34b6d fix(dotnet): include stacktrace in the error report 2024-04-09 11:35:48 -04:00
Benoît CORTIER d70c9f0e98 style(dotnet): run dotnet format 2024-04-09 11:35:48 -04:00
irvingouj @ Devolutions ef2055235a feat(ffi): screenshot example using C# bindings (#437) 2024-04-09 10:13:12 -04:00
irvingouj @ Devolutions 3bcf51306d feat(ffi): initial C# bindings (connector only) (#423) 2024-04-05 15:11:32 +00:00