Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions crates/ironrdp-server/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ pub struct BuilderDone {
autodetect_rtt: Option<Arc<AtomicU32>>,
autodetect_baseline_rtt: Option<Arc<AtomicU32>>,
autodetect_bandwidth: Option<Arc<AtomicU32>>,
autodetect_bandwidth_generation: Option<Arc<AtomicU32>>,
honor_client_desktop_size: Option<DesktopSize>,
auto_reconnect_cookie: Option<ServerAutoReconnect>,
connection_policy: ConnectionPolicy,
Expand Down Expand Up @@ -168,6 +169,7 @@ impl RdpServerBuilder<WantsDisplay> {
autodetect_rtt: None,
autodetect_baseline_rtt: None,
autodetect_bandwidth: None,
autodetect_bandwidth_generation: None,
honor_client_desktop_size: None,
connection_policy: ConnectionPolicy::default(),
auto_reconnect_cookie: None,
Expand Down Expand Up @@ -201,6 +203,7 @@ impl RdpServerBuilder<WantsDisplay> {
autodetect_rtt: None,
autodetect_baseline_rtt: None,
autodetect_bandwidth: None,
autodetect_bandwidth_generation: None,
honor_client_desktop_size: None,
connection_policy: ConnectionPolicy::default(),
auto_reconnect_cookie: None,
Expand Down Expand Up @@ -406,6 +409,18 @@ impl RdpServerBuilder<BuilderDone> {
self
}

/// Inject a shared handle that increments every time a Bandwidth Measure
/// transaction completes, whether or not it produced a usable figure.
/// Pairs with [`Self::with_autodetect_bandwidth_handle`]: the bandwidth
/// figure alone repeats too often to tell a fresh measurement window
/// apart from a stale one. When not called, the server allocates its own
/// (still readable via
/// [`RdpServer::autodetect_bandwidth_generation_handle`]).
pub fn with_autodetect_bandwidth_generation_handle(mut self, handle: Arc<AtomicU32>) -> Self {
self.state.autodetect_bandwidth_generation = Some(handle);
self
}

/// Provision the Server Auto-Reconnect Cookie (MS-RDPBCGR 2.2.4.2
/// `ARC_SC_PRIVATE_PACKET`) handed to the client during logon.
///
Expand Down Expand Up @@ -480,6 +495,7 @@ impl RdpServerBuilder<BuilderDone> {
self.state.autodetect_rtt,
self.state.autodetect_baseline_rtt,
self.state.autodetect_bandwidth,
self.state.autodetect_bandwidth_generation,
);
server.set_credential_validator(self.state.credential_validator);
server.set_auto_reconnect_cookie(self.state.auto_reconnect_cookie);
Expand Down
43 changes: 43 additions & 0 deletions crates/ironrdp-server/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,16 @@ pub struct RdpServer {
/// alone does not fix.
autodetect_bandwidth: Arc<AtomicU32>,

/// Increments every time a Bandwidth Measure transaction completes
/// (whether or not it produced a usable figure — see
/// [`Self::autodetect_bandwidth`]'s doc comment on the None case).
/// [`Self::autodetect_bandwidth`] alone cannot tell an embedder "a new
/// window just closed" apart from "the value happens to repeat" — this
/// value repeats often (a quiet link reads the same low figure for
/// several consecutive windows), so diffing it is not a valid freshness
/// signal. Exposed via [`Self::autodetect_bandwidth_generation_handle`].
autodetect_bandwidth_generation: Arc<AtomicU32>,

/// Optional Server Auto-Reconnect Cookie (MS-RDPBCGR 2.2.4.2
/// `ARC_SC_PRIVATE_PACKET`). When `Some`, the server validates a returning
/// `ARC_CS_PRIVATE_PACKET`, replaces its random after every connection, and
Expand Down Expand Up @@ -1363,6 +1373,7 @@ impl RdpServer {
autodetect_rtt: Option<Arc<AtomicU32>>,
autodetect_baseline_rtt: Option<Arc<AtomicU32>>,
autodetect_bandwidth: Option<Arc<AtomicU32>>,
autodetect_bandwidth_generation: Option<Arc<AtomicU32>>,
) -> Self {
let (ev_sender, ev_receiver) = ServerEvent::create_channel();
if let Some(cliprdr) = cliprdr_factory.as_mut() {
Expand Down Expand Up @@ -1425,6 +1436,8 @@ impl RdpServer {
handle.store(u32::MAX, Ordering::Relaxed);
handle
},
autodetect_bandwidth_generation: autodetect_bandwidth_generation
.unwrap_or_else(|| Arc::new(AtomicU32::new(0))),
auto_reconnect_cookie: None,
previous_auto_reconnect_cookie: None,
auto_reconnect_sent: false,
Expand Down Expand Up @@ -1767,6 +1780,18 @@ impl RdpServer {
Arc::clone(&self.autodetect_bandwidth)
}

/// Returns a handle that increments every time a Bandwidth Measure
/// transaction completes, whether or not it produced a usable figure.
/// Pairs with [`Self::autodetect_bandwidth_handle`]: read this first to
/// detect a fresh measurement window (the bandwidth figure itself
/// repeats too often to be its own freshness signal), then read the
/// bandwidth handle for the value. Inject a shared instance at
/// construction with
/// [`RdpServerBuilder::with_autodetect_bandwidth_generation_handle`](crate::RdpServerBuilder::with_autodetect_bandwidth_generation_handle).
pub fn autodetect_bandwidth_generation_handle(&self) -> Arc<AtomicU32> {
Arc::clone(&self.autodetect_bandwidth_generation)
}

/// Returns the shared ECHO server handle for runtime probe requests and RTT measurements.
pub fn echo_handle(&self) -> &EchoServerHandle {
&self.echo_handle
Expand Down Expand Up @@ -3793,8 +3818,25 @@ impl RdpServer {
}
AutoDetectOutcome::Bandwidth(Some(bandwidth_kbps)) => {
self.autodetect_bandwidth.store(bandwidth_kbps, Ordering::Relaxed);
self.autodetect_bandwidth_generation.fetch_add(1, Ordering::Relaxed);
// Logging the raw inputs, not just the computed figure: a
// damage-driven video source makes any single measurement
// window's byte count wildly bimodal (near-idle vs. a real
// frame landing in it), so bandwidth_kbps alone reads as
// noise without time_delta_ms/byte_count alongside it to
// show why.
let rdp::autodetect::AutoDetectResponse::BandwidthMeasureResults {
time_delta_ms,
byte_count,
..
} = &pdu.response
else {
unreachable!("computed_bandwidth_kbps() only returns Some for this variant")
};
debug!(
bandwidth_kbps,
time_delta_ms,
byte_count,
seq = pdu.response.sequence_number(),
"Bandwidth measured"
);
Expand All @@ -3804,6 +3846,7 @@ impl RdpServer {
// reporting a stale one (see `handle_response`'s doc comment);
// mirror that here so the exposed handle does not disagree.
self.autodetect_bandwidth.store(u32::MAX, Ordering::Relaxed);
self.autodetect_bandwidth_generation.fetch_add(1, Ordering::Relaxed);
trace!(
seq = pdu.response.sequence_number(),
"Bandwidth measurement completed without a usable figure"
Expand Down
Loading