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