From 00a597b08b99f306f6daba8c5529c97665a94972 Mon Sep 17 00:00:00 2001 From: Joseph Doherty Date: Sat, 21 Nov 2026 08:56:01 -0500 Subject: [PATCH] Stream live MXAccess advice events from the Rust worker --- .../WorkerLiveMxAccessSmokeTests.cs | 164 +++++ src/mxgateway-worker-rust/Cargo.toml | 4 + src/mxgateway-worker-rust/README.md | 43 +- src/mxgateway-worker-rust/src/automation.rs | 563 ++++++++++++++++++ src/mxgateway-worker-rust/src/main.rs | 4 + src/mxgateway-worker-rust/src/mxaccess.rs | 81 ++- src/mxgateway-worker-rust/src/mxevent.rs | 545 +++++++++++++++++ src/mxgateway-worker-rust/src/session.rs | 155 ++++- .../tests/live_mxaccess.rs | 47 +- 9 files changed, 1560 insertions(+), 46 deletions(-) create mode 100644 src/mxgateway-worker-rust/src/automation.rs create mode 100644 src/mxgateway-worker-rust/src/mxevent.rs diff --git a/src/ZB.MOM.WW.MxGateway.IntegrationTests/WorkerLiveMxAccessSmokeTests.cs b/src/ZB.MOM.WW.MxGateway.IntegrationTests/WorkerLiveMxAccessSmokeTests.cs index 392bf85f..c579f3b9 100644 --- a/src/ZB.MOM.WW.MxGateway.IntegrationTests/WorkerLiveMxAccessSmokeTests.cs +++ b/src/ZB.MOM.WW.MxGateway.IntegrationTests/WorkerLiveMxAccessSmokeTests.cs @@ -1,9 +1,15 @@ using System.Collections.Concurrent; using System.Diagnostics; using System.Diagnostics.CodeAnalysis; +using System.Net; using System.Text; using Google.Protobuf.WellKnownTypes; using Grpc.Core; +using Grpc.Net.Client; +using Microsoft.AspNetCore.Builder; +using Microsoft.AspNetCore.Hosting; +using Microsoft.AspNetCore.Server.Kestrel.Core; +using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using ZB.MOM.WW.MxGateway.Contracts; @@ -17,6 +23,7 @@ using ZB.MOM.WW.MxGateway.Server.Sessions; using ZB.MOM.WW.MxGateway.Server.Workers; using ZB.MOM.WW.MxGateway.IntegrationTests.TestSupport; using Xunit.Abstractions; +using SessionOptions = ZB.MOM.WW.MxGateway.Server.Configuration.SessionOptions; namespace ZB.MOM.WW.MxGateway.IntegrationTests; @@ -115,6 +122,163 @@ public sealed class WorkerLiveMxAccessSmokeTests(ITestOutputHelper output) } } + /// Verifies Rust worker callbacks over the gateway's live HTTP/2 gRPC endpoint. + /// A task that represents the live test. + [RustWorkerLiveFact] + public async Task GatewayGrpc_WithRustWorker_StreamsChangingIntegersAndUnadvises() + { + const string itemDefinition = "TestMachine_001.TestChangingInt"; + string workerPath = IntegrationTestEnvironment.ResolveLiveMxAccessWorkerExecutablePath(); + TestWorkerProcessFactory processFactory = new(output); + await using GatewayServiceFixture fixture = new(workerPath, processFactory, output); + WebApplicationBuilder builder = WebApplication.CreateSlimBuilder(); + builder.WebHost.ConfigureKestrel(options => + options.Listen(IPAddress.Loopback, 0, listener => listener.Protocols = HttpProtocols.Http2)); + builder.Services.AddGrpc(); + builder.Services.AddSingleton(fixture.Service); + await using WebApplication app = builder.Build(); + app.MapGrpcService(); + using CancellationTokenSource deadline = new(TimeSpan.FromSeconds(90)); + CancellationToken token = deadline.Token; + await app.StartAsync(token).ConfigureAwait(false); + using GrpcChannel channel = GrpcChannel.ForAddress(app.Urls.Single()); + MxAccessGateway.MxAccessGatewayClient client = new(channel); + OpenSessionReply opened = await client.OpenSessionAsync( + new OpenSessionRequest { ClientSessionName = "rust-worker-live-events" }, + cancellationToken: token).ResponseAsync.ConfigureAwait(false); + Assert.Equal(ProtocolStatusCode.Ok, opened.ProtocolStatus.Code); + string sessionId = opened.SessionId; + + try + { + async Task InvokeAsync(MxCommandRequest request) + { + MxCommandReply reply = await client.InvokeAsync(request, cancellationToken: token) + .ResponseAsync.ConfigureAwait(false); + Assert.True(reply.ProtocolStatus.Code == ProtocolStatusCode.Ok, reply.ToString()); + return reply; + } + + MxCommandReply worker = await InvokeAsync(new MxCommandRequest + { + SessionId = sessionId, + ClientCorrelationId = "rust-worker-info", + Command = new MxCommand { Kind = MxCommandKind.GetWorkerInfo, GetWorkerInfo = new GetWorkerInfoCommand() }, + }).ConfigureAwait(false); + Assert.EndsWith("-rust", worker.WorkerInfo.WorkerVersion, StringComparison.Ordinal); + Assert.Equal(opened.WorkerProcessId, worker.WorkerInfo.WorkerProcessId); + + MxCommandReply registration = await InvokeAsync(CreateRegisterRequest(sessionId)).ConfigureAwait(false); + int serverHandle = registration.Register.ServerHandle; + MxCommandRequest addItem = CreateAddItemRequest(sessionId, serverHandle); + addItem.Command.AddItem.ItemDefinition = itemDefinition; + MxCommandReply added = await InvokeAsync(addItem).ConfigureAwait(false); + int itemHandle = added.AddItem.ItemHandle; + + using (AsyncServerStreamingCall stream = client.StreamEvents( + new StreamEventsRequest { SessionId = sessionId, EmitStreamAttached = true }, + cancellationToken: token)) + { + Assert.True(await stream.ResponseStream.MoveNext(token).ConfigureAwait(false)); + Assert.NotNull(stream.ResponseStream.Current.StreamAttached); + await InvokeAsync(CreateAdviseRequest(sessionId, serverHandle, itemHandle)).ConfigureAwait(false); + + MxEvent? first = null; + bool observedChange = false; + ulong lastSequence = 0; + while (await stream.ResponseStream.MoveNext(token).ConfigureAwait(false)) + { + MxEvent change = stream.ResponseStream.Current; + Assert.True(change.WorkerSequence > lastSequence); + lastSequence = change.WorkerSequence; + if (change.Family != MxEventFamily.OnDataChange || + change.Value?.KindCase != MxValue.KindOneofCase.Int32Value || + (change.Quality & 0xc0) != 0xc0) + { + continue; + } + + Assert.Equal(sessionId, change.SessionId); + Assert.Equal(serverHandle, change.ServerHandle); + Assert.Equal(itemHandle, change.ItemHandle); + Assert.NotNull(change.SourceTimestamp); + Assert.NotNull(change.WorkerTimestamp); + Assert.Contains(change.Statuses, status => status.Category == MxStatusCategory.Ok); + if (first is not null && change.Value.Int32Value != first.Value.Int32Value) + { + output.WriteLine( + $"{itemDefinition} delivered through gateway: {first.Value.Int32Value} -> {change.Value.Int32Value}, " + + $"worker sequences {first.WorkerSequence} -> {change.WorkerSequence}, quality {change.Quality}."); + observedChange = true; + break; + } + + first ??= change; + } + + Assert.True(observedChange, "The gateway did not deliver distinct TestChangingInt values."); + + await InvokeAsync(new MxCommandRequest + { + SessionId = sessionId, + ClientCorrelationId = "rust-unadvise", + Command = new MxCommand + { + Kind = MxCommandKind.UnAdvise, + UnAdvise = new UnAdviseCommand { ServerHandle = serverHandle, ItemHandle = itemHandle }, + }, + }).ConfigureAwait(false); + + + // A callback queued before UnAdvise may still cross the pipe. + // Observe the same stream and reject updates generated after acknowledgement. + DateTime unadvisedAt = DateTime.UtcNow; + using CancellationTokenSource quiet = CancellationTokenSource.CreateLinkedTokenSource(token); + quiet.CancelAfter(TimeSpan.FromSeconds(2)); + try + { + while (await stream.ResponseStream.MoveNext(quiet.Token).ConfigureAwait(false)) + { + MxEvent change = stream.ResponseStream.Current; + Assert.False(change.Family == MxEventFamily.OnDataChange && + change.ServerHandle == serverHandle && change.ItemHandle == itemHandle && + change.WorkerTimestamp.ToDateTime() >= unadvisedAt, + "The worker sent a new data change after UnAdvise completed."); + } + + Assert.Fail("The event stream ended before the quiet period elapsed."); + } + catch (RpcException exception) when (exception.StatusCode == StatusCode.Cancelled && quiet.IsCancellationRequested) + { + Assert.False(token.IsCancellationRequested); + } + catch (OperationCanceledException) when (quiet.IsCancellationRequested) + { + Assert.False(token.IsCancellationRequested); + } + } + } + finally + { + CloseSessionReply closed = await client.CloseSessionAsync( + new CloseSessionRequest { SessionId = sessionId }, + deadline: DateTime.UtcNow.AddSeconds(25)).ResponseAsync.ConfigureAwait(false); + Assert.Equal(ProtocolStatusCode.Ok, closed.ProtocolStatus.Code); + } + } + + private sealed class RustWorkerLiveFactAttribute : FactAttribute + { + public RustWorkerLiveFactAttribute() + { + const string optIn = "MXGATEWAY_RUN_RUST_WORKER_LIVE_TESTS"; + if (!IntegrationTestEnvironment.IsEnabled(optIn)) + { + Skip = $"Set {optIn}=1 to run the Rust worker live test."; + } + } + } + /// /// Verifies that a Write command round-trips through live MXAccess against an advised item /// and that the worker emits a matching event diff --git a/src/mxgateway-worker-rust/Cargo.toml b/src/mxgateway-worker-rust/Cargo.toml index 00117b78..94376a95 100644 --- a/src/mxgateway-worker-rust/Cargo.toml +++ b/src/mxgateway-worker-rust/Cargo.toml @@ -13,9 +13,13 @@ tokio = { version = "1.48", features = ["io-util"] } tokio = { version = "1.48", features = ["macros", "net", "rt", "sync", "time"] } windows-sys = { version = "0.61", features = [ "Win32_Foundation", + "Win32_Globalization", "Win32_Security", "Win32_System_Com", + "Win32_System_Ole", "Win32_System_Threading", + "Win32_System_Time", + "Win32_System_Variant", "Win32_UI_WindowsAndMessaging", ] } diff --git a/src/mxgateway-worker-rust/README.md b/src/mxgateway-worker-rust/README.md index 53fd5668..64fa2ee8 100644 --- a/src/mxgateway-worker-rust/README.md +++ b/src/mxgateway-worker-rust/README.md @@ -6,15 +6,21 @@ schemas at build time. The worker authenticates the launch nonce, negotiates a frame limit, activates LMXProxy on a dedicated message-pumping STA, and reports readiness and heartbeats. -It supports Register, Unregister, AddItem, RemoveItem, and the worker diagnostic -commands. Shutdown unregisters any remaining clients before releasing COM. The -existing C# worker is the gateway default. +It supports Register, Unregister, AddItem, RemoveItem, Advise, UnAdvise, and the +worker diagnostic commands. The worker uses the base MXAccess COM connection +point for OnDataChange, OperationComplete, and OnWriteComplete notifications. +OnDataChange carries the value, quality, MXAccess status records, source +timestamp when available, and worker sequence and timestamp. The bounded queue +faults the session on overflow. Shutdown detaches the sink, unadvises remaining +items, and unregisters clients before releasing COM. The C# worker is the +gateway default. -COM connection points, event delivery, Advise, value and status conversion beyond -integer handles, writes, bulk commands, alarms, and record-info pinning remain to -be implemented. Unsupported commands return `InvalidRequest`. The prototype -serializes commands and does not interrupt COM calls on cancellation. If a call -exceeds the STA timeout, the worker faults the session and exits. +Event conversion supports standard Automation scalar values and homogeneous +arrays of those types. Other variant types are reported as unknown with the +Automation type in the diagnostic. Writes, buffered or supervisory advice, +bulk commands, and alarms remain to be implemented. Unsupported commands +return `InvalidRequest`. The worker serializes commands. If a COM call exceeds +the STA timeout, the worker faults the session and exits. ## Build and test @@ -41,15 +47,34 @@ handshake, heartbeat, registration, item lifecycle when configured, and graceful shutdown. It runs without a gateway service. Portable framing tests also run with `cargo test --locked` on macOS and Linux. +From the repository root on a Windows development host with +`TestMachine_001.TestChangingInt` deployed, run the opt-in HTTP/2 test to verify +the Rust worker through the gateway: + +```powershell +$env:MXGATEWAY_RUN_RUST_WORKER_LIVE_TESTS = '1' +$env:MXGATEWAY_LIVE_MXACCESS_WORKER_EXE = (Resolve-Path 'src/mxgateway-worker-rust/target/i686-pc-windows-msvc/release/mxgateway-worker-rust.exe').Path +dotnet test src/ZB.MOM.WW.MxGateway.IntegrationTests --filter FullyQualifiedName~GatewayGrpc_WithRustWorker_StreamsChangingIntegersAndUnadvises +``` + +The test starts the gateway gRPC service on an ephemeral loopback port and +launches the configured Rust worker. It verifies that the client receives +distinct good-quality integer values in sequence and that the worker emits no +new data for the item after UnAdvise. + ## COM interface evidence The native adapter uses the dual `ILMXProxyServer` interface from the installed `ArchestrA.MxAccess` 3.2.0.0 assembly. REA inspection confirmed the declaration order and managed signatures for Register, Unregister, AddItem, and RemoveItem. Windows reflection confirmed the interface IID, dual-interface flag, DISPIDs, and -BSTR input annotations. The live test exercises the corresponding native calls. +BSTR input annotations. REA and Windows reflection also identified the base +event dispatch interface and the `MXSTATUS_PROXY` layout. The live tests exercise +the corresponding native calls and event callbacks. - Assembly SHA-256 `cba8b607305ee0c70ca38b09eca645ae3b98c35a7b78ec051eb4381f48d9e0b1` - REA managed member evidence `ev_78053da509335e3e3749b8e6bb020f28c45255b54d910546659c6760eb52160b` +- REA event member evidence + `ev_97162abcd831b2d84b5b43fb72cd9876252bbe3bb0af5c4673a6954911500e54` diff --git a/src/mxgateway-worker-rust/src/automation.rs b/src/mxgateway-worker-rust/src/automation.rs new file mode 100644 index 00000000..1dd6e4ed --- /dev/null +++ b/src/mxgateway-worker-rust/src/automation.rs @@ -0,0 +1,563 @@ +use std::ffi::c_void; +use std::mem::{align_of, size_of}; +use std::ptr::{null, null_mut}; +use std::slice; + +use prost_types::Timestamp; +use windows_sys::core::BSTR; +use windows_sys::Win32::Foundation::{SysStringLen, FILETIME, SYSTEMTIME}; +use windows_sys::Win32::Globalization::GetUserDefaultLCID; +use windows_sys::Win32::System::Com::SAFEARRAY; +use windows_sys::Win32::System::Ole::{ + SafeArrayAccessData, SafeArrayGetDim, SafeArrayGetElemsize, SafeArrayGetLBound, + SafeArrayGetUBound, SafeArrayGetVartype, SafeArrayPtrOfIndex, SafeArrayUnaccessData, + VarDateFromStr, +}; +use windows_sys::Win32::System::Time::{SystemTimeToFileTime, TzSpecificLocalTimeToSystemTime}; +use windows_sys::Win32::System::Variant::*; + +use crate::wire::mxaccess_gateway::v1::{ + mx_array::Values, mx_value::Kind, BoolArray, DoubleArray, FloatArray, Int32Array, Int64Array, + MxArray, MxDataType, MxStatusCategory, MxStatusProxy, MxStatusSource, MxValue, StringArray, + TimestampArray, +}; + +pub fn int32(value: i32) -> MxValue { + value_of(MxDataType::Integer, "VT_I4", Kind::Int32Value(value)) +} + +// A dispatch argument can wrap the actual VARIANT in VT_VARIANT|VT_BYREF. +pub unsafe fn unwrap_variant(mut value: VARIANT) -> Option { + for _ in 0..8 { + let inner = unsafe { &value.Anonymous.Anonymous }; + if inner.vt != (VT_VARIANT | VT_BYREF) { + return Some(value); + } + let by_ref = unsafe { inner.Anonymous.pvarVal }; + if by_ref.is_null() { + return None; + } + value = unsafe { by_ref.read_unaligned() }; + } + None +} + +pub unsafe fn dispatch_i32(value: &VARIANT) -> Option { + let inner = unsafe { &unwrap_variant(*value)?.Anonymous.Anonymous }; + match inner.vt { + VT_I4 | VT_INT => Some(unsafe { inner.Anonymous.lVal }), + VT_I2 => Some(unsafe { inner.Anonymous.iVal.into() }), + _ => None, + } +} + +pub unsafe fn dispatch_value(value: &VARIANT) -> MxValue { + let Some(value) = (unsafe { unwrap_variant(*value) }) else { + return unknown("VT_VARIANT|VT_BYREF".into()); + }; + let inner = unsafe { &value.Anonymous.Anonymous }; + if inner.vt & VT_ARRAY != 0 { + let array = unsafe { variant_array(&value) }; + return unsafe { dispatch_array(array, inner.vt & !(VT_ARRAY | VT_BYREF)) }.unwrap_or_else( + || { + unknown(format!( + "SAFEARRAY(VT_0x{:04X})", + inner.vt & !(VT_ARRAY | VT_BYREF) + )) + }, + ); + } + let data = &inner.Anonymous; + // SAFETY: each field read is selected by the type tag of a live VARIANT. + unsafe { + match inner.vt { + VT_EMPTY | VT_NULL => MxValue { + data_type: MxDataType::NoData as i32, + variant_type: if inner.vt == VT_EMPTY { + "VT_EMPTY" + } else { + "VT_NULL" + } + .into(), + is_null: true, + ..Default::default() + }, + VT_I1 => value_of( + MxDataType::Integer, + "VT_I1", + Kind::Int32Value(data.cVal.into()), + ), + VT_UI1 => value_of( + MxDataType::Integer, + "VT_UI1", + Kind::Int32Value(data.bVal.into()), + ), + VT_I2 => value_of( + MxDataType::Integer, + "VT_I2", + Kind::Int32Value(data.iVal.into()), + ), + VT_UI2 => value_of( + MxDataType::Integer, + "VT_UI2", + Kind::Int32Value(data.uiVal.into()), + ), + VT_I4 | VT_INT => int32(data.lVal), + VT_UI4 | VT_UINT => value_of( + MxDataType::Integer, + "VT_UI4", + Kind::Int64Value(data.ulVal.into()), + ), + VT_I8 => value_of(MxDataType::Integer, "VT_I8", Kind::Int64Value(data.llVal)), + VT_UI8 => i64::try_from(data.ullVal).map_or_else( + |_| unknown("VT_UI8".into()), + |value| value_of(MxDataType::Integer, "VT_UI8", Kind::Int64Value(value)), + ), + VT_BOOL => value_of( + MxDataType::Boolean, + "VT_BOOL", + Kind::BoolValue(data.boolVal != 0), + ), + VT_R4 => value_of(MxDataType::Float, "VT_R4", Kind::FloatValue(data.fltVal)), + VT_R8 => value_of(MxDataType::Double, "VT_R8", Kind::DoubleValue(data.dblVal)), + VT_BSTR => value_of( + MxDataType::String, + "VT_BSTR", + Kind::StringValue(bstr(data.bstrVal)), + ), + VT_DATE => utc_ole_date(data.date).map_or_else( + || unknown("VT_DATE".into()), + |value| value_of(MxDataType::Time, "VT_DATE", Kind::TimestampValue(value)), + ), + vt => unknown(format!("VT_0x{vt:04X}")), + } + } +} + +unsafe fn variant_array(value: &VARIANT) -> *mut SAFEARRAY { + let inner = unsafe { &value.Anonymous.Anonymous }; + if inner.vt & VT_BYREF == 0 { + return unsafe { inner.Anonymous.parray }; + } + let by_ref = unsafe { inner.Anonymous.pparray }; + if by_ref.is_null() { + null_mut() + } else { + unsafe { by_ref.read_unaligned() } + } +} + +unsafe fn dispatch_array(array: *mut SAFEARRAY, tag: u16) -> Option { + if array.is_null() { + return None; + } + let mut element_type = 0; + if unsafe { SafeArrayGetVartype(array, &mut element_type) } < 0 { + element_type = tag; + } + let array = unsafe { ArrayElements::new(array) }?; + macro_rules! read_array { + ($native:ty, $type:ident, $variant:literal, $values:ident, $message:ident, $convert:expr) => { + ( + MxDataType::$type, + $variant, + Values::$values($message { + values: unsafe { array.copy::<$native, _>($convert) }?, + }), + ) + }; + } + let (data_type, variant, values) = match element_type { + VT_I1 => read_array!(i8, Integer, "VT_I1", Int32Values, Int32Array, |v| Some( + i32::from(v) + )), + VT_UI1 => read_array!(u8, Integer, "VT_UI1", Int32Values, Int32Array, |v| Some( + i32::from(v) + )), + VT_I2 => read_array!(i16, Integer, "VT_I2", Int32Values, Int32Array, |v| Some( + i32::from(v) + )), + VT_UI2 => read_array!(u16, Integer, "VT_UI2", Int32Values, Int32Array, |v| Some( + i32::from(v) + )), + VT_I4 | VT_INT => read_array!(i32, Integer, "VT_I4", Int32Values, Int32Array, Some), + VT_UI4 | VT_UINT => read_array!(u32, Integer, "VT_UI4", Int64Values, Int64Array, |v| Some( + i64::from(v) + )), + VT_I8 => read_array!(i64, Integer, "VT_I8", Int64Values, Int64Array, Some), + VT_UI8 => read_array!(u64, Integer, "VT_UI8", Int64Values, Int64Array, |v| { + i64::try_from(v).ok() + }), + VT_BOOL => read_array!(i16, Boolean, "VT_BOOL", BoolValues, BoolArray, |v| Some( + v != 0 + )), + VT_R4 => read_array!(f32, Float, "VT_R4", FloatValues, FloatArray, Some), + VT_R8 => read_array!(f64, Double, "VT_R8", DoubleValues, DoubleArray, Some), + VT_BSTR => read_array!( + BSTR, + String, + "VT_BSTR", + StringValues, + StringArray, + |v| Some(bstr(v)) + ), + VT_DATE => read_array!(f64, Time, "VT_DATE", TimestampValues, TimestampArray, |v| { + utc_ole_date(v) + }), + _ => return None, + }; + let variant_type = format!("SAFEARRAY({variant})"); + let data = MxArray { + element_data_type: data_type as i32, + variant_type: variant_type.clone(), + dimensions: array.dimensions(), + values: Some(values), + ..Default::default() + }; + Some(MxValue { + variant_type, + kind: Some(Kind::ArrayValue(data)), + ..Default::default() + }) +} + +struct ArrayElements { + array: *mut SAFEARRAY, + bounds: Vec<(i32, i32)>, +} + +impl ArrayElements { + unsafe fn new(array: *mut SAFEARRAY) -> Option { + let dimensions = unsafe { SafeArrayGetDim(array) }; + if dimensions == 0 { + return None; + } + let mut bounds = Vec::with_capacity(dimensions as usize); + // SafeArrayPtrOfIndex expects the rightmost dimension first. + for dimension in 1..=dimensions { + let mut lower = 0; + let mut upper = 0; + if unsafe { SafeArrayGetLBound(array, dimension, &mut lower) } < 0 + || unsafe { SafeArrayGetUBound(array, dimension, &mut upper) } < 0 + { + return None; + } + bounds.push((lower, upper)); + } + let mut data = null_mut(); + if unsafe { SafeArrayAccessData(array, &mut data) } < 0 { + return None; + } + Some(Self { array, bounds }) + } + + fn dimensions(&self) -> Vec { + self.bounds + .iter() + .rev() + .map(|&(lower, upper)| (i64::from(upper) - i64::from(lower) + 1).max(0) as u32) + .collect() + } + + // The caller selects a Rust representation matching the Automation VARTYPE. + unsafe fn copy(&self, convert: impl Fn(T) -> Option) -> Option> { + if unsafe { SafeArrayGetElemsize(self.array) } as usize != size_of::() { + return None; + } + let mut values = Vec::new(); + if self.bounds.iter().any(|&(lower, upper)| upper < lower) { + return Some(values); + } + let mut indices: Vec<_> = self.bounds.iter().map(|&(lower, _)| lower).collect(); + loop { + let mut element: *mut c_void = null_mut(); + if unsafe { SafeArrayPtrOfIndex(self.array, indices.as_ptr(), &mut element) } < 0 + || element.is_null() + { + return None; + } + values.push(convert(unsafe { element.cast::().read_unaligned() })?); + let Some(next) = indices + .iter() + .zip(&self.bounds) + .position(|(&index, &(_, upper))| index < upper) + else { + break; + }; + indices[next] += 1; + for (index, &(lower, _)) in indices[..next].iter_mut().zip(&self.bounds) { + *index = lower; + } + } + Some(values) + } +} + +impl Drop for ArrayElements { + fn drop(&mut self) { + // SAFETY: new acquired an Automation array data lock. + unsafe { SafeArrayUnaccessData(self.array) }; + } +} + +fn value_of(data_type: MxDataType, variant_type: &str, kind: Kind) -> MxValue { + MxValue { + data_type: data_type as i32, + variant_type: variant_type.into(), + kind: Some(kind), + ..Default::default() + } +} + +fn unknown(variant_type: String) -> MxValue { + MxValue { + data_type: MxDataType::Unknown as i32, + raw_diagnostic: format!("Unsupported Automation value type {variant_type}"), + variant_type, + ..Default::default() + } +} + +unsafe fn bstr(value: BSTR) -> String { + if value.is_null() { + return String::new(); + } + let length = unsafe { SysStringLen(value) } as usize; + String::from_utf16_lossy(unsafe { slice::from_raw_parts(value, length) }) +} + +pub unsafe fn dispatch_timestamp(value: &VARIANT) -> Option { + let inner = unsafe { &unwrap_variant(*value)?.Anonymous.Anonymous }; + match inner.vt { + VT_DATE => utc_ole_date(unsafe { inner.Anonymous.date }), + VT_BSTR => parse_timestamp(&unsafe { bstr(inner.Anonymous.bstrVal) }), + VT_I8 => file_time_timestamp(unsafe { inner.Anonymous.llVal }), + VT_UI8 => i64::try_from(unsafe { inner.Anonymous.ullVal }) + .ok() + .and_then(file_time_timestamp), + _ => None, + } +} + +fn parse_timestamp(text: &str) -> Option { + // MXAccess appends fractional seconds to the local Automation date text. + // Parse the whole seconds with the user's locale and restore the fraction. + let mut text = text.trim().to_owned(); + let mut milliseconds = 0; + if let Some((colon, _)) = text.match_indices(':').nth(1) { + let seconds = text[colon + 1..] + .bytes() + .take_while(u8::is_ascii_digit) + .count(); + let separator = colon + 1 + seconds; + if seconds > 0 && matches!(text.as_bytes().get(separator), Some(b'.' | b',')) { + let digits = text[separator + 1..] + .bytes() + .take_while(u8::is_ascii_digit) + .count(); + if digits > 0 { + let count = digits.min(3); + milliseconds = text[separator + 1..separator + 1 + count] + .parse::() + .ok()? + * 10_u16.pow(3 - count as u32); + text.drain(separator..separator + 1 + digits); + } + } + } + let text: Vec<_> = text.encode_utf16().chain([0]).collect(); + let mut date = 0.0; + let locale = unsafe { GetUserDefaultLCID() }; + if unsafe { VarDateFromStr(text.as_ptr(), locale, 0, &mut date) } < 0 + && unsafe { VarDateFromStr(text.as_ptr(), 0x0409, 0, &mut date) } < 0 + { + return None; + } + local_ole_date(date, milliseconds) +} + +fn utc_ole_date(date: f64) -> Option { + if !(-657_435.0 < date && date < 2_958_466.0) { + return None; + } + // Preserve the existing worker's UTC interpretation of an unspecified + // COM DATE. For negative OLE values the fraction is still time after midnight. + let millis = (date.trunc() as i64 - 25_569) * 86_400_000 + + (date.fract().abs() * 86_400_000.0).round() as i64; + Some(Timestamp { + seconds: millis.div_euclid(1_000), + nanos: (millis.rem_euclid(1_000) * 1_000_000) as i32, + }) +} + +fn local_ole_date(date: f64, milliseconds: u16) -> Option { + let mut local = SYSTEMTIME::default(); + let mut utc = SYSTEMTIME::default(); + let mut file_time = FILETIME::default(); + // SAFETY: all output pointers refer to initialized writable Win32 structures. + if unsafe { VariantTimeToSystemTime(date, &mut local) } == 0 { + return None; + } + local.wMilliseconds = milliseconds; + if unsafe { TzSpecificLocalTimeToSystemTime(null(), &local, &mut utc) } == 0 + || unsafe { SystemTimeToFileTime(&utc, &mut file_time) } == 0 + { + return None; + } + let ticks = (i64::from(file_time.dwHighDateTime) << 32) | i64::from(file_time.dwLowDateTime); + file_time_timestamp(ticks) +} + +fn file_time_timestamp(ticks: i64) -> Option { + if !(0..=2_650_467_743_999_999_999).contains(&ticks) { + return None; + } + let unix_ticks = ticks - 116_444_736_000_000_000; + Some(Timestamp { + seconds: unix_ticks.div_euclid(10_000_000), + nanos: (unix_ticks.rem_euclid(10_000_000) * 100) as i32, + }) +} + +// Installed MXSTATUS_PROXY 3.2: 16 bytes with members at offsets 0, 4, 8, 12. +#[repr(C)] +struct StatusRecord { + success: i16, + category: i32, + source: i32, + detail: i16, +} + +pub unsafe fn dispatch_statuses(value: &VARIANT) -> Result, &'static str> { + let value = unsafe { unwrap_variant(*value) }.ok_or("null status VARIANT")?; + let inner = unsafe { &value.Anonymous.Anonymous }; + let array = match inner.vt { + VT_EMPTY | VT_NULL => return Ok(Vec::new()), + tag if tag & VT_ARRAY != 0 => unsafe { variant_array(&value) }, + _ => return Err("status VARIANT is not a SAFEARRAY"), + }; + if array.is_null() { + return Ok(Vec::new()); + } + unsafe { read_status_records(array) } +} + +unsafe fn read_status_records(array: *const SAFEARRAY) -> Result, &'static str> { + let mut element_type = 0; + if unsafe { SafeArrayGetDim(array) } != 1 + || unsafe { SafeArrayGetVartype(array, &mut element_type) } < 0 + || element_type != VT_RECORD + || unsafe { SafeArrayGetElemsize(array) } as usize != size_of::() + { + return Err("unexpected MXSTATUS_PROXY array layout"); + } + let count = unsafe { (*array).rgsabound[0].cElements } as usize; + if count == 0 { + return Ok(Vec::new()); + } + let mut data: *mut c_void = null_mut(); + if unsafe { SafeArrayAccessData(array, &mut data) } < 0 { + return Err("could not access MXSTATUS_PROXY array"); + } + let result = if data.is_null() + || !(data as usize).is_multiple_of(align_of::()) + || count > isize::MAX as usize / size_of::() + { + Err("invalid MXSTATUS_PROXY array data") + } else { + // SAFETY: the checked one-dimensional record array remains locked while copied. + let records = unsafe { slice::from_raw_parts(data.cast::(), count) }; + Ok(records + .iter() + .map(|record| MxStatusProxy { + success: record.success.into(), + category: match record.category { + 0..=8 => record.category + 2, + _ => MxStatusCategory::Unknown as i32, + }, + detected_by: match record.source { + 0..=5 => record.source + 2, + _ => MxStatusSource::Unknown as i32, + }, + detail: record.detail.into(), + raw_category: record.category, + raw_detected_by: record.source, + ..Default::default() + }) + .collect()) + }; + unsafe { SafeArrayUnaccessData(array) }; + result +} + +#[cfg(test)] +mod tests { + use super::*; + use windows_sys::Win32::System::Com::SAFEARRAYBOUND; + use windows_sys::Win32::System::Ole::{SafeArrayCreate, SafeArrayDestroy, SafeArrayPutElement}; + + #[test] + fn preserves_utc_automation_dates_and_file_time_precision() { + let epoch = Timestamp { + seconds: 0, + nanos: 0, + }; + assert_eq!(utc_ole_date(25_569.0), Some(epoch)); + assert_eq!(utc_ole_date(25_569.5).unwrap().seconds, 43_200); + assert_eq!( + file_time_timestamp(116_444_736_000_000_123).unwrap().nanos, + 12_300 + ); + assert_eq!(file_time_timestamp(-1), None); + } + + #[test] + fn maps_multidimensional_automation_array_in_logical_order() { + struct TestArray(*mut SAFEARRAY); + impl Drop for TestArray { + fn drop(&mut self) { + unsafe { SafeArrayDestroy(self.0) }; + } + } + + let bounds = [ + SAFEARRAYBOUND { + cElements: 3, + lLbound: -1, + }, + SAFEARRAYBOUND { + cElements: 2, + lLbound: 10, + }, + ]; + // SAFETY: the bounds match the number of dimensions passed to Automation. + let array = TestArray(unsafe { SafeArrayCreate(VT_I4, 2, bounds.as_ptr()) }); + assert!(!array.0.is_null()); + for row in 10_i32..=11 { + for column in -1_i32..=1 { + let value = row * 10 + column; + let indices = [column, row]; + assert_eq!( + unsafe { + SafeArrayPutElement( + array.0, + indices.as_ptr(), + (&value as *const i32).cast(), + ) + }, + 0, + "invalid test SAFEARRAY indices {indices:?}", + ); + } + } + let result = unsafe { dispatch_array(array.0, VT_I4) }.unwrap(); + let Some(Kind::ArrayValue(result)) = result.kind else { + panic!("expected an array value"); + }; + assert_eq!(result.dimensions, [2, 3]); + let Some(Values::Int32Values(values)) = result.values else { + panic!("expected 32-bit integer values"); + }; + assert_eq!(values.values, [99, 100, 101, 109, 110, 111]); + } +} diff --git a/src/mxgateway-worker-rust/src/main.rs b/src/mxgateway-worker-rust/src/main.rs index 91387d26..a3fdda2b 100644 --- a/src/mxgateway-worker-rust/src/main.rs +++ b/src/mxgateway-worker-rust/src/main.rs @@ -1,9 +1,13 @@ #[cfg(all(windows, not(target_arch = "x86")))] compile_error!("The MXAccess COM server requires a 32-bit x86 Windows worker."); +#[cfg(all(windows, target_arch = "x86"))] +mod automation; #[cfg(all(windows, target_arch = "x86"))] mod mxaccess; #[cfg(all(windows, target_arch = "x86"))] +mod mxevent; +#[cfg(all(windows, target_arch = "x86"))] mod session; #[cfg(any(windows, test))] mod wire; diff --git a/src/mxgateway-worker-rust/src/mxaccess.rs b/src/mxgateway-worker-rust/src/mxaccess.rs index 74a70e5c..3feef7d5 100644 --- a/src/mxgateway-worker-rust/src/mxaccess.rs +++ b/src/mxgateway-worker-rust/src/mxaccess.rs @@ -1,3 +1,4 @@ +use crate::mxevent::{Connection, Publisher, RecordInfoPin}; use std::collections::BTreeSet; use std::ffi::c_void; use std::io; @@ -29,6 +30,8 @@ pub enum Command { Unregister(i32), AddItem(i32, String), RemoveItem(i32, i32), + Advise(i32, i32), + UnAdvise(i32, i32), } pub enum CommandError { @@ -55,7 +58,7 @@ pub struct Sta { } impl Sta { - pub async fn start() -> Result { + pub async fn start(publisher: Publisher) -> Result<(Self, String), StartError> { // SAFETY: unnamed auto-reset event. Ownership transfers to OwnedHandle. let event = unsafe { CreateEventW(null(), 0, 0, null()) }; if event.is_null() { @@ -68,7 +71,7 @@ impl Sta { let thread_wake = Arc::clone(&wake); thread::Builder::new() .name("mxgateway-sta".into()) - .spawn(move || run(requests, thread_wake, ready, finished)) + .spawn(move || run(requests, thread_wake, ready, finished, publisher)) .map_err(StartError::Io)?; let sta = Self { @@ -76,11 +79,11 @@ impl Sta { wake, stopped: Some(stopped), }; - startup + let pin_status = startup .await .map_err(|_| StartError::Io(sta_stopped()))? .map_err(StartError::Com)?; - Ok(sta) + Ok((sta, pin_status)) } pub async fn execute(&self, command: Command) -> io::Result { @@ -128,8 +131,9 @@ fn sta_stopped() -> io::Error { fn run( requests: mpsc::Receiver, wake: Arc, - ready: oneshot::Sender>, + ready: oneshot::Sender>, finished: oneshot::Sender, + publisher: Publisher, ) { let apartment = match Apartment::initialize() { Ok(apartment) => apartment, @@ -138,18 +142,20 @@ fn run( return; } }; - let mut proxy = match Proxy::activate() { + let pin = RecordInfoPin::load(); + let mut proxy = match Proxy::activate(publisher) { Ok(proxy) => proxy, Err(error) => { let _ = ready.send(Err(error)); return; } }; - if ready.send(Ok(())).is_ok() { + if ready.send(Ok(RecordInfoPin::status(&pin))).is_ok() { pump(&requests, &wake, &mut proxy); } let failures = proxy.cleanup(); drop(proxy); + drop(pin); drop(apartment); let _ = finished.send(failures); } @@ -206,8 +212,8 @@ impl Drop for Apartment { } } -// First eleven slots of the installed dual ILMXProxyServer interface. Slots -// 0–6 are IUnknown and IDispatch. The next four slots follow the installed +// First thirteen slots of the installed dual ILMXProxyServer interface. Slots +// 0–6 are IUnknown and IDispatch. The next six slots follow the installed // interop assembly, with HRESULT and [out, retval] for the handle results. #[repr(C)] struct ProxyVtable { @@ -220,15 +226,19 @@ struct ProxyVtable { unregister: unsafe extern "system" fn(*mut c_void, i32) -> HRESULT, add_item: unsafe extern "system" fn(*mut c_void, i32, BSTR, *mut i32) -> HRESULT, remove_item: unsafe extern "system" fn(*mut c_void, i32, i32) -> HRESULT, + advise: unsafe extern "system" fn(*mut c_void, i32, i32) -> HRESULT, + unadvise: unsafe extern "system" fn(*mut c_void, i32, i32) -> HRESULT, } struct Proxy { interface: NonNull, registrations: BTreeSet, + publisher: Publisher, + connection: Option, } impl Proxy { - fn activate() -> Result { + fn activate(publisher: Publisher) -> Result { let mut interface = null_mut(); // SAFETY: the output is for the exact IID declared by ProxyVtable. check(unsafe { @@ -240,10 +250,15 @@ impl Proxy { &mut interface, ) })?; - Ok(Self { + let mut proxy = Self { interface: NonNull::new(interface).ok_or(0x8000_4003_u32 as i32)?, registrations: BTreeSet::new(), - }) + publisher, + connection: None, + }; + // SAFETY: this STA owns the live proxy and its connection point. + proxy.connection = Some(unsafe { Connection::attach(interface, proxy.publisher.clone()) }?); + Ok(proxy) } fn execute(&mut self, command: Command) -> Outcome { @@ -265,6 +280,7 @@ impl Proxy { check(unsafe { (table.unregister)(object, server) }) .map_err(CommandError::HResult)?; self.registrations.remove(&server); + self.publisher.unregister(server); Ok(None) } Command::AddItem(server, definition) => { @@ -279,6 +295,25 @@ impl Proxy { self.require_registration(server)?; check(unsafe { (table.remove_item)(object, server, item) }) .map_err(CommandError::HResult)?; + self.publisher.unadvise(server, item); + Ok(None) + } + Command::Advise(server, item) => { + self.require_registration(server)?; + // The COM call may publish the initial value before it returns. + let added = self.publisher.advise(server, item); + let status = unsafe { (table.advise)(object, server, item) }; + if status < 0 && added { + self.publisher.unadvise(server, item); + } + check(status).map_err(CommandError::HResult)?; + Ok(None) + } + Command::UnAdvise(server, item) => { + self.require_registration(server)?; + check(unsafe { (table.unadvise)(object, server, item) }) + .map_err(CommandError::HResult)?; + self.publisher.unadvise(server, item); Ok(None) } } @@ -293,7 +328,28 @@ impl Proxy { } fn cleanup(&mut self) -> usize { + let advice = self.publisher.take_advice(); let mut failures = 0; + if let Some(mut connection) = self.connection.take() { + if let Err(hr) = connection.detach() { + failures += 1; + eprintln!( + "MXAccess event sink detach failed (HRESULT 0x{:08X})", + hr as u32 + ); + } + } + for (server, item) in advice { + // SAFETY: the interface is still live on this STA. + let hr = unsafe { (self.vtable().unadvise)(self.interface.as_ptr(), server, item) }; + if hr < 0 { + failures += 1; + eprintln!( + "MXAccess unadvise during shutdown failed (HRESULT 0x{:08X})", + hr as u32 + ); + } + } for server in std::mem::take(&mut self.registrations) { // SAFETY: the interface is still live on this STA. let hr = unsafe { (self.vtable().unregister)(self.interface.as_ptr(), server) }; @@ -316,6 +372,7 @@ impl Proxy { impl Drop for Proxy { fn drop(&mut self) { + drop(self.connection.take()); // SAFETY: release the reference returned by CoCreateInstance on this STA. unsafe { (self.vtable().release)(self.interface.as_ptr()) }; } diff --git a/src/mxgateway-worker-rust/src/mxevent.rs b/src/mxgateway-worker-rust/src/mxevent.rs new file mode 100644 index 00000000..4ec9449d --- /dev/null +++ b/src/mxgateway-worker-rust/src/mxevent.rs @@ -0,0 +1,545 @@ +use std::collections::BTreeSet; +use std::ffi::c_void; +use std::ptr::{null_mut, NonNull}; +use std::sync::{ + atomic::{AtomicU32, Ordering}, + Arc, Mutex, +}; +use std::time::SystemTime; + +use tokio::sync::{mpsc, watch}; +use windows_sys::core::{IID_IUnknown, IUnknown_Vtbl, GUID, HRESULT, PWSTR}; +use windows_sys::Win32::Foundation::{ + DISP_E_BADPARAMCOUNT, DISP_E_EXCEPTION, DISP_E_MEMBERNOTFOUND, DISP_E_TYPEMISMATCH, + DISP_E_UNKNOWNNAME, E_NOINTERFACE, E_NOTIMPL, E_POINTER, S_OK, +}; +use windows_sys::Win32::System::Com::{DISPPARAMS, EXCEPINFO}; +use windows_sys::Win32::System::Ole::{GetRecordInfoFromGuids, LoadRegTypeLib}; +use windows_sys::Win32::System::Variant::VARIANT; + +use crate::automation::{dispatch_i32, dispatch_statuses, dispatch_timestamp, dispatch_value}; +use crate::wire::mxaccess_gateway::v1::{ + mx_event, MxEvent, MxEventFamily, OnDataChangeEvent, OnWriteCompleteEvent, + OperationCompleteEvent, +}; + +const EVENTS_IID: GUID = GUID::from_u128(0x848299b6_dd61_4a0d_a304_3947a564b89c); +const DISPATCH_IID: GUID = GUID::from_u128(0x00020400_0000_0000_c000_000000000046); +const CONNECTION_POINT_CONTAINER_IID: GUID = + GUID::from_u128(0xb196b284_bab4_101a_b69c_00aa00341d07); +const TYPELIB_ID: GUID = GUID::from_u128(0x77e896ec_b3e3_4de4_b96f_f32570164211); +const RECORD_ID: GUID = GUID::from_u128(0xa162d9e4_9019_42f3_a087_79264007e542); + +#[derive(Clone, Copy)] +pub enum EventFault { + Overflow, + Conversion(&'static str), +} + +#[derive(Clone)] +pub struct Publisher { + session: Arc, + events: mpsc::Sender, + faults: watch::Sender>, + state: Arc>, +} + +#[derive(Default)] +struct PublisherState { + advised: BTreeSet<(i32, i32)>, + last_sequence: u64, +} + +pub fn event_channel( + session: &str, + capacity: usize, +) -> ( + Publisher, + mpsc::Receiver, + watch::Receiver>, +) { + let (events, receiver) = mpsc::channel(capacity); + let (faults, fault_receiver) = watch::channel(None); + ( + Publisher { + session: session.into(), + events, + faults, + state: Arc::default(), + }, + receiver, + fault_receiver, + ) +} + +impl Publisher { + pub fn advise(&self, server: i32, item: i32) -> bool { + self.state.lock().unwrap().advised.insert((server, item)) + } + + pub fn unadvise(&self, server: i32, item: i32) { + self.state.lock().unwrap().advised.remove(&(server, item)); + } + + pub fn unregister(&self, server: i32) { + self.state + .lock() + .unwrap() + .advised + .retain(|(handle, _)| *handle != server); + } + + pub fn take_advice(&self) -> BTreeSet<(i32, i32)> { + std::mem::take(&mut self.state.lock().unwrap().advised) + } + + pub fn last_sequence(&self) -> u64 { + self.state.lock().unwrap().last_sequence + } + + fn is_advised(&self, server: i32, item: i32) -> bool { + self.state.lock().unwrap().advised.contains(&(server, item)) + } + + fn report_fault(&self, fault: EventFault) { + self.faults.send_if_modified(|current| { + if current.is_some() { + return false; + } + *current = Some(fault); + true + }); + } + + fn publish(&self, mut event: MxEvent) { + let mut state = self.state.lock().unwrap(); + event.session_id = self.session.to_string(); + event.worker_timestamp = Some(SystemTime::now().into()); + event.worker_sequence = state.last_sequence + 1; + match self.events.try_send(event) { + Ok(()) => state.last_sequence += 1, + Err(mpsc::error::TrySendError::Full(_)) => self.report_fault(EventFault::Overflow), + Err(mpsc::error::TrySendError::Closed(_)) => {} + } + } +} + +#[repr(C)] +struct ConnectionPointContainerVtable { + base: IUnknown_Vtbl, + enum_connection_points: usize, + find_connection_point: + unsafe extern "system" fn(*mut c_void, *const GUID, *mut *mut c_void) -> HRESULT, +} + +#[repr(C)] +struct ConnectionPointVtable { + base: IUnknown_Vtbl, + get_connection_interface: usize, + get_connection_point_container: usize, + advise: unsafe extern "system" fn(*mut c_void, *mut c_void, *mut u32) -> HRESULT, + unadvise: unsafe extern "system" fn(*mut c_void, u32) -> HRESULT, +} + +pub struct Connection { + point: ComReference, + cookie: Option, +} + +impl Connection { + // The caller supplies the live LMXProxy interface on the owning STA. + pub unsafe fn attach(proxy: *mut c_void, publisher: Publisher) -> Result { + let mut container = null_mut(); + let unknown = unsafe { vtable::(proxy) }; + check(unsafe { + (unknown.QueryInterface)(proxy, &CONNECTION_POINT_CONTAINER_IID, &mut container) + })?; + let container = ComReference::new(container)?; + let mut point = null_mut(); + let table = unsafe { vtable::(container.as_ptr()) }; + check(unsafe { + (table.find_connection_point)(container.as_ptr(), &EVENTS_IID, &mut point) + })?; + let point = ComReference::new(point)?; + + let sink = Box::new(Sink { + table: &SINK_VTABLE, + reference_count: AtomicU32::new(1), + publisher, + }); + let sink = ComReference::new(Box::into_raw(sink).cast())?; + let mut cookie = 0; + let table = unsafe { vtable::(point.as_ptr()) }; + check(unsafe { (table.advise)(point.as_ptr(), sink.as_ptr(), &mut cookie) })?; + Ok(Self { + point, + cookie: Some(cookie), + }) + } + + pub fn detach(&mut self) -> Result<(), HRESULT> { + if let Some(cookie) = self.cookie.take() { + // SAFETY: the cookie was returned by this live connection point on this STA. + let pointer = self.point.as_ptr(); + let table = unsafe { vtable::(pointer) }; + check(unsafe { (table.unadvise)(pointer, cookie) })?; + } + Ok(()) + } +} + +impl Drop for Connection { + fn drop(&mut self) { + let _ = self.detach(); + } +} + +struct ComReference(NonNull); + +impl ComReference { + fn new(pointer: *mut c_void) -> Result { + NonNull::new(pointer).map(Self).ok_or(E_POINTER) + } + + fn as_ptr(&self) -> *mut c_void { + self.0.as_ptr() + } +} + +impl Drop for ComReference { + fn drop(&mut self) { + // SAFETY: this holds one owned COM reference and runs on its STA. + let pointer = self.as_ptr(); + unsafe { (vtable::(pointer).Release)(pointer) }; + } +} + +unsafe fn vtable(pointer: *mut c_void) -> &'static V { + unsafe { &**pointer.cast::<*const V>() } +} + +pub struct RecordInfoPin { + _library: ComReference, + _record: ComReference, +} + +impl RecordInfoPin { + pub fn load() -> Result { + let mut library = null_mut(); + let mut record = null_mut(); + // SAFETY: these are the verified registered typelib and record IDs of MXAccess 3.2. + check(unsafe { LoadRegTypeLib(&TYPELIB_ID, 3, 2, 0, &mut library) })?; + let library = ComReference::new(library)?; + check(unsafe { GetRecordInfoFromGuids(&TYPELIB_ID, 3, 2, 0, &RECORD_ID, &mut record) })?; + Ok(Self { + _library: library, + _record: ComReference::new(record)?, + }) + } + + pub fn status(result: &Result) -> String { + match result { + Ok(_) => "pinned {77e896ec-b3e3-4de4-b96f-f32570164211} v3.2 record {a162d9e4-9019-42f3-a087-79264007e542}".into(), + Err(hr) => format!("MXSTATUS_PROXY pin failed (HRESULT 0x{:08X})", *hr as u32), + } + } +} + +fn check(hr: HRESULT) -> Result<(), HRESULT> { + if hr < 0 { + Err(hr) + } else { + Ok(()) + } +} + +#[repr(C)] +struct Sink { + table: &'static DispatchVtable, + reference_count: AtomicU32, + publisher: Publisher, +} + +#[repr(C)] +struct DispatchVtable { + base: IUnknown_Vtbl, + type_info_count: unsafe extern "system" fn(*mut c_void, *mut u32) -> HRESULT, + type_info: unsafe extern "system" fn(*mut c_void, u32, u32, *mut *mut c_void) -> HRESULT, + names: unsafe extern "system" fn( + *mut c_void, + *const GUID, + *const PWSTR, + u32, + u32, + *mut i32, + ) -> HRESULT, + invoke: unsafe extern "system" fn( + *mut c_void, + i32, + *const GUID, + u32, + u16, + *mut DISPPARAMS, + *mut VARIANT, + *mut EXCEPINFO, + *mut u32, + ) -> HRESULT, +} + +static SINK_VTABLE: DispatchVtable = DispatchVtable { + base: IUnknown_Vtbl { + QueryInterface: query_interface, + AddRef: add_ref, + Release: release, + }, + type_info_count, + type_info, + names, + invoke, +}; + +unsafe extern "system" fn query_interface( + this: *mut c_void, + iid: *const GUID, + result: *mut *mut c_void, +) -> HRESULT { + let Some(result) = (unsafe { result.as_mut() }) else { + return E_POINTER; + }; + *result = null_mut(); + let Some(iid) = (unsafe { iid.as_ref() }) else { + return E_POINTER; + }; + if [EVENTS_IID, DISPATCH_IID, IID_IUnknown] + .iter() + .any(|supported| { + supported.data1 == iid.data1 + && supported.data2 == iid.data2 + && supported.data3 == iid.data3 + && supported.data4 == iid.data4 + }) + { + unsafe { add_ref(this) }; + *result = this; + S_OK + } else { + E_NOINTERFACE + } +} + +unsafe extern "system" fn add_ref(this: *mut c_void) -> u32 { + let sink = unsafe { &*this.cast::() }; + sink.reference_count.fetch_add(1, Ordering::Relaxed) + 1 +} + +unsafe extern "system" fn release(this: *mut c_void) -> u32 { + let sink = unsafe { &*this.cast::() }; + let remaining = sink.reference_count.fetch_sub(1, Ordering::AcqRel) - 1; + if remaining == 0 { + unsafe { drop(Box::from_raw(this.cast::())) }; + } + remaining +} + +unsafe extern "system" fn type_info_count(_: *mut c_void, count: *mut u32) -> HRESULT { + let Some(count) = (unsafe { count.as_mut() }) else { + return E_POINTER; + }; + *count = 0; + S_OK +} + +unsafe extern "system" fn type_info( + _: *mut c_void, + _: u32, + _: u32, + output: *mut *mut c_void, +) -> HRESULT { + if let Some(output) = unsafe { output.as_mut() } { + *output = null_mut(); + } + E_NOTIMPL +} + +unsafe extern "system" fn names( + _: *mut c_void, + _: *const GUID, + _: *const PWSTR, + _: u32, + _: u32, + _: *mut i32, +) -> HRESULT { + DISP_E_UNKNOWNNAME +} + +unsafe extern "system" fn invoke( + this: *mut c_void, + member: i32, + _: *const GUID, + _: u32, + _: u16, + parameters: *mut DISPPARAMS, + _: *mut VARIANT, + _: *mut EXCEPINFO, + _: *mut u32, +) -> HRESULT { + let sink = unsafe { &*this.cast::() }; + // Panics must never cross the COM ABI. + match std::panic::catch_unwind(|| unsafe { sink.on_dispatch(member, parameters) }) { + Ok(hr) => hr, + Err(_) => { + sink.publisher + .report_fault(EventFault::Conversion("panic in MXAccess event sink")); + DISP_E_EXCEPTION + } + } +} + +impl Sink { + unsafe fn on_dispatch(&self, member: i32, parameters: *const DISPPARAMS) -> HRESULT { + let (count, family, body) = match member { + 1 => ( + 6, + MxEventFamily::OnDataChange, + mx_event::Body::OnDataChange(OnDataChangeEvent {}), + ), + 2 => ( + 3, + MxEventFamily::OnWriteComplete, + mx_event::Body::OnWriteComplete(OnWriteCompleteEvent {}), + ), + 3 => ( + 3, + MxEventFamily::OperationComplete, + mx_event::Body::OperationComplete(OperationCompleteEvent {}), + ), + _ => return DISP_E_MEMBERNOTFOUND, + }; + let Some(parameters) = (unsafe { parameters.as_ref() }) else { + return E_POINTER; + }; + if parameters.cArgs as usize != count || parameters.rgvarg.is_null() { + self.publisher.report_fault(EventFault::Conversion( + "unexpected MXAccess callback argument count", + )); + return DISP_E_BADPARAMCOUNT; + } + // IDispatch packs its positional arguments in reverse order. LMXProxy + // puts them at four-byte aligned addresses on x86, so copy them before + // forming Rust references to the eight-byte-aligned VARIANT type. + let mut args = [VARIANT::default(); 6]; + for (index, argument) in args[..count].iter_mut().enumerate() { + *argument = unsafe { parameters.rgvarg.add(index).read_unaligned() }; + } + let (Some(server), Some(item)) = (unsafe { dispatch_i32(&args[count - 1]) }, unsafe { + dispatch_i32(&args[count - 2]) + }) else { + self.publisher.report_fault(EventFault::Conversion( + "unexpected MXAccess callback handle types", + )); + return DISP_E_TYPEMISMATCH; + }; + if member == 1 && !self.publisher.is_advised(server, item) { + return S_OK; + } + let statuses = match unsafe { dispatch_statuses(&args[0]) } { + Ok(statuses) => statuses, + Err(error) => { + self.publisher.report_fault(EventFault::Conversion(error)); + return DISP_E_TYPEMISMATCH; + } + }; + let mut event = MxEvent { + family: family as i32, + server_handle: server, + item_handle: item, + statuses, + body: Some(body), + ..Default::default() + }; + if member == 1 { + let Some(quality) = (unsafe { dispatch_i32(&args[2]) }) else { + self.publisher + .report_fault(EventFault::Conversion("unexpected MXAccess quality type")); + return DISP_E_TYPEMISMATCH; + }; + event.quality = quality; + event.value = Some(unsafe { dispatch_value(&args[3]) }); + event.source_timestamp = unsafe { dispatch_timestamp(&args[1]) }; + } + self.publisher.publish(event); + S_OK + } +} + +#[cfg(test)] +mod tests { + use super::*; + use crate::wire::mxaccess_gateway::v1::mx_value; + use std::mem::size_of; + use windows_sys::Win32::System::Variant::VT_I4; + + #[test] + fn copies_x86_dispatch_arguments_and_filters_unadvised_items() { + #[repr(align(8))] + struct PackedArguments([u8; 4 + 6 * size_of::()]); + + let (publisher, mut events, faults) = event_channel("session", 1); + let sink = Sink { + table: &SINK_VTABLE, + reference_count: AtomicU32::new(1), + publisher, + }; + let mut packed = PackedArguments([0; 4 + 6 * size_of::()]); + let values = [ + VARIANT::default(), + VARIANT::default(), + integer(192), + integer(42), + integer(7), + integer(5), + ]; + // SAFETY: each of the six complete VARIANTs fits the byte buffer, + // whose four-byte offset reproduces the native callback alignment. + let parameters = unsafe { + let args = packed.0.as_mut_ptr().add(4).cast::(); + for (index, value) in values.into_iter().enumerate() { + args.add(index).write_unaligned(value); + } + DISPPARAMS { + rgvarg: args, + cArgs: 6, + ..Default::default() + } + }; + + assert!(sink.publisher.advise(5, 7)); + assert_eq!(unsafe { sink.on_dispatch(1, ¶meters) }, S_OK); + assert_eq!(unsafe { sink.on_dispatch(1, ¶meters) }, S_OK); + assert!(matches!(*faults.borrow(), Some(EventFault::Overflow))); + assert_eq!(sink.publisher.last_sequence(), 1); + + let event = events.try_recv().unwrap(); + assert_eq!(event.session_id, "session"); + assert_eq!(event.worker_sequence, 1); + assert_eq!(event.quality, 192); + assert!(matches!( + event.value.unwrap().kind, + Some(mx_value::Kind::Int32Value(42)) + )); + + sink.publisher.unadvise(5, 7); + assert_eq!(unsafe { sink.on_dispatch(1, ¶meters) }, S_OK); + assert!(matches!( + events.try_recv(), + Err(mpsc::error::TryRecvError::Empty) + )); + } + + fn integer(value: i32) -> VARIANT { + let mut result = VARIANT::default(); + result.Anonymous.Anonymous.vt = VT_I4; + result.Anonymous.Anonymous.Anonymous.lVal = value; + result + } +} diff --git a/src/mxgateway-worker-rust/src/session.rs b/src/mxgateway-worker-rust/src/session.rs index 9be3a781..5608b685 100644 --- a/src/mxgateway-worker-rust/src/session.rs +++ b/src/mxgateway-worker-rust/src/session.rs @@ -2,23 +2,27 @@ use std::collections::HashMap; use std::io; use std::time::{Duration, SystemTime}; +use prost::Message; use prost_types::Timestamp; use tokio::io::{split, AsyncWrite}; use tokio::net::windows::named_pipe::{ClientOptions, NamedPipeClient}; -use tokio::sync::mpsc; +use tokio::sync::{mpsc, watch}; use tokio::time::{interval, sleep, timeout, Instant, MissedTickBehavior}; +use crate::automation::int32; use crate::mxaccess::{Command, CommandError, Sta, StartError, CLASS_ID, PROG_ID}; +use crate::mxevent::{event_channel, EventFault, Publisher}; use crate::wire::{ self, mxaccess_gateway::v1::{ - mx_command, mx_command_reply, mx_value, AddItemReply, DrainEventsReply, MxCommand, - MxCommandKind, MxCommandReply, MxDataType, MxValue, ProtocolStatus, ProtocolStatusCode, - RegisterReply, SessionState, SessionStateReply, WorkerInfoReply, + mx_command, mx_command_reply, AddItemReply, DrainEventsReply, MxCommand, MxCommandKind, + MxCommandReply, MxEvent, ProtocolStatus, ProtocolStatusCode, RegisterReply, SessionState, + SessionStateReply, WorkerInfoReply, }, mxaccess_worker::v1::{ - GatewayHello, WorkerCommand, WorkerCommandReply, WorkerFault, WorkerFaultCategory, - WorkerHeartbeat, WorkerHello, WorkerReady, WorkerShutdownAck, WorkerState, + GatewayHello, WorkerCommand, WorkerCommandReply, WorkerEvent, WorkerFault, + WorkerFaultCategory, WorkerHeartbeat, WorkerHello, WorkerReady, WorkerShutdownAck, + WorkerState, }, Body, Writer, DEFAULT_MAX, VERSION, }; @@ -65,6 +69,7 @@ struct Options { heartbeat_interval: Duration, sta_grace: Duration, command_ceiling: Duration, + event_capacity: usize, } impl Options { @@ -112,6 +117,11 @@ impl Options { heartbeat_interval: env_duration("MXGATEWAY_WORKER_HEARTBEAT_SEND_INTERVAL_MS", 5000), sta_grace: env_duration("MXGATEWAY_WORKER_HEARTBEAT_GRACE_MS", 15000), command_ceiling: env_duration("MXGATEWAY_WORKER_HEARTBEAT_STUCK_CEILING_MS", 75000), + event_capacity: std::env::var("MXGATEWAY_EVENT_QUEUE_CAPACITY") + .ok() + .and_then(|value| value.parse().ok()) + .filter(|value| (1_000..=1_000_000).contains(value)) + .unwrap_or(10_000), }) } } @@ -175,8 +185,9 @@ pub async fn run() -> Result<(), Error> { } }; - let sta = match Sta::start().await { - Ok(sta) => sta, + let mut events = EventStream::new(&options); + let (sta, pin_status) = match Sta::start(events.publisher.clone()).await { + Ok(started) => started, Err(error) => { let (hresult, diagnostic) = match error { StartError::Io(error) => (None, error.to_string()), @@ -206,7 +217,7 @@ pub async fn run() -> Result<(), Error> { mxaccess_progid: PROG_ID.into(), mxaccess_clsid: CLASS_ID.into(), ready_timestamp: Some(now()), - record_info_pin_status: String::new(), + record_info_pin_status: pin_status, }), max, ) @@ -225,7 +236,7 @@ pub async fn run() -> Result<(), Error> { } }); - let result = command_loop(&options, max, &sta, &mut writer, &mut frames).await; + let result = command_loop(&options, max, &sta, &mut writer, &mut frames, &mut events).await; // Keep the read half alive during graceful shutdown so the gateway can // finish a control write before reading the ack. let acknowledgement = match result { @@ -299,11 +310,43 @@ async fn command_loop( sta: &Sta, writer: &mut Writer, frames: &mut mpsc::Receiver>, + events: &mut EventStream, ) -> Result { let mut heartbeat = interval(options.heartbeat_interval); heartbeat.set_missed_tick_behavior(MissedTickBehavior::Delay); loop { + let callback_fault = *events.faults.borrow_and_update(); + if let Some(failure) = callback_fault { + let (category, message, exit_code) = match failure { + EventFault::Overflow => ( + WorkerFaultCategory::QueueOverflow, + "MXAccess event backlog exceeded the outbound queue capacity", + 8, + ), + EventFault::Conversion(message) => ( + WorkerFaultCategory::MxaccessEventConversionFailed, + message, + 1, + ), + }; + fault(writer, &options.session, max, category, None, message).await; + return Err(Error(exit_code, message.into())); + } tokio::select! { + _ = events.faults.changed() => continue, + next = recv_event(&mut events.pending, &mut events.receiver) => { + let event = next.ok_or_else(|| Error(1, "MXAccess event channel closed".into()))?; + let result = writer.send( + &options.session, "", Body::WorkerEvent(WorkerEvent { event: Some(event) }), max, + ).await; + if let Err(error) = result { + if error.kind() == io::ErrorKind::InvalidData { + fault(writer, &options.session, max, WorkerFaultCategory::ProtocolViolation, None, + "MXAccess event exceeds the negotiated frame maximum").await; + } + return Err(error.into()); + } + } frame = frames.recv() => { let frame = frame.ok_or_else(|| Error(5, "Worker pipe reader exited".into()))??; let correlation = frame.correlation_id; @@ -312,7 +355,7 @@ async fn command_loop( let kind = command.as_ref().map_or(0, |command| command.kind); let execution = timeout( options.command_ceiling, - execute(sta, command, &options.session, &correlation), + execute(sta, command, &options.session, &correlation, events, max), ).await; let mut reply = match execution { Ok(Ok(reply)) => reply, @@ -364,6 +407,8 @@ async fn command_loop( worker_process_id: pid(), state: WorkerState::Ready as i32, last_sta_activity_timestamp: Some(now()), + outbound_event_queue_depth: events.depth() as u32, + last_event_sequence: events.publisher.last_sequence(), ..Default::default() }), max).await?; } @@ -376,6 +421,8 @@ async fn execute( command: Option, session: &str, correlation: &str, + events: &mut EventStream, + max: usize, ) -> Result { use mx_command::Payload; use mx_command_reply::Payload as Reply; @@ -408,6 +455,12 @@ async fn execute( (Kind::RemoveItem, Some(Payload::RemoveItem(request))) => { Command::RemoveItem(request.server_handle, request.item_handle) } + (Kind::Advise, Some(Payload::Advise(request))) => { + Command::Advise(request.server_handle, request.item_handle) + } + (Kind::UnAdvise, Some(Payload::UnAdvise(request))) => { + Command::UnAdvise(request.server_handle, request.item_handle) + } (Kind::Ping, Some(Payload::Ping(request))) => { reply.diagnostic_message = request.message; return Ok(reply); @@ -429,8 +482,12 @@ async fn execute( })); return Ok(reply); } - (Kind::DrainEvents, None | Some(Payload::DrainEvents(_))) => { - reply.payload = Some(Reply::DrainEvents(DrainEventsReply::default())); + (Kind::DrainEvents, payload @ (None | Some(Payload::DrainEvents(_)))) => { + let requested = match payload { + Some(Payload::DrainEvents(request)) => request.max_events, + _ => 0, + }; + reply.payload = Some(Reply::DrainEvents(events.drain(requested, max))); return Ok(reply); } _ => { @@ -444,12 +501,7 @@ async fn execute( }; match sta.execute(native).await.map_err(Error::sta)? { Ok(Some(handle)) => { - reply.return_value = Some(MxValue { - data_type: MxDataType::Integer as i32, - variant_type: "VT_I4".into(), - kind: Some(mx_value::Kind::Int32Value(handle)), - ..Default::default() - }); + reply.return_value = Some(int32(handle)); reply.payload = Some(match kind { Kind::Register => Reply::Register(RegisterReply { server_handle: handle, @@ -477,6 +529,71 @@ async fn execute( Ok(reply) } +struct EventStream { + publisher: Publisher, + receiver: mpsc::Receiver, + faults: watch::Receiver>, + pending: Option, +} + +impl EventStream { + fn new(options: &Options) -> Self { + let (publisher, receiver, faults) = event_channel(&options.session, options.event_capacity); + Self { + publisher, + receiver, + faults, + pending: None, + } + } + + fn depth(&self) -> usize { + self.receiver.len() + usize::from(self.pending.is_some()) + } + + fn drain(&mut self, requested: u32, max_frame: usize) -> DrainEventsReply { + const LIMIT: u32 = 10_000; + let count = if requested == 0 { + LIMIT + } else { + requested.min(LIMIT) + }; + // The C# worker reserves space for the pipe and public gRPC envelopes. + let mut bytes_remaining = max_frame.saturating_sub(2 * 64 * 1024).max(max_frame / 2); + let mut events = Vec::new(); + for _ in 0..count { + let Some(event) = self + .pending + .take() + .or_else(|| self.receiver.try_recv().ok()) + else { + break; + }; + let size = event.encoded_len(); + let cost = 1 + prost::length_delimiter_len(size) + size; + if cost > bytes_remaining { + self.pending = Some(event); + break; + } + bytes_remaining -= cost; + events.push(event); + } + DrainEventsReply { events } + } +} + +// Receiver::recv is cancellation-safe. A pending event returns immediately +// when this branch is polled, so select never cancels after taking it. +async fn recv_event( + pending: &mut Option, + receiver: &mut mpsc::Receiver, +) -> Option { + match pending.take() { + Some(event) => Some(event), + None => receiver.recv().await, + } +} + fn command_reply(session: &str, correlation: &str, kind: i32) -> MxCommandReply { MxCommandReply { session_id: session.into(), diff --git a/src/mxgateway-worker-rust/tests/live_mxaccess.rs b/src/mxgateway-worker-rust/tests/live_mxaccess.rs index 33654d35..3bae9d81 100644 --- a/src/mxgateway-worker-rust/tests/live_mxaccess.rs +++ b/src/mxgateway-worker-rust/tests/live_mxaccess.rs @@ -9,13 +9,13 @@ use tokio::io::{split, AsyncRead, AsyncWrite}; use tokio::net::windows::named_pipe::ServerOptions; use tokio::time::{sleep, timeout}; use wire::mxaccess_gateway::v1::{ - mx_command, mx_command_reply, AddItemCommand, MxCommand, MxCommandKind, MxCommandReply, - ProtocolStatusCode, RegisterCommand, RemoveItemCommand, UnregisterCommand, + mx_command, mx_command_reply, AddItemCommand, AdviseCommand, MxCommand, MxCommandKind, + MxCommandReply, ProtocolStatusCode, RegisterCommand, RemoveItemCommand, UnregisterCommand, }; use wire::mxaccess_worker::v1::{GatewayHello, WorkerCommand, WorkerShutdown}; use wire::{Body, Writer, DEFAULT_MAX, VERSION}; -const DEADLINE: Duration = Duration::from_secs(20); +const DEADLINE: Duration = Duration::from_secs(40); #[tokio::test] #[ignore = "requires a registered 32-bit System Platform MXAccess installation"] @@ -141,8 +141,9 @@ async fn activates_registers_and_shuts_down_over_a_windows_pipe() { ) .await; - // Leave a registration active to exercise the worker's shutdown cleanup. - invoke( + // Leave a registration and, when a live item is configured, an advice + // active to check that the shutdown acknowledgement is sent after cleanup. + let cleanup_registration = invoke( &mut reader, &mut writer, session, @@ -153,6 +154,40 @@ async fn activates_registers_and_shuts_down_over_a_windows_pipe() { }), ) .await; + if let Ok(definition) = std::env::var("MXGATEWAY_RUST_SMOKE_ITEM") { + let Some(mx_command_reply::Payload::Register(registration)) = + cleanup_registration.payload + else { + panic!("missing cleanup registration handle"); + }; + let added = invoke( + &mut reader, + &mut writer, + session, + "add-for-cleanup", + MxCommandKind::AddItem, + mx_command::Payload::AddItem(AddItemCommand { + server_handle: registration.server_handle, + item_definition: definition, + }), + ) + .await; + let Some(mx_command_reply::Payload::AddItem(item)) = added.payload else { + panic!("missing live item handle"); + }; + invoke( + &mut reader, + &mut writer, + session, + "advise-for-cleanup", + MxCommandKind::Advise, + mx_command::Payload::Advise(AdviseCommand { + server_handle: registration.server_handle, + item_handle: item.item_handle, + }), + ) + .await; + } writer .send( session, @@ -164,7 +199,7 @@ async fn activates_registers_and_shuts_down_over_a_windows_pipe() { .unwrap(); loop { match read_body(&mut reader, session).await { - Body::WorkerHeartbeat(_) => {} + Body::WorkerHeartbeat(_) | Body::WorkerEvent(_) => {} Body::WorkerShutdownAck(ack) => { let status = ack.status.unwrap(); assert_eq!(status.code(), ProtocolStatusCode::Ok);