15 Commits

Author SHA1 Message Date
Joseph Doherty 8e0e232fd6 deploy(prod): mxgw e74918cc on par-vp03/par-vp04 (site-par Connected; galaxy-sql waits on scadaproj#60)
ci / windows-x86 (push) Successful in 2m6s
ci / java (push) Failing after 6m33s
ci / portable (push) Failing after 10m42s
Claude-Session: https://claude.ai/code/session_01GT67AD5mc2BnaaAfY3hqqQ
2026-11-22 07:45:06 -05:00
Joseph Doherty cfaacf7485 Require Galaxy query readiness on automatic startup 2026-11-21 20:52:38 -05:00
Joseph Doherty 8fe50f7cad Let WnWrap release shared calls after Alarmmgr exits 2026-11-21 14:46:51 -05:00
Joseph Doherty f2720d4e56 Keep subtag failover until the Alarmmgr query completes 2026-11-21 14:23:39 -05:00
Joseph Doherty c102f881b7 Recover WnWrap after local Alarmmgr exits 2026-11-21 13:51:12 -05:00
Joseph Doherty 3bb6182f91 Isolate WnWrap recovery from the MXAccess STA 2026-11-21 11:50:51 -05:00
Joseph Doherty 04655b78fa Add a live test for a real Alarmmgr outage 2026-11-21 10:28:54 -05:00
Joseph Doherty 8045afe174 Verify automatic alarm failover through the gateway 2026-11-21 10:13:38 -05:00
Joseph Doherty 9293c4c913 Run alarm providers natively in the Rust worker 2026-11-21 09:53:37 -05:00
Joseph Doherty dbc5ea0c25 Complete Rust worker command coverage and bridge the alarm provider 2026-11-21 09:34:41 -05:00
Joseph Doherty d1769f81ff Implement native MXAccess writes in the Rust worker 2026-11-21 09:15:32 -05:00
Joseph Doherty 00a597b08b Stream live MXAccess advice events from the Rust worker 2026-11-21 08:56:01 -05:00
Joseph Doherty 1a44b5ac31 Prototype native Rust MXAccess worker 2026-11-21 08:25:33 -05:00
Joseph Doherty 30ac3f3225 feat(worker,gateway): report record-info pin outcome in WorkerReady; drain-thread completion tests
The worker logged its MXSTATUS_PROXY record-info pin outcome only to its own
stderr, which the gateway's launcher does not capture, so operators never saw
whether the pin took.

- Contract (additive, internal IPC): WorkerReady.record_info_pin_status = 5.
  No protocol version bump; an older peer ignores it or sends it empty.
  Regenerated Contracts/Generated, the client protoset, the Rust vendored
  proto, and the Go/Python/Java worker bindings (real delta only).
- Worker: the handshake copies MxStatusProxyRecordInfoPin.Status into
  WorkerReady unless the runtime already set it.
- Gateway: WorkerClient records it (IWorkerClient.RecordInfoPinStatus, default
  null) and logs it at Information when pinned, Warning otherwise; empty is
  not logged. Shown as "Record-info pin" in the dashboard session details
  Worker table via DashboardSessionSummary.WorkerRecordInfoPinStatus.
- Tests: drain-thread cancellation (task ends Canceled; RunAsync cancels,
  runtime disposed, thread exits, no EventDrain stop-failure log) and fault
  (exception on the thread faults RunAsync with the same instance) paths;
  WorkerReady carries the pin status; gateway logging/recording and the
  dashboard projection.
- Docs: gateway.md STA thread model (drain thread, pin step in COM lifetime),
  MxAccessWorkerInstanceDesign, GatewayProcessDesign, GatewayDashboardDesign,
  WorkerFrameProtocol.
2026-11-21 08:13:27 -05:00
Joseph Doherty 659bb831a2 perf(worker,gateway): pin MXSTATUS_PROXY record info, dedicated event-drain thread, cap thread-pool spin
At ~240 events/s the worker burned 8-12% system CPU on wonder-app-vd03. A WPR
trace put 72% of it in lmxproxy's per-event GetRecordInfoFromGuids (type
library reload through the registry, amplified by WOW64 and the EDR registry
filter) and 26% in the .NET Framework thread pool spinning after each
per-event work item.

- Worker: MxStatusProxyRecordInfoPin holds the LMXProxy ITypeLib and the
  MXSTATUS_PROXY IRecordInfo for the process lifetime, so oleaut32 serves the
  per-event lookup from cache (~1 ms -> <1 us per call). Best-effort, never
  fatal; status logged at handshake.
- Worker: the event drain loop runs on a dedicated background thread with a
  synchronous wait and a synchronous pipe flush, removing the per-event
  thread-pool hops.
- Gateway: runtimeconfig sets ThreadPool.UnfairSemaphoreSpinLimit=10 to cut
  PortableThreadPool spin-wait CPU.

windev bench at ~242 ev/s: worker 67% -> 6.2% of one core; gateway 8.1% -> 6.9%.
Worker suite 785 passed / 14 skipped (x86, windev).
2026-11-21 08:00:13 -05:00
54 changed files with 9443 additions and 183 deletions
@@ -580,8 +580,15 @@ type WorkerReady struct {
MxaccessProgid string `protobuf:"bytes,2,opt,name=mxaccess_progid,json=mxaccessProgid,proto3" json:"mxaccess_progid,omitempty"` MxaccessProgid string `protobuf:"bytes,2,opt,name=mxaccess_progid,json=mxaccessProgid,proto3" json:"mxaccess_progid,omitempty"`
MxaccessClsid string `protobuf:"bytes,3,opt,name=mxaccess_clsid,json=mxaccessClsid,proto3" json:"mxaccess_clsid,omitempty"` MxaccessClsid string `protobuf:"bytes,3,opt,name=mxaccess_clsid,json=mxaccessClsid,proto3" json:"mxaccess_clsid,omitempty"`
ReadyTimestamp *timestamppb.Timestamp `protobuf:"bytes,4,opt,name=ready_timestamp,json=readyTimestamp,proto3" json:"ready_timestamp,omitempty"` ReadyTimestamp *timestamppb.Timestamp `protobuf:"bytes,4,opt,name=ready_timestamp,json=readyTimestamp,proto3" json:"ready_timestamp,omitempty"`
unknownFields protoimpl.UnknownFields // Outcome of the worker's process-wide MXSTATUS_PROXY record-info pin
sizeCache protoimpl.SizeCache // (MxStatusProxyRecordInfoPin): "pinned {library-guid} vM.m record {record-guid}" on
// success, or the failing call and HRESULT / exception type otherwise. Empty when the
// worker made no attempt (an older worker, or a runtime that never created the
// MXAccess COM object). Diagnostic only: the gateway logs it and shows it on the
// dashboard, and never gates readiness on it, because the pin is a cache warm-up.
RecordInfoPinStatus string `protobuf:"bytes,5,opt,name=record_info_pin_status,json=recordInfoPinStatus,proto3" json:"record_info_pin_status,omitempty"`
unknownFields protoimpl.UnknownFields
sizeCache protoimpl.SizeCache
} }
func (x *WorkerReady) Reset() { func (x *WorkerReady) Reset() {
@@ -642,6 +649,13 @@ func (x *WorkerReady) GetReadyTimestamp() *timestamppb.Timestamp {
return nil return nil
} }
func (x *WorkerReady) GetRecordInfoPinStatus() string {
if x != nil {
return x.RecordInfoPinStatus
}
return ""
}
type WorkerCommand struct { type WorkerCommand struct {
state protoimpl.MessageState `protogen:"open.v1"` state protoimpl.MessageState `protogen:"open.v1"`
Command *MxCommand `protobuf:"bytes,1,opt,name=command,proto3" json:"command,omitempty"` Command *MxCommand `protobuf:"bytes,1,opt,name=command,proto3" json:"command,omitempty"`
@@ -1139,12 +1153,13 @@ const file_mxaccess_worker_proto_rawDesc = "" +
"\x10protocol_version\x18\x01 \x01(\rR\x0fprotocolVersion\x12\x14\n" + "\x10protocol_version\x18\x01 \x01(\rR\x0fprotocolVersion\x12\x14\n" +
"\x05nonce\x18\x02 \x01(\tR\x05nonce\x12*\n" + "\x05nonce\x18\x02 \x01(\tR\x05nonce\x12*\n" +
"\x11worker_process_id\x18\x03 \x01(\x05R\x0fworkerProcessId\x12%\n" + "\x11worker_process_id\x18\x03 \x01(\x05R\x0fworkerProcessId\x12%\n" +
"\x0eworker_version\x18\x04 \x01(\tR\rworkerVersion\"\xce\x01\n" + "\x0eworker_version\x18\x04 \x01(\tR\rworkerVersion\"\x83\x02\n" +
"\vWorkerReady\x12*\n" + "\vWorkerReady\x12*\n" +
"\x11worker_process_id\x18\x01 \x01(\x05R\x0fworkerProcessId\x12'\n" + "\x11worker_process_id\x18\x01 \x01(\x05R\x0fworkerProcessId\x12'\n" +
"\x0fmxaccess_progid\x18\x02 \x01(\tR\x0emxaccessProgid\x12%\n" + "\x0fmxaccess_progid\x18\x02 \x01(\tR\x0emxaccessProgid\x12%\n" +
"\x0emxaccess_clsid\x18\x03 \x01(\tR\rmxaccessClsid\x12C\n" + "\x0emxaccess_clsid\x18\x03 \x01(\tR\rmxaccessClsid\x12C\n" +
"\x0fready_timestamp\x18\x04 \x01(\v2\x1a.google.protobuf.TimestampR\x0ereadyTimestamp\"\x92\x01\n" + "\x0fready_timestamp\x18\x04 \x01(\v2\x1a.google.protobuf.TimestampR\x0ereadyTimestamp\x123\n" +
"\x16record_info_pin_status\x18\x05 \x01(\tR\x13recordInfoPinStatus\"\x92\x01\n" +
"\rWorkerCommand\x128\n" + "\rWorkerCommand\x128\n" +
"\acommand\x18\x01 \x01(\v2\x1e.mxaccess_gateway.v1.MxCommandR\acommand\x12G\n" + "\acommand\x18\x01 \x01(\v2\x1e.mxaccess_gateway.v1.MxCommandR\acommand\x12G\n" +
"\x11enqueue_timestamp\x18\x02 \x01(\v2\x1a.google.protobuf.TimestampR\x10enqueueTimestamp\"\x9c\x01\n" + "\x11enqueue_timestamp\x18\x02 \x01(\v2\x1a.google.protobuf.TimestampR\x10enqueueTimestamp\"\x9c\x01\n" +
@@ -5477,6 +5477,36 @@ public final class MxaccessWorker extends com.google.protobuf.GeneratedFile {
* <code>.google.protobuf.Timestamp ready_timestamp = 4;</code> * <code>.google.protobuf.Timestamp ready_timestamp = 4;</code>
*/ */
com.google.protobuf.TimestampOrBuilder getReadyTimestampOrBuilder(); com.google.protobuf.TimestampOrBuilder getReadyTimestampOrBuilder();
/**
* <pre>
* Outcome of the worker's process-wide MXSTATUS_PROXY record-info pin
* (MxStatusProxyRecordInfoPin): "pinned {library-guid} vM.m record {record-guid}" on
* success, or the failing call and HRESULT / exception type otherwise. Empty when the
* worker made no attempt (an older worker, or a runtime that never created the
* MXAccess COM object). Diagnostic only: the gateway logs it and shows it on the
* dashboard, and never gates readiness on it, because the pin is a cache warm-up.
* </pre>
*
* <code>string record_info_pin_status = 5;</code>
* @return The recordInfoPinStatus.
*/
java.lang.String getRecordInfoPinStatus();
/**
* <pre>
* Outcome of the worker's process-wide MXSTATUS_PROXY record-info pin
* (MxStatusProxyRecordInfoPin): "pinned {library-guid} vM.m record {record-guid}" on
* success, or the failing call and HRESULT / exception type otherwise. Empty when the
* worker made no attempt (an older worker, or a runtime that never created the
* MXAccess COM object). Diagnostic only: the gateway logs it and shows it on the
* dashboard, and never gates readiness on it, because the pin is a cache warm-up.
* </pre>
*
* <code>string record_info_pin_status = 5;</code>
* @return The bytes for recordInfoPinStatus.
*/
com.google.protobuf.ByteString
getRecordInfoPinStatusBytes();
} }
/** /**
* Protobuf type {@code mxaccess_worker.v1.WorkerReady} * Protobuf type {@code mxaccess_worker.v1.WorkerReady}
@@ -5502,6 +5532,7 @@ public final class MxaccessWorker extends com.google.protobuf.GeneratedFile {
private WorkerReady() { private WorkerReady() {
mxaccessProgid_ = ""; mxaccessProgid_ = "";
mxaccessClsid_ = ""; mxaccessClsid_ = "";
recordInfoPinStatus_ = "";
} }
public static final com.google.protobuf.Descriptors.Descriptor public static final com.google.protobuf.Descriptors.Descriptor
@@ -5633,6 +5664,63 @@ public final class MxaccessWorker extends com.google.protobuf.GeneratedFile {
return readyTimestamp_ == null ? com.google.protobuf.Timestamp.getDefaultInstance() : readyTimestamp_; return readyTimestamp_ == null ? com.google.protobuf.Timestamp.getDefaultInstance() : readyTimestamp_;
} }
public static final int RECORD_INFO_PIN_STATUS_FIELD_NUMBER = 5;
@SuppressWarnings("serial")
private volatile java.lang.Object recordInfoPinStatus_ = "";
/**
* <pre>
* Outcome of the worker's process-wide MXSTATUS_PROXY record-info pin
* (MxStatusProxyRecordInfoPin): "pinned {library-guid} vM.m record {record-guid}" on
* success, or the failing call and HRESULT / exception type otherwise. Empty when the
* worker made no attempt (an older worker, or a runtime that never created the
* MXAccess COM object). Diagnostic only: the gateway logs it and shows it on the
* dashboard, and never gates readiness on it, because the pin is a cache warm-up.
* </pre>
*
* <code>string record_info_pin_status = 5;</code>
* @return The recordInfoPinStatus.
*/
@java.lang.Override
public java.lang.String getRecordInfoPinStatus() {
java.lang.Object ref = recordInfoPinStatus_;
if (ref instanceof java.lang.String) {
return (java.lang.String) ref;
} else {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
recordInfoPinStatus_ = s;
return s;
}
}
/**
* <pre>
* Outcome of the worker's process-wide MXSTATUS_PROXY record-info pin
* (MxStatusProxyRecordInfoPin): "pinned {library-guid} vM.m record {record-guid}" on
* success, or the failing call and HRESULT / exception type otherwise. Empty when the
* worker made no attempt (an older worker, or a runtime that never created the
* MXAccess COM object). Diagnostic only: the gateway logs it and shows it on the
* dashboard, and never gates readiness on it, because the pin is a cache warm-up.
* </pre>
*
* <code>string record_info_pin_status = 5;</code>
* @return The bytes for recordInfoPinStatus.
*/
@java.lang.Override
public com.google.protobuf.ByteString
getRecordInfoPinStatusBytes() {
java.lang.Object ref = recordInfoPinStatus_;
if (ref instanceof java.lang.String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
recordInfoPinStatus_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
private byte memoizedIsInitialized = -1; private byte memoizedIsInitialized = -1;
@java.lang.Override @java.lang.Override
public final boolean isInitialized() { public final boolean isInitialized() {
@@ -5659,6 +5747,9 @@ public final class MxaccessWorker extends com.google.protobuf.GeneratedFile {
if (((bitField0_ & 0x00000001) != 0)) { if (((bitField0_ & 0x00000001) != 0)) {
output.writeMessage(4, getReadyTimestamp()); output.writeMessage(4, getReadyTimestamp());
} }
if (!com.google.protobuf.GeneratedMessage.isStringEmpty(recordInfoPinStatus_)) {
com.google.protobuf.GeneratedMessage.writeString(output, 5, recordInfoPinStatus_);
}
getUnknownFields().writeTo(output); getUnknownFields().writeTo(output);
} }
@@ -5682,6 +5773,9 @@ public final class MxaccessWorker extends com.google.protobuf.GeneratedFile {
size += com.google.protobuf.CodedOutputStream size += com.google.protobuf.CodedOutputStream
.computeMessageSize(4, getReadyTimestamp()); .computeMessageSize(4, getReadyTimestamp());
} }
if (!com.google.protobuf.GeneratedMessage.isStringEmpty(recordInfoPinStatus_)) {
size += com.google.protobuf.GeneratedMessage.computeStringSize(5, recordInfoPinStatus_);
}
size += getUnknownFields().getSerializedSize(); size += getUnknownFields().getSerializedSize();
memoizedSize = size; memoizedSize = size;
return size; return size;
@@ -5708,6 +5802,8 @@ public final class MxaccessWorker extends com.google.protobuf.GeneratedFile {
if (!getReadyTimestamp() if (!getReadyTimestamp()
.equals(other.getReadyTimestamp())) return false; .equals(other.getReadyTimestamp())) return false;
} }
if (!getRecordInfoPinStatus()
.equals(other.getRecordInfoPinStatus())) return false;
if (!getUnknownFields().equals(other.getUnknownFields())) return false; if (!getUnknownFields().equals(other.getUnknownFields())) return false;
return true; return true;
} }
@@ -5729,6 +5825,8 @@ public final class MxaccessWorker extends com.google.protobuf.GeneratedFile {
hash = (37 * hash) + READY_TIMESTAMP_FIELD_NUMBER; hash = (37 * hash) + READY_TIMESTAMP_FIELD_NUMBER;
hash = (53 * hash) + getReadyTimestamp().hashCode(); hash = (53 * hash) + getReadyTimestamp().hashCode();
} }
hash = (37 * hash) + RECORD_INFO_PIN_STATUS_FIELD_NUMBER;
hash = (53 * hash) + getRecordInfoPinStatus().hashCode();
hash = (29 * hash) + getUnknownFields().hashCode(); hash = (29 * hash) + getUnknownFields().hashCode();
memoizedHashCode = hash; memoizedHashCode = hash;
return hash; return hash;
@@ -5874,6 +5972,7 @@ public final class MxaccessWorker extends com.google.protobuf.GeneratedFile {
readyTimestampBuilder_.dispose(); readyTimestampBuilder_.dispose();
readyTimestampBuilder_ = null; readyTimestampBuilder_ = null;
} }
recordInfoPinStatus_ = "";
return this; return this;
} }
@@ -5923,6 +6022,9 @@ public final class MxaccessWorker extends com.google.protobuf.GeneratedFile {
: readyTimestampBuilder_.build(); : readyTimestampBuilder_.build();
to_bitField0_ |= 0x00000001; to_bitField0_ |= 0x00000001;
} }
if (((from_bitField0_ & 0x00000010) != 0)) {
result.recordInfoPinStatus_ = recordInfoPinStatus_;
}
result.bitField0_ |= to_bitField0_; result.bitField0_ |= to_bitField0_;
} }
@@ -5954,6 +6056,11 @@ public final class MxaccessWorker extends com.google.protobuf.GeneratedFile {
if (other.hasReadyTimestamp()) { if (other.hasReadyTimestamp()) {
mergeReadyTimestamp(other.getReadyTimestamp()); mergeReadyTimestamp(other.getReadyTimestamp());
} }
if (!other.getRecordInfoPinStatus().isEmpty()) {
recordInfoPinStatus_ = other.recordInfoPinStatus_;
bitField0_ |= 0x00000010;
onChanged();
}
this.mergeUnknownFields(other.getUnknownFields()); this.mergeUnknownFields(other.getUnknownFields());
onChanged(); onChanged();
return this; return this;
@@ -6002,6 +6109,11 @@ public final class MxaccessWorker extends com.google.protobuf.GeneratedFile {
bitField0_ |= 0x00000008; bitField0_ |= 0x00000008;
break; break;
} // case 34 } // case 34
case 42: {
recordInfoPinStatus_ = input.readStringRequireUtf8();
bitField0_ |= 0x00000010;
break;
} // case 42
default: { default: {
if (!super.parseUnknownField(input, extensionRegistry, tag)) { if (!super.parseUnknownField(input, extensionRegistry, tag)) {
done = true; // was an endgroup tag done = true; // was an endgroup tag
@@ -6316,6 +6428,123 @@ public final class MxaccessWorker extends com.google.protobuf.GeneratedFile {
return readyTimestampBuilder_; return readyTimestampBuilder_;
} }
private java.lang.Object recordInfoPinStatus_ = "";
/**
* <pre>
* Outcome of the worker's process-wide MXSTATUS_PROXY record-info pin
* (MxStatusProxyRecordInfoPin): "pinned {library-guid} vM.m record {record-guid}" on
* success, or the failing call and HRESULT / exception type otherwise. Empty when the
* worker made no attempt (an older worker, or a runtime that never created the
* MXAccess COM object). Diagnostic only: the gateway logs it and shows it on the
* dashboard, and never gates readiness on it, because the pin is a cache warm-up.
* </pre>
*
* <code>string record_info_pin_status = 5;</code>
* @return The recordInfoPinStatus.
*/
public java.lang.String getRecordInfoPinStatus() {
java.lang.Object ref = recordInfoPinStatus_;
if (!(ref instanceof java.lang.String)) {
com.google.protobuf.ByteString bs =
(com.google.protobuf.ByteString) ref;
java.lang.String s = bs.toStringUtf8();
recordInfoPinStatus_ = s;
return s;
} else {
return (java.lang.String) ref;
}
}
/**
* <pre>
* Outcome of the worker's process-wide MXSTATUS_PROXY record-info pin
* (MxStatusProxyRecordInfoPin): "pinned {library-guid} vM.m record {record-guid}" on
* success, or the failing call and HRESULT / exception type otherwise. Empty when the
* worker made no attempt (an older worker, or a runtime that never created the
* MXAccess COM object). Diagnostic only: the gateway logs it and shows it on the
* dashboard, and never gates readiness on it, because the pin is a cache warm-up.
* </pre>
*
* <code>string record_info_pin_status = 5;</code>
* @return The bytes for recordInfoPinStatus.
*/
public com.google.protobuf.ByteString
getRecordInfoPinStatusBytes() {
java.lang.Object ref = recordInfoPinStatus_;
if (ref instanceof String) {
com.google.protobuf.ByteString b =
com.google.protobuf.ByteString.copyFromUtf8(
(java.lang.String) ref);
recordInfoPinStatus_ = b;
return b;
} else {
return (com.google.protobuf.ByteString) ref;
}
}
/**
* <pre>
* Outcome of the worker's process-wide MXSTATUS_PROXY record-info pin
* (MxStatusProxyRecordInfoPin): "pinned {library-guid} vM.m record {record-guid}" on
* success, or the failing call and HRESULT / exception type otherwise. Empty when the
* worker made no attempt (an older worker, or a runtime that never created the
* MXAccess COM object). Diagnostic only: the gateway logs it and shows it on the
* dashboard, and never gates readiness on it, because the pin is a cache warm-up.
* </pre>
*
* <code>string record_info_pin_status = 5;</code>
* @param value The recordInfoPinStatus to set.
* @return This builder for chaining.
*/
public Builder setRecordInfoPinStatus(
java.lang.String value) {
if (value == null) { throw new NullPointerException(); }
recordInfoPinStatus_ = value;
bitField0_ |= 0x00000010;
onChanged();
return this;
}
/**
* <pre>
* Outcome of the worker's process-wide MXSTATUS_PROXY record-info pin
* (MxStatusProxyRecordInfoPin): "pinned {library-guid} vM.m record {record-guid}" on
* success, or the failing call and HRESULT / exception type otherwise. Empty when the
* worker made no attempt (an older worker, or a runtime that never created the
* MXAccess COM object). Diagnostic only: the gateway logs it and shows it on the
* dashboard, and never gates readiness on it, because the pin is a cache warm-up.
* </pre>
*
* <code>string record_info_pin_status = 5;</code>
* @return This builder for chaining.
*/
public Builder clearRecordInfoPinStatus() {
recordInfoPinStatus_ = getDefaultInstance().getRecordInfoPinStatus();
bitField0_ = (bitField0_ & ~0x00000010);
onChanged();
return this;
}
/**
* <pre>
* Outcome of the worker's process-wide MXSTATUS_PROXY record-info pin
* (MxStatusProxyRecordInfoPin): "pinned {library-guid} vM.m record {record-guid}" on
* success, or the failing call and HRESULT / exception type otherwise. Empty when the
* worker made no attempt (an older worker, or a runtime that never created the
* MXAccess COM object). Diagnostic only: the gateway logs it and shows it on the
* dashboard, and never gates readiness on it, because the pin is a cache warm-up.
* </pre>
*
* <code>string record_info_pin_status = 5;</code>
* @param value The bytes for recordInfoPinStatus to set.
* @return This builder for chaining.
*/
public Builder setRecordInfoPinStatusBytes(
com.google.protobuf.ByteString value) {
if (value == null) { throw new NullPointerException(); }
checkByteStringIsUtf8(value);
recordInfoPinStatus_ = value;
bitField0_ |= 0x00000010;
onChanged();
return this;
}
// @@protoc_insertion_point(builder_scope:mxaccess_worker.v1.WorkerReady) // @@protoc_insertion_point(builder_scope:mxaccess_worker.v1.WorkerReady)
} }
@@ -12698,60 +12927,61 @@ public final class MxaccessWorker extends com.google.protobuf.GeneratedFile {
"ay_version\030\003 \001(\t\022\027\n\017max_frame_bytes\030\004 \001(" + "ay_version\030\003 \001(\t\022\027\n\017max_frame_bytes\030\004 \001(" +
"\r\"i\n\013WorkerHello\022\030\n\020protocol_version\030\001 \001" + "\r\"i\n\013WorkerHello\022\030\n\020protocol_version\030\001 \001" +
"(\r\022\r\n\005nonce\030\002 \001(\t\022\031\n\021worker_process_id\030\003" + "(\r\022\r\n\005nonce\030\002 \001(\t\022\031\n\021worker_process_id\030\003" +
" \001(\005\022\026\n\016worker_version\030\004 \001(\t\"\216\001\n\013WorkerR" + " \001(\005\022\026\n\016worker_version\030\004 \001(\t\"\256\001\n\013WorkerR" +
"eady\022\031\n\021worker_process_id\030\001 \001(\005\022\027\n\017mxacc" + "eady\022\031\n\021worker_process_id\030\001 \001(\005\022\027\n\017mxacc" +
"ess_progid\030\002 \001(\t\022\026\n\016mxaccess_clsid\030\003 \001(\t" + "ess_progid\030\002 \001(\t\022\026\n\016mxaccess_clsid\030\003 \001(\t" +
"\0223\n\017ready_timestamp\030\004 \001(\0132\032.google.proto" + "\0223\n\017ready_timestamp\030\004 \001(\0132\032.google.proto" +
"buf.Timestamp\"w\n\rWorkerCommand\022/\n\007comman" + "buf.Timestamp\022\036\n\026record_info_pin_status\030" +
"d\030\001 \001(\0132\036.mxaccess_gateway.v1.MxCommand\022" + "\005 \001(\t\"w\n\rWorkerCommand\022/\n\007command\030\001 \001(\0132" +
"5\n\021enqueue_timestamp\030\002 \001(\0132\032.google.prot" + "\036.mxaccess_gateway.v1.MxCommand\0225\n\021enque" +
"obuf.Timestamp\"\201\001\n\022WorkerCommandReply\0222\n" + "ue_timestamp\030\002 \001(\0132\032.google.protobuf.Tim" +
"\005reply\030\001 \001(\0132#.mxaccess_gateway.v1.MxCom" + "estamp\"\201\001\n\022WorkerCommandReply\0222\n\005reply\030\001" +
"mandReply\0227\n\023completed_timestamp\030\002 \001(\0132\032" + " \001(\0132#.mxaccess_gateway.v1.MxCommandRepl" +
".google.protobuf.Timestamp\"\036\n\014WorkerCanc" + "y\0227\n\023completed_timestamp\030\002 \001(\0132\032.google." +
"el\022\016\n\006reason\030\001 \001(\t\"Q\n\016WorkerShutdown\022/\n\014" + "protobuf.Timestamp\"\036\n\014WorkerCancel\022\016\n\006re" +
"grace_period\030\001 \001(\0132\031.google.protobuf.Dur" + "ason\030\001 \001(\t\"Q\n\016WorkerShutdown\022/\n\014grace_pe" +
"ation\022\016\n\006reason\030\002 \001(\t\"H\n\021WorkerShutdownA" + "riod\030\001 \001(\0132\031.google.protobuf.Duration\022\016\n" +
"ck\0223\n\006status\030\001 \001(\0132#.mxaccess_gateway.v1" + "\006reason\030\002 \001(\t\"H\n\021WorkerShutdownAck\0223\n\006st" +
".ProtocolStatus\":\n\013WorkerEvent\022+\n\005event\030" + "atus\030\001 \001(\0132#.mxaccess_gateway.v1.Protoco" +
"\001 \001(\0132\034.mxaccess_gateway.v1.MxEvent\"\245\002\n\017" + "lStatus\":\n\013WorkerEvent\022+\n\005event\030\001 \001(\0132\034." +
"WorkerHeartbeat\022\031\n\021worker_process_id\030\001 \001" + "mxaccess_gateway.v1.MxEvent\"\245\002\n\017WorkerHe" +
"(\005\022.\n\005state\030\002 \001(\0162\037.mxaccess_worker.v1.W" + "artbeat\022\031\n\021worker_process_id\030\001 \001(\005\022.\n\005st" +
"orkerState\022?\n\033last_sta_activity_timestam" + "ate\030\002 \001(\0162\037.mxaccess_worker.v1.WorkerSta" +
"p\030\003 \001(\0132\032.google.protobuf.Timestamp\022\035\n\025p" + "te\022?\n\033last_sta_activity_timestamp\030\003 \001(\0132" +
"ending_command_count\030\004 \001(\r\022\"\n\032outbound_e" + "\032.google.protobuf.Timestamp\022\035\n\025pending_c" +
"vent_queue_depth\030\005 \001(\r\022\033\n\023last_event_seq" + "ommand_count\030\004 \001(\r\022\"\n\032outbound_event_que" +
"uence\030\006 \001(\004\022&\n\036current_command_correlati" + "ue_depth\030\005 \001(\r\022\033\n\023last_event_sequence\030\006 " +
"on_id\030\007 \001(\t\"\364\001\n\013WorkerFault\0229\n\010category\030" + "\001(\004\022&\n\036current_command_correlation_id\030\007 " +
"\001 \001(\0162\'.mxaccess_worker.v1.WorkerFaultCa" + "\001(\t\"\364\001\n\013WorkerFault\0229\n\010category\030\001 \001(\0162\'." +
"tegory\022\026\n\016command_method\030\002 \001(\t\022\024\n\007hresul" + "mxaccess_worker.v1.WorkerFaultCategory\022\026" +
"t\030\003 \001(\005H\000\210\001\001\022\026\n\016exception_type\030\004 \001(\t\022\032\n\022" + "\n\016command_method\030\002 \001(\t\022\024\n\007hresult\030\003 \001(\005H" +
"diagnostic_message\030\005 \001(\t\022<\n\017protocol_sta" + "\000\210\001\001\022\026\n\016exception_type\030\004 \001(\t\022\032\n\022diagnost" +
"tus\030\006 \001(\0132#.mxaccess_gateway.v1.Protocol" + "ic_message\030\005 \001(\t\022<\n\017protocol_status\030\006 \001(" +
"StatusB\n\n\010_hresult*\227\002\n\013WorkerState\022\034\n\030WO" + "\0132#.mxaccess_gateway.v1.ProtocolStatusB\n" +
"RKER_STATE_UNSPECIFIED\020\000\022\031\n\025WORKER_STATE" + "\n\010_hresult*\227\002\n\013WorkerState\022\034\n\030WORKER_STA" +
"_STARTING\020\001\022\034\n\030WORKER_STATE_HANDSHAKING\020" + "TE_UNSPECIFIED\020\000\022\031\n\025WORKER_STATE_STARTIN" +
"\002\022!\n\035WORKER_STATE_INITIALIZING_STA\020\003\022\026\n\022" + "G\020\001\022\034\n\030WORKER_STATE_HANDSHAKING\020\002\022!\n\035WOR" +
"WORKER_STATE_READY\020\004\022\"\n\036WORKER_STATE_EXE" + "KER_STATE_INITIALIZING_STA\020\003\022\026\n\022WORKER_S" +
"CUTING_COMMAND\020\005\022\036\n\032WORKER_STATE_SHUTTIN" + "TATE_READY\020\004\022\"\n\036WORKER_STATE_EXECUTING_C" +
"G_DOWN\020\006\022\030\n\024WORKER_STATE_STOPPED\020\007\022\030\n\024WO" + "OMMAND\020\005\022\036\n\032WORKER_STATE_SHUTTING_DOWN\020\006" +
"RKER_STATE_FAULTED\020\010*\307\004\n\023WorkerFaultCate" + "\022\030\n\024WORKER_STATE_STOPPED\020\007\022\030\n\024WORKER_STA" +
"gory\022%\n!WORKER_FAULT_CATEGORY_UNSPECIFIE" + "TE_FAULTED\020\010*\307\004\n\023WorkerFaultCategory\022%\n!" +
"D\020\000\022+\n\'WORKER_FAULT_CATEGORY_INVALID_ARG" + "WORKER_FAULT_CATEGORY_UNSPECIFIED\020\000\022+\n\'W" +
"UMENTS\020\001\0227\n3WORKER_FAULT_CATEGORY_GATEWA" + "ORKER_FAULT_CATEGORY_INVALID_ARGUMENTS\020\001" +
"Y_AUTHENTICATION_FAILED\020\002\022+\n\'WORKER_FAUL" + "\0227\n3WORKER_FAULT_CATEGORY_GATEWAY_AUTHEN" +
"T_CATEGORY_PROTOCOL_MISMATCH\020\003\022,\n(WORKER" + "TICATION_FAILED\020\002\022+\n\'WORKER_FAULT_CATEGO" +
"_FAULT_CATEGORY_PROTOCOL_VIOLATION\020\004\022+\n\'" + "RY_PROTOCOL_MISMATCH\020\003\022,\n(WORKER_FAULT_C" +
"WORKER_FAULT_CATEGORY_PIPE_DISCONNECTED\020" + "ATEGORY_PROTOCOL_VIOLATION\020\004\022+\n\'WORKER_F" +
"\005\0222\n.WORKER_FAULT_CATEGORY_MXACCESS_CREA" + "AULT_CATEGORY_PIPE_DISCONNECTED\020\005\0222\n.WOR" +
"TION_FAILED\020\006\0221\n-WORKER_FAULT_CATEGORY_M" + "KER_FAULT_CATEGORY_MXACCESS_CREATION_FAI" +
"XACCESS_COMMAND_FAILED\020\007\022:\n6WORKER_FAULT" + "LED\020\006\0221\n-WORKER_FAULT_CATEGORY_MXACCESS_" +
"_CATEGORY_MXACCESS_EVENT_CONVERSION_FAIL" + "COMMAND_FAILED\020\007\022:\n6WORKER_FAULT_CATEGOR" +
"ED\020\010\022\"\n\036WORKER_FAULT_CATEGORY_STA_HUNG\020\t" + "Y_MXACCESS_EVENT_CONVERSION_FAILED\020\010\022\"\n\036" +
"\022(\n$WORKER_FAULT_CATEGORY_QUEUE_OVERFLOW" + "WORKER_FAULT_CATEGORY_STA_HUNG\020\t\022(\n$WORK" +
"\020\n\022*\n&WORKER_FAULT_CATEGORY_SHUTDOWN_TIM" + "ER_FAULT_CATEGORY_QUEUE_OVERFLOW\020\n\022*\n&WO" +
"EOUT\020\013B&\252\002#ZB.MOM.WW.MxGateway.Contracts" + "RKER_FAULT_CATEGORY_SHUTDOWN_TIMEOUT\020\013B&" +
".Protob\006proto3" "\252\002#ZB.MOM.WW.MxGateway.Contracts.Protob\006" +
"proto3"
}; };
descriptor = com.google.protobuf.Descriptors.FileDescriptor descriptor = com.google.protobuf.Descriptors.FileDescriptor
.internalBuildGeneratedFileFrom(descriptorData, .internalBuildGeneratedFileFrom(descriptorData,
@@ -12783,7 +13013,7 @@ public final class MxaccessWorker extends com.google.protobuf.GeneratedFile {
internal_static_mxaccess_worker_v1_WorkerReady_fieldAccessorTable = new internal_static_mxaccess_worker_v1_WorkerReady_fieldAccessorTable = new
com.google.protobuf.GeneratedMessage.FieldAccessorTable( com.google.protobuf.GeneratedMessage.FieldAccessorTable(
internal_static_mxaccess_worker_v1_WorkerReady_descriptor, internal_static_mxaccess_worker_v1_WorkerReady_descriptor,
new java.lang.String[] { "WorkerProcessId", "MxaccessProgid", "MxaccessClsid", "ReadyTimestamp", }); new java.lang.String[] { "WorkerProcessId", "MxaccessProgid", "MxaccessClsid", "ReadyTimestamp", "RecordInfoPinStatus", });
internal_static_mxaccess_worker_v1_WorkerCommand_descriptor = internal_static_mxaccess_worker_v1_WorkerCommand_descriptor =
getDescriptor().getMessageType(4); getDescriptor().getMessageType(4);
internal_static_mxaccess_worker_v1_WorkerCommand_fieldAccessorTable = new internal_static_mxaccess_worker_v1_WorkerCommand_fieldAccessorTable = new
@@ -27,7 +27,7 @@ from google.protobuf import timestamp_pb2 as google_dot_protobuf_dot_timestamp__
import mxaccess_gateway_pb2 as mxaccess__gateway__pb2 import mxaccess_gateway_pb2 as mxaccess__gateway__pb2
DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x15mxaccess_worker.proto\x12\x12mxaccess_worker.v1\x1a\x1egoogle/protobuf/duration.proto\x1a\x1fgoogle/protobuf/timestamp.proto\x1a\x16mxaccess_gateway.proto\"\x95\x06\n\x0eWorkerEnvelope\x12\x18\n\x10protocol_version\x18\x01 \x01(\r\x12\x12\n\nsession_id\x18\x02 \x01(\t\x12\x10\n\x08sequence\x18\x03 \x01(\x04\x12\x16\n\x0e\x63orrelation_id\x18\x04 \x01(\t\x12\x39\n\rgateway_hello\x18\n \x01(\x0b\x32 .mxaccess_worker.v1.GatewayHelloH\x00\x12\x37\n\x0cworker_hello\x18\x0b \x01(\x0b\x32\x1f.mxaccess_worker.v1.WorkerHelloH\x00\x12\x37\n\x0cworker_ready\x18\x0c \x01(\x0b\x32\x1f.mxaccess_worker.v1.WorkerReadyH\x00\x12;\n\x0eworker_command\x18\r \x01(\x0b\x32!.mxaccess_worker.v1.WorkerCommandH\x00\x12\x46\n\x14worker_command_reply\x18\x0e \x01(\x0b\x32&.mxaccess_worker.v1.WorkerCommandReplyH\x00\x12\x39\n\rworker_cancel\x18\x0f \x01(\x0b\x32 .mxaccess_worker.v1.WorkerCancelH\x00\x12=\n\x0fworker_shutdown\x18\x10 \x01(\x0b\x32\".mxaccess_worker.v1.WorkerShutdownH\x00\x12\x44\n\x13worker_shutdown_ack\x18\x11 \x01(\x0b\x32%.mxaccess_worker.v1.WorkerShutdownAckH\x00\x12\x37\n\x0cworker_event\x18\x12 \x01(\x0b\x32\x1f.mxaccess_worker.v1.WorkerEventH\x00\x12?\n\x10worker_heartbeat\x18\x13 \x01(\x0b\x32#.mxaccess_worker.v1.WorkerHeartbeatH\x00\x12\x37\n\x0cworker_fault\x18\x14 \x01(\x0b\x32\x1f.mxaccess_worker.v1.WorkerFaultH\x00\x42\x06\n\x04\x62ody\"s\n\x0cGatewayHello\x12\"\n\x1asupported_protocol_version\x18\x01 \x01(\r\x12\r\n\x05nonce\x18\x02 \x01(\t\x12\x17\n\x0fgateway_version\x18\x03 \x01(\t\x12\x17\n\x0fmax_frame_bytes\x18\x04 \x01(\r\"i\n\x0bWorkerHello\x12\x18\n\x10protocol_version\x18\x01 \x01(\r\x12\r\n\x05nonce\x18\x02 \x01(\t\x12\x19\n\x11worker_process_id\x18\x03 \x01(\x05\x12\x16\n\x0eworker_version\x18\x04 \x01(\t\"\x8e\x01\n\x0bWorkerReady\x12\x19\n\x11worker_process_id\x18\x01 \x01(\x05\x12\x17\n\x0fmxaccess_progid\x18\x02 \x01(\t\x12\x16\n\x0emxaccess_clsid\x18\x03 \x01(\t\x12\x33\n\x0fready_timestamp\x18\x04 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\"w\n\rWorkerCommand\x12/\n\x07\x63ommand\x18\x01 \x01(\x0b\x32\x1e.mxaccess_gateway.v1.MxCommand\x12\x35\n\x11\x65nqueue_timestamp\x18\x02 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\"\x81\x01\n\x12WorkerCommandReply\x12\x32\n\x05reply\x18\x01 \x01(\x0b\x32#.mxaccess_gateway.v1.MxCommandReply\x12\x37\n\x13\x63ompleted_timestamp\x18\x02 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\"\x1e\n\x0cWorkerCancel\x12\x0e\n\x06reason\x18\x01 \x01(\t\"Q\n\x0eWorkerShutdown\x12/\n\x0cgrace_period\x18\x01 \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x0e\n\x06reason\x18\x02 \x01(\t\"H\n\x11WorkerShutdownAck\x12\x33\n\x06status\x18\x01 \x01(\x0b\x32#.mxaccess_gateway.v1.ProtocolStatus\":\n\x0bWorkerEvent\x12+\n\x05\x65vent\x18\x01 \x01(\x0b\x32\x1c.mxaccess_gateway.v1.MxEvent\"\xa5\x02\n\x0fWorkerHeartbeat\x12\x19\n\x11worker_process_id\x18\x01 \x01(\x05\x12.\n\x05state\x18\x02 \x01(\x0e\x32\x1f.mxaccess_worker.v1.WorkerState\x12?\n\x1blast_sta_activity_timestamp\x18\x03 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12\x1d\n\x15pending_command_count\x18\x04 \x01(\r\x12\"\n\x1aoutbound_event_queue_depth\x18\x05 \x01(\r\x12\x1b\n\x13last_event_sequence\x18\x06 \x01(\x04\x12&\n\x1e\x63urrent_command_correlation_id\x18\x07 \x01(\t\"\xf4\x01\n\x0bWorkerFault\x12\x39\n\x08\x63\x61tegory\x18\x01 \x01(\x0e\x32\'.mxaccess_worker.v1.WorkerFaultCategory\x12\x16\n\x0e\x63ommand_method\x18\x02 \x01(\t\x12\x14\n\x07hresult\x18\x03 \x01(\x05H\x00\x88\x01\x01\x12\x16\n\x0e\x65xception_type\x18\x04 \x01(\t\x12\x1a\n\x12\x64iagnostic_message\x18\x05 \x01(\t\x12<\n\x0fprotocol_status\x18\x06 \x01(\x0b\x32#.mxaccess_gateway.v1.ProtocolStatusB\n\n\x08_hresult*\x97\x02\n\x0bWorkerState\x12\x1c\n\x18WORKER_STATE_UNSPECIFIED\x10\x00\x12\x19\n\x15WORKER_STATE_STARTING\x10\x01\x12\x1c\n\x18WORKER_STATE_HANDSHAKING\x10\x02\x12!\n\x1dWORKER_STATE_INITIALIZING_STA\x10\x03\x12\x16\n\x12WORKER_STATE_READY\x10\x04\x12\"\n\x1eWORKER_STATE_EXECUTING_COMMAND\x10\x05\x12\x1e\n\x1aWORKER_STATE_SHUTTING_DOWN\x10\x06\x12\x18\n\x14WORKER_STATE_STOPPED\x10\x07\x12\x18\n\x14WORKER_STATE_FAULTED\x10\x08*\xc7\x04\n\x13WorkerFaultCategory\x12%\n!WORKER_FAULT_CATEGORY_UNSPECIFIED\x10\x00\x12+\n\'WORKER_FAULT_CATEGORY_INVALID_ARGUMENTS\x10\x01\x12\x37\n3WORKER_FAULT_CATEGORY_GATEWAY_AUTHENTICATION_FAILED\x10\x02\x12+\n\'WORKER_FAULT_CATEGORY_PROTOCOL_MISMATCH\x10\x03\x12,\n(WORKER_FAULT_CATEGORY_PROTOCOL_VIOLATION\x10\x04\x12+\n\'WORKER_FAULT_CATEGORY_PIPE_DISCONNECTED\x10\x05\x12\x32\n.WORKER_FAULT_CATEGORY_MXACCESS_CREATION_FAILED\x10\x06\x12\x31\n-WORKER_FAULT_CATEGORY_MXACCESS_COMMAND_FAILED\x10\x07\x12:\n6WORKER_FAULT_CATEGORY_MXACCESS_EVENT_CONVERSION_FAILED\x10\x08\x12\"\n\x1eWORKER_FAULT_CATEGORY_STA_HUNG\x10\t\x12(\n$WORKER_FAULT_CATEGORY_QUEUE_OVERFLOW\x10\n\x12*\n&WORKER_FAULT_CATEGORY_SHUTDOWN_TIMEOUT\x10\x0b\x42&\xaa\x02#ZB.MOM.WW.MxGateway.Contracts.Protob\x06proto3') DESCRIPTOR = _descriptor_pool.Default().AddSerializedFile(b'\n\x15mxaccess_worker.proto\x12\x12mxaccess_worker.v1\x1a\x1egoogle/protobuf/duration.proto\x1a\x1fgoogle/protobuf/timestamp.proto\x1a\x16mxaccess_gateway.proto\"\x95\x06\n\x0eWorkerEnvelope\x12\x18\n\x10protocol_version\x18\x01 \x01(\r\x12\x12\n\nsession_id\x18\x02 \x01(\t\x12\x10\n\x08sequence\x18\x03 \x01(\x04\x12\x16\n\x0e\x63orrelation_id\x18\x04 \x01(\t\x12\x39\n\rgateway_hello\x18\n \x01(\x0b\x32 .mxaccess_worker.v1.GatewayHelloH\x00\x12\x37\n\x0cworker_hello\x18\x0b \x01(\x0b\x32\x1f.mxaccess_worker.v1.WorkerHelloH\x00\x12\x37\n\x0cworker_ready\x18\x0c \x01(\x0b\x32\x1f.mxaccess_worker.v1.WorkerReadyH\x00\x12;\n\x0eworker_command\x18\r \x01(\x0b\x32!.mxaccess_worker.v1.WorkerCommandH\x00\x12\x46\n\x14worker_command_reply\x18\x0e \x01(\x0b\x32&.mxaccess_worker.v1.WorkerCommandReplyH\x00\x12\x39\n\rworker_cancel\x18\x0f \x01(\x0b\x32 .mxaccess_worker.v1.WorkerCancelH\x00\x12=\n\x0fworker_shutdown\x18\x10 \x01(\x0b\x32\".mxaccess_worker.v1.WorkerShutdownH\x00\x12\x44\n\x13worker_shutdown_ack\x18\x11 \x01(\x0b\x32%.mxaccess_worker.v1.WorkerShutdownAckH\x00\x12\x37\n\x0cworker_event\x18\x12 \x01(\x0b\x32\x1f.mxaccess_worker.v1.WorkerEventH\x00\x12?\n\x10worker_heartbeat\x18\x13 \x01(\x0b\x32#.mxaccess_worker.v1.WorkerHeartbeatH\x00\x12\x37\n\x0cworker_fault\x18\x14 \x01(\x0b\x32\x1f.mxaccess_worker.v1.WorkerFaultH\x00\x42\x06\n\x04\x62ody\"s\n\x0cGatewayHello\x12\"\n\x1asupported_protocol_version\x18\x01 \x01(\r\x12\r\n\x05nonce\x18\x02 \x01(\t\x12\x17\n\x0fgateway_version\x18\x03 \x01(\t\x12\x17\n\x0fmax_frame_bytes\x18\x04 \x01(\r\"i\n\x0bWorkerHello\x12\x18\n\x10protocol_version\x18\x01 \x01(\r\x12\r\n\x05nonce\x18\x02 \x01(\t\x12\x19\n\x11worker_process_id\x18\x03 \x01(\x05\x12\x16\n\x0eworker_version\x18\x04 \x01(\t\"\xae\x01\n\x0bWorkerReady\x12\x19\n\x11worker_process_id\x18\x01 \x01(\x05\x12\x17\n\x0fmxaccess_progid\x18\x02 \x01(\t\x12\x16\n\x0emxaccess_clsid\x18\x03 \x01(\t\x12\x33\n\x0fready_timestamp\x18\x04 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12\x1e\n\x16record_info_pin_status\x18\x05 \x01(\t\"w\n\rWorkerCommand\x12/\n\x07\x63ommand\x18\x01 \x01(\x0b\x32\x1e.mxaccess_gateway.v1.MxCommand\x12\x35\n\x11\x65nqueue_timestamp\x18\x02 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\"\x81\x01\n\x12WorkerCommandReply\x12\x32\n\x05reply\x18\x01 \x01(\x0b\x32#.mxaccess_gateway.v1.MxCommandReply\x12\x37\n\x13\x63ompleted_timestamp\x18\x02 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\"\x1e\n\x0cWorkerCancel\x12\x0e\n\x06reason\x18\x01 \x01(\t\"Q\n\x0eWorkerShutdown\x12/\n\x0cgrace_period\x18\x01 \x01(\x0b\x32\x19.google.protobuf.Duration\x12\x0e\n\x06reason\x18\x02 \x01(\t\"H\n\x11WorkerShutdownAck\x12\x33\n\x06status\x18\x01 \x01(\x0b\x32#.mxaccess_gateway.v1.ProtocolStatus\":\n\x0bWorkerEvent\x12+\n\x05\x65vent\x18\x01 \x01(\x0b\x32\x1c.mxaccess_gateway.v1.MxEvent\"\xa5\x02\n\x0fWorkerHeartbeat\x12\x19\n\x11worker_process_id\x18\x01 \x01(\x05\x12.\n\x05state\x18\x02 \x01(\x0e\x32\x1f.mxaccess_worker.v1.WorkerState\x12?\n\x1blast_sta_activity_timestamp\x18\x03 \x01(\x0b\x32\x1a.google.protobuf.Timestamp\x12\x1d\n\x15pending_command_count\x18\x04 \x01(\r\x12\"\n\x1aoutbound_event_queue_depth\x18\x05 \x01(\r\x12\x1b\n\x13last_event_sequence\x18\x06 \x01(\x04\x12&\n\x1e\x63urrent_command_correlation_id\x18\x07 \x01(\t\"\xf4\x01\n\x0bWorkerFault\x12\x39\n\x08\x63\x61tegory\x18\x01 \x01(\x0e\x32\'.mxaccess_worker.v1.WorkerFaultCategory\x12\x16\n\x0e\x63ommand_method\x18\x02 \x01(\t\x12\x14\n\x07hresult\x18\x03 \x01(\x05H\x00\x88\x01\x01\x12\x16\n\x0e\x65xception_type\x18\x04 \x01(\t\x12\x1a\n\x12\x64iagnostic_message\x18\x05 \x01(\t\x12<\n\x0fprotocol_status\x18\x06 \x01(\x0b\x32#.mxaccess_gateway.v1.ProtocolStatusB\n\n\x08_hresult*\x97\x02\n\x0bWorkerState\x12\x1c\n\x18WORKER_STATE_UNSPECIFIED\x10\x00\x12\x19\n\x15WORKER_STATE_STARTING\x10\x01\x12\x1c\n\x18WORKER_STATE_HANDSHAKING\x10\x02\x12!\n\x1dWORKER_STATE_INITIALIZING_STA\x10\x03\x12\x16\n\x12WORKER_STATE_READY\x10\x04\x12\"\n\x1eWORKER_STATE_EXECUTING_COMMAND\x10\x05\x12\x1e\n\x1aWORKER_STATE_SHUTTING_DOWN\x10\x06\x12\x18\n\x14WORKER_STATE_STOPPED\x10\x07\x12\x18\n\x14WORKER_STATE_FAULTED\x10\x08*\xc7\x04\n\x13WorkerFaultCategory\x12%\n!WORKER_FAULT_CATEGORY_UNSPECIFIED\x10\x00\x12+\n\'WORKER_FAULT_CATEGORY_INVALID_ARGUMENTS\x10\x01\x12\x37\n3WORKER_FAULT_CATEGORY_GATEWAY_AUTHENTICATION_FAILED\x10\x02\x12+\n\'WORKER_FAULT_CATEGORY_PROTOCOL_MISMATCH\x10\x03\x12,\n(WORKER_FAULT_CATEGORY_PROTOCOL_VIOLATION\x10\x04\x12+\n\'WORKER_FAULT_CATEGORY_PIPE_DISCONNECTED\x10\x05\x12\x32\n.WORKER_FAULT_CATEGORY_MXACCESS_CREATION_FAILED\x10\x06\x12\x31\n-WORKER_FAULT_CATEGORY_MXACCESS_COMMAND_FAILED\x10\x07\x12:\n6WORKER_FAULT_CATEGORY_MXACCESS_EVENT_CONVERSION_FAILED\x10\x08\x12\"\n\x1eWORKER_FAULT_CATEGORY_STA_HUNG\x10\t\x12(\n$WORKER_FAULT_CATEGORY_QUEUE_OVERFLOW\x10\n\x12*\n&WORKER_FAULT_CATEGORY_SHUTDOWN_TIMEOUT\x10\x0b\x42&\xaa\x02#ZB.MOM.WW.MxGateway.Contracts.Protob\x06proto3')
_globals = globals() _globals = globals()
_builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals) _builder.BuildMessageAndEnumDescriptors(DESCRIPTOR, _globals)
@@ -35,10 +35,10 @@ _builder.BuildTopDescriptorsAndMessages(DESCRIPTOR, 'mxaccess_worker_pb2', _glob
if not _descriptor._USE_C_DESCRIPTORS: if not _descriptor._USE_C_DESCRIPTORS:
_globals['DESCRIPTOR']._loaded_options = None _globals['DESCRIPTOR']._loaded_options = None
_globals['DESCRIPTOR']._serialized_options = b'\252\002#ZB.MOM.WW.MxGateway.Contracts.Proto' _globals['DESCRIPTOR']._serialized_options = b'\252\002#ZB.MOM.WW.MxGateway.Contracts.Proto'
_globals['_WORKERSTATE']._serialized_start=2341 _globals['_WORKERSTATE']._serialized_start=2373
_globals['_WORKERSTATE']._serialized_end=2620 _globals['_WORKERSTATE']._serialized_end=2652
_globals['_WORKERFAULTCATEGORY']._serialized_start=2623 _globals['_WORKERFAULTCATEGORY']._serialized_start=2655
_globals['_WORKERFAULTCATEGORY']._serialized_end=3206 _globals['_WORKERFAULTCATEGORY']._serialized_end=3238
_globals['_WORKERENVELOPE']._serialized_start=135 _globals['_WORKERENVELOPE']._serialized_start=135
_globals['_WORKERENVELOPE']._serialized_end=924 _globals['_WORKERENVELOPE']._serialized_end=924
_globals['_GATEWAYHELLO']._serialized_start=926 _globals['_GATEWAYHELLO']._serialized_start=926
@@ -46,21 +46,21 @@ if not _descriptor._USE_C_DESCRIPTORS:
_globals['_WORKERHELLO']._serialized_start=1043 _globals['_WORKERHELLO']._serialized_start=1043
_globals['_WORKERHELLO']._serialized_end=1148 _globals['_WORKERHELLO']._serialized_end=1148
_globals['_WORKERREADY']._serialized_start=1151 _globals['_WORKERREADY']._serialized_start=1151
_globals['_WORKERREADY']._serialized_end=1293 _globals['_WORKERREADY']._serialized_end=1325
_globals['_WORKERCOMMAND']._serialized_start=1295 _globals['_WORKERCOMMAND']._serialized_start=1327
_globals['_WORKERCOMMAND']._serialized_end=1414 _globals['_WORKERCOMMAND']._serialized_end=1446
_globals['_WORKERCOMMANDREPLY']._serialized_start=1417 _globals['_WORKERCOMMANDREPLY']._serialized_start=1449
_globals['_WORKERCOMMANDREPLY']._serialized_end=1546 _globals['_WORKERCOMMANDREPLY']._serialized_end=1578
_globals['_WORKERCANCEL']._serialized_start=1548 _globals['_WORKERCANCEL']._serialized_start=1580
_globals['_WORKERCANCEL']._serialized_end=1578 _globals['_WORKERCANCEL']._serialized_end=1610
_globals['_WORKERSHUTDOWN']._serialized_start=1580 _globals['_WORKERSHUTDOWN']._serialized_start=1612
_globals['_WORKERSHUTDOWN']._serialized_end=1661 _globals['_WORKERSHUTDOWN']._serialized_end=1693
_globals['_WORKERSHUTDOWNACK']._serialized_start=1663 _globals['_WORKERSHUTDOWNACK']._serialized_start=1695
_globals['_WORKERSHUTDOWNACK']._serialized_end=1735 _globals['_WORKERSHUTDOWNACK']._serialized_end=1767
_globals['_WORKEREVENT']._serialized_start=1737 _globals['_WORKEREVENT']._serialized_start=1769
_globals['_WORKEREVENT']._serialized_end=1795 _globals['_WORKEREVENT']._serialized_end=1827
_globals['_WORKERHEARTBEAT']._serialized_start=1798 _globals['_WORKERHEARTBEAT']._serialized_start=1830
_globals['_WORKERHEARTBEAT']._serialized_end=2091 _globals['_WORKERHEARTBEAT']._serialized_end=2123
_globals['_WORKERFAULT']._serialized_start=2094 _globals['_WORKERFAULT']._serialized_start=2126
_globals['_WORKERFAULT']._serialized_end=2338 _globals['_WORKERFAULT']._serialized_end=2370
# @@protoc_insertion_point(module_scope) # @@protoc_insertion_point(module_scope)
+11
View File
@@ -65,6 +65,13 @@ message WorkerReady {
string mxaccess_progid = 2; string mxaccess_progid = 2;
string mxaccess_clsid = 3; string mxaccess_clsid = 3;
google.protobuf.Timestamp ready_timestamp = 4; google.protobuf.Timestamp ready_timestamp = 4;
// Outcome of the worker's process-wide MXSTATUS_PROXY record-info pin
// (MxStatusProxyRecordInfoPin): "pinned {library-guid} vM.m record {record-guid}" on
// success, or the failing call and HRESULT / exception type otherwise. Empty when the
// worker made no attempt (an older worker, or a runtime that never created the
// MXAccess COM object). Diagnostic only: the gateway logs it and shows it on the
// dashboard, and never gates readiness on it, because the pin is a cache warm-up.
string record_info_pin_status = 5;
} }
message WorkerCommand { message WorkerCommand {
@@ -127,6 +134,10 @@ enum WorkerState {
enum WorkerFaultCategory { enum WorkerFaultCategory {
WORKER_FAULT_CATEGORY_UNSPECIFIED = 0; WORKER_FAULT_CATEGORY_UNSPECIFIED = 0;
// Values 1 and 2 are reserved: never emitted. Bootstrap failures (bad
// arguments, gateway authentication) exit via WorkerExitCode before the pipe
// exists, so there is no channel to carry a fault. Names kept so the numbers
// are not reused.
WORKER_FAULT_CATEGORY_INVALID_ARGUMENTS = 1; WORKER_FAULT_CATEGORY_INVALID_ARGUMENTS = 1;
WORKER_FAULT_CATEGORY_GATEWAY_AUTHENTICATION_FAILED = 2; WORKER_FAULT_CATEGORY_GATEWAY_AUTHENTICATION_FAILED = 2;
WORKER_FAULT_CATEGORY_PROTOCOL_MISMATCH = 3; WORKER_FAULT_CATEGORY_PROTOCOL_MISMATCH = 3;
+15
View File
@@ -807,6 +807,21 @@ Worst-case on-disk log footprint is therefore about `retainedFileCountLimit x fi
Raise or lower these in the deployed `appsettings.json` (or via `Serilog__WriteTo__1__Args__*` environment Raise or lower these in the deployed `appsettings.json` (or via `Serilog__WriteTo__1__Args__*` environment
variables) to fit the host's disk budget. variables) to fit the host's disk budget.
## Runtime Settings (runtimeconfig)
Some settings belong to the .NET runtime rather than to `MxGateway:*` options. They ship in the
build through `RuntimeHostConfigurationOption` items in the Server csproj, which write them into
`ZB.MOM.WW.MxGateway.Server.runtimeconfig.json`. They therefore travel with every deploy and need
no NSSM environment variables.
| Setting | Shipped value | Why |
|---|---|---|
| `System.Threading.ThreadPool.UnfairSemaphoreSpinLimit` | `10` (runtime default `70`) | A pool worker that finds no work spins before parking, and past step 10 it calls `Sleep(0)` on every other step. The event relay queues several pool work items per event, so on a busy host those spins became ~93% of the gateway's context switches (WPR trace on a 4-CPU production host, ~240 events/s). With `10`, idle workers spin briefly and then park. Measured on the dev box at ~240 events/s: gateway CPU −15%. |
To override a setting on one host without rebuilding, use the environment-variable form, which
takes precedence over runtimeconfig: `DOTNET_ThreadPool_UnfairSemaphoreSpinLimit=<n>`. `0`
disables the spin entirely.
## Related Documentation ## Related Documentation
- [Gateway Process Detailed Design](./GatewayProcessDesign.md) - [Gateway Process Detailed Design](./GatewayProcessDesign.md)
+3 -1
View File
@@ -482,7 +482,9 @@ Shows:
- a session section: state badge, backend, client identity, client session name, - a session section: state badge, backend, client identity, client session name,
client correlation id (all redacted), open time, last activity, lease expiry, client correlation id (all redacted), open time, last activity, lease expiry,
events received, and last fault; events received, and last fault;
- a worker section: process id, worker state, and last heartbeat time; - a worker section: process id, worker state, last heartbeat time, and the
worker's `MXSTATUS_PROXY` record-info pin outcome from `WorkerReady` (`-` for an
older worker that does not report it);
- a "Recent events" table — the newest 50 events from the in-process mirror, with - a "Recent events" table — the newest 50 events from the in-process mirror, with
worker sequence, family, server handle, item handle, and status (no values), plus worker sequence, family, server handle, item handle, and status (no values), plus
a live/offline pill; gated by the session ACL, so a caller who may not observe the a live/offline pill; gated by the session ACL, so a caller who may not observe the
+9
View File
@@ -507,6 +507,15 @@ and nonce, waits for `WorkerReady`, and only then exposes `Ready` state. The
read loop starts after readiness so the handshake has a single owner for its read loop starts after readiness so the handshake has a single owner for its
ordered frames. ordered frames.
`WorkerReady.record_info_pin_status` carries the worker's `MXSTATUS_PROXY`
record-info pin outcome (see `MxAccessWorkerInstanceDesign.md`). The gateway does
not capture worker stderr, so `WorkerClient` is where operators see it: a status
starting `pinned ` is logged at Information, anything else at Warning, because an
unpinned worker pays a type-library reload on every data change. It is kept as
`IWorkerClient.RecordInfoPinStatus` and shown on the dashboard's session details
page. It never gates readiness, and an empty status (an older worker) is neither
logged nor shown.
### Read loop ### Read loop
The read loop: The read loop:
+51 -3
View File
@@ -479,6 +479,34 @@ Creation rules:
- Never marshal the raw COM object to pipe reader/writer threads. - Never marshal the raw COM object to pipe reader/writer threads.
- Capture COM creation HRESULT or exception details. - Capture COM creation HRESULT or exception details.
**Record-info pin.** Before the first `LMXProxyServerClass` is created,
`MxAccessComObjectFactory.Create()` calls `MxStatusProxyRecordInfoPin.EnsurePinned()`. Once per
process, this loads the LMXProxy type library and the `MXSTATUS_PROXY` record info, and holds
both references until exit. The GUIDs and version come from the interop assembly's metadata:
library `{77e896ec-b3e3-4de4-b96f-f32570164211}` v3.2 (`MXAccess32.tlb`), record
`{A162D9E4-9019-42F3-A087-79264007E542}`.
The pin exists because every `OnDataChange` carries a SAFEARRAY of `MXSTATUS_PROXY` records, and
`lmxproxy.dll` resolves that record type with `GetRecordInfoFromGuids` each time it builds one.
With no live reference, oleaut32 re-reads the registry and reloads the type library on every
call:
- about 1 ms per event on the dev box;
- worse under WOW64 registry redirection plus an EDR registry filter, where a production WPR trace
put it at ~70% of the STA's CPU.
While one `IRecordInfo` is held, the same call returns oleaut32's cached instance in under a
microsecond. Measured at ~240 events/s on the dev box, worker CPU fell from 67% to 10% of a core
with the pin alone.
The pin only warms a cache. It changes no MXAccess behaviour, and a failure is never fatal. The
outcome is in `MxStatusProxyRecordInfoPin.Status`. During the handshake `WorkerPipeSession` logs it
as `WorkerRecordInfoPin` and copies it into `WorkerReady.record_info_pin_status`, unless the runtime
already filled that field. The copy is the one operators see: the worker's own log goes to stderr,
which the gateway's launcher does not redirect. The gateway logs the reported status (Warning when
it is not a successful pin) and shows it on the dashboard's session details page; see
`GatewayProcessDesign.md`.
If COM creation fails, the worker should send a structured fault with: If COM creation fails, the worker should send a structured fault with:
- fault category, - fault category,
@@ -564,9 +592,29 @@ everything that arrived — the loop must therefore re-check `DrainFault()` and
re-drain after every wait, never treat a wake as "exactly one event". The 25 ms re-drain after every wait, never treat a wake as "exactly one event". The 25 ms
`EventDrainInterval` survives as the **fallback ceiling** on an unsignalled wait, `EventDrainInterval` survives as the **fallback ceiling** on an unsignalled wait,
not as a latency floor: an event arriving at an idle worker is framed at signal not as a latency floor: an event arriving at an idle worker is framed at signal
latency instead of waiting out a tick, an idle worker parks instead of waking 40 latency instead of waiting out a tick, and the interval only bounds how long the
times a second, and the interval only bounds how long the loop may sleep if some loop may sleep if some future path mutates the queue without signalling. An idle
future path mutates the queue without signalling. worker still wakes at that ceiling, but a wake on an empty queue is cheap.
The loop runs on a **dedicated background thread** (`mxgw-event-drain`), not on the
CLR thread pool:
- It parks in the blocking `IWorkerRuntimeSession.WaitForEvents`, which uses the
same coalesced signal and single-waiter contract as `WaitForEventsAsync`.
- It blocks on the frame writer's batch write.
- `RunEventDrainLoopAsync` starts the thread and returns a task that completes,
faults or cancels exactly as the loop does, so the message loop's `Task.WhenAny`
supervision is unchanged. Cancelling the session ends the task Canceled, which
teardown treats as a clean stop; an exception thrown on the thread faults the task
with that same exception, which ends `RunAsync` with it as the async loop did.
The reason is .NET Framework's thread pool. As an async loop, every enqueue's
semaphore release queued a continuation to the pool, and each pool worker that ran
one then spun in `SleepEx` before parking. A production WPR trace measured ~150
context switches per event and ~26% of the worker's CPU in that spin. With the
dedicated thread and the frame writer's synchronous flush, the steady-state event
path schedules no managed pool work. The overlapped pipe write still completes on
an I/O completion thread.
A single event whose envelope exceeds the negotiated frame maximum is A single event whose envelope exceeds the negotiated frame maximum is
**undeliverable end to end** — the pipe maximum sits only the envelope-overhead **undeliverable end to end** — the pipe maximum sits only the envelope-overhead
+15 -2
View File
@@ -90,6 +90,12 @@ Protocol violations throw `WorkerFrameProtocolException` with a
`WorkerFrameProtocolErrorCode` so callers can distinguish malformed frames, `WorkerFrameProtocolErrorCode` so callers can distinguish malformed frames,
oversized frames, protocol version mismatches, and session mismatches. oversized frames, protocol version mismatches, and session mismatches.
Fields added to a body without a protocol version bump must be ones both ends can
ignore. `WorkerReady.record_info_pin_status` (field 5) is the current example: it is
diagnostic only, so a newer worker talking to an older gateway is simply not
reported, and an older worker leaves it empty, which the gateway treats as "not
reported". Neither end gates readiness on it.
## Write Scheduling And Sequencing ## Write Scheduling And Sequencing
This section covers write scheduling (priority classes, enqueue-then-contend, This section covers write scheduling (priority classes, enqueue-then-contend,
@@ -135,7 +141,7 @@ Two failure shapes are distinguished during a drain pass:
Flushes are coalesced across a *run of same-class frames* inside a drain Flushes are coalesced across a *run of same-class frames* inside a drain
pass: each frame in the run is written to the stream without an individual pass: each frame in the run is written to the stream without an individual
flush, then one `FlushAsync` runs — at the end of the pass, and additionally flush, then one flush runs — at the end of the pass, and additionally
at every control-to-event boundary — and only then does every at every control-to-event boundary — and only then does every
successfully-written frame of that run resolve its completion. A caller's successfully-written frame of that run resolve its completion. A caller's
`WriteAsync` therefore still does not complete until its bytes are both `WriteAsync` therefore still does not complete until its bytes are both
@@ -166,11 +172,18 @@ end-of-pass flush, and additionally fails the event frame the drain had
already claimed off its queue (nothing else would ever complete it) along already claimed off its queue (nothing else would ever complete it) along
with every frame still queued. with every frame still queued.
The flush is a synchronous `Stream.Flush()`, not `FlushAsync`. `PipeStream` does not
override `FlushAsync`, so on .NET Framework the base implementation runs `Flush()`
through `Task.Factory.StartNew`. That is one thread-pool work item per drain pass for
a `PipeStream.Flush` that does no I/O, because it never calls `FlushFileBuffers`.
Calling `Flush()` inline keeps the same written-and-flushed contract for any stream.
Note the ordering all of this implies at the peer: the frames reach the pipe Note the ordering all of this implies at the peer: the frames reach the pipe
before the flush that follows them, so the gateway can read a whole batch before the flush that follows them, so the gateway can read a whole batch
while the writer has not yet flushed it. Anything observing the flush itself while the writer has not yet flushed it. Anything observing the flush itself
(a test counting flushes, for instance) must wait for the flush, not infer it (a test counting flushes, for instance) must wait for the flush, not infer it
from frames arriving. The event drain loop (`WorkerPipeSession.RunEventDrainLoopAsync`) from frames arriving. The event drain loop (`WorkerPipeSession.RunEventDrainLoopAsync`, which
runs on a dedicated thread; see `MxAccessWorkerInstanceDesign.md`)
submits a whole drained event batch through `WriteBatchAsync`, which enqueues submits a whole drained event batch through `WriteBatchAsync`, which enqueues
every frame under one `_gate` acquisition, takes the write lock once, and every frame under one `_gate` acquisition, takes the write lock once, and
drains them together, so a burst of N events costs one flush rather than N — drains them together, so a burst of N events costs one flush rather than N —
@@ -0,0 +1,88 @@
# 2026-11-22: PROD install of MxAccessGateway `e74918cc` on wonder-par-vp03 / wonder-par-vp04 (site-par)
Owner: "keep checking par-vp04 every minute. when its ready then execute the par server updates (both)". This follows the
2026-11-20 app-box install ([`2026-11-20-prod-e74918cc.md`](2026-11-20-prod-e74918cc.md)); the build, stage pack and env
are the same. Driven from the Mac with the `winrm` CLI (profiles `parvp03`/`parvp04`, NAM\adm_dohertj2). No firewall changes, and no
ScadaBridge service was touched.
## Gate: WinPlatform / MXAccess on the PAR boxes
Checked over SMB only.
- **par-vp03** already had `LmxProxy.dll` at 12:0xZ: 318,496 bytes, sha256 `bd75a2fd…7ef15459`, the same file as vp03. Its
`ArchestrA\Framework\Bin` held the full platform runtime (102 entries).
- **par-vp04** started with 49 entries and no MXAccess. It was polled every minute. `LmxProxy.dll` appeared at about
12:19Z (101 entries), and the folder was stable at 102 entries from 12:21Z. The poller declared it ready at 12:23:45Z
with the same sha256 `bd75a2fde222…`.
## Stage fixes (by SMB, before anything ran)
- **PATCH 4 (`Install-MxGatewayService.ps1`, nssm status probe under `Continue`; mxaccessgw#234):** both PAR boxes still had
the PATCH 3 copy (`11a23e9f…`). The patched copy (`fb6f0f47…`) was pushed, the old one kept as
`Install-MxGatewayService.ps1.patch3`, and the `SHA256SUMS.txt` line updated on both boxes.
- **Value files:** `_values\mxgw-<box>\` on each box held three files, all from the Keychain, never printed and never on argv:
- `MxGateway__ApiKeyPepper.txt`: `zb-prod/mxgw-pepper`, the same as vp03/vp04;
- `MxGateway__Galaxy__ConnectionString.txt`:
`Server=wonder-app-vp03.zmr.zimmer.com;Database=ZB;Integrated Security=True;TrustServerCertificate=True;Encrypt=False;`;
- the LDAP seed file `nam-ldap-mxgw.txt`.
- **API-key store:** vp03's gateway could not be stopped for a clean copy, because site-app runs on it. So vp04's
`gateway-auth.db` (+wal/shm) was pulled live; it is vp03's store as copied on 2026-11-20. Checked on the Mac:
- consolidated (the WAL held 0 frames);
- `integrity_check` ok;
- one key, `scadabridge-prod`, scopes `events:read invoke:read invoke:write metadata:read session:close session:open`,
not revoked;
- single-file sha256 `3c5705d7…43bffe55`.
## Install (UTC)
| Step | par-vp03 | par-vp04 |
|---|---|---|
| dry | 12:06:28 DONE (LmxProxy `6000.0045.2657.1` / `BD75A2FDE222`, 38 env entries, Server and Worker `0.1.2+e74918cc`, one benign WorkerDir WARN); `dry\` removed | 12:23:54 DONE, same output; `dry\` removed |
| real | 12:24:00 DONE; KEK generated `AA55B221`; service `MxAccessGw` Stopped, Automatic, LocalSystem | 12:24:09 DONE; KEK `01F9EE28`; Stopped, Automatic, LocalSystem |
| seed | 12:24:21 `nam-ldap-svc-password` → `data\secrets.db`, `readback-match=True`, value file removed | 12:24:26 same |
| key store | 12:24:25 `data\gateway-auth.db` = `3C5705D78769` (no earlier store existed) | 12:24:30 same |
| start | 12:24:35; Running; listening on 5120 and 5130; stderr empty | 12:26:11; same |
| `/health/ready` | **503**: auth-store Healthy, worker-executable Healthy, **galaxy-sql Unhealthy** | **503**, same |
**galaxy-sql: expected, same cause as vp04 (scadaproj#60).** The log shows
`Login failed for user 'ZMR\WONDER-PAR-VP03$'` / `'ZMR\WONDER-PAR-VP04$'`, `Error Number:4060`. The machine accounts reach
vp03's SQL Server but have no access to `ZB`. The gateway retries every 30 s and turns ready without a restart once the DBA
grants it (T-SQL on #60). MXAccess sessions do not depend on it.
## Consumer: ScadaBridge site-par
The site-par node on par-vp03 logged `[MxGateway] Reconnected successfully on "Primary" endpoint` → `Entering Connected
state` at **12:24:56Z**, 20 s after par-vp03's gateway started. There was no ScadaBridge change; the copied store makes the
existing `scadabridge-prod` token valid. (A no-op `deploy artifacts --site-id 1` at 12:29Z applied nothing; it was not needed.)
## Unrelated event in the same window: app-box services restarted at 12:31Z
Every ScadaBridge and mxgw service on **wonder-app-vp03 and vp04** (centrals, site-app nodes, mxgw) received a process
termination ("CLR termination hook") at 12:31:03–12:31:43Z. They came back at about 12:34:30Z. The central pair re-formed
(vp03 active), and all health endpoints were 200 by 12:35:58Z. Nothing in this run touched those boxes (SMB reads only). The
pattern looks like an external restart or reboot of both app boxes. The cause was not investigated: that would need
remote diagnostics, which the deploy-only WinRM scope does not cover.
Separately, at 10:51Z vp03 stalled for about 47.6 s ("heartbeat delayed 47592 ms", which looks like a VM freeze). vp04
downed it under `leader-auto-down`, and vp03 restarted with exit 75 as designed.
After the restart, site-app's `MxGateway` connection was not rebuilt. This is by design: on boot the site builds only the
connections a deployed instance references, and PROD has no instances yet. `deploy artifacts --site-id 2` at 12:43:13Z
rebuilt it: **Connected at 12:43:17Z**.
## Clean-up
- `_values\mxgw-par*`, `_values\mxgw-store` and the seed file were deleted on both boxes (verified; only the empty `_values\seed` folder is left).
- The local value files and key-store copies on the Mac were deleted.
## Open
- **scadaproj#60:** `ZB` read access for `ZMR\WONDER-APP-VP04$`, `ZMR\WONDER-PAR-VP03$` and `ZMR\WONDER-PAR-VP04$`.
- **Alarms are off on all four gateways** until the owner picks an area.
- **mxaccessgw#234:** the repo copy of the install script still needs PATCH 4.
## Rollback
On each PAR box: `nssm stop MxAccessGw; nssm remove MxAccessGw confirm`, then delete `E:\ApiInstall\MxAccessGateway` and
`C:\ProgramData\ZB\kek\mxgateway.kek`. site-par's connection then goes back to retrying. To serve it from the app-box
gateways instead: `data-connection update --id 1` with `10.220.153.205:5120` / `10.220.153.210:5120`, then
`deploy artifacts --site-id 1`.
+31 -7
View File
@@ -837,16 +837,30 @@ STA thread
MXAccess event handler on STA MXAccess event handler on STA
-> convert event args to WorkerEvent -> convert event args to WorkerEvent
-> enqueue outbound event -> enqueue outbound event, signal the drain thread
pipe writer thread event drain thread (mxgw-event-drain, dedicated, not thread pool)
-> dequeue replies/events -> park on the event queue's wake signal
-> write framed protobuf messages -> drain a batch of WorkerEvents
-> write them as one framed batch, flushed synchronously
command replies and heartbeats
-> written through the same frame writer, which orders control frames
ahead of queued events
``` ```
Do not block the STA on pipe writes or gRPC calls. The STA should enqueue Do not block the STA on pipe writes or gRPC calls. The STA should enqueue
results/events and return to pumping messages. results/events and return to pumping messages.
The event drain loop has its own background thread because of the .NET
Framework thread pool. As an async loop, every event woke a pool worker, and
each one spun before parking again; a production trace put that spin at about a
quarter of the worker's CPU at ~240 events/s. The dedicated thread parks on the
signal, and with a synchronous flush the steady-state event path queues no pool
work. The drain loop's task still completes, faults or cancels exactly as the
loop does, so the session's supervision of it is unchanged. See
`docs/MxAccessWorkerInstanceDesign.md`.
### Message Pump ### Message Pump
The STA loop must pump Windows messages and service command work. A typical The STA loop must pump Windows messages and service command work. A typical
@@ -878,9 +892,19 @@ Worker startup:
1. set apartment state to STA, 1. set apartment state to STA,
2. initialize COM on the STA, 2. initialize COM on the STA,
3. instantiate `LMXProxyServerClass` or the installed MXAccess interop class, 3. pin the LMXProxy type library and the `MXSTATUS_PROXY` record info for the
4. attach event handlers, process lifetime (once per process, best effort),
5. send `WorkerReady`. 4. instantiate `LMXProxyServerClass` or the installed MXAccess interop class,
5. attach event handlers,
6. send `WorkerReady`, carrying the pin outcome in `record_info_pin_status`.
The pin exists because `lmxproxy.dll` looks up the `MXSTATUS_PROXY` record type
for every `OnDataChange`. With no live reference to it, oleaut32 re-reads the
registry and reloads the type library on each call, which a production trace put
at about 70% of the STA's CPU. Holding one reference makes the lookup a cache
hit. A failed pin is never fatal, since MXAccess behaves the same either way, but
the gateway logs it as a warning and shows it on the dashboard's session details
page, because the worker's own stderr is not captured.
Worker shutdown: Worker shutdown:
@@ -49,61 +49,62 @@ namespace ZB.MOM.WW.MxGateway.Contracts.Proto {
"CRIXCg9nYXRld2F5X3ZlcnNpb24YAyABKAkSFwoPbWF4X2ZyYW1lX2J5dGVz", "CRIXCg9nYXRld2F5X3ZlcnNpb24YAyABKAkSFwoPbWF4X2ZyYW1lX2J5dGVz",
"GAQgASgNImkKC1dvcmtlckhlbGxvEhgKEHByb3RvY29sX3ZlcnNpb24YASAB", "GAQgASgNImkKC1dvcmtlckhlbGxvEhgKEHByb3RvY29sX3ZlcnNpb24YASAB",
"KA0SDQoFbm9uY2UYAiABKAkSGQoRd29ya2VyX3Byb2Nlc3NfaWQYAyABKAUS", "KA0SDQoFbm9uY2UYAiABKAkSGQoRd29ya2VyX3Byb2Nlc3NfaWQYAyABKAUS",
"FgoOd29ya2VyX3ZlcnNpb24YBCABKAkijgEKC1dvcmtlclJlYWR5EhkKEXdv", "FgoOd29ya2VyX3ZlcnNpb24YBCABKAkirgEKC1dvcmtlclJlYWR5EhkKEXdv",
"cmtlcl9wcm9jZXNzX2lkGAEgASgFEhcKD214YWNjZXNzX3Byb2dpZBgCIAEo", "cmtlcl9wcm9jZXNzX2lkGAEgASgFEhcKD214YWNjZXNzX3Byb2dpZBgCIAEo",
"CRIWCg5teGFjY2Vzc19jbHNpZBgDIAEoCRIzCg9yZWFkeV90aW1lc3RhbXAY", "CRIWCg5teGFjY2Vzc19jbHNpZBgDIAEoCRIzCg9yZWFkeV90aW1lc3RhbXAY",
"BCABKAsyGi5nb29nbGUucHJvdG9idWYuVGltZXN0YW1wIncKDVdvcmtlckNv", "BCABKAsyGi5nb29nbGUucHJvdG9idWYuVGltZXN0YW1wEh4KFnJlY29yZF9p",
"bW1hbmQSLwoHY29tbWFuZBgBIAEoCzIeLm14YWNjZXNzX2dhdGV3YXkudjEu", "bmZvX3Bpbl9zdGF0dXMYBSABKAkidwoNV29ya2VyQ29tbWFuZBIvCgdjb21t",
"TXhDb21tYW5kEjUKEWVucXVldWVfdGltZXN0YW1wGAIgASgLMhouZ29vZ2xl", "YW5kGAEgASgLMh4ubXhhY2Nlc3NfZ2F0ZXdheS52MS5NeENvbW1hbmQSNQoR",
"LnByb3RvYnVmLlRpbWVzdGFtcCKBAQoSV29ya2VyQ29tbWFuZFJlcGx5EjIK", "ZW5xdWV1ZV90aW1lc3RhbXAYAiABKAsyGi5nb29nbGUucHJvdG9idWYuVGlt",
"BXJlcGx5GAEgASgLMiMubXhhY2Nlc3NfZ2F0ZXdheS52MS5NeENvbW1hbmRS", "ZXN0YW1wIoEBChJXb3JrZXJDb21tYW5kUmVwbHkSMgoFcmVwbHkYASABKAsy",
"ZXBseRI3ChNjb21wbGV0ZWRfdGltZXN0YW1wGAIgASgLMhouZ29vZ2xlLnBy", "Iy5teGFjY2Vzc19nYXRld2F5LnYxLk14Q29tbWFuZFJlcGx5EjcKE2NvbXBs",
"b3RvYnVmLlRpbWVzdGFtcCIeCgxXb3JrZXJDYW5jZWwSDgoGcmVhc29uGAEg", "ZXRlZF90aW1lc3RhbXAYAiABKAsyGi5nb29nbGUucHJvdG9idWYuVGltZXN0",
"ASgJIlEKDldvcmtlclNodXRkb3duEi8KDGdyYWNlX3BlcmlvZBgBIAEoCzIZ", "YW1wIh4KDFdvcmtlckNhbmNlbBIOCgZyZWFzb24YASABKAkiUQoOV29ya2Vy",
"Lmdvb2dsZS5wcm90b2J1Zi5EdXJhdGlvbhIOCgZyZWFzb24YAiABKAkiSAoR", "U2h1dGRvd24SLwoMZ3JhY2VfcGVyaW9kGAEgASgLMhkuZ29vZ2xlLnByb3Rv",
"V29ya2VyU2h1dGRvd25BY2sSMwoGc3RhdHVzGAEgASgLMiMubXhhY2Nlc3Nf", "YnVmLkR1cmF0aW9uEg4KBnJlYXNvbhgCIAEoCSJIChFXb3JrZXJTaHV0ZG93",
"Z2F0ZXdheS52MS5Qcm90b2NvbFN0YXR1cyI6CgtXb3JrZXJFdmVudBIrCgVl", "bkFjaxIzCgZzdGF0dXMYASABKAsyIy5teGFjY2Vzc19nYXRld2F5LnYxLlBy",
"dmVudBgBIAEoCzIcLm14YWNjZXNzX2dhdGV3YXkudjEuTXhFdmVudCKlAgoP", "b3RvY29sU3RhdHVzIjoKC1dvcmtlckV2ZW50EisKBWV2ZW50GAEgASgLMhwu",
"V29ya2VySGVhcnRiZWF0EhkKEXdvcmtlcl9wcm9jZXNzX2lkGAEgASgFEi4K", "bXhhY2Nlc3NfZ2F0ZXdheS52MS5NeEV2ZW50IqUCCg9Xb3JrZXJIZWFydGJl",
"BXN0YXRlGAIgASgOMh8ubXhhY2Nlc3Nfd29ya2VyLnYxLldvcmtlclN0YXRl", "YXQSGQoRd29ya2VyX3Byb2Nlc3NfaWQYASABKAUSLgoFc3RhdGUYAiABKA4y",
"Ej8KG2xhc3Rfc3RhX2FjdGl2aXR5X3RpbWVzdGFtcBgDIAEoCzIaLmdvb2ds", "Hy5teGFjY2Vzc193b3JrZXIudjEuV29ya2VyU3RhdGUSPwobbGFzdF9zdGFf",
"ZS5wcm90b2J1Zi5UaW1lc3RhbXASHQoVcGVuZGluZ19jb21tYW5kX2NvdW50", "YWN0aXZpdHlfdGltZXN0YW1wGAMgASgLMhouZ29vZ2xlLnByb3RvYnVmLlRp",
"GAQgASgNEiIKGm91dGJvdW5kX2V2ZW50X3F1ZXVlX2RlcHRoGAUgASgNEhsK", "bWVzdGFtcBIdChVwZW5kaW5nX2NvbW1hbmRfY291bnQYBCABKA0SIgoab3V0",
"E2xhc3RfZXZlbnRfc2VxdWVuY2UYBiABKAQSJgoeY3VycmVudF9jb21tYW5k", "Ym91bmRfZXZlbnRfcXVldWVfZGVwdGgYBSABKA0SGwoTbGFzdF9ldmVudF9z",
"X2NvcnJlbGF0aW9uX2lkGAcgASgJIvQBCgtXb3JrZXJGYXVsdBI5CghjYXRl", "ZXF1ZW5jZRgGIAEoBBImCh5jdXJyZW50X2NvbW1hbmRfY29ycmVsYXRpb25f",
"Z29yeRgBIAEoDjInLm14YWNjZXNzX3dvcmtlci52MS5Xb3JrZXJGYXVsdENh", "aWQYByABKAki9AEKC1dvcmtlckZhdWx0EjkKCGNhdGVnb3J5GAEgASgOMicu",
"dGVnb3J5EhYKDmNvbW1hbmRfbWV0aG9kGAIgASgJEhQKB2hyZXN1bHQYAyAB", "bXhhY2Nlc3Nfd29ya2VyLnYxLldvcmtlckZhdWx0Q2F0ZWdvcnkSFgoOY29t",
"KAVIAIgBARIWCg5leGNlcHRpb25fdHlwZRgEIAEoCRIaChJkaWFnbm9zdGlj", "bWFuZF9tZXRob2QYAiABKAkSFAoHaHJlc3VsdBgDIAEoBUgAiAEBEhYKDmV4",
"X21lc3NhZ2UYBSABKAkSPAoPcHJvdG9jb2xfc3RhdHVzGAYgASgLMiMubXhh", "Y2VwdGlvbl90eXBlGAQgASgJEhoKEmRpYWdub3N0aWNfbWVzc2FnZRgFIAEo",
"Y2Nlc3NfZ2F0ZXdheS52MS5Qcm90b2NvbFN0YXR1c0IKCghfaHJlc3VsdCqX", "CRI8Cg9wcm90b2NvbF9zdGF0dXMYBiABKAsyIy5teGFjY2Vzc19nYXRld2F5",
"AgoLV29ya2VyU3RhdGUSHAoYV09SS0VSX1NUQVRFX1VOU1BFQ0lGSUVEEAAS", "LnYxLlByb3RvY29sU3RhdHVzQgoKCF9ocmVzdWx0KpcCCgtXb3JrZXJTdGF0",
"GQoVV09SS0VSX1NUQVRFX1NUQVJUSU5HEAESHAoYV09SS0VSX1NUQVRFX0hB", "ZRIcChhXT1JLRVJfU1RBVEVfVU5TUEVDSUZJRUQQABIZChVXT1JLRVJfU1RB",
"TkRTSEFLSU5HEAISIQodV09SS0VSX1NUQVRFX0lOSVRJQUxJWklOR19TVEEQ", "VEVfU1RBUlRJTkcQARIcChhXT1JLRVJfU1RBVEVfSEFORFNIQUtJTkcQAhIh",
"AxIWChJXT1JLRVJfU1RBVEVfUkVBRFkQBBIiCh5XT1JLRVJfU1RBVEVfRVhF", "Ch1XT1JLRVJfU1RBVEVfSU5JVElBTElaSU5HX1NUQRADEhYKEldPUktFUl9T",
"Q1VUSU5HX0NPTU1BTkQQBRIeChpXT1JLRVJfU1RBVEVfU0hVVFRJTkdfRE9X", "VEFURV9SRUFEWRAEEiIKHldPUktFUl9TVEFURV9FWEVDVVRJTkdfQ09NTUFO",
"ThAGEhgKFFdPUktFUl9TVEFURV9TVE9QUEVEEAcSGAoUV09SS0VSX1NUQVRF", "RBAFEh4KGldPUktFUl9TVEFURV9TSFVUVElOR19ET1dOEAYSGAoUV09SS0VS",
"X0ZBVUxURUQQCCrHBAoTV29ya2VyRmF1bHRDYXRlZ29yeRIlCiFXT1JLRVJf", "X1NUQVRFX1NUT1BQRUQQBxIYChRXT1JLRVJfU1RBVEVfRkFVTFRFRBAIKscE",
"RkFVTFRfQ0FURUdPUllfVU5TUEVDSUZJRUQQABIrCidXT1JLRVJfRkFVTFRf", "ChNXb3JrZXJGYXVsdENhdGVnb3J5EiUKIVdPUktFUl9GQVVMVF9DQVRFR09S",
"Q0FURUdPUllfSU5WQUxJRF9BUkdVTUVOVFMQARI3CjNXT1JLRVJfRkFVTFRf", "WV9VTlNQRUNJRklFRBAAEisKJ1dPUktFUl9GQVVMVF9DQVRFR09SWV9JTlZB",
"Q0FURUdPUllfR0FURVdBWV9BVVRIRU5USUNBVElPTl9GQUlMRUQQAhIrCidX", "TElEX0FSR1VNRU5UUxABEjcKM1dPUktFUl9GQVVMVF9DQVRFR09SWV9HQVRF",
"T1JLRVJfRkFVTFRfQ0FURUdPUllfUFJPVE9DT0xfTUlTTUFUQ0gQAxIsCihX", "V0FZX0FVVEhFTlRJQ0FUSU9OX0ZBSUxFRBACEisKJ1dPUktFUl9GQVVMVF9D",
"T1JLRVJfRkFVTFRfQ0FURUdPUllfUFJPVE9DT0xfVklPTEFUSU9OEAQSKwon", "QVRFR09SWV9QUk9UT0NPTF9NSVNNQVRDSBADEiwKKFdPUktFUl9GQVVMVF9D",
"V09SS0VSX0ZBVUxUX0NBVEVHT1JZX1BJUEVfRElTQ09OTkVDVEVEEAUSMgou", "QVRFR09SWV9QUk9UT0NPTF9WSU9MQVRJT04QBBIrCidXT1JLRVJfRkFVTFRf",
"V09SS0VSX0ZBVUxUX0NBVEVHT1JZX01YQUNDRVNTX0NSRUFUSU9OX0ZBSUxF", "Q0FURUdPUllfUElQRV9ESVNDT05ORUNURUQQBRIyCi5XT1JLRVJfRkFVTFRf",
"RBAGEjEKLVdPUktFUl9GQVVMVF9DQVRFR09SWV9NWEFDQ0VTU19DT01NQU5E", "Q0FURUdPUllfTVhBQ0NFU1NfQ1JFQVRJT05fRkFJTEVEEAYSMQotV09SS0VS",
"X0ZBSUxFRBAHEjoKNldPUktFUl9GQVVMVF9DQVRFR09SWV9NWEFDQ0VTU19F", "X0ZBVUxUX0NBVEVHT1JZX01YQUNDRVNTX0NPTU1BTkRfRkFJTEVEEAcSOgo2",
"VkVOVF9DT05WRVJTSU9OX0ZBSUxFRBAIEiIKHldPUktFUl9GQVVMVF9DQVRF", "V09SS0VSX0ZBVUxUX0NBVEVHT1JZX01YQUNDRVNTX0VWRU5UX0NPTlZFUlNJ",
"R09SWV9TVEFfSFVORxAJEigKJFdPUktFUl9GQVVMVF9DQVRFR09SWV9RVUVV", "T05fRkFJTEVEEAgSIgoeV09SS0VSX0ZBVUxUX0NBVEVHT1JZX1NUQV9IVU5H",
"RV9PVkVSRkxPVxAKEioKJldPUktFUl9GQVVMVF9DQVRFR09SWV9TSFVURE9X", "EAkSKAokV09SS0VSX0ZBVUxUX0NBVEVHT1JZX1FVRVVFX09WRVJGTE9XEAoS",
"Tl9USU1FT1VUEAtCJqoCI1pCLk1PTS5XVy5NeEdhdGV3YXkuQ29udHJhY3Rz", "KgomV09SS0VSX0ZBVUxUX0NBVEVHT1JZX1NIVVRET1dOX1RJTUVPVVQQC0Im",
"LlByb3RvYgZwcm90bzM=")); "qgIjWkIuTU9NLldXLk14R2F0ZXdheS5Db250cmFjdHMuUHJvdG9iBnByb3Rv",
"Mw=="));
descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData, descriptor = pbr::FileDescriptor.FromGeneratedCode(descriptorData,
new pbr::FileDescriptor[] { global::Google.Protobuf.WellKnownTypes.DurationReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.TimestampReflection.Descriptor, global::ZB.MOM.WW.MxGateway.Contracts.Proto.MxaccessGatewayReflection.Descriptor, }, new pbr::FileDescriptor[] { global::Google.Protobuf.WellKnownTypes.DurationReflection.Descriptor, global::Google.Protobuf.WellKnownTypes.TimestampReflection.Descriptor, global::ZB.MOM.WW.MxGateway.Contracts.Proto.MxaccessGatewayReflection.Descriptor, },
new pbr::GeneratedClrTypeInfo(new[] {typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerState), typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerFaultCategory), }, null, new pbr::GeneratedClrTypeInfo[] { new pbr::GeneratedClrTypeInfo(new[] {typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerState), typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerFaultCategory), }, null, new pbr::GeneratedClrTypeInfo[] {
new pbr::GeneratedClrTypeInfo(typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerEnvelope), global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerEnvelope.Parser, new[]{ "ProtocolVersion", "SessionId", "Sequence", "CorrelationId", "GatewayHello", "WorkerHello", "WorkerReady", "WorkerCommand", "WorkerCommandReply", "WorkerCancel", "WorkerShutdown", "WorkerShutdownAck", "WorkerEvent", "WorkerHeartbeat", "WorkerFault" }, new[]{ "Body" }, null, null, null), new pbr::GeneratedClrTypeInfo(typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerEnvelope), global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerEnvelope.Parser, new[]{ "ProtocolVersion", "SessionId", "Sequence", "CorrelationId", "GatewayHello", "WorkerHello", "WorkerReady", "WorkerCommand", "WorkerCommandReply", "WorkerCancel", "WorkerShutdown", "WorkerShutdownAck", "WorkerEvent", "WorkerHeartbeat", "WorkerFault" }, new[]{ "Body" }, null, null, null),
new pbr::GeneratedClrTypeInfo(typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.GatewayHello), global::ZB.MOM.WW.MxGateway.Contracts.Proto.GatewayHello.Parser, new[]{ "SupportedProtocolVersion", "Nonce", "GatewayVersion", "MaxFrameBytes" }, null, null, null, null), new pbr::GeneratedClrTypeInfo(typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.GatewayHello), global::ZB.MOM.WW.MxGateway.Contracts.Proto.GatewayHello.Parser, new[]{ "SupportedProtocolVersion", "Nonce", "GatewayVersion", "MaxFrameBytes" }, null, null, null, null),
new pbr::GeneratedClrTypeInfo(typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerHello), global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerHello.Parser, new[]{ "ProtocolVersion", "Nonce", "WorkerProcessId", "WorkerVersion" }, null, null, null, null), new pbr::GeneratedClrTypeInfo(typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerHello), global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerHello.Parser, new[]{ "ProtocolVersion", "Nonce", "WorkerProcessId", "WorkerVersion" }, null, null, null, null),
new pbr::GeneratedClrTypeInfo(typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerReady), global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerReady.Parser, new[]{ "WorkerProcessId", "MxaccessProgid", "MxaccessClsid", "ReadyTimestamp" }, null, null, null, null), new pbr::GeneratedClrTypeInfo(typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerReady), global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerReady.Parser, new[]{ "WorkerProcessId", "MxaccessProgid", "MxaccessClsid", "ReadyTimestamp", "RecordInfoPinStatus" }, null, null, null, null),
new pbr::GeneratedClrTypeInfo(typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerCommand), global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerCommand.Parser, new[]{ "Command", "EnqueueTimestamp" }, null, null, null, null), new pbr::GeneratedClrTypeInfo(typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerCommand), global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerCommand.Parser, new[]{ "Command", "EnqueueTimestamp" }, null, null, null, null),
new pbr::GeneratedClrTypeInfo(typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerCommandReply), global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerCommandReply.Parser, new[]{ "Reply", "CompletedTimestamp" }, null, null, null, null), new pbr::GeneratedClrTypeInfo(typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerCommandReply), global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerCommandReply.Parser, new[]{ "Reply", "CompletedTimestamp" }, null, null, null, null),
new pbr::GeneratedClrTypeInfo(typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerCancel), global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerCancel.Parser, new[]{ "Reason" }, null, null, null, null), new pbr::GeneratedClrTypeInfo(typeof(global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerCancel), global::ZB.MOM.WW.MxGateway.Contracts.Proto.WorkerCancel.Parser, new[]{ "Reason" }, null, null, null, null),
@@ -1744,6 +1745,7 @@ namespace ZB.MOM.WW.MxGateway.Contracts.Proto {
mxaccessProgid_ = other.mxaccessProgid_; mxaccessProgid_ = other.mxaccessProgid_;
mxaccessClsid_ = other.mxaccessClsid_; mxaccessClsid_ = other.mxaccessClsid_;
readyTimestamp_ = other.readyTimestamp_ != null ? other.readyTimestamp_.Clone() : null; readyTimestamp_ = other.readyTimestamp_ != null ? other.readyTimestamp_.Clone() : null;
recordInfoPinStatus_ = other.recordInfoPinStatus_;
_unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields); _unknownFields = pb::UnknownFieldSet.Clone(other._unknownFields);
} }
@@ -1801,6 +1803,26 @@ namespace ZB.MOM.WW.MxGateway.Contracts.Proto {
} }
} }
/// <summary>Field number for the "record_info_pin_status" field.</summary>
public const int RecordInfoPinStatusFieldNumber = 5;
private string recordInfoPinStatus_ = "";
/// <summary>
/// Outcome of the worker's process-wide MXSTATUS_PROXY record-info pin
/// (MxStatusProxyRecordInfoPin): "pinned {library-guid} vM.m record {record-guid}" on
/// success, or the failing call and HRESULT / exception type otherwise. Empty when the
/// worker made no attempt (an older worker, or a runtime that never created the
/// MXAccess COM object). Diagnostic only: the gateway logs it and shows it on the
/// dashboard, and never gates readiness on it, because the pin is a cache warm-up.
/// </summary>
[global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public string RecordInfoPinStatus {
get { return recordInfoPinStatus_; }
set {
recordInfoPinStatus_ = pb::ProtoPreconditions.CheckNotNull(value, "value");
}
}
[global::System.Diagnostics.DebuggerNonUserCodeAttribute] [global::System.Diagnostics.DebuggerNonUserCodeAttribute]
[global::System.CodeDom.Compiler.GeneratedCode("protoc", null)] [global::System.CodeDom.Compiler.GeneratedCode("protoc", null)]
public override bool Equals(object other) { public override bool Equals(object other) {
@@ -1820,6 +1842,7 @@ namespace ZB.MOM.WW.MxGateway.Contracts.Proto {
if (MxaccessProgid != other.MxaccessProgid) return false; if (MxaccessProgid != other.MxaccessProgid) return false;
if (MxaccessClsid != other.MxaccessClsid) return false; if (MxaccessClsid != other.MxaccessClsid) return false;
if (!object.Equals(ReadyTimestamp, other.ReadyTimestamp)) return false; if (!object.Equals(ReadyTimestamp, other.ReadyTimestamp)) return false;
if (RecordInfoPinStatus != other.RecordInfoPinStatus) return false;
return Equals(_unknownFields, other._unknownFields); return Equals(_unknownFields, other._unknownFields);
} }
@@ -1831,6 +1854,7 @@ namespace ZB.MOM.WW.MxGateway.Contracts.Proto {
if (MxaccessProgid.Length != 0) hash ^= MxaccessProgid.GetHashCode(); if (MxaccessProgid.Length != 0) hash ^= MxaccessProgid.GetHashCode();
if (MxaccessClsid.Length != 0) hash ^= MxaccessClsid.GetHashCode(); if (MxaccessClsid.Length != 0) hash ^= MxaccessClsid.GetHashCode();
if (readyTimestamp_ != null) hash ^= ReadyTimestamp.GetHashCode(); if (readyTimestamp_ != null) hash ^= ReadyTimestamp.GetHashCode();
if (RecordInfoPinStatus.Length != 0) hash ^= RecordInfoPinStatus.GetHashCode();
if (_unknownFields != null) { if (_unknownFields != null) {
hash ^= _unknownFields.GetHashCode(); hash ^= _unknownFields.GetHashCode();
} }
@@ -1865,6 +1889,10 @@ namespace ZB.MOM.WW.MxGateway.Contracts.Proto {
output.WriteRawTag(34); output.WriteRawTag(34);
output.WriteMessage(ReadyTimestamp); output.WriteMessage(ReadyTimestamp);
} }
if (RecordInfoPinStatus.Length != 0) {
output.WriteRawTag(42);
output.WriteString(RecordInfoPinStatus);
}
if (_unknownFields != null) { if (_unknownFields != null) {
_unknownFields.WriteTo(output); _unknownFields.WriteTo(output);
} }
@@ -1891,6 +1919,10 @@ namespace ZB.MOM.WW.MxGateway.Contracts.Proto {
output.WriteRawTag(34); output.WriteRawTag(34);
output.WriteMessage(ReadyTimestamp); output.WriteMessage(ReadyTimestamp);
} }
if (RecordInfoPinStatus.Length != 0) {
output.WriteRawTag(42);
output.WriteString(RecordInfoPinStatus);
}
if (_unknownFields != null) { if (_unknownFields != null) {
_unknownFields.WriteTo(ref output); _unknownFields.WriteTo(ref output);
} }
@@ -1913,6 +1945,9 @@ namespace ZB.MOM.WW.MxGateway.Contracts.Proto {
if (readyTimestamp_ != null) { if (readyTimestamp_ != null) {
size += 1 + pb::CodedOutputStream.ComputeMessageSize(ReadyTimestamp); size += 1 + pb::CodedOutputStream.ComputeMessageSize(ReadyTimestamp);
} }
if (RecordInfoPinStatus.Length != 0) {
size += 1 + pb::CodedOutputStream.ComputeStringSize(RecordInfoPinStatus);
}
if (_unknownFields != null) { if (_unknownFields != null) {
size += _unknownFields.CalculateSize(); size += _unknownFields.CalculateSize();
} }
@@ -1940,6 +1975,9 @@ namespace ZB.MOM.WW.MxGateway.Contracts.Proto {
} }
ReadyTimestamp.MergeFrom(other.ReadyTimestamp); ReadyTimestamp.MergeFrom(other.ReadyTimestamp);
} }
if (other.RecordInfoPinStatus.Length != 0) {
RecordInfoPinStatus = other.RecordInfoPinStatus;
}
_unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields); _unknownFields = pb::UnknownFieldSet.MergeFrom(_unknownFields, other._unknownFields);
} }
@@ -1978,6 +2016,10 @@ namespace ZB.MOM.WW.MxGateway.Contracts.Proto {
input.ReadMessage(ReadyTimestamp); input.ReadMessage(ReadyTimestamp);
break; break;
} }
case 42: {
RecordInfoPinStatus = input.ReadString();
break;
}
} }
} }
#endif #endif
@@ -2016,6 +2058,10 @@ namespace ZB.MOM.WW.MxGateway.Contracts.Proto {
input.ReadMessage(ReadyTimestamp); input.ReadMessage(ReadyTimestamp);
break; break;
} }
case 42: {
RecordInfoPinStatus = input.ReadString();
break;
}
} }
} }
} }
@@ -65,6 +65,13 @@ message WorkerReady {
string mxaccess_progid = 2; string mxaccess_progid = 2;
string mxaccess_clsid = 3; string mxaccess_clsid = 3;
google.protobuf.Timestamp ready_timestamp = 4; google.protobuf.Timestamp ready_timestamp = 4;
// Outcome of the worker's process-wide MXSTATUS_PROXY record-info pin
// (MxStatusProxyRecordInfoPin): "pinned {library-guid} vM.m record {record-guid}" on
// success, or the failing call and HRESULT / exception type otherwise. Empty when the
// worker made no attempt (an older worker, or a runtime that never created the
// MXAccess COM object). Diagnostic only: the gateway logs it and shows it on the
// dashboard, and never gates readiness on it, because the pin is a cache warm-up.
string record_info_pin_status = 5;
} }
message WorkerCommand { message WorkerCommand {
File diff suppressed because it is too large Load Diff
@@ -102,6 +102,7 @@ else
<tr><th scope="row">Process id</th><td>@(CurrentSession.WorkerProcessId?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? "-")</td></tr> <tr><th scope="row">Process id</th><td>@(CurrentSession.WorkerProcessId?.ToString(System.Globalization.CultureInfo.InvariantCulture) ?? "-")</td></tr>
<tr><th scope="row">State</th><td><StatusBadge Text="@(CurrentSession.WorkerState?.ToString() ?? "-")" /></td></tr> <tr><th scope="row">State</th><td><StatusBadge Text="@(CurrentSession.WorkerState?.ToString() ?? "-")" /></td></tr>
<tr><th scope="row">Last heartbeat</th><td>@DashboardDisplay.DateTime(CurrentSession.LastWorkerHeartbeatAt)</td></tr> <tr><th scope="row">Last heartbeat</th><td>@DashboardDisplay.DateTime(CurrentSession.LastWorkerHeartbeatAt)</td></tr>
<tr><th scope="row">Record-info pin</th><td>@DashboardDisplay.Text(CurrentSession.WorkerRecordInfoPinStatus)</td></tr>
</tbody> </tbody>
</table> </table>
</div> </div>
@@ -17,4 +17,5 @@ public sealed record DashboardSessionSummary(
WorkerClientState? WorkerState, WorkerClientState? WorkerState,
DateTimeOffset? LastWorkerHeartbeatAt, DateTimeOffset? LastWorkerHeartbeatAt,
long EventsReceived, long EventsReceived,
string? LastFault); string? LastFault,
string? WorkerRecordInfoPinStatus = null);
@@ -285,7 +285,8 @@ public sealed class DashboardSnapshotService : IDashboardSnapshotService
WorkerState: workerClient?.State, WorkerState: workerClient?.State,
LastWorkerHeartbeatAt: workerClient?.LastHeartbeatAt, LastWorkerHeartbeatAt: workerClient?.LastHeartbeatAt,
EventsReceived: eventsReceived, EventsReceived: eventsReceived,
LastFault: DashboardRedactor.Redact(session.FinalFault)); LastFault: DashboardRedactor.Redact(session.FinalFault),
WorkerRecordInfoPinStatus: workerClient?.RecordInfoPinStatus);
} }
private static DashboardWorkerSummary CreateWorkerSummary(GatewaySession session) private static DashboardWorkerSummary CreateWorkerSummary(GatewaySession session)
@@ -27,6 +27,14 @@ public interface IWorkerClient : IAsyncDisposable
/// </returns> /// </returns>
Task? GetStateChangedSignal() => null; Task? GetStateChangedSignal() => null;
/// <summary>
/// Outcome of the worker's <c>MXSTATUS_PROXY</c> record-info pin, as reported in
/// <see cref="WorkerReady"/>, or <see langword="null"/> before the handshake or when the
/// worker did not report one. Diagnostic only; the default implementation returns
/// <see langword="null"/>.
/// </summary>
string? RecordInfoPinStatus => null;
/// <summary>UTC timestamp of the most recent heartbeat from the worker.</summary> /// <summary>UTC timestamp of the most recent heartbeat from the worker.</summary>
DateTimeOffset LastHeartbeatAt { get; } DateTimeOffset LastHeartbeatAt { get; }
@@ -13,6 +13,8 @@ namespace ZB.MOM.WW.MxGateway.Server.Workers;
public sealed class WorkerClient : IWorkerClient public sealed class WorkerClient : IWorkerClient
{ {
private const string GatewayVersionFallback = "unknown"; private const string GatewayVersionFallback = "unknown";
// Prefix of a successful MxStatusProxyRecordInfoPin.Status in the worker; anything else is a failure.
private const string RecordInfoPinnedPrefix = "pinned ";
private static readonly TimeSpan DisposeTaskTimeout = TimeSpan.FromSeconds(5); private static readonly TimeSpan DisposeTaskTimeout = TimeSpan.FromSeconds(5);
private readonly object _syncRoot = new(); private readonly object _syncRoot = new();
private readonly WorkerClientConnection _connection; private readonly WorkerClientConnection _connection;
@@ -55,6 +57,7 @@ public sealed class WorkerClient : IWorkerClient
private TaskCompletionSource _stateChanged = new(TaskCreationOptions.RunContinuationsAsynchronously); private TaskCompletionSource _stateChanged = new(TaskCreationOptions.RunContinuationsAsynchronously);
private DateTimeOffset _lastHeartbeatAt; private DateTimeOffset _lastHeartbeatAt;
private int? _processId; private int? _processId;
private string? _recordInfoPinStatus;
private int _eventQueueDepth; private int _eventQueueDepth;
private int _eventsReaderClaimed; private int _eventsReaderClaimed;
private Task? _readLoopTask; private Task? _readLoopTask;
@@ -161,6 +164,9 @@ public sealed class WorkerClient : IWorkerClient
} }
} }
/// <inheritdoc />
public string? RecordInfoPinStatus => Volatile.Read(ref _recordInfoPinStatus);
/// <inheritdoc /> /// <inheritdoc />
public DateTimeOffset LastHeartbeatAt public DateTimeOffset LastHeartbeatAt
{ {
@@ -1074,6 +1080,45 @@ public sealed class WorkerClient : IWorkerClient
DateTimeOffset readyAt = _timeProvider.GetUtcNow(); DateTimeOffset readyAt = _timeProvider.GetUtcNow();
DateTimeOffset launchedAt = _connection.ProcessHandle?.LaunchedAt ?? readyAt; DateTimeOffset launchedAt = _connection.ProcessHandle?.LaunchedAt ?? readyAt;
_metrics?.WorkerStarted(readyAt - launchedAt); _metrics?.WorkerStarted(readyAt - launchedAt);
RecordRecordInfoPinStatus(ready.RecordInfoPinStatus);
}
/// <summary>
/// Records and logs the worker's <c>MXSTATUS_PROXY</c> record-info pin outcome from
/// <see cref="WorkerReady"/>.
/// </summary>
/// <remarks>
/// The worker logs the outcome too, but to its own stderr, which the gateway does not
/// capture; this is the line operators actually see. A failed pin is a warning rather than a
/// fault: MXAccess still works, but every data change then pays a type-library reload, which
/// is the dominant worker CPU cost at high event rates. An empty status means an older
/// worker, so nothing is logged.
/// </remarks>
/// <param name="status">The status string the worker reported; empty when absent.</param>
private void RecordRecordInfoPinStatus(string status)
{
if (string.IsNullOrEmpty(status))
{
return;
}
Volatile.Write(ref _recordInfoPinStatus, status);
if (status.StartsWith(RecordInfoPinnedPrefix, StringComparison.Ordinal))
{
_logger.LogInformation(
"Worker {ProcessId} for session {SessionId} pinned the MXSTATUS_PROXY record info: {RecordInfoPinStatus}",
ProcessId,
SessionId,
status);
}
else
{
_logger.LogWarning(
"Worker {ProcessId} for session {SessionId} could not pin the MXSTATUS_PROXY record info; each data change will reload the MXAccess type library: {RecordInfoPinStatus}",
ProcessId,
SessionId,
status);
}
} }
/// <summary>Updates the heartbeat timestamp and process ID.</summary> /// <summary>Updates the heartbeat timestamp and process ID.</summary>
@@ -38,6 +38,17 @@
<PackageReference Include="Polly.Core" Version="8.6.6" /> <PackageReference Include="Polly.Core" Version="8.6.6" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<!--
Cap the thread pool's idle spin. The default (70 steps, Sleep(0) on every other step past
the tenth) makes a worker that finds no work yield ~30 times before parking; with several
pool work items per relayed event, a WPR trace on a busy 4-CPU host put ~93% of the
gateway's context switches in those yields. 10 keeps a short busy-spin and then parks.
Measured on windev at ~240 events/s: gateway CPU -15%. See docs/GatewayConfiguration.md.
-->
<RuntimeHostConfigurationOption Include="System.Threading.ThreadPool.UnfairSemaphoreSpinLimit" Value="10" />
</ItemGroup>
<ItemGroup> <ItemGroup>
<ProjectReference Include="..\ZB.MOM.WW.MxGateway.Contracts\ZB.MOM.WW.MxGateway.Contracts.csproj" /> <ProjectReference Include="..\ZB.MOM.WW.MxGateway.Contracts\ZB.MOM.WW.MxGateway.Contracts.csproj" />
</ItemGroup> </ItemGroup>
@@ -182,6 +182,31 @@ public sealed class DashboardSnapshotServiceTests
Assert.Equal("worker pipe disconnected", fault.Message); Assert.Equal("worker pipe disconnected", fault.Message);
} }
/// <summary>
/// The worker's record-info pin outcome, reported in <c>WorkerReady</c>, reaches the session
/// summary the session-details page renders, since the worker's own log line is not captured.
/// </summary>
[Fact]
public void GetSnapshot_ProjectsWorkerRecordInfoPinStatus()
{
const string pinStatus = "LoadRegTypeLib failed (HRESULT 0x8002801D)";
SessionRegistry registry = new();
GatewaySession session = CreateSession(
"session-pin",
"client-one",
DateTimeOffset.Parse("2026-04-26T10:00:00Z", CultureInfo.InvariantCulture));
session.AttachWorkerClient(
new FakeWorkerClient("session-pin", 1301, WorkerClientState.Ready) { RecordInfoPinStatus = pinStatus });
session.MarkReady();
registry.TryAdd(session);
using GatewayMetrics metrics = new();
DashboardSnapshotService service = CreateService(registry, metrics);
DashboardSessionSummary summary = Assert.Single(service.GetSnapshot().Sessions);
Assert.Equal(pinStatus, summary.WorkerRecordInfoPinStatus);
}
/// <summary> /// <summary>
/// Verifies snapshot metrics include the cumulative alarm provider switch count. /// Verifies snapshot metrics include the cumulative alarm provider switch count.
/// </summary> /// </summary>
@@ -929,6 +954,9 @@ public sealed class DashboardSnapshotServiceTests
/// <inheritdoc /> /// <inheritdoc />
public WorkerClientState State { get; private set; } = state; public WorkerClientState State { get; private set; } = state;
/// <inheritdoc />
public string? RecordInfoPinStatus { get; init; }
/// <inheritdoc /> /// <inheritdoc />
public DateTimeOffset LastHeartbeatAt { get; } = DateTimeOffset.Parse("2026-04-26T10:02:00Z", CultureInfo.InvariantCulture); public DateTimeOffset LastHeartbeatAt { get; } = DateTimeOffset.Parse("2026-04-26T10:02:00Z", CultureInfo.InvariantCulture);
@@ -1,6 +1,7 @@
using System.Buffers.Binary; using System.Buffers.Binary;
using System.IO.Pipes; using System.IO.Pipes;
using Google.Protobuf.WellKnownTypes; using Google.Protobuf.WellKnownTypes;
using Microsoft.Extensions.Logging;
using ZB.MOM.WW.MxGateway.Contracts; using ZB.MOM.WW.MxGateway.Contracts;
using ZB.MOM.WW.MxGateway.Contracts.Proto; using ZB.MOM.WW.MxGateway.Contracts.Proto;
using ZB.MOM.WW.MxGateway.Server.Metrics; using ZB.MOM.WW.MxGateway.Server.Metrics;
@@ -108,11 +109,13 @@ public sealed class FakeWorkerHarness : IAsyncDisposable
/// <param name="options">Configuration options for the worker client.</param> /// <param name="options">Configuration options for the worker client.</param>
/// <param name="metrics">Gateway metrics collector.</param> /// <param name="metrics">Gateway metrics collector.</param>
/// <param name="timeProvider">Time provider for timestamps.</param> /// <param name="timeProvider">Time provider for timestamps.</param>
/// <param name="logger">Logger for the worker client; null uses a no-op logger.</param>
/// <returns>A configured worker client connected to this harness.</returns> /// <returns>A configured worker client connected to this harness.</returns>
public WorkerClient CreateClient( public WorkerClient CreateClient(
WorkerClientOptions? options = null, WorkerClientOptions? options = null,
GatewayMetrics? metrics = null, GatewayMetrics? metrics = null,
TimeProvider? timeProvider = null) TimeProvider? timeProvider = null,
ILogger<WorkerClient>? logger = null)
{ {
if (_gatewayStream is null) if (_gatewayStream is null)
{ {
@@ -125,7 +128,7 @@ public sealed class FakeWorkerHarness : IAsyncDisposable
_gatewayStream, _gatewayStream,
_frameOptions); _frameOptions);
return new WorkerClient(connection, options, metrics, timeProvider); return new WorkerClient(connection, options, metrics, timeProvider, logger);
} }
/// <summary>Completes the worker startup handshake by reading the gateway hello and sending worker hello and ready.</summary> /// <summary>Completes the worker startup handshake by reading the gateway hello and sending worker hello and ready.</summary>
@@ -229,12 +232,14 @@ public sealed class FakeWorkerHarness : IAsyncDisposable
/// <param name="mxaccessProgid">MXAccess COM ProgID.</param> /// <param name="mxaccessProgid">MXAccess COM ProgID.</param>
/// <param name="mxaccessClsid">MXAccess COM CLSID.</param> /// <param name="mxaccessClsid">MXAccess COM CLSID.</param>
/// <param name="cancellationToken">Token to cancel the asynchronous operation.</param> /// <param name="cancellationToken">Token to cancel the asynchronous operation.</param>
/// <param name="recordInfoPinStatus">Record-info pin outcome to report; empty mimics an older worker.</param>
/// <returns>A task that represents the asynchronous operation.</returns> /// <returns>A task that represents the asynchronous operation.</returns>
public async Task SendWorkerReadyAsync( public async Task SendWorkerReadyAsync(
int workerProcessId = DefaultWorkerProcessId, int workerProcessId = DefaultWorkerProcessId,
string mxaccessProgid = "LMXProxy.LMXProxyServer.1", string mxaccessProgid = "LMXProxy.LMXProxyServer.1",
string mxaccessClsid = "{C30B52F5-2CB5-4760-AF0A-3A344A7EB5DC}", string mxaccessClsid = "{C30B52F5-2CB5-4760-AF0A-3A344A7EB5DC}",
CancellationToken cancellationToken = default) CancellationToken cancellationToken = default,
string recordInfoPinStatus = "")
{ {
await _writer.WriteAsync( await _writer.WriteAsync(
CreateEnvelope( CreateEnvelope(
@@ -245,6 +250,7 @@ public sealed class FakeWorkerHarness : IAsyncDisposable
MxaccessProgid = mxaccessProgid, MxaccessProgid = mxaccessProgid,
MxaccessClsid = mxaccessClsid, MxaccessClsid = mxaccessClsid,
ReadyTimestamp = Timestamp.FromDateTimeOffset(DateTimeOffset.UtcNow), ReadyTimestamp = Timestamp.FromDateTimeOffset(DateTimeOffset.UtcNow),
RecordInfoPinStatus = recordInfoPinStatus,
}), }),
cancellationToken).ConfigureAwait(false); cancellationToken).ConfigureAwait(false);
} }
@@ -0,0 +1,96 @@
using Microsoft.Extensions.Logging;
using ZB.MOM.WW.MxGateway.Contracts.Proto;
using ZB.MOM.WW.MxGateway.Server.Workers;
using ZB.MOM.WW.MxGateway.Tests.Gateway.Workers.Fakes;
using ZB.MOM.WW.MxGateway.Tests.TestSupport;
namespace ZB.MOM.WW.MxGateway.Tests.Gateway.Workers;
/// <summary>
/// The worker reports its <c>MXSTATUS_PROXY</c> record-info pin outcome in <see cref="WorkerReady"/>
/// because the gateway never sees the worker's own stderr log. These tests pin how the gateway
/// surfaces it: kept on the client, logged at Information on success and Warning on failure,
/// and silently absent for an older worker that sends no status.
/// </summary>
public sealed class WorkerClientRecordInfoPinTests
{
private const string PinnedStatus =
"pinned {77e896ec-b3e3-4de4-b96f-f32570164211} v3.2 record {a162d9e4-9019-42f3-a087-79264007e542}";
private static readonly TimeSpan TestTimeout = TimeSpan.FromSeconds(5);
/// <summary>A successful pin is recorded on the client and logged at Information.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task StartAsync_WorkerReportsPinned_RecordsStatusAndLogsInformation()
{
RecordingLogger<WorkerClient> logger = new();
await using FakeWorkerHarness harness = await FakeWorkerHarness.CreateConnectedPairAsync();
await using WorkerClient client = harness.CreateClient(logger: logger);
await StartWithReadyAsync(client, harness, PinnedStatus);
Assert.Equal(PinnedStatus, client.RecordInfoPinStatus);
(LogLevel level, string message, _) = Assert.Single(
logger.Entries,
entry => entry.Message.Contains("record info", StringComparison.Ordinal));
Assert.Equal(LogLevel.Information, level);
Assert.Contains(PinnedStatus, message, StringComparison.Ordinal);
Assert.Contains(FakeWorkerHarness.DefaultSessionId, message, StringComparison.Ordinal);
}
/// <summary>
/// A failed pin still lets the worker reach Ready (the pin is a cache warm-up, not a
/// prerequisite) but is logged at Warning so the per-event CPU cost it implies is visible.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task StartAsync_WorkerReportsPinFailure_StaysReadyAndLogsWarning()
{
const string failedStatus = "LoadRegTypeLib failed (HRESULT 0x8002801D)";
RecordingLogger<WorkerClient> logger = new();
await using FakeWorkerHarness harness = await FakeWorkerHarness.CreateConnectedPairAsync();
await using WorkerClient client = harness.CreateClient(logger: logger);
await StartWithReadyAsync(client, harness, failedStatus);
Assert.Equal(WorkerClientState.Ready, client.State);
Assert.Equal(failedStatus, client.RecordInfoPinStatus);
(LogLevel level, string message, _) = Assert.Single(
logger.Entries,
entry => entry.Message.Contains("record info", StringComparison.Ordinal));
Assert.Equal(LogLevel.Warning, level);
Assert.Contains(failedStatus, message, StringComparison.Ordinal);
}
/// <summary>An older worker sends no status: nothing is recorded and nothing is logged about it.</summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task StartAsync_WorkerReportsNoStatus_RecordsNullAndDoesNotLog()
{
RecordingLogger<WorkerClient> logger = new();
await using FakeWorkerHarness harness = await FakeWorkerHarness.CreateConnectedPairAsync();
await using WorkerClient client = harness.CreateClient(logger: logger);
await StartWithReadyAsync(client, harness, recordInfoPinStatus: string.Empty);
Assert.Equal(WorkerClientState.Ready, client.State);
Assert.Null(client.RecordInfoPinStatus);
Assert.DoesNotContain(
logger.Entries,
entry => entry.Message.Contains("record info", StringComparison.Ordinal));
}
private static async Task StartWithReadyAsync(
WorkerClient client,
FakeWorkerHarness harness,
string recordInfoPinStatus)
{
Task startTask = client.StartAsync(CancellationToken.None);
WorkerEnvelope gatewayHello = await harness.ReadGatewayEnvelopeAsync().WaitAsync(TestTimeout);
Assert.Equal(WorkerEnvelope.BodyOneofCase.GatewayHello, gatewayHello.BodyCase);
await harness.SendWorkerHelloAsync().WaitAsync(TestTimeout);
await harness.SendWorkerReadyAsync(recordInfoPinStatus: recordInfoPinStatus).WaitAsync(TestTimeout);
await startTask.WaitAsync(TestTimeout);
}
}
@@ -375,7 +375,7 @@ public sealed class WorkerFrameProtocolTests
/// <summary> /// <summary>
/// Verifies the writer coalesces the flush across a batch of frames drained together: four frames /// Verifies the writer coalesces the flush across a batch of frames drained together: four frames
/// queued behind an in-progress write drain in a single pass and share one FlushAsync, not four. /// queued behind an in-progress write drain in a single pass and share one Flush, not four.
/// Every frame still reaches the wire intact. /// Every frame still reaches the wire intact.
/// <para> /// <para>
/// The burst is all-event on purpose. The control-frame completion decoupling made the drain flush /// The burst is all-event on purpose. The control-frame completion decoupling made the drain flush
@@ -1386,20 +1386,20 @@ public sealed class WorkerFrameProtocolTests
}; };
} }
// A MemoryStream that counts FlushAsync calls without gating any write, so a batch write can be // A MemoryStream that counts Flush calls without gating any write, so a batch write can be
// asserted to flush exactly once. // asserted to flush exactly once.
private sealed class FlushCountingStream : MemoryStream private sealed class FlushCountingStream : MemoryStream
{ {
private int _flushCount; private int _flushCount;
/// <summary>Gets the number of <see cref="FlushAsync"/> calls observed so far.</summary> /// <summary>Gets the number of <see cref="Flush"/> calls observed so far.</summary>
public int FlushCount => Volatile.Read(ref _flushCount); public int FlushCount => Volatile.Read(ref _flushCount);
/// <inheritdoc /> /// <inheritdoc />
public override Task FlushAsync(CancellationToken cancellationToken) public override void Flush()
{ {
Interlocked.Increment(ref _flushCount); Interlocked.Increment(ref _flushCount);
return base.FlushAsync(cancellationToken); base.Flush();
} }
} }
@@ -1500,7 +1500,7 @@ public sealed class WorkerFrameProtocolTests
/// <summary>Gets a task that completes once the second gated <see cref="WriteAsync"/> call has started blocking.</summary> /// <summary>Gets a task that completes once the second gated <see cref="WriteAsync"/> call has started blocking.</summary>
public Task SecondGateWriteStarted => _secondGateWriteStarted.Task; public Task SecondGateWriteStarted => _secondGateWriteStarted.Task;
/// <summary>Gets the number of <see cref="FlushAsync"/> calls observed so far.</summary> /// <summary>Gets the number of <see cref="Flush"/> calls observed so far.</summary>
public int FlushCount => Volatile.Read(ref _flushCount); public int FlushCount => Volatile.Read(ref _flushCount);
/// <summary>Releases the first blocked write so it can complete.</summary> /// <summary>Releases the first blocked write so it can complete.</summary>
@@ -1528,10 +1528,10 @@ public sealed class WorkerFrameProtocolTests
} }
/// <inheritdoc /> /// <inheritdoc />
public override Task FlushAsync(CancellationToken cancellationToken) public override void Flush()
{ {
Interlocked.Increment(ref _flushCount); Interlocked.Increment(ref _flushCount);
return base.FlushAsync(cancellationToken); base.Flush();
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -1547,7 +1547,7 @@ public sealed class WorkerFrameProtocolTests
} }
} }
// A MemoryStream whose first write blocks until released and whose every FlushAsync throws, so a test // A MemoryStream whose first write blocks until released and whose every Flush throws, so a test
// can fault the class-boundary flush with control frames already written and an event frame already // can fault the class-boundary flush with control frames already written and an event frame already
// claimed off its queue. // claimed off its queue.
private sealed class FlushFaultingGatedStream : MemoryStream private sealed class FlushFaultingGatedStream : MemoryStream
@@ -1584,9 +1584,9 @@ public sealed class WorkerFrameProtocolTests
} }
/// <inheritdoc /> /// <inheritdoc />
public override Task FlushAsync(CancellationToken cancellationToken) public override void Flush()
{ {
return Task.FromException(new IOException(_faultMessage)); throw new IOException(_faultMessage);
} }
/// <inheritdoc /> /// <inheritdoc />
@@ -367,6 +367,190 @@ public sealed class WorkerPipeSessionTests
await SendShutdownAndWaitAsync(pipePair, runTask, cancellation.Token); await SendShutdownAndWaitAsync(pipePair, runTask, cancellation.Token);
} }
/// <summary>
/// The event drain loop runs on its own dedicated background thread, not the CLR thread
/// pool: on .NET Framework every pool continuation per event also paid the pool's idle
/// spin, which a production trace measured at ~150 context switches per event. The parked
/// wait is observed from the fake, which records the calling thread.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task RunAsync_EventDrainLoop_RunsOnDedicatedNonPoolThread()
{
using CancellationTokenSource cancellation = new(TimeSpan.FromSeconds(10));
using PipePair pipePair = await PipePair.CreateAsync(cancellation.Token);
FakeRuntimeSession runtime = new()
{
WaitForEventsOnSignalOnly = true,
};
WorkerPipeSession session = CreatePipeSession(pipePair.WorkerStream, runtime);
Task runTask = session.RunAsync(cancellation.Token);
await CompleteGatewayHandshakeAsync(pipePair, cancellation.Token);
while (runtime.LastWaitForEventsTimeout is null)
{
await Task.Delay(TimeSpan.FromMilliseconds(5), cancellation.Token);
}
Assert.Equal("mxgw-event-drain", runtime.LastWaitForEventsThreadName);
Assert.False(runtime.LastWaitForEventsOnThreadPool);
// The dedicated thread still ships events and still stops cleanly on shutdown.
runtime.EnqueueEvent(CreateWorkerEvent(sequence: 3));
WorkerEnvelope workerEvent = await ReadUntilAsync(
pipePair.GatewayReader,
WorkerEnvelope.BodyOneofCase.WorkerEvent,
cancellation.Token);
Assert.Equal(3UL, workerEvent.WorkerEvent.Event.WorkerSequence);
await SendShutdownAndWaitAsync(pipePair, runTask, cancellation.Token);
}
/// <summary>
/// The drain loop's task mirrors the dedicated thread's outcome: cancelling its token ends
/// the task Canceled (not faulted, not still running), which is what lets the message loop's
/// teardown treat it as an ordinary stop. Exercised directly, before any runtime session is
/// attached, so the loop is in its runtime-less poll and only the cancellation can end it.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task RunEventDrainLoopAsync_WhenCancelled_EndsCanceled()
{
WorkerFrameProtocolOptions options = CreateOptions();
using MemoryStream inbound = new();
using MemoryStream outbound = new();
WorkerPipeSession session = CreateSession(inbound, outbound, options);
using CancellationTokenSource stop = new();
Task drainTask = session.RunEventDrainLoopAsync(stop.Token);
Assert.False(drainTask.IsCompleted);
stop.Cancel();
Task completedTask = await Task.WhenAny(drainTask, Task.Delay(TimeSpan.FromSeconds(5)));
Assert.Same(drainTask, completedTask);
Assert.True(drainTask.IsCanceled, $"Expected Canceled but the drain task ended {drainTask.Status}.");
}
/// <summary>
/// Cancelling the session while the dedicated drain thread is parked on the event signal
/// ends <see cref="WorkerPipeSession.RunAsync"/> as cancelled, disposes the runtime, and lets
/// the drain thread exit. Teardown must not log the drain task as faulted or overrunning its
/// stop budget: a cancelled drain is a clean stop.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task RunAsync_WhenCancelledWhileDrainThreadParked_EndsCanceledAndDrainThreadExits()
{
using CancellationTokenSource timeout = new(TimeSpan.FromSeconds(10));
using CancellationTokenSource sessionCancellation = new();
using PipePair pipePair = await PipePair.CreateAsync(timeout.Token);
RecordingWorkerLogger logger = new();
FakeRuntimeSession runtime = new()
{
WaitForEventsOnSignalOnly = true,
};
WorkerPipeSession session = CreatePipeSession(
pipePair.WorkerStream,
runtime,
new WorkerPipeSessionOptions
{
HeartbeatInterval = TimeSpan.FromMilliseconds(100),
HeartbeatGrace = TimeSpan.FromSeconds(5),
},
logger);
Task runTask = session.RunAsync(sessionCancellation.Token);
await CompleteGatewayHandshakeAsync(pipePair, timeout.Token);
Thread drainThread = await WaitForDrainThreadAsync(runtime, timeout.Token);
sessionCancellation.Cancel();
Task completedTask = await Task.WhenAny(runTask, Task.Delay(TimeSpan.FromSeconds(5), timeout.Token));
Assert.Same(runTask, completedTask);
await Assert.ThrowsAnyAsync<OperationCanceledException>(async () => await runTask);
Assert.True(runtime.Disposed);
Assert.True(drainThread.Join(TimeSpan.FromSeconds(5)), "The dedicated drain thread did not exit.");
Assert.DoesNotContain(
logger.Events,
entry => entry.Fields.TryGetValue("task", out object? task) && (task as string) == "EventDrain");
}
/// <summary>
/// An unexpected exception inside the loop on the dedicated drain thread faults the drain
/// task with that same exception, so the message loop's <c>Task.WhenAny</c> supervision ends
/// <see cref="WorkerPipeSession.RunAsync"/> with it exactly as it did when the loop was an
/// async method: the session stops, the runtime is disposed, and the thread exits.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task RunAsync_WhenDrainLoopThrows_FaultsSessionWithTheSameException()
{
using CancellationTokenSource cancellation = new(TimeSpan.FromSeconds(10));
using PipePair pipePair = await PipePair.CreateAsync(cancellation.Token);
FakeRuntimeSession runtime = new()
{
WaitForEventsOnSignalOnly = true,
};
WorkerPipeSession session = CreatePipeSession(pipePair.WorkerStream, runtime);
Task runTask = session.RunAsync(cancellation.Token);
await CompleteGatewayHandshakeAsync(pipePair, cancellation.Token);
Thread drainThread = await WaitForDrainThreadAsync(runtime, cancellation.Token);
InvalidOperationException scripted = new("scripted drain-loop failure");
runtime.DrainEventsException = scripted;
// Wake the parked thread; its next pass calls DrainEvents, which now throws.
runtime.EnqueueEvent(CreateWorkerEvent(sequence: 4));
Task completedTask = await Task.WhenAny(runTask, Task.Delay(TimeSpan.FromSeconds(5), cancellation.Token));
Assert.Same(runTask, completedTask);
InvalidOperationException observed =
await Assert.ThrowsAsync<InvalidOperationException>(async () => await runTask);
Assert.Same(scripted, observed);
Assert.True(runtime.Disposed);
Assert.True(drainThread.Join(TimeSpan.FromSeconds(5)), "The dedicated drain thread did not exit.");
}
/// <summary>
/// The handshake copies the process-wide record-info pin outcome into <c>WorkerReady</c>, so
/// the gateway can log it: the worker's own log goes to stderr, which the gateway does not
/// capture. Whether the pin succeeds depends on the host, so only the hand-off is asserted.
/// </summary>
/// <returns>A task that represents the asynchronous operation.</returns>
[Fact]
public async Task CompleteStartupHandshakeAsync_ReportsRecordInfoPinStatusInWorkerReady()
{
_ = MxStatusProxyRecordInfoPin.EnsurePinned();
string? pinStatus = MxStatusProxyRecordInfoPin.Status;
Assert.False(string.IsNullOrEmpty(pinStatus));
WorkerFrameProtocolOptions options = CreateOptions();
using MemoryStream inbound = new();
await new WorkerFrameWriter(inbound, options).WriteAsync(CreateGatewayHelloEnvelope());
inbound.Position = 0;
using MemoryStream outbound = new();
WorkerPipeSession session = CreateSession(inbound, outbound, options);
await session.CompleteStartupHandshakeAsync(
_ => Task.FromResult(new WorkerReady { WorkerProcessId = 1234 }));
WorkerEnvelope[] written = ReadWrittenFrames(outbound, options);
Assert.Equal(WorkerEnvelope.BodyOneofCase.WorkerReady, written[1].BodyCase);
Assert.Equal(pinStatus, written[1].WorkerReady.RecordInfoPinStatus);
}
// Waits until the drain loop has parked on the fake's event signal at least once and returns the
// thread it parked on: the dedicated drain thread.
private static async Task<Thread> WaitForDrainThreadAsync(
FakeRuntimeSession runtime,
CancellationToken cancellationToken)
{
Thread? drainThread;
while ((drainThread = runtime.LastWaitForEventsThread) is null)
{
await Task.Delay(TimeSpan.FromMilliseconds(5), cancellationToken).ConfigureAwait(false);
}
return drainThread;
}
/// <summary> /// <summary>
/// Verifies that a Ping control command is answered on the worker side /// Verifies that a Ping control command is answered on the worker side
/// (not dispatched to the STA) with an OK reply that echoes the ping /// (not dispatched to the STA) with an OK reply that echoes the ping
@@ -0,0 +1,28 @@
using ZB.MOM.WW.MxGateway.Worker.MxAccess;
namespace ZB.MOM.WW.MxGateway.Worker.Tests.MxAccess;
/// <summary>
/// The record-info pin is a best-effort, process-wide cache warm-up. Whether it succeeds depends
/// on the host's MXAccess registration, so these tests assert only what must hold on every host:
/// it never throws, it always reports an outcome, and only the first call does work.
/// </summary>
public sealed class MxStatusProxyRecordInfoPinTests
{
/// <summary>Repeated calls return the same outcome and leave a status describing it.</summary>
[Fact]
public void EnsurePinned_CalledTwice_IsIdempotentAndReportsStatus()
{
bool first = MxStatusProxyRecordInfoPin.EnsurePinned();
string? statusAfterFirst = MxStatusProxyRecordInfoPin.Status;
bool second = MxStatusProxyRecordInfoPin.EnsurePinned();
Assert.Equal(first, second);
Assert.False(string.IsNullOrWhiteSpace(statusAfterFirst));
Assert.Same(statusAfterFirst, MxStatusProxyRecordInfoPin.Status);
if (first)
{
Assert.StartsWith("pinned ", statusAfterFirst);
}
}
}
@@ -42,6 +42,8 @@ internal sealed class FakeRuntimeSession : IWorkerRuntimeSession
// disposed SemaphoreSlim would turn that shutdown into an ObjectDisposedException. // disposed SemaphoreSlim would turn that shutdown into an ObjectDisposedException.
private readonly SemaphoreSlim eventSignal = new(0, 1); private readonly SemaphoreSlim eventSignal = new(0, 1);
private TimeSpan? lastWaitForEventsTimeout; private TimeSpan? lastWaitForEventsTimeout;
private volatile Exception? drainEventsException;
private volatile Thread? lastWaitForEventsThread;
private bool refreshStaActivityOnCapture; private bool refreshStaActivityOnCapture;
private WorkerRuntimeHeartbeatSnapshot snapshot = new( private WorkerRuntimeHeartbeatSnapshot snapshot = new(
DateTimeOffset.UtcNow, DateTimeOffset.UtcNow,
@@ -235,9 +237,25 @@ internal sealed class FakeRuntimeSession : IWorkerRuntimeSession
/// </summary> /// </summary>
public int? LastDrainMaxTotalBytes { get; private set; } public int? LastDrainMaxTotalBytes { get; private set; }
/// <summary>
/// Gets or sets an exception every <see cref="DrainEvents(uint)"/> call throws, standing in
/// for an unexpected failure inside the event drain loop. Safe to set from the test thread
/// while the drain thread is running.
/// </summary>
public Exception? DrainEventsException
{
get => drainEventsException;
set => drainEventsException = value;
}
/// <inheritdoc /> /// <inheritdoc />
public IReadOnlyList<WorkerEvent> DrainEvents(uint maxEvents) public IReadOnlyList<WorkerEvent> DrainEvents(uint maxEvents)
{ {
if (drainEventsException is { } drainException)
{
throw drainException;
}
if (SuppressDrainForBatchSize is uint suppressed && maxEvents == suppressed) if (SuppressDrainForBatchSize is uint suppressed && maxEvents == suppressed)
{ {
return Array.Empty<WorkerEvent>(); return Array.Empty<WorkerEvent>();
@@ -387,6 +405,29 @@ internal sealed class FakeRuntimeSession : IWorkerRuntimeSession
return eventSignal.WaitAsync(timeout, cancellationToken); return eventSignal.WaitAsync(timeout, cancellationToken);
} }
/// <summary>Gets the name of the thread that made the most recent <see cref="WaitForEvents"/> call.</summary>
public string? LastWaitForEventsThreadName { get; private set; }
/// <summary>Gets whether the most recent <see cref="WaitForEvents"/> call ran on a thread-pool thread.</summary>
public bool? LastWaitForEventsOnThreadPool { get; private set; }
/// <summary>
/// Gets the thread that made the most recent <see cref="WaitForEvents"/> call, so a test can
/// assert the dedicated drain thread has exited after the session stops.
/// </summary>
public Thread? LastWaitForEventsThread => lastWaitForEventsThread;
/// <inheritdoc />
public void WaitForEvents(TimeSpan timeout, CancellationToken cancellationToken)
{
lastWaitForEventsThread = Thread.CurrentThread;
LastWaitForEventsThreadName = Thread.CurrentThread.Name;
LastWaitForEventsOnThreadPool = Thread.CurrentThread.IsThreadPoolThread;
// Same knobs as the async form (signal-only, recorded timeout, backing queue); the drain
// loop now runs on a dedicated thread, so blocking on the task here is the production shape.
WaitForEventsAsync(timeout, cancellationToken).GetAwaiter().GetResult();
}
/// <summary> /// <summary>
/// Gets or sets a fault the next <see cref="DrainFault"/> call hands back once, standing in /// Gets or sets a fault the next <see cref="DrainFault"/> call hands back once, standing in
/// for the fault a real event queue self-records on overflow. /// for the fault a real event queue self-records on overflow.
@@ -588,7 +588,7 @@ public sealed class WorkerFrameWriter
// and now flushed, so completing them early is the same contract, earlier. // and now flushed, so completing them early is the same contract, earlier.
try try
{ {
await _stream.FlushAsync(CancellationToken.None).ConfigureAwait(false); FlushStream();
} }
catch (Exception exception) catch (Exception exception)
{ {
@@ -640,7 +640,7 @@ public sealed class WorkerFrameWriter
try try
{ {
await _stream.FlushAsync(CancellationToken.None).ConfigureAwait(false); FlushStream();
} }
catch (Exception exception) catch (Exception exception)
{ {
@@ -655,6 +655,15 @@ public sealed class WorkerFrameWriter
CompleteFrames(written); CompleteFrames(written);
} }
// Synchronous on purpose. PipeStream does not override FlushAsync, so the base Stream.FlushAsync
// runs Flush() through Task.Factory.StartNew: one thread-pool work item per drain pass, for a
// PipeStream.Flush that has nothing to do (it never calls FlushFileBuffers). Calling Flush()
// inline keeps the contract for any stream and drops the pool hop.
private void FlushStream()
{
_stream.Flush();
}
private static void CompleteFrames(List<PendingFrame> frames) private static void CompleteFrames(List<PendingFrame> frames)
{ {
foreach (PendingFrame frame in frames) foreach (PendingFrame frame in frames)
@@ -355,6 +355,21 @@ public sealed class WorkerPipeSession
await WriteWorkerHelloAsync(cancellationToken).ConfigureAwait(false); await WriteWorkerHelloAsync(cancellationToken).ConfigureAwait(false);
_state = WorkerState.InitializingSta; _state = WorkerState.InitializingSta;
WorkerReady ready = await initializeMxAccessAsync(cancellationToken).ConfigureAwait(false); WorkerReady ready = await initializeMxAccessAsync(cancellationToken).ConfigureAwait(false);
if (MxStatusProxyRecordInfoPin.Status is { } pinStatus)
{
_logger?.Information(
"WorkerRecordInfoPin",
new Dictionary<string, object?> { ["status"] = pinStatus });
// The gateway does not capture worker stderr, so the log line above is rarely
// seen; WorkerReady carries the outcome to the gateway, which logs it and shows it
// on the dashboard. A runtime that already filled the field keeps its own value.
if (string.IsNullOrEmpty(ready.RecordInfoPinStatus))
{
ready.RecordInfoPinStatus = pinStatus;
}
}
await WriteWorkerReadyAsync(ready, cancellationToken).ConfigureAwait(false); await WriteWorkerReadyAsync(ready, cancellationToken).ConfigureAwait(false);
_state = WorkerState.Ready; _state = WorkerState.Ready;
} }
@@ -589,14 +604,74 @@ public sealed class WorkerPipeSession
TaskScheduler.Default); TaskScheduler.Default);
} }
private async Task RunEventDrainLoopAsync(CancellationToken cancellationToken) /// <summary>
/// Starts the event drain loop on a dedicated background thread and returns a task that
/// completes, faults, or cancels exactly as the loop does, so the message loop's
/// <c>Task.WhenAny</c> supervision is unchanged.
/// </summary>
/// <remarks>
/// The loop used to be an async method woken by <c>SemaphoreSlim.WaitAsync</c>. On .NET
/// Framework every release of that semaphore queues the continuation to the CLR thread pool,
/// and each pool worker that runs one then spins in <c>SleepEx</c> before parking: a WPR trace
/// on wonder-app-vd03 measured ~150 context switches per event and ~26% of the worker's CPU
/// there. A dedicated thread parks on the signal instead, and with a synchronous flush the
/// steady-state event path schedules no managed thread-pool work at all. The pipe write itself
/// stays overlapped; its completion runs on an I/O completion thread, not a worker thread.
/// Internal so the completion mapping can be tested directly; production code calls it only
/// from <see cref="RunMessageLoopAsync"/>.
/// </remarks>
/// <param name="cancellationToken">Token that stops the loop; the returned task then ends Canceled.</param>
/// <returns>A task that mirrors the loop's outcome.</returns>
internal Task RunEventDrainLoopAsync(CancellationToken cancellationToken)
{
TaskCompletionSource<bool> completion = new(TaskCreationOptions.RunContinuationsAsynchronously);
Thread thread = new(() =>
{
try
{
RunEventDrainLoop(cancellationToken);
// The loop returns only once its token is cancelled. Report that as Canceled
// whether the loop noticed at its head or inside a wait (which throws), so a
// stopped drain has one outcome rather than a timing-dependent pair.
if (cancellationToken.IsCancellationRequested)
{
completion.TrySetCanceled(cancellationToken);
}
else
{
completion.TrySetResult(true);
}
}
catch (OperationCanceledException) when (cancellationToken.IsCancellationRequested)
{
completion.TrySetCanceled(cancellationToken);
}
catch (Exception exception)
{
completion.TrySetException(exception);
}
})
{
IsBackground = true,
Name = "mxgw-event-drain",
};
thread.Start();
return completion.Task;
}
private void RunEventDrainLoop(CancellationToken cancellationToken)
{ {
while (!cancellationToken.IsCancellationRequested) while (!cancellationToken.IsCancellationRequested)
{ {
IWorkerRuntimeSession? runtimeSession = _runtimeSession; IWorkerRuntimeSession? runtimeSession = _runtimeSession;
if (runtimeSession is null) if (runtimeSession is null)
{ {
await Task.Delay(EventDrainInterval, cancellationToken).ConfigureAwait(false); if (cancellationToken.WaitHandle.WaitOne(EventDrainInterval))
{
cancellationToken.ThrowIfCancellationRequested();
}
continue; continue;
} }
@@ -604,7 +679,7 @@ public sealed class WorkerPipeSession
if (fault is not null) if (fault is not null)
{ {
_state = WorkerState.Faulted; _state = WorkerState.Faulted;
await TryWriteFaultAsync(fault, cancellationToken).ConfigureAwait(false); TryWriteFaultAsync(fault, cancellationToken).GetAwaiter().GetResult();
throw new WorkerEventQueueFaultException( throw new WorkerEventQueueFaultException(
fault.Category, fault.Category,
string.IsNullOrWhiteSpace(fault.DiagnosticMessage) string.IsNullOrWhiteSpace(fault.DiagnosticMessage)
@@ -615,15 +690,13 @@ public sealed class WorkerPipeSession
IReadOnlyList<WorkerEvent> events = runtimeSession.DrainEvents(EventDrainBatchSize); IReadOnlyList<WorkerEvent> events = runtimeSession.DrainEvents(EventDrainBatchSize);
if (events.Count == 0) if (events.Count == 0)
{ {
// Wait on the queue's wake signal rather than sleeping a fixed tick: an event // Park on the queue's wake signal rather than sleeping a fixed tick: an event
// enqueued by the STA completes this immediately, so the first event of a burst is // enqueued by the STA releases this immediately, so the first event of a burst is
// framed at signal latency instead of waiting out EventDrainInterval, and a session // framed at signal latency, and a session with no traffic wakes only at the fallback
// with no traffic stops waking at all. The wait's outcome is intentionally ignored — // ceiling. The wait's outcome is intentionally ignored — whether a signal or the
// whether a signal or the fallback ended it, the next pass re-checks DrainFault() // fallback ended it, the next pass re-checks DrainFault() and re-drains, which is
// and re-drains, which is also why one coalesced wake for many enqueues is safe. // also why one coalesced wake for many enqueues is safe.
await runtimeSession runtimeSession.WaitForEvents(EventDrainInterval, cancellationToken);
.WaitForEventsAsync(EventDrainInterval, cancellationToken)
.ConfigureAwait(false);
continue; continue;
} }
@@ -640,9 +713,10 @@ public sealed class WorkerPipeSession
try try
{ {
await _writer _writer
.WriteBatchAsync(envelopes, WorkerFrameWritePriority.Event, cancellationToken) .WriteBatchAsync(envelopes, WorkerFrameWritePriority.Event, cancellationToken)
.ConfigureAwait(false); .GetAwaiter()
.GetResult();
} }
catch (WorkerFrameProtocolException exception) catch (WorkerFrameProtocolException exception)
when (exception.ErrorCode == WorkerFrameProtocolErrorCode.MessageTooLarge) when (exception.ErrorCode == WorkerFrameProtocolErrorCode.MessageTooLarge)
@@ -651,11 +725,12 @@ public sealed class WorkerPipeSession
// still the structured, event-naming fault. Map the rejection back to the first // still the structured, event-naming fault. Map the rejection back to the first
// event in batch order whose envelope overshoots the negotiated maximum — the same // event in batch order whose envelope overshoots the negotiated maximum — the same
// frame the writer rejected first. // frame the writer rejected first.
await FaultOnOversizedEventAsync( FaultOnOversizedEventAsync(
FindOversizedEvent(events, envelopes), FindOversizedEvent(events, envelopes),
exception, exception,
cancellationToken) cancellationToken)
.ConfigureAwait(false); .GetAwaiter()
.GetResult();
} }
} }
} }
@@ -79,6 +79,16 @@ public interface IWorkerRuntimeSession : IDisposable
/// <returns>A task that completes when the queue is signalled or the timeout elapses.</returns> /// <returns>A task that completes when the queue is signalled or the timeout elapses.</returns>
Task WaitForEventsAsync(TimeSpan timeout, CancellationToken cancellationToken); Task WaitForEventsAsync(TimeSpan timeout, CancellationToken cancellationToken);
/// <summary>
/// Blocking form of <see cref="WaitForEventsAsync"/> for a drain loop that runs on its own
/// dedicated thread. Same contract (coalesced signal, timeout is a ceiling, at most one
/// waiter); it parks the calling thread instead of scheduling a thread-pool continuation per
/// wake, which on .NET Framework also triggers the pool's spin-wait on every event.
/// </summary>
/// <param name="timeout">Maximum time to wait before the wait completes unsignalled.</param>
/// <param name="cancellationToken">Token cancelling the wait.</param>
void WaitForEvents(TimeSpan timeout, CancellationToken cancellationToken);
/// <summary> /// <summary>
/// Drains a pending fault from the queue, if any. /// Drains a pending fault from the queue, if any.
/// </summary> /// </summary>
@@ -8,6 +8,9 @@ public sealed class MxAccessComObjectFactory : IMxAccessComObjectFactory
/// <inheritdoc /> /// <inheritdoc />
public object Create() public object Create()
{ {
// Warm oleaut32's record-info cache before the first OnDataChange can fire; see
// MxStatusProxyRecordInfoPin for why the per-event lookup is otherwise expensive.
MxStatusProxyRecordInfoPin.EnsurePinned();
return new LMXProxyServerClass(); return new LMXProxyServerClass();
} }
} }
@@ -320,6 +320,17 @@ public sealed class MxAccessEventQueue
return eventSignal.WaitAsync(timeout, cancellationToken); return eventSignal.WaitAsync(timeout, cancellationToken);
} }
/// <summary>
/// Blocking form of <see cref="WaitForEventsAsync"/>: parks the calling (dedicated drain)
/// thread on the same coalesced signal. Same single-waiter precondition.
/// </summary>
/// <param name="timeout">Maximum time to wait before the wait completes unsignalled.</param>
/// <param name="cancellationToken">Token cancelling the wait.</param>
public void WaitForEvents(TimeSpan timeout, CancellationToken cancellationToken)
{
_ = eventSignal.Wait(timeout, cancellationToken);
}
/// <summary> /// <summary>
/// Attempts to dequeue the next event without removing it if empty. /// Attempts to dequeue the next event without removing it if empty.
/// </summary> /// </summary>
@@ -682,6 +682,12 @@ public sealed class MxAccessStaSession : IWorkerRuntimeSession
return eventQueue.WaitForEventsAsync(timeout, cancellationToken); return eventQueue.WaitForEventsAsync(timeout, cancellationToken);
} }
/// <inheritdoc />
public void WaitForEvents(TimeSpan timeout, CancellationToken cancellationToken)
{
eventQueue.WaitForEvents(timeout, cancellationToken);
}
/// <inheritdoc /> /// <inheritdoc />
public WorkerFault? DrainFault() public WorkerFault? DrainFault()
{ {
@@ -0,0 +1,113 @@
using System;
using System.Linq;
using System.Runtime.InteropServices;
using ArchestrA.MxAccess;
namespace ZB.MOM.WW.MxGateway.Worker.MxAccess;
/// <summary>
/// Keeps the LMXProxy type library and the <c>MXSTATUS_PROXY</c> record info alive for the
/// lifetime of the worker process, so the MXAccess proxy's per-event
/// <c>GetRecordInfoFromGuids</c> call is served from oleaut32's cache.
/// </summary>
/// <remarks>
/// Every <c>OnDataChange</c> carries a SAFEARRAY of <c>MXSTATUS_PROXY</c> records, and
/// <c>lmxproxy.dll</c> resolves that record type with <c>GetRecordInfoFromGuids</c> each time it
/// builds the array. With no live reference, oleaut32 re-resolves the type library through the
/// registry and reloads it on every call: about 1 ms per event on an idle dev box. Under WOW64
/// and an EDR registry filter it is worse; a WPR trace on wonder-app-vd03 put it at about 70% of
/// the STA's CPU. While one <c>IRecordInfo</c> for the type is held, the same call returns the
/// cached instance in well under a microsecond. The pin is a process-wide cache warm-up only: it
/// changes no MXAccess behavior, and failure to pin is reported but never fatal.
/// </remarks>
public static class MxStatusProxyRecordInfoPin
{
private static readonly object Gate = new();
private static IntPtr pinnedTypeLib;
private static IntPtr pinnedRecordInfo;
private static bool attempted;
/// <summary>Gets the outcome of the pin attempt, or <see langword="null"/> before the first attempt.</summary>
public static string? Status { get; private set; }
/// <summary>
/// Pins the type library and record info once per process. Safe to call repeatedly and from
/// any thread; only the first call does work.
/// </summary>
/// <returns><see langword="true"/> when the record info is pinned.</returns>
public static bool EnsurePinned()
{
lock (Gate)
{
if (attempted)
{
return pinnedRecordInfo != IntPtr.Zero;
}
attempted = true;
try
{
Guid libraryGuid = typeof(LMXProxyServerClass).Assembly
.GetCustomAttributes(typeof(GuidAttribute), inherit: false)
.Cast<GuidAttribute>()
.Select(attribute => new Guid(attribute.Value))
.Single();
TypeLibVersionAttribute version = typeof(LMXProxyServerClass).Assembly
.GetCustomAttributes(typeof(TypeLibVersionAttribute), inherit: false)
.Cast<TypeLibVersionAttribute>()
.Single();
Guid recordGuid = typeof(MXSTATUS_PROXY).GUID;
int hr = LoadRegTypeLib(
ref libraryGuid,
(ushort)version.MajorVersion,
(ushort)version.MinorVersion,
0,
out pinnedTypeLib);
if (hr != 0)
{
Status = $"LoadRegTypeLib failed (HRESULT 0x{hr:X8})";
return false;
}
hr = GetRecordInfoFromGuids(
ref libraryGuid,
(uint)version.MajorVersion,
(uint)version.MinorVersion,
0,
ref recordGuid,
out pinnedRecordInfo);
if (hr != 0)
{
Status = $"GetRecordInfoFromGuids failed (HRESULT 0x{hr:X8})";
return false;
}
Status = $"pinned {libraryGuid:B} v{version.MajorVersion}.{version.MinorVersion} record {recordGuid:B}";
return true;
}
catch (Exception exception)
{
Status = $"pin failed: {exception.GetType().Name}: {exception.Message}";
return false;
}
}
}
[DllImport("oleaut32.dll")]
private static extern int LoadRegTypeLib(
ref Guid libraryGuid,
ushort majorVersion,
ushort minorVersion,
uint lcid,
out IntPtr typeLib);
[DllImport("oleaut32.dll")]
private static extern int GetRecordInfoFromGuids(
ref Guid libraryGuid,
uint majorVersion,
uint minorVersion,
uint lcid,
ref Guid typeGuid,
out IntPtr recordInfo);
}
+454
View File
@@ -0,0 +1,454 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "aho-corasick"
version = "1.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c982642fa9e8606056828ee9a8505737230110bb1099153c79efe865c59d12ba"
dependencies = [
"memchr",
]
[[package]]
name = "anyhow"
version = "1.0.104"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "330a5ed07fa54e4702c9d6c4174f74427fc0ef6e214bbd677ae50a5099946470"
[[package]]
name = "autocfg"
version = "1.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f2032f911046de80f0a198e0901378627c33f59ea0ac00e363d481118bd70a53"
[[package]]
name = "bitflags"
version = "2.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3ded4057c258ba199e2d26386d3af3780957ecaee6c4ef4041c6b4b8b97c0b06"
[[package]]
name = "bytes"
version = "1.12.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "776dfebd015e2d8732c684a2c71003674ccb49457b539b9e82151ba38e96dd7c"
[[package]]
name = "cfg-if"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4e7648175b45a9a48536d676f68d918270699102aa8dab5496df06904c914600"
[[package]]
name = "chrono"
version = "0.4.45"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1aa79e62e7697b8e29b513a68abacf485adcd1fe8284a4316c5ae868e6633327"
dependencies = [
"num-traits",
]
[[package]]
name = "either"
version = "1.19.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0e9c71c2167ca323c882b99918929403426e2373ea17242ff5653e0d5e1058be"
[[package]]
name = "equivalent"
version = "1.0.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "00d174d5400e5e8fd687ad1049e2f578285fa914201b1af7e8b112a4546bd826"
[[package]]
name = "errno"
version = "0.3.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e35d6e78f4cebb90e51d543db8fe41528851d6deeef6ef21ebd4bfdc1e784776"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "fastrand"
version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "da7c62ceae207dd37ea5b845da6a0696c799f85e97da1ab5b7910be3c1c80223"
[[package]]
name = "fixedbitset"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5905ec755cc8386d7928353e4d356d2be3e89e81e5ca01fe630a1ca4d08dc175"
[[package]]
name = "getrandom"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "300e883d756b2e4ec94e02791f39b04b522276138852cfc41d9fb7e904106099"
dependencies = [
"cfg-if",
"libc",
"r-efi",
]
[[package]]
name = "hashbrown"
version = "0.17.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed5909b6e89a2db4456e54cd5f673791d7eca6732202bbf2a9cc504fe2f9b84a"
[[package]]
name = "heck"
version = "0.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea"
[[package]]
name = "indexmap"
version = "2.14.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cc4e190f5d26ca7051642629da2c52fc03bde85a03197c99408dcd291734c855"
dependencies = [
"equivalent",
"hashbrown",
]
[[package]]
name = "itertools"
version = "0.14.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b192c782037fadd9cfa75548310488aabdbf3d2da73885b31bd0abd03351285"
dependencies = [
"either",
]
[[package]]
name = "libc"
version = "0.2.191"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "65cd87c9a6d60536580b316ad985726104730a5151c99845f2490428ef91240a"
[[package]]
name = "linux-raw-sys"
version = "0.12.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32a66949e030da00e8c7d4434b251670a91556f4144941d37452769c25d58a53"
[[package]]
name = "log"
version = "0.4.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5d15f86d12da44cd8d4ec3c73984537eb7f4cadcb9776a575a0a8a05ca11005c"
[[package]]
name = "memchr"
version = "2.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cf8baf1c55e62ffcace7a9f06f4bd9cd3f0c4beb022d3b367256b91b87513d98"
[[package]]
name = "mio"
version = "1.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "20521a903f02b0a573cd6840e9d0a5a0cc043d361fe924092cd70f8556cc9c4a"
dependencies = [
"libc",
"wasi",
"windows-sys",
]
[[package]]
name = "multimap"
version = "0.10.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1d87ecb2933e8aeadb3e3a02b828fed80a7528047e68b4f424523a0981a3a084"
[[package]]
name = "mxgateway-worker-rust"
version = "0.1.0"
dependencies = [
"chrono",
"prost",
"prost-build",
"prost-types",
"roxmltree",
"tokio",
"uuid",
"windows-sys",
]
[[package]]
name = "num-traits"
version = "0.2.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "071dfc062690e90b734c0b2273ce72ad0ffa95f0c74596bc250dcfd960262841"
dependencies = [
"autocfg",
]
[[package]]
name = "once_cell"
version = "1.21.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f7c3e4beb33f85d45ae3e3a1792185706c8e16d043238c593331cc7cd313b50"
[[package]]
name = "petgraph"
version = "0.7.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3672b37090dbd86368a4145bc067582552b29c27377cad4e0a306c97f9bd7772"
dependencies = [
"fixedbitset",
"indexmap",
]
[[package]]
name = "pin-project-lite"
version = "0.2.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c938015719552d665fd0553b90d96cd7ff58e83e4c061ca99e6bb78865a72752"
[[package]]
name = "prettyplease"
version = "0.2.37"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "479ca8adacdd7ce8f1fb39ce9ecccbfe93a3f1344b3d0d97f20bc0196208f62b"
dependencies = [
"proc-macro2",
"syn 2.0.119",
]
[[package]]
name = "proc-macro2"
version = "1.0.107"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "985e7ec9bb745e6ce6535b544d84d6cd6f7ad8bd711c398938ae983b91a766d9"
dependencies = [
"unicode-ident",
]
[[package]]
name = "prost"
version = "0.13.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2796faa41db3ec313a31f7624d9286acf277b52de526150b7e69f3debf891ee5"
dependencies = [
"bytes",
"prost-derive",
]
[[package]]
name = "prost-build"
version = "0.13.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "be769465445e8c1474e9c5dac2018218498557af32d9ed057325ec9a41ae81bf"
dependencies = [
"heck",
"itertools",
"log",
"multimap",
"once_cell",
"petgraph",
"prettyplease",
"prost",
"prost-types",
"regex",
"syn 2.0.119",
"tempfile",
]
[[package]]
name = "prost-derive"
version = "0.13.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a56d757972c98b346a9b766e3f02746cde6dd1cd1d1d563472929fdd74bec4d"
dependencies = [
"anyhow",
"itertools",
"proc-macro2",
"quote",
"syn 2.0.119",
]
[[package]]
name = "prost-types"
version = "0.13.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "52c2c1bf36ddb1a1c396b3601a3cec27c2462e45f07c386894ec3ccf5332bd16"
dependencies = [
"prost",
]
[[package]]
name = "quote"
version = "1.0.47"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1fbf4db142a473a8d80c26bbf18454ed458bf8d26c8219c331daecfdbd079001"
dependencies = [
"proc-macro2",
]
[[package]]
name = "r-efi"
version = "6.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f8dcc9c7d52a811697d2151c701e0d08956f92b0e24136cf4cf27b57a6a0d9bf"
[[package]]
name = "regex"
version = "1.13.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9807af0e638c12e753aa1b4e601791bf2a8f71274023c8bbeec12fc149950857"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "41d60ef07df457506a0a1a2073112eb4aae5733dbd2813c3da77d1b0fd441158"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d902046e6a28a1e4c3cab97258a0ce11e994903f01c2d97a9447a0b3df85f4f6"
[[package]]
name = "roxmltree"
version = "0.21.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9da7f59328b09405b181fee1018925fad50f157afb43c0c0f5c522c256dd5907"
dependencies = [
"memchr",
]
[[package]]
name = "rustix"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e5e76742e5294153d4dde1207dbec5bdd81eef6fabdc0d2118babadabfd858aa"
dependencies = [
"bitflags",
"errno",
"libc",
"linux-raw-sys",
"windows-sys",
]
[[package]]
name = "socket2"
version = "0.6.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d1e2c7f27f8d4cb10542a02c49005dbd6e93095799d6f3be745fae9f8fedd4"
dependencies = [
"libc",
"windows-sys",
]
[[package]]
name = "syn"
version = "2.0.119"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "872831b642d1a07999a962a351ed35b955ea2cfc8f3862091e2a240a84f17297"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "syn"
version = "3.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4ae9ccbea3a7b0b09443036490c8f34a8b1b760fd345e5c0dae0aac718faea79"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "tempfile"
version = "3.27.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "32497e9a4c7b38532efcdebeef879707aa9f794296a4f0244f6f69e9bc8574bd"
dependencies = [
"fastrand",
"getrandom",
"once_cell",
"rustix",
"windows-sys",
]
[[package]]
name = "tokio"
version = "1.54.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b52fd3255258d26fac7264ab5a76b53c13367136dc011d3d6ad4f8ed070396a3"
dependencies = [
"bytes",
"libc",
"mio",
"pin-project-lite",
"socket2",
"tokio-macros",
"windows-sys",
]
[[package]]
name = "tokio-macros"
version = "2.7.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "78773a2a397f451582ce068015985c33193cf6dea8b74d2a639fe457b2f07b0e"
dependencies = [
"proc-macro2",
"quote",
"syn 3.0.10",
]
[[package]]
name = "unicode-ident"
version = "1.0.27"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a2c754d6c33795a1c324727428e5a7dedb5b06195f9890bdbcba760d3e246563"
[[package]]
name = "uuid"
version = "1.29.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3c14b15dffa2f4bd712103e857140aa04a4f2ff6e955c77d7eb8e53ee02a3b64"
[[package]]
name = "wasi"
version = "0.11.1+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ccf3ec651a847eb01de73ccad15eb7d99f80485de043efb2f370cd654f4ea44b"
[[package]]
name = "windows-link"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5"
[[package]]
name = "windows-sys"
version = "0.61.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc"
dependencies = [
"windows-link",
]
+39
View File
@@ -0,0 +1,39 @@
[package]
name = "mxgateway-worker-rust"
version = "0.1.0"
edition = "2021"
publish = false
[features]
alarm-test-faults = []
[dependencies]
prost = "0.13.5"
prost-types = "0.13.5"
tokio = { version = "1.48", features = ["io-util"] }
[target.'cfg(windows)'.dependencies]
tokio = { version = "1.48", features = ["macros", "net", "rt", "sync", "time"] }
chrono = { version = "0.4", default-features = false, features = ["std"] }
roxmltree = "0.21"
uuid = { version = "1", default-features = false }
windows-sys = { version = "0.61", features = [
"Win32_Foundation",
"Win32_Globalization",
"Win32_Security",
"Win32_System_Com",
"Win32_System_Diagnostics_ToolHelp",
"Win32_System_JobObjects",
"Win32_System_Ole",
"Win32_System_RemoteDesktop",
"Win32_System_Threading",
"Win32_System_Time",
"Win32_System_Variant",
"Win32_UI_WindowsAndMessaging",
] }
[build-dependencies]
prost-build = "0.13.5"
[dev-dependencies]
tokio = { version = "1.48", features = ["macros", "rt"] }
+176
View File
@@ -0,0 +1,176 @@
# Rust MXAccess worker prototype
This crate exercises the gateway's existing worker contract with native 32-bit
Windows COM. Cargo generates the Rust protobuf types from the gateway's canonical
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 registration, item lifecycle and advice, value snapshots, identity
lookup, writes, and the worker diagnostic commands. The worker attaches to
both MXAccess COM connection points to deliver OnDataChange, OperationComplete,
OnWriteComplete, and OnBufferedDataChange 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.
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. The worker serializes COM commands, accepts
cancellation for commands still waiting, and continues sending events and
heartbeats while a command runs. A COM call that exceeds the STA ceiling faults
the session.
Bulk item operations return one ordered result per requested item. ReadBulk
uses a cached value for an existing advice when available and otherwise uses
temporary advice with a shared timeout. Duplicate snapshot reads share one
subscription.
The worker supports Write, Write2, WriteSecured, and WriteSecured2, including
their bulk forms. It converts scalar and typed array inputs to Automation
VARIANTs. For a timestamped write, a protobuf timestamp becomes local
wall-clock time for MXAccess. Single-item replies carry the matching
OnWriteComplete status rows when MXAccess responds within the configured wait
(default 1,500 ms). This correlation uses the server and item handles supplied
by MXAccess. The event also flows through StreamEvents. Bulk results report
per-item COM acceptance and clients can observe commit outcomes on the event
stream.
## Native alarm provider
The Rust worker handles alarm subscription, active snapshots, standby watch-list
replacement, and authenticated acknowledgement in the worker process. It uses
the installed 32-bit AVEVA WnWrap COM alarm consumer for Alarmmgr and MXAccess
advice for the optional subtag fallback. The Rust executable launches itself in
a helper mode with a dedicated COM thread for WnWrap registration and polling.
Each snapshot request has a five-second deadline. If WnWrap stalls or fails,
the worker discards the helper and starts a fresh one for the next attempt,
while the MXAccess STA keeps serving commands and subtag subscriptions.
For a local Galaxy subscription with automatic fallback, the worker also watches
the Session 0 Alarmmgr process once it can open a handle to it. It waits for a
replacement process before probing and requires consecutive successful snapshots
before switching back. Snapshots retain records a capped fetch may have omitted.
To acknowledge a Galaxy alarm, the worker authenticates the operator with
MXAccess, writes the comment to the alarm's `AckMsg` attribute, and waits for
`OnWriteComplete` within the configured write-completion window. Deploy the
Rust executable on a Windows host with the System Platform COM components
registered. The Rust worker has no .NET worker companion.
## Build and test
From this directory on Windows, with Rust, the x86 MSVC target and linker, and
`protoc` on PATH:
```powershell
cargo build --locked --release --target i686-pc-windows-msvc
cargo test --locked --target i686-pc-windows-msvc
```
On a development Windows host with 32-bit System Platform MXAccess registered,
run the opt-in test against a temporary pipe. Set
`MXGATEWAY_RUST_SMOKE_ITEM` to an item available in that host's Galaxy to include
AddItem and RemoveItem in the smoke test.
```powershell
$env:MXGATEWAY_RUST_SMOKE_ITEM = 'TestChildObject.TestInt'
cargo test --locked --target i686-pc-windows-msvc --test live_mxaccess -- --ignored
```
The smoke test starts and terminates its own worker and exercises the pipe
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.
Run the write round-trip on a development host with a writable integer test
item. The test writes and reads back distinct values, checks OnWriteComplete
and unary status rows, exercises partial bulk failure, and restores the
original value through Write2.
```powershell
$env:MXGATEWAY_LIVE_MXACCESS_ITEM = 'TestChildObject.TestInt'
dotnet test src/ZB.MOM.WW.MxGateway.IntegrationTests --filter FullyQualifiedName~GatewayGrpc_WithRustWorker_WritesAndReceivesOnWriteComplete
```
The bulk and subtag tests use the deployed `TestChangingInt` items. The
Alarmmgr test raises `Galaxy!TestArea.TestMachine_001.TestAlarm001`, checks that
the gateway rejects an unknown operator, and verifies the authenticated
acknowledgement and its operator attribution on `StreamAlarms`. It restores the
test UDA afterward. The development rig uses `Administrator` with an empty
password. The test accepts credentials through
`MXGATEWAY_LIVE_MXACCESS_WRITE_SECURED_USER` and
`MXGATEWAY_LIVE_MXACCESS_WRITE_SECURED_PASSWORD`.
```powershell
dotnet test src/ZB.MOM.WW.MxGateway.IntegrationTests --filter 'FullyQualifiedName~GatewayGrpc_WithRustWorker_'
```
### Verify automatic alarm failover and failback
Build a debug worker with the test fault enabled and run the dedicated live
gateway test on the Windows development host:
```powershell
cargo build --locked --features alarm-test-faults --target i686-pc-windows-msvc --manifest-path src/mxgateway-worker-rust/Cargo.toml
$env:MXGATEWAY_RUN_RUST_WORKER_LIVE_TESTS = '1'
$env:MXGATEWAY_RUN_RUST_ALARM_FAILOVER_TEST = '1'
$env:MXGATEWAY_LIVE_MXACCESS_WORKER_EXE = (Resolve-Path 'src/mxgateway-worker-rust/target/i686-pc-windows-msvc/debug/mxgateway-worker-rust.exe').Path
dotnet test src/ZB.MOM.WW.MxGateway.IntegrationTests --filter 'FullyQualifiedName~StreamsAcrossAutomaticAlarmFailoverAndFailback'
```
The test stalls the Rust helper's Alarmmgr snapshot fetches past the worker's
request deadline. It drives the same development alarm through
`Alarmmgr → Subtag → Alarmmgr` and checks both provider statuses and subsequent
transitions on one open `StreamAlarms` call. The fault injection is compiled only
when both debug assertions and the Cargo feature are enabled.
For a real outage, build the normal worker and point
`MXGATEWAY_TEST_ALARMMGR_OUTAGE_DIRECTORY` at a new, empty directory on the host.
Run `GatewayGrpc_WithRustWorker_StreamsAcrossRealAlarmmgrOutage` with the live
test options above. An external controller stops Alarmmgr when the test creates
`stop-alarmmgr` and restores it when the test creates `restore-alarmmgr`.
The controller creates `ready-alarmmgr` when the replacement is running and
the test alarm's AppEngine is on scan. The controller should also restore
Alarmmgr on timeout or failure. The test uses live subtags while Alarmmgr is
stopped and waits for a fresh Alarmmgr raise after recovery on the original
stream.
## 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. 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`
- REA write member evidence for `ILMXProxyServer` and `ILMXProxyServer4`
`ev_1b4fe7ab436437391b76f9fa3a300f45b58a4e1cff6aa84e30854f653c0a20df`
- REA extended COM and buffered event member evidence
`ev_39de39d626e3a933b259fa2650b42311ba60d6844d7b012284467284e8b82977`
- REA member evidence for the WnWrap v1 lifecycle and v2 XML snapshot interfaces
`ev_4fd97bf483b1cde2544b93a90f4de97569ea95c24f1eaa8aee49b5a1ed552558`
+10
View File
@@ -0,0 +1,10 @@
use std::{io, path::Path};
fn main() -> io::Result<()> {
let directory = Path::new("../ZB.MOM.WW.MxGateway.Contracts/Protos");
let worker = directory.join("mxaccess_worker.proto");
let gateway = directory.join("mxaccess_gateway.proto");
println!("cargo:rerun-if-changed={}", worker.display());
println!("cargo:rerun-if-changed={}", gateway.display());
prost_build::compile_protos(&[worker, gateway], &[directory])
}
+654
View File
@@ -0,0 +1,654 @@
mod ack;
mod local_alarmmgr;
mod record;
pub(crate) mod snapshot_worker;
mod subtag;
mod wnwrap;
use std::collections::BTreeMap;
use std::sync::mpsc::TryRecvError;
use std::time::{Duration, Instant, SystemTime};
use tokio::sync::oneshot;
use uuid::Uuid;
use windows_sys::core::HRESULT;
use windows_sys::Win32::Foundation::{
ERROR_NOT_READY, ERROR_SERVICE_NOT_ACTIVE, ERROR_TIMEOUT, E_FAIL,
};
use windows_sys::Win32::System::Threading::INFINITE;
use crate::mxaccess::{CommandError, CommandResult, Outcome, Proxy};
use crate::mxevent::{EventFault, Publisher};
use crate::wire::mxaccess_gateway::v1::{
mx_command::Payload, mx_command_reply::Payload as Reply, mx_event,
AcknowledgeAlarmReplyPayload, AlarmFailoverConfig, AlarmProviderMode as Mode, MxCommand,
MxCommandKind as Kind, MxCommandReply, MxEvent, MxEventFamily, OnAlarmProviderModeChangedEvent,
ProtocolStatus, ProtocolStatusCode as Code, QueryActiveAlarmsReplyPayload,
SubscribeAlarmsCommand,
};
use local_alarmmgr::{LocalAlarmmgr, Status as ManagerStatus};
use record::{parse_snapshot, Record, State};
use snapshot_worker::SnapshotProcess;
use subtag::Subtags;
pub fn handles(kind: Kind) -> bool {
matches!(
kind,
Kind::SubscribeAlarms
| Kind::UnsubscribeAlarms
| Kind::UpdateAlarmWatchList
| Kind::QueryActiveAlarms
| Kind::AcknowledgeAlarm
| Kind::AcknowledgeAlarmByName
)
}
struct AlarmError {
code: Code,
hr: Option<HRESULT>,
message: String,
}
impl AlarmError {
fn invalid(message: &str) -> Self {
Self {
code: Code::InvalidRequest,
hr: Some(-1),
message: message.into(),
}
}
fn failed(step: &str, hr: HRESULT) -> Self {
Self {
code: Code::MxaccessFailure,
hr: Some(hr),
message: format!("{step} failed (HRESULT 0x{:08X})", hr as u32),
}
}
fn unavailable(message: &str) -> Self {
Self {
code: Code::MxaccessFailure,
hr: None,
message: message.into(),
}
}
fn com(step: &str, error: CommandError) -> Self {
Self::failed(
step,
match error {
CommandError::HResult(hr) => hr,
_ => E_FAIL,
},
)
}
}
fn handle(result: Outcome, step: &str) -> Result<i32, AlarmError> {
match result.map_err(|error| AlarmError::com(step, error))? {
CommandResult::Handle(value) => Ok(value),
_ => unreachable!("handle-returning command"),
}
}
struct Primary {
expression: String,
source: Option<SnapshotProcess>,
local_manager: Option<LocalAlarmmgr>,
pending_until: Option<Instant>,
require_live_query: bool,
records: BTreeMap<Uuid, Record>,
truncated: bool,
}
impl Primary {
fn poll(
&mut self,
proxy: &Proxy,
limit: i32,
emit: bool,
may_request: bool,
now: Instant,
) -> Option<Result<(), HRESULT>> {
let manager = self.local_manager.as_mut().map(LocalAlarmmgr::observe);
let manager_missing = manager == Some(ManagerStatus::Missing);
let manager_exited = manager == Some(ManagerStatus::Exited);
self.require_live_query |= manager_exited;
if manager_exited || (emit && manager_missing) {
let lost_source = self.source.is_some();
if manager_exited {
self.pending_until = None;
if let Some(source) = self.source.take() {
source.finish_after_manager_exit();
}
} else {
self.reset();
}
return (may_request || lost_source)
.then_some(Err((0x8007_0000 | ERROR_SERVICE_NOT_ACTIVE) as HRESULT));
}
if let Some(deadline) = self.pending_until {
let reply = self
.source
.as_ref()
.expect("request has a helper")
.replies
.try_recv();
return match reply {
Ok(reply) => {
self.pending_until = None;
let result = match reply {
Ok(None) if !manager_missing => {
// Keep the registration while the replacement connects to Galaxy.
return Some(Err((0x8007_0000 | ERROR_NOT_READY) as HRESULT));
}
Ok(Some(xml)) if !manager_missing => {
self.apply_snapshot(proxy, limit, emit, &xml)
}
Ok(_) => Err((0x8007_0000 | ERROR_SERVICE_NOT_ACTIVE) as HRESULT),
Err(hr) => Err(hr),
};
if result.is_err() {
self.reset();
}
Some(result)
}
Err(TryRecvError::Empty) if now < deadline => None,
Err(error) => {
self.reset();
Some(Err(if error == TryRecvError::Empty {
(0x8007_0000 | ERROR_TIMEOUT) as HRESULT
} else {
E_FAIL
}))
}
};
}
if may_request {
let starting = self.source.is_none();
if starting {
self.source = match SnapshotProcess::start(&self.expression, limit) {
Ok(source) => Some(source),
Err(error) => {
eprintln!("Failed to start WnWrap snapshot worker: {error}");
return Some(Err(E_FAIL));
}
};
}
if self
.source
.as_mut()
.expect("started helper")
.request(self.require_live_query)
.is_err()
{
self.reset();
return Some(Err(E_FAIL));
}
// A recovery probe can activate WnWrap while Alarmmgr is absent.
// Its initial request also has to register, subscribe and query.
let seconds = if starting && !emit { 12 } else { 5 };
self.pending_until = Some(now + Duration::from_secs(seconds));
}
None
}
fn reset(&mut self) {
let idle = self.pending_until.take().is_none();
if let Some(source) = self.source.take() {
if idle {
// Let WnWrap release its registration and shared Alarmmgr user count.
source.finish();
}
}
}
fn apply_snapshot(
&mut self,
proxy: &Proxy,
limit: i32,
emit: bool,
xml: &str,
) -> Result<(), HRESULT> {
if xml.trim().is_empty() {
return if emit { Ok(()) } else { Err(E_FAIL) };
}
let (records, count) = parse_snapshot(xml).ok_or(E_FAIL)?;
let previous = std::mem::replace(&mut self.records, records);
self.truncated = count >= limit as usize;
if emit {
for (&id, record) in &self.records {
let prior = previous
.get(&id)
.map_or(State::Unknown, |record| record.state);
publish_record(&proxy.publisher, record, prior, Mode::Alarmmgr);
}
}
if self.truncated {
for (id, record) in previous {
self.records.entry(id).or_insert(record);
}
}
Ok(())
}
}
struct Subscription {
mode: Mode,
primary: Option<Primary>,
subtags: Option<Subtags>,
automatic: bool,
next_probe: Instant,
failures: i32,
stable_probes: i32,
startup: Option<oneshot::Sender<Result<(), HRESULT>>>,
settings: AlarmFailoverConfig,
}
pub struct AlarmRuntime {
subscription: Option<Subscription>,
interval: Duration,
next_poll: Instant,
limit: i32,
}
impl AlarmRuntime {
pub fn new() -> Self {
Self {
subscription: None,
interval: Duration::from_millis(setting(
"MXGATEWAY_ALARM_POLL_INTERVAL_MS",
500,
100..=3_600_000,
) as u64),
next_poll: Instant::now(),
limit: setting("MXGATEWAY_ALARM_MAX_ALARMS_PER_FETCH", 1024, 64..=65_536),
}
}
pub fn wait_millis(&self) -> u32 {
if self.subscription.is_none() {
return INFINITE;
}
let remaining = self.next_poll.saturating_duration_since(Instant::now());
remaining.as_nanos().div_ceil(1_000_000) as u32
}
pub fn close(&mut self, proxy: &mut Proxy) {
if let Some(mut subscription) = self.subscription.take() {
if let Some(source) = subscription.primary.and_then(|primary| primary.source) {
source.finish();
}
if let Some(subtags) = &mut subscription.subtags {
subtags.close(proxy);
}
}
}
pub fn poll(&mut self, proxy: &Proxy) {
let Some(subscription) = &mut self.subscription else {
return;
};
if let Some(subtags) = &mut subscription.subtags {
subtags.drain(proxy, subscription.mode == Mode::Subtag);
}
let now = Instant::now();
if now < self.next_poll {
return;
}
self.next_poll = now + self.interval;
let Some(primary) = &mut subscription.primary else {
return;
};
let probe_interval =
Duration::from_secs(subscription.settings.failback_probe_interval_seconds as u64);
let may_request = subscription.mode == Mode::Alarmmgr || now >= subscription.next_probe;
let outcome = primary.poll(
proxy,
self.limit,
subscription.mode == Mode::Alarmmgr,
may_request,
now,
);
if primary.pending_until.is_some() {
self.next_poll = now + self.interval.min(Duration::from_millis(50));
}
let Some(outcome) = outcome else {
return;
};
if let Some(startup) = subscription.startup.take() {
let _ = startup.send(outcome);
if outcome.is_err() {
self.subscription = None;
return;
}
}
match subscription.mode {
Mode::Alarmmgr => match outcome {
Ok(()) => {
primary.require_live_query = false;
subscription.failures = 0;
}
Err(hr) if subscription.automatic => {
subscription.failures += 1;
if subscription.failures >= subscription.settings.consecutive_failure_threshold
{
subscription.mode = Mode::Subtag;
subscription.next_probe = now + probe_interval;
subscription.stable_probes = 0;
publish_mode(&proxy.publisher, Mode::Subtag, "primary poll failed", hr);
}
}
Err(hr) => {
subscription.failures += 1;
if subscription.failures >= 3 {
proxy.publisher.report_fault(EventFault::AlarmPoll(hr));
}
}
},
Mode::Subtag => {
subscription.next_probe = now + probe_interval;
if outcome.is_ok() {
subscription.stable_probes += 1;
if subscription.stable_probes >= subscription.settings.failback_stable_probes {
subscription.mode = Mode::Alarmmgr;
subscription.failures = 0;
primary.require_live_query = false;
publish_mode(&proxy.publisher, Mode::Alarmmgr, "recovered", 0);
}
} else {
subscription.stable_probes = 0;
}
}
_ => {}
}
}
fn subscribe(
&mut self,
proxy: &mut Proxy,
request: SubscribeAlarmsCommand,
) -> Result<(), AlarmError> {
if self.subscription.is_some() {
return Err(AlarmError::unavailable(
"Alarm provider is already subscribed",
));
}
let mode = Mode::try_from(request.forced_mode)
.map_err(|_| AlarmError::invalid("Invalid alarm provider mode"))?;
if request.subscription_expression.trim().is_empty()
|| request.subscription_expression.contains('\0')
{
return Err(AlarmError::invalid(
"A valid alarm subscription expression is required",
));
}
let automatic = mode == Mode::Unspecified
&& (request.fallback_enabled || !request.watch_list.is_empty());
let settings = request.failover.unwrap_or(AlarmFailoverConfig {
consecutive_failure_threshold: 3,
failback_probe_interval_seconds: 30,
failback_stable_probes: 3,
});
if automatic
&& (settings.consecutive_failure_threshold < 1
|| settings.failback_probe_interval_seconds < 1
|| settings.failback_stable_probes < 1)
{
return Err(AlarmError::invalid(
"Alarm failover settings must be positive",
));
}
let local_manager = automatic
.then(|| LocalAlarmmgr::for_subscription(&request.subscription_expression))
.flatten();
let require_live_query = local_manager.is_some();
let mut subscription = Subscription {
mode: if mode == Mode::Subtag {
Mode::Subtag
} else {
Mode::Alarmmgr
},
primary: (mode != Mode::Subtag).then(|| Primary {
local_manager,
expression: request.subscription_expression,
source: None,
pending_until: None,
require_live_query,
records: BTreeMap::new(),
truncated: false,
}),
subtags: None,
automatic,
next_probe: Instant::now(),
failures: 0,
stable_probes: 0,
startup: None,
settings,
};
if automatic || mode == Mode::Subtag {
subscription.subtags = Some(Subtags::attach(proxy, request.watch_list)?);
}
self.subscription = Some(subscription);
self.next_poll = Instant::now();
Ok(())
}
pub fn execute(
&mut self,
proxy: &mut Proxy,
command: MxCommand,
) -> (
MxCommandReply,
Option<oneshot::Receiver<Result<(), HRESULT>>>,
) {
let mut reply = MxCommandReply {
kind: command.kind,
hresult: Some(0),
..Default::default()
};
let result = self.dispatch(proxy, command, &mut reply);
let mut readiness = None;
if result.is_ok() && reply.kind() == Kind::SubscribeAlarms {
if let Some(subscription) = self
.subscription
.as_mut()
.filter(|subscription| subscription.primary.is_some() && !subscription.automatic)
{
let (sender, receiver) = oneshot::channel();
subscription.startup = Some(sender);
readiness = Some(receiver);
}
}
let (code, message) = match result {
Ok(()) => (Code::Ok, std::mem::take(&mut reply.diagnostic_message)),
Err(error) => {
reply.hresult = error.hr;
if matches!(
reply.kind(),
Kind::AcknowledgeAlarm | Kind::AcknowledgeAlarmByName
) {
reply.payload = Some(Reply::AcknowledgeAlarm(AcknowledgeAlarmReplyPayload {
native_status: error.hr.unwrap_or(-1),
}));
}
(error.code, error.message)
}
};
reply.protocol_status = Some(ProtocolStatus {
code: code as i32,
message: message.clone(),
});
reply.diagnostic_message = message;
(reply, readiness)
}
fn dispatch(
&mut self,
proxy: &mut Proxy,
command: MxCommand,
reply: &mut MxCommandReply,
) -> Result<(), AlarmError> {
match (command.kind(), command.payload) {
(Kind::SubscribeAlarms, Some(Payload::SubscribeAlarms(request))) => {
self.subscribe(proxy, request)
}
(Kind::UnsubscribeAlarms, Some(Payload::UnsubscribeAlarms(_))) => {
self.close(proxy);
Ok(())
}
(Kind::UpdateAlarmWatchList, Some(Payload::UpdateAlarmWatchList(request))) => {
let subscription = self.subscription.as_mut().filter(|subscription| subscription.automatic)
.ok_or_else(|| AlarmError::unavailable("Alarm watch list replacement requires an automatic fallback subscription"))?;
let replacement = Subtags::attach(proxy, request.watch_list)?;
if let Some(mut old) = subscription.subtags.replace(replacement) {
old.close(proxy);
}
Ok(())
}
(Kind::QueryActiveAlarms, Some(Payload::QueryActiveAlarmsCommand(request))) => {
let subscription = self
.subscription
.as_ref()
.ok_or_else(|| AlarmError::unavailable("Alarm provider is not subscribed"))?;
let mut result = QueryActiveAlarmsReplyPayload::default();
{
let mut append = |record: &Record, truncated| {
if record.state.active()
&& record.reference().starts_with(&request.alarm_filter_prefix)
{
result
.snapshots
.push(record.snapshot(subscription.mode, truncated));
}
};
if subscription.mode == Mode::Subtag {
if let Some(subtags) = &subscription.subtags {
for record in subtags.records() {
append(record, false);
}
}
} else if let Some(primary) = &subscription.primary {
for record in primary.records.values() {
append(record, primary.truncated);
}
result.snapshot_truncated = primary.truncated;
}
}
reply.payload = Some(Reply::QueryActiveAlarms(result));
Ok(())
}
(Kind::AcknowledgeAlarm, Some(Payload::AcknowledgeAlarmCommand(request))) => {
let id = Uuid::parse_str(&request.alarm_guid)
.map_err(|_| AlarmError::invalid("Invalid alarm GUID"))?;
let address = self.ack_address(Some(id), None)?;
ack::acknowledge(
proxy,
address,
request.operator_user,
request.operator_domain,
request.operator_password,
request.comment,
reply,
)
}
(
Kind::AcknowledgeAlarmByName,
Some(Payload::AcknowledgeAlarmByNameCommand(request)),
) => {
let address = self.ack_address(
None,
Some((
&request.provider_name,
&request.group_name,
&request.alarm_name,
)),
)?;
ack::acknowledge(
proxy,
address,
request.operator_user,
request.operator_domain,
request.operator_password,
request.comment,
reply,
)
}
_ => Err(AlarmError::invalid(
"Alarm command payload does not match its kind",
)),
}
}
fn ack_address(
&self,
id: Option<Uuid>,
name: Option<(&str, &str, &str)>,
) -> Result<String, AlarmError> {
let subscription = self
.subscription
.as_ref()
.ok_or_else(|| AlarmError::invalid("Alarm provider is not subscribed"))?;
let not_found =
|| AlarmError::invalid("Alarm was not found or does not support acknowledgement");
if subscription.mode == Mode::Subtag {
let subtags = subscription.subtags.as_ref().ok_or_else(not_found)?;
let address = if let Some(id) = id {
subtags.ack_address(id)
} else if let Some((provider, group, name)) = name {
let reference = record::full_reference(provider, group, name);
subtags.ack_address_by_reference(&reference)
} else {
None
};
return address.map(str::to_owned).ok_or_else(not_found);
}
let (provider, name) = if let Some(id) = id {
let record = subscription
.primary
.as_ref()
.and_then(|primary| primary.records.get(&id))
.ok_or_else(not_found)?;
(record.provider.as_str(), record.name.as_str())
} else {
let (provider, _, name) = name.ok_or_else(not_found)?;
(provider, name)
};
if !provider.eq_ignore_ascii_case("Galaxy") || name.trim().is_empty() {
return Err(not_found());
}
Ok(format!("{name}.AckMsg"))
}
}
fn setting(name: &str, default: i32, range: std::ops::RangeInclusive<i32>) -> i32 {
std::env::var(name)
.ok()
.and_then(|value| value.parse().ok())
.filter(|value| range.contains(value))
.unwrap_or(default)
}
fn publish_mode(publisher: &Publisher, mode: Mode, reason: &str, hr: HRESULT) {
publisher.publish(MxEvent {
family: MxEventFamily::OnAlarmProviderModeChanged as i32,
body: Some(mx_event::Body::OnAlarmProviderModeChanged(
OnAlarmProviderModeChangedEvent {
mode: mode as i32,
reason: reason.into(),
hresult: hr,
at: Some(SystemTime::now().into()),
},
)),
..Default::default()
});
}
fn publish_record(publisher: &Publisher, record: &Record, previous: State, mode: Mode) {
if let Some(event) = record.event(previous, mode) {
publisher.publish(event);
}
}
+150
View File
@@ -0,0 +1,150 @@
use std::time::Instant;
use tokio::sync::oneshot::error::TryRecvError;
use windows_sys::Win32::Foundation::{E_FAIL, WAIT_OBJECT_0, WAIT_TIMEOUT};
use windows_sys::Win32::UI::WindowsAndMessaging::{
MsgWaitForMultipleObjectsEx, MWMO_INPUTAVAILABLE, QS_ALLINPUT,
};
use super::{handle, AlarmError};
use crate::mxaccess::{pump_messages, Command, CommandResult, Proxy, WriteAccess, WriteRequest};
use crate::mxevent::WriteCompletion;
use crate::wire::mxaccess_gateway::v1::{
mx_command_reply::Payload as Reply, mx_value, AcknowledgeAlarmReplyPayload, MxCommandReply,
MxDataType, MxStatusCategory, MxValue, ProtocolStatusCode as Code,
};
pub(super) fn acknowledge(
proxy: &mut Proxy,
address: String,
user: String,
domain: String,
password: String,
comment: String,
reply: &mut MxCommandReply,
) -> Result<(), AlarmError> {
if user.trim().is_empty() {
return Err(AlarmError::invalid("Alarm operator user is required"));
}
let principal = if domain.is_empty() {
user
} else {
format!("{domain}\\{user}")
};
let client = std::env::var("MXGATEWAY_ALARM_ACK_CLIENT_NAME").unwrap_or_default();
let client = match client.trim() {
"" => "MxGateway",
name => name,
};
let server = handle(
proxy.execute(Command::Register(client.into())),
"Register alarm acknowledgement",
)?;
proxy.publisher.private_registration(server, false);
let result = (|| {
let user = handle(
proxy.execute(Command::Authenticate(server, principal, password)),
"Alarm operator authentication",
)?;
if user == 0 {
return Err(AlarmError::failed(
"Alarm operator authentication (no user id)",
-1,
));
}
let item = handle(
proxy.execute(Command::AddItem(server, address)),
"Add AckMsg item",
)?;
proxy
.execute(Command::Advise {
server,
item,
supervisory: false,
})
.map_err(|error| AlarmError::com("Advise AckMsg item", error))?;
if !pump_messages() {
return Err(AlarmError::failed("AckMsg message pump", E_FAIL));
}
let value = MxValue {
data_type: MxDataType::String as i32,
kind: Some(mx_value::Kind::StringValue(comment)),
..Default::default()
};
let CommandResult::Write(completion) = proxy
.execute(Command::Write(Box::new(WriteRequest {
server,
item,
value: Some(value),
timestamp: None,
access: WriteAccess::Plain(user),
})))
.map_err(|error| AlarmError::com("AckMsg write", error))?
else {
unreachable!("write command returns a completion");
};
complete_ack(completion, reply)
})();
proxy.publisher.discard_pending_write();
let _ = proxy.execute(Command::Unregister(server));
result
}
fn complete_ack(
mut completion: WriteCompletion,
reply: &mut MxCommandReply,
) -> Result<(), AlarmError> {
let wait = crate::mxaccess::write_completion_wait();
let deadline = Instant::now() + wait;
loop {
match completion.try_recv() {
Ok(statuses) => {
if let Some(status) = statuses
.iter()
.find(|status| status.category() != MxStatusCategory::Ok)
{
return Err(AlarmError {
code: Code::MxaccessFailure,
hr: Some(status.detail),
message: format!(
"AckMsg write rejected (category {}, detail {})",
status.category, status.detail
),
});
}
break;
}
Err(TryRecvError::Closed) => {
return Err(AlarmError::failed("AckMsg completion channel", E_FAIL))
}
Err(TryRecvError::Empty) => {}
}
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
reply.diagnostic_message = format!(
"AckMsg write sent; OnWriteComplete not observed within {} ms",
wait.as_millis()
);
break;
}
// SAFETY: waits only on the current STA's queue.
let result = unsafe {
MsgWaitForMultipleObjectsEx(
0,
std::ptr::null(),
remaining.as_nanos().div_ceil(1_000_000) as u32,
QS_ALLINPUT,
MWMO_INPUTAVAILABLE,
)
};
match result {
WAIT_TIMEOUT => {}
WAIT_OBJECT_0 if pump_messages() => {}
_ => return Err(AlarmError::failed("AckMsg message pump", E_FAIL)),
}
}
reply.payload = Some(Reply::AcknowledgeAlarm(AcknowledgeAlarmReplyPayload {
native_status: 0,
}));
Ok(())
}
@@ -0,0 +1,95 @@
use std::mem::size_of;
use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle};
use windows_sys::Win32::Foundation::{INVALID_HANDLE_VALUE, WAIT_OBJECT_0};
use windows_sys::Win32::System::Diagnostics::ToolHelp::{
CreateToolhelp32Snapshot, Process32FirstW, Process32NextW, PROCESSENTRY32W, TH32CS_SNAPPROCESS,
};
use windows_sys::Win32::System::RemoteDesktop::ProcessIdToSessionId;
use windows_sys::Win32::System::Threading::{
OpenProcess, WaitForSingleObject, PROCESS_SYNCHRONIZE,
};
pub enum LocalAlarmmgr {
Unobserved,
Running(OwnedHandle),
Stopped,
}
#[derive(PartialEq, Eq)]
pub enum Status {
Available,
Exited,
Missing,
}
impl LocalAlarmmgr {
pub fn for_subscription(expression: &str) -> Option<Self> {
let (node, query) = expression.trim_start_matches('\\').split_once('\\')?;
if !query.split('!').next()?.eq_ignore_ascii_case("Galaxy") {
return None;
}
let computer = std::env::var("COMPUTERNAME").unwrap_or_default();
(node == "."
|| node.eq_ignore_ascii_case("localhost")
|| (!computer.is_empty() && node.eq_ignore_ascii_case(&computer)))
.then_some(Self::Unobserved)
}
pub fn observe(&mut self) -> Status {
if let Self::Running(process) = self {
// SAFETY: this is an owned, waitable process handle. A zero timeout never blocks.
if unsafe { WaitForSingleObject(process.as_raw_handle(), 0) } != WAIT_OBJECT_0 {
return Status::Available;
}
*self = Self::Stopped;
return Status::Exited;
}
if let Some(process) = find_alarmmgr() {
*self = Self::Running(process);
}
// A process we could never observe does not override COM health.
match self {
Self::Stopped => Status::Missing,
_ => Status::Available,
}
}
}
fn find_alarmmgr() -> Option<OwnedHandle> {
// SAFETY: a process snapshot owns its handle independently of the processes it enumerates.
let snapshot = unsafe { CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0) };
if snapshot == INVALID_HANDLE_VALUE {
return None;
}
let snapshot = unsafe { OwnedHandle::from_raw_handle(snapshot) };
let mut entry = PROCESSENTRY32W {
dwSize: size_of::<PROCESSENTRY32W>() as u32,
..Default::default()
};
// SAFETY: the entry is initialized with the size expected by both enumeration functions.
let mut present = unsafe { Process32FirstW(snapshot.as_raw_handle(), &mut entry) };
while present != 0 {
let name = String::from_utf16_lossy(
&entry.szExeFile[..entry
.szExeFile
.iter()
.position(|&c| c == 0)
.unwrap_or(entry.szExeFile.len())],
);
let mut session = u32::MAX;
// The Galaxy provider uses Session 0. Interactive InTouch instances are independent.
if name.eq_ignore_ascii_case("alarmmgr.exe")
&& unsafe { ProcessIdToSessionId(entry.th32ProcessID, &mut session) } != 0
&& session == 0
{
// SAFETY: Windows either returns a new owned handle with wait access or returns null.
let process = unsafe { OpenProcess(PROCESS_SYNCHRONIZE, 0, entry.th32ProcessID) };
if !process.is_null() {
return Some(unsafe { OwnedHandle::from_raw_handle(process) });
}
}
present = unsafe { Process32NextW(snapshot.as_raw_handle(), &mut entry) };
}
None
}
@@ -0,0 +1,214 @@
use std::collections::BTreeMap;
use chrono::{NaiveDate, NaiveTime};
use prost_types::Timestamp;
use uuid::Uuid;
use crate::wire::mxaccess_gateway::v1::{
mx_event, ActiveAlarmSnapshot, AlarmConditionState, AlarmProviderMode, AlarmTransitionKind,
MxEvent, MxEventFamily, OnAlarmTransitionEvent,
};
#[derive(Clone, Copy, Default, PartialEq, Eq)]
pub enum State {
#[default]
Unknown,
Active,
ActiveAcked,
Returned,
ReturnedAcked,
}
impl State {
pub fn active(self) -> bool {
matches!(self, Self::Active | Self::ActiveAcked)
}
fn returned(self) -> bool {
matches!(self, Self::Returned | Self::ReturnedAcked)
}
fn acked(self) -> bool {
matches!(self, Self::ActiveAcked | Self::ReturnedAcked)
}
fn transition(self, previous: Self) -> Option<AlarmTransitionKind> {
use AlarmTransitionKind::{Acknowledge, Clear, Raise};
match self {
Self::Unknown => None,
_ if previous == Self::Unknown => Some(if self.active() { Raise } else { Clear }),
_ if previous.active() && self.returned() => Some(Clear),
_ if previous.returned() && self.active() => Some(Raise),
_ if !previous.acked() && self.acked() => Some(Acknowledge),
_ => None,
}
}
}
#[derive(Default)]
pub struct Record {
pub provider: String,
pub group: String,
pub name: String,
pub alarm_type: String,
pub priority: i32,
pub state: State,
pub at: Option<Timestamp>,
pub operator: String,
pub comment: String,
pub ack_operator: String,
pub ack_comment: String,
}
impl Record {
pub fn reference(&self) -> String {
full_reference(&self.provider, &self.group, &self.name)
}
pub fn event(&self, previous: State, mode: AlarmProviderMode) -> Option<MxEvent> {
let transition = self.state.transition(previous)?;
let (operator_user, operator_comment) =
self.attribution(transition == AlarmTransitionKind::Acknowledge);
let body = OnAlarmTransitionEvent {
alarm_full_reference: self.reference(),
source_object_reference: self.name.clone(),
alarm_type_name: self.alarm_type.clone(),
transition_kind: transition as i32,
severity: self.priority,
transition_timestamp: self.at.or(Some(Timestamp {
seconds: -62_135_596_800,
nanos: 0,
})),
operator_user,
operator_comment,
category: self.group.clone(),
degraded: mode == AlarmProviderMode::Subtag,
source_provider: mode as i32,
..Default::default()
};
Some(MxEvent {
family: MxEventFamily::OnAlarmTransition as i32,
body: Some(mx_event::Body::OnAlarmTransition(body)),
..Default::default()
})
}
pub fn snapshot(&self, mode: AlarmProviderMode, truncated: bool) -> ActiveAlarmSnapshot {
let (operator_user, operator_comment) = self.attribution(self.state.acked());
ActiveAlarmSnapshot {
alarm_full_reference: self.reference(),
source_object_reference: self.name.clone(),
alarm_type_name: self.alarm_type.clone(),
severity: self.priority,
current_state: if self.state.acked() {
AlarmConditionState::ActiveAcked
} else {
AlarmConditionState::Active
} as i32,
last_transition_timestamp: self.at,
operator_user,
operator_comment,
category: self.group.clone(),
degraded: mode == AlarmProviderMode::Subtag,
source_provider: mode as i32,
from_truncated_snapshot: truncated,
..Default::default()
}
}
fn attribution(&self, ack: bool) -> (String, String) {
if ack && (!self.ack_operator.is_empty() || !self.ack_comment.is_empty()) {
(self.ack_operator.clone(), self.ack_comment.clone())
} else {
(self.operator.clone(), self.comment.clone())
}
}
}
pub fn full_reference(provider: &str, group: &str, name: &str) -> String {
let separator = if provider.is_empty() { "" } else { "!" };
let dot = if group.is_empty() { "" } else { "." };
format!("{provider}{separator}{group}{dot}{name}")
}
pub fn parse_snapshot(xml: &str) -> Option<(BTreeMap<Uuid, Record>, usize)> {
let document = roxmltree::Document::parse(xml).ok()?;
let root = document.root_element();
if !unqualified(root, "ALARM_RECORDS") {
return None;
}
let mut records = BTreeMap::new();
let mut count = 0;
for alarm in root.children().filter(|&child| unqualified(child, "ALARM")) {
count += 1;
let mut fields = BTreeMap::new();
for field in alarm.children().filter(roxmltree::Node::is_element) {
fields.entry(field.tag_name().name()).or_insert_with(|| {
field
.descendants()
.filter(roxmltree::Node::is_text)
.filter_map(|node| node.text())
.collect::<String>()
});
}
let field = |name| fields.get(name).map_or("", String::as_str);
let Ok(id) = Uuid::parse_str(field("GUID").trim()) else {
continue;
};
records.insert(
id,
Record {
provider: field("PROVIDER_NAME").into(),
group: field("GROUP").into(),
name: field("TAGNAME").into(),
alarm_type: field("TYPE").into(),
priority: field("PRIORITY").trim().parse().unwrap_or_default(),
state: match field("STATE").trim().to_ascii_uppercase().as_str() {
"UNACK_ALM" => State::Active,
"ACK_ALM" => State::ActiveAcked,
"UNACK_RTN" => State::Returned,
"ACK_RTN" => State::ReturnedAcked,
_ => State::Unknown,
},
at: parse_timestamp(field("DATE"), field("TIME")),
operator: field("OPERATOR_NAME").into(),
comment: field("ALARM_COMMENT").into(),
ack_operator: field("ACK_OPERATOR_NAME").into(),
ack_comment: field("ACK_COMMENT").into(),
},
);
}
Some((records, count))
}
fn unqualified(node: roxmltree::Node<'_, '_>, name: &str) -> bool {
node.is_element() && node.tag_name().namespace().is_none() && node.tag_name().name() == name
}
fn parse_timestamp(date: &str, time: &str) -> Option<Timestamp> {
let date = ["%Y/%m/%d", "%m/%d/%Y", "%d/%m/%Y"]
.into_iter()
.find_map(|format| NaiveDate::parse_from_str(date.trim(), format).ok())?;
let time = NaiveTime::parse_from_str(time.trim(), "%H:%M:%S%.f").ok()?;
let timestamp = date.and_time(time).and_utc();
Some(Timestamp {
seconds: timestamp.timestamp(),
nanos: timestamp.timestamp_subsec_nanos() as i32,
})
}
pub fn synthetic_guid(reference: &str) -> Uuid {
const PRIME: u64 = 1_099_511_628_211;
let mut low = 14_695_981_039_346_656_037_u64;
let mut high = 1_469_598_103_934_665_603_u64;
for (index, byte) in reference.bytes().enumerate() {
low = (low ^ u64::from(byte)).wrapping_mul(PRIME);
high = (high ^ (u64::from(byte) + index as u64)).wrapping_mul(PRIME);
}
low = (low ^ reference.len() as u64).wrapping_mul(PRIME);
let mut bytes = [0; 16];
bytes[..8].copy_from_slice(&low.to_le_bytes());
bytes[8..].copy_from_slice(&high.to_le_bytes());
Uuid::from_bytes_le(bytes)
}
@@ -0,0 +1,240 @@
//! The helper uses the same executable. Only it loads WnWrap, so a stuck
//! registration, poll, or teardown can be terminated without stopping MXAccess.
use std::io::{self, Read, Write};
use std::mem::size_of;
use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle};
use std::os::windows::process::CommandExt;
use std::process::{Child, ChildStdin, Command, ExitCode, Stdio};
use std::ptr::null;
use std::sync::mpsc::{self, Receiver};
use std::thread;
use windows_sys::core::HRESULT;
use windows_sys::Win32::System::JobObjects::{
AssignProcessToJobObject, CreateJobObjectW, JobObjectExtendedLimitInformation,
SetInformationJobObject, JOBOBJECT_EXTENDED_LIMIT_INFORMATION,
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE, JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK,
};
use windows_sys::Win32::System::Threading::{WaitForSingleObject, CREATE_NO_WINDOW};
use super::wnwrap::WnWrap;
use crate::mxaccess::{pump_messages, Apartment};
pub const ARGUMENT: &str = "--wnwrap-snapshot-worker";
const MAX_SNAPSHOT_BYTES: usize = 128 * 1024 * 1024;
const SNAPSHOT: u8 = 1;
const CHECK_QUERY: u8 = 2;
const QUERY_PENDING: i32 = 1;
type SnapshotReply = Result<Option<String>, HRESULT>;
pub struct SnapshotProcess {
// Closing the job terminates the child even during an uninterruptible COM call.
job: OwnedHandle,
child: Child,
requests: ChildStdin,
pub replies: Receiver<SnapshotReply>,
}
impl SnapshotProcess {
pub fn start(expression: &str, limit: i32) -> io::Result<Self> {
let job = kill_on_close_job()?;
let mut child = Command::new(std::env::current_exe()?)
.args([ARGUMENT, expression, &limit.to_string()])
.env_remove("MXGATEWAY_WORKER_NONCE")
.stdin(Stdio::piped())
.stdout(Stdio::piped())
.creation_flags(CREATE_NO_WINDOW)
.spawn()?;
// SAFETY: both handles remain owned in this scope.
if unsafe { AssignProcessToJobObject(job.as_raw_handle(), child.as_raw_handle()) } == 0 {
let error = io::Error::last_os_error();
let _ = child.kill();
return Err(error);
}
let requests = child.stdin.take().expect("piped stdin");
let mut output = child.stdout.take().expect("piped stdout");
let (sender, replies) = mpsc::sync_channel(1);
thread::Builder::new()
.name("wnwrap-snapshot-reader".into())
.spawn(move || {
while let Ok(reply) = read_reply(&mut output) {
if sender.send(reply).is_err() {
break;
}
}
})?;
Ok(Self {
job,
child,
requests,
replies,
})
}
pub fn finish(self) {
self.finish_with_grace(2_000);
}
pub fn finish_after_manager_exit(self) {
// AlarmBuf waits five seconds for a reply before it checks whether
// Alarmmgr exited. Let it release its shared call mutex before killing
// the helper. The parent stops reading this helper as soon as it retires.
self.finish_with_grace(7_000);
}
fn finish_with_grace(self, grace_ms: u32) {
let Self {
job,
child,
requests,
replies: _,
} = self;
drop(requests);
// Give the helper time to deregister. A COM teardown that stalls
// cannot delay UnsubscribeAlarms or keep the job alive indefinitely.
let _ = thread::Builder::new()
.name("wnwrap-snapshot-cleanup".into())
.spawn(move || {
// SAFETY: the child process handle is owned by this thread.
unsafe { WaitForSingleObject(child.as_raw_handle(), grace_ms) };
drop(job);
});
}
pub fn request(&mut self, check_query: bool) -> io::Result<()> {
// The parent allows only one outstanding byte on the request pipe.
self.requests
.write_all(&[if check_query { CHECK_QUERY } else { SNAPSHOT }])
}
}
fn kill_on_close_job() -> io::Result<OwnedHandle> {
// SAFETY: null arguments create an unnamed job with default security.
let raw = unsafe { CreateJobObjectW(null(), null()) };
if raw.is_null() {
return Err(io::Error::last_os_error());
}
let job = unsafe { OwnedHandle::from_raw_handle(raw) };
let mut limits = JOBOBJECT_EXTENDED_LIMIT_INFORMATION::default();
// The vendor may start Alarmmgr from the helper during registration.
limits.BasicLimitInformation.LimitFlags =
JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE | JOB_OBJECT_LIMIT_SILENT_BREAKAWAY_OK;
// SAFETY: the settings structure matches the job information class.
if unsafe {
SetInformationJobObject(
job.as_raw_handle(),
JobObjectExtendedLimitInformation,
(&limits as *const JOBOBJECT_EXTENDED_LIMIT_INFORMATION).cast(),
size_of::<JOBOBJECT_EXTENDED_LIMIT_INFORMATION>() as u32,
)
} == 0
{
return Err(io::Error::last_os_error());
}
Ok(job)
}
pub fn run() -> ExitCode {
let mut args = std::env::args().skip(2);
let Some(expression) = args.next() else {
return ExitCode::FAILURE;
};
let Some(limit) = args.next().and_then(|value| value.parse().ok()) else {
return ExitCode::FAILURE;
};
let Ok(_apartment) = Apartment::initialize() else {
return ExitCode::FAILURE;
};
let mut input = io::stdin().lock();
let mut output = io::stdout().lock();
let mut subscription = None;
let mut request = [0];
while input.read_exact(&mut request).is_ok()
&& matches!(request[0], SNAPSHOT | CHECK_QUERY)
&& pump_messages()
{
let reply = (|| {
if subscription.is_none() {
subscription = Some(WnWrap::subscribe(&expression)?);
}
let consumer = subscription.as_ref().expect("subscribed");
let xml = consumer.snapshot(limit)?;
if request[0] == CHECK_QUERY && !consumer.query_complete()? {
return Ok(None);
}
Ok(Some(xml))
})();
let failed = reply.is_err();
if write_reply(&mut output, reply).is_err() || failed {
break;
}
}
ExitCode::SUCCESS
}
fn write_reply(mut output: impl Write, reply: SnapshotReply) -> io::Result<()> {
match reply {
Err(hr) => output.write_all(&hr.to_le_bytes())?,
Ok(None) => output.write_all(&QUERY_PENDING.to_le_bytes())?,
Ok(Some(xml)) if xml.len() <= MAX_SNAPSHOT_BYTES => {
output.write_all(&0_i32.to_le_bytes())?;
output.write_all(&(xml.len() as u32).to_le_bytes())?;
output.write_all(xml.as_bytes())?;
}
Ok(Some(_)) => {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"WnWrap snapshot is too large",
))
}
}
output.flush()
}
fn read_reply(mut input: impl Read) -> io::Result<SnapshotReply> {
let mut field = [0; 4];
input.read_exact(&mut field)?;
let hr = i32::from_le_bytes(field);
if hr == QUERY_PENDING {
return Ok(Ok(None));
}
if hr != 0 {
return Ok(Err(hr));
}
input.read_exact(&mut field)?;
let length = u32::from_le_bytes(field) as usize;
if length > MAX_SNAPSHOT_BYTES {
return Err(io::Error::new(
io::ErrorKind::InvalidData,
"WnWrap snapshot is too large",
));
}
let mut bytes = vec![0; length];
input.read_exact(&mut bytes)?;
String::from_utf8(bytes)
.map(|xml| Ok(Some(xml)))
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn pending_probe_keeps_the_following_snapshot_aligned() {
let mut frames = Vec::new();
write_reply(&mut frames, Ok(None)).unwrap();
write_reply(&mut frames, Ok(Some("<ALARM_RECORDS/>".into()))).unwrap();
let mut frames = frames.as_slice();
assert_eq!(read_reply(&mut frames).unwrap(), Ok(None));
assert_eq!(
read_reply(&mut frames).unwrap(),
Ok(Some("<ALARM_RECORDS/>".into()))
);
assert!(frames.is_empty());
}
}
@@ -0,0 +1,225 @@
use std::collections::{BTreeMap, BTreeSet};
use uuid::Uuid;
use super::record::{synthetic_guid, Record, State};
use super::{handle, AlarmError};
use crate::mxaccess::{Command, Proxy};
use crate::wire::mxaccess_gateway::v1::{
mx_value::Kind as Value, AlarmProviderMode, AlarmSubtagTarget,
};
#[derive(Clone, Copy)]
enum Role {
Active,
Acked,
Priority,
}
struct Target {
config: AlarmSubtagTarget,
record: Record,
acked_during_episode: bool,
}
pub struct Subtags {
server: Option<i32>,
items: BTreeMap<i32, (Uuid, Role)>,
targets: BTreeMap<Uuid, Target>,
}
impl Subtags {
pub fn attach(proxy: &mut Proxy, configs: Vec<AlarmSubtagTarget>) -> Result<Self, AlarmError> {
let mut targets = BTreeMap::new();
let mut references = BTreeSet::new();
let mut addresses = BTreeSet::new();
let mut bindings = Vec::new();
for config in configs {
let reference = &config.alarm_full_reference;
if reference.trim().is_empty() || !references.insert(reference.to_lowercase()) {
return Err(AlarmError::invalid(
"Alarm watch list has an empty or duplicate reference",
));
}
let id = synthetic_guid(reference);
for (address, role) in [
(&config.active_subtag, Role::Active),
(&config.acked_subtag, Role::Acked),
(&config.priority_subtag, Role::Priority),
] {
if address.trim().is_empty() {
continue;
}
if !addresses.insert(address.to_lowercase()) {
return Err(AlarmError::invalid(
"Alarm watch list has a duplicate subtag address",
));
}
bindings.push((address.clone(), id, role));
}
let (provider, path) = reference.split_once('!').unwrap_or(("", reference));
let mut offset = 0;
let mut group_and_name = ("", path);
for segment in path.split('.') {
if segment.eq_ignore_ascii_case(&config.source_object_reference) {
if offset > 0 {
group_and_name = (&path[..offset - 1], &path[offset..]);
}
break;
}
offset += segment.len() + 1;
}
let (group, name) = group_and_name;
let record = Record {
provider: provider.into(),
group: group.into(),
name: name.into(),
..Default::default()
};
targets.insert(
id,
Target {
config,
record,
acked_during_episode: false,
},
);
}
let mut subtags = Self {
server: None,
items: BTreeMap::new(),
targets,
};
if !bindings.is_empty() {
let server = handle(
proxy.execute(Command::Register(
"OtOpcUa.ZB.MOM.WW.MxGateway.Worker.SubtagAlarms".into(),
)),
"Register alarm subtags",
)?;
proxy.publisher.private_registration(server, true);
subtags.server = Some(server);
for (address, id, role) in bindings {
let result = (|| {
let item = handle(
proxy.execute(Command::AddItem(server, address)),
"Add alarm subtag",
)?;
proxy
.execute(Command::Advise {
server,
item,
supervisory: false,
})
.map_err(|error| AlarmError::com("Advise alarm subtag", error))?;
subtags.items.insert(item, (id, role));
Ok(())
})();
if let Err(error) = result {
subtags.close(proxy);
return Err(error);
}
}
}
Ok(subtags)
}
pub fn close(&mut self, proxy: &mut Proxy) {
if let Some(server) = self.server.take() {
let _ = proxy.execute(Command::Unregister(server));
}
}
pub fn drain(&mut self, proxy: &Proxy, emit: bool) {
let Some(server) = self.server else {
return;
};
for event in proxy.publisher.private_events(server) {
let Some(&(id, role)) = self.items.get(&event.item_handle) else {
continue;
};
let target = self.targets.get_mut(&id).expect("binding has a target");
let record = &mut target.record;
let previous = record.state;
let value = event.value.and_then(|value| value.kind);
let number = numeric(value.as_ref());
let flag = number.is_some_and(|number| number != 0.0);
record.state = match role {
Role::Priority => {
if let Some(priority) = number
.map(f64::round_ties_even)
.filter(|value| (i32::MIN as f64..=i32::MAX as f64).contains(value))
{
record.priority = priority as i32;
}
continue;
}
Role::Active if flag && !previous.active() => {
target.acked_during_episode = false;
State::Active
}
Role::Active if !flag && previous.active() => {
if target.acked_during_episode {
State::ReturnedAcked
} else {
State::Returned
}
}
Role::Acked if previous.active() && flag => {
target.acked_during_episode = true;
State::ActiveAcked
}
Role::Acked if previous.active() => State::Active,
_ => continue,
};
if record.state == previous {
continue;
}
record.at = event.source_timestamp.or(event.worker_timestamp);
if emit {
super::publish_record(
&proxy.publisher,
record,
previous,
AlarmProviderMode::Subtag,
);
}
}
}
pub fn records(&self) -> impl Iterator<Item = &Record> {
self.targets.values().map(|target| &target.record)
}
pub fn ack_address_by_reference(&self, reference: &str) -> Option<&str> {
let (id, _) = self.targets.iter().find(|(_, target)| {
target
.config
.alarm_full_reference
.eq_ignore_ascii_case(reference)
})?;
self.ack_address(*id)
}
pub fn ack_address(&self, id: Uuid) -> Option<&str> {
self.targets
.get(&id)
.map(|target| target.config.ack_comment_subtag.as_str())
.filter(|address| !address.trim().is_empty())
}
}
fn numeric(value: Option<&Value>) -> Option<f64> {
match value? {
Value::BoolValue(value) => Some(if *value { 1.0 } else { 0.0 }),
Value::Int32Value(value) => Some((*value).into()),
Value::Int64Value(value) => Some(*value as f64),
Value::FloatValue(value) => Some((*value).into()),
Value::DoubleValue(value) => Some(*value),
Value::StringValue(value) if value.trim().eq_ignore_ascii_case("true") => Some(1.0),
Value::StringValue(value) if value.trim().eq_ignore_ascii_case("false") => Some(0.0),
Value::StringValue(value) => value.trim().parse().ok(),
_ => None,
}
}
@@ -0,0 +1,265 @@
//! x86 WnWrap interfaces from Interop.WNWRAPCONSUMERLib. The working
//! sequence uses v1 to register/subscribe and v2 to fetch the XML snapshot.
use std::ffi::c_void;
use std::ptr::null_mut;
use windows_sys::core::{IUnknown_Vtbl, BSTR, GUID, HRESULT, PCWSTR};
use windows_sys::Win32::Foundation::E_FAIL;
use windows_sys::Win32::System::Com::{CoCreateInstance, CoTaskMemFree, CLSCTX_INPROC_SERVER};
use windows_sys::Win32::System::Variant::{VariantClear, VARIANT, VT_BSTR};
use crate::automation::AutomationString;
use crate::mxevent::ComReference;
const CLSID: GUID = GUID::from_u128(0x7ab52e5f_36b2_4a30_ae46_952a746f667c);
const IID_V1: GUID = GUID::from_u128(0x72006e35_651a_4d05_b3cb_8e4324b2e2d3);
const IID_V2: GUID = GUID::from_u128(0x9bcc2673_e5f9_4bba_b262_17c2780e276a);
const SUMMARY: i32 = 1;
const NEWEST_FIRST: i32 = 1;
const ACTIVE_NOW: i32 = 1;
#[repr(C)]
struct Vtable {
unknown: IUnknown_Vtbl,
dispatch: [usize; 4],
register:
unsafe extern "system" fn(*mut c_void, i32, PCWSTR, PCWSTR, PCWSTR, *mut i32) -> HRESULT,
initialize: unsafe extern "system" fn(*mut c_void, PCWSTR, *mut i32) -> HRESULT,
deregister: unsafe extern "system" fn(*mut c_void, *mut i32) -> HRESULT,
uninitialize: unsafe extern "system" fn(*mut c_void, *mut i32) -> HRESULT,
select_ack_and_get_query: [usize; 5],
set_query: unsafe extern "system" fn(*mut c_void, BSTR) -> HRESULT,
get_alarms: unsafe extern "system" fn(*mut c_void, i32, *mut VARIANT) -> HRESULT,
subscribe: unsafe extern "system" fn(
*mut c_void,
PCWSTR,
i16,
i16,
i32,
i32,
i32,
i32,
*mut i32,
) -> HRESULT,
ack_and_select: [usize; 3],
get_statistics: unsafe extern "system" fn(
*mut c_void,
*mut i32,
*mut i32,
*mut i32,
*mut i32,
*mut i32,
*mut *mut c_void,
*mut *mut c_void,
*mut *mut c_void,
*mut i32,
) -> HRESULT,
}
pub struct WnWrap {
v1: ComReference,
v2: ComReference,
initialized: bool,
registered: bool,
#[cfg(all(debug_assertions, feature = "alarm-test-faults"))]
snapshot_fault_marker: Option<std::path::PathBuf>,
}
impl WnWrap {
pub fn subscribe(subscription: &str) -> Result<Self, HRESULT> {
let mut v1 = null_mut();
// SAFETY: the two confirmed dual interfaces stay on the calling STA.
check(unsafe {
CoCreateInstance(&CLSID, null_mut(), CLSCTX_INPROC_SERVER, &IID_V1, &mut v1)
})?;
let v1 = ComReference::new(v1)?;
let mut v2 = null_mut();
check(unsafe { (table(&v1).unknown.QueryInterface)(v1.as_ptr(), &IID_V2, &mut v2) })?;
let mut consumer = Self {
v1,
v2: ComReference::new(v2)?,
initialized: false,
registered: false,
#[cfg(all(debug_assertions, feature = "alarm-test-faults"))]
snapshot_fault_marker: std::env::var_os("MXGATEWAY_TEST_ALARM_SNAPSHOT_FAULT_MARKER")
.map(std::path::PathBuf::from),
};
let application = wide("OtOpcUa.ZB.MOM.WW.MxGateway.Worker");
let product = wide("OtOpcUa.MxGateway");
let version = wide("1.0");
let expression = wide(subscription);
let query = AutomationString::new(&query_xml(subscription))?;
let v1 = consumer.v1.as_ptr();
let methods = table(&consumer.v1);
let mut result = 0;
// SAFETY: LPWSTR inputs are null-terminated for the duration of each call.
native_result(
unsafe { (methods.initialize)(v1, application.as_ptr(), &mut result) },
result,
)?;
consumer.initialized = true;
native_result(
unsafe {
(methods.register)(
v1,
0,
product.as_ptr(),
application.as_ptr(),
version.as_ptr(),
&mut result,
)
},
result,
)?;
consumer.registered = true;
native_result(
unsafe {
(methods.subscribe)(
v1,
expression.as_ptr(),
1,
999,
SUMMARY,
NEWEST_FIRST,
ACTIVE_NOW,
ACTIVE_NOW,
&mut result,
)
},
result,
)?;
check(unsafe { (table(&consumer.v2).set_query)(consumer.v2.as_ptr(), query.as_raw()) })?;
Ok(consumer)
}
pub fn snapshot(&self, limit: i32) -> Result<String, HRESULT> {
// The debug-only seam can stall this helper without stopping shared AVEVA services.
#[cfg(all(debug_assertions, feature = "alarm-test-faults"))]
if let Some(marker) = &self.snapshot_fault_marker {
if let Ok(fault) = std::fs::read(marker) {
if fault == b"stall" {
std::thread::sleep(std::time::Duration::from_secs(60));
}
return Err(E_FAIL);
}
}
struct XmlVariant(VARIANT);
impl Drop for XmlVariant {
fn drop(&mut self) {
// SAFETY: the out parameter owns the returned Automation value.
unsafe { VariantClear(&mut self.0) };
}
}
let mut result = XmlVariant(VARIANT::default());
check(unsafe { (table(&self.v2).get_alarms)(self.v2.as_ptr(), limit, &mut result.0) })?;
let value = unsafe { &result.0.Anonymous.Anonymous };
match value.vt {
VT_BSTR => Ok(unsafe { crate::automation::bstr(value.Anonymous.bstrVal) }),
0 | 1 => Ok(String::new()),
_ => Err(E_FAIL),
}
}
pub fn query_complete(&self) -> Result<bool, HRESULT> {
let mut percent = 0;
let mut total = 0;
let mut active = 0;
let mut new_alarms = 0;
let mut change_count = 0;
let mut codes = null_mut();
let mut positions = null_mut();
let mut handles = null_mut();
let mut result = 0;
// SAFETY: the v2 method takes five LONG outputs and three output pointers.
// WnWrap allocates those arrays with CoTaskMemAlloc, including on native error.
let hr = unsafe {
let hr = (table(&self.v2).get_statistics)(
self.v2.as_ptr(),
&mut percent,
&mut total,
&mut active,
&mut new_alarms,
&mut change_count,
&mut codes,
&mut positions,
&mut handles,
&mut result,
);
CoTaskMemFree(codes);
CoTaskMemFree(positions);
CoTaskMemFree(handles);
hr
};
native_result(hr, result)?;
Ok(percent >= 100)
}
}
impl Drop for WnWrap {
fn drop(&mut self) {
let mut ignored = 0;
let v1 = self.v1.as_ptr();
let methods = table(&self.v1);
// SAFETY: only tear down lifecycle steps that completed successfully.
unsafe {
if self.registered {
(methods.deregister)(v1, &mut ignored);
}
if self.initialized {
(methods.uninitialize)(v1, &mut ignored);
}
}
}
}
fn table(reference: &ComReference) -> &Vtable {
// SAFETY: references in this module own IwwAlarmConsumer or IwwAlarmConsumer2.
unsafe { &**reference.as_ptr().cast::<*const Vtable>() }
}
fn check(hr: HRESULT) -> Result<(), HRESULT> {
if hr < 0 {
Err(hr)
} else {
Ok(())
}
}
fn native_result(hr: HRESULT, status: i32) -> Result<(), HRESULT> {
check(hr)?;
match status {
0 => Ok(()),
status if status < 0 => Err(status),
_ => Err(E_FAIL),
}
}
fn wide(value: &str) -> Vec<u16> {
value.encode_utf16().chain([0]).collect()
}
fn query_xml(subscription: &str) -> String {
use std::fmt::Write;
let default_node = std::env::var("COMPUTERNAME").unwrap_or_default();
let expression = subscription.trim_start_matches('\\');
let (node, remainder) = expression
.split_once('\\')
.unwrap_or((&default_node, expression));
let (provider, group) = remainder.split_once('!').unwrap_or((remainder, ""));
let mut query = String::from(
r#"<QUERIES FROM_PRIORITY="1" TO_PRIORITY="999" ALARM_STATE="ALL" DISPLAY_MODE="Summary"><QUERY>"#,
);
for (element, value) in [("NODE", node), ("PROVIDER", provider), ("GROUP", group)] {
if !value.is_empty() {
let escaped = value
.replace('&', "&amp;")
.replace('<', "&lt;")
.replace('>', "&gt;");
write!(query, "<{element}>{escaped}</{element}>").unwrap();
}
}
query.push_str("</QUERY></QUERIES>");
query
}
+996
View File
@@ -0,0 +1,996 @@
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, HRESULT};
use windows_sys::Win32::Foundation::{
SysAllocStringLen, SysFreeString, SysStringLen, E_OUTOFMEMORY, FILETIME, SYSTEMTIME,
};
use windows_sys::Win32::Globalization::GetUserDefaultLCID;
use windows_sys::Win32::System::Com::SAFEARRAY;
use windows_sys::Win32::System::Ole::{
SafeArrayAccessData, SafeArrayCreateVector, SafeArrayGetDim, SafeArrayGetElemsize,
SafeArrayGetLBound, SafeArrayGetUBound, SafeArrayGetVartype, SafeArrayPtrOfIndex,
SafeArrayPutElement, SafeArrayUnaccessData, VarDateFromStr,
};
use windows_sys::Win32::System::Time::{
FileTimeToSystemTime, SystemTimeToFileTime, SystemTimeToTzSpecificLocalTimeEx,
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))
}
pub struct AutomationString(BSTR);
impl AutomationString {
pub fn new(value: &str) -> Result<Self, HRESULT> {
Ok(Self(allocate_bstr(value)?))
}
pub fn as_raw(&self) -> BSTR {
self.0
}
}
impl Drop for AutomationString {
fn drop(&mut self) {
// SAFETY: this BSTR came from SysAllocStringLen.
unsafe { SysFreeString(self.0) };
}
}
fn allocate_bstr(value: &str) -> Result<BSTR, HRESULT> {
let utf16: Vec<_> = value.encode_utf16().collect();
// SAFETY: SysAllocStringLen copies exactly the supplied code units.
let string = unsafe { SysAllocStringLen(utf16.as_ptr(), utf16.len() as u32) };
if string.is_null() {
Err(E_OUTOFMEMORY)
} else {
Ok(string)
}
}
#[derive(Debug)]
pub enum WriteValueError {
Invalid(&'static str),
HResult(HRESULT),
}
impl From<HRESULT> for WriteValueError {
fn from(value: HRESULT) -> Self {
Self::HResult(value)
}
}
// Owns BSTRs and SAFEARRAYs until the synchronous [in] COM call returns.
pub struct WriteValue(VARIANT);
impl WriteValue {
pub fn new(value: MxValue, local_timestamp: bool) -> Result<Self, WriteValueError> {
let mut variant = VARIANT::default();
if value.is_null {
return Ok(Self(variant));
}
use WriteValueError::Invalid;
let (tag, data) = match value.kind {
Some(Kind::BoolValue(value)) => (
VT_BOOL,
VARIANT_0_0_0 {
boolVal: variant_bool(value),
},
),
Some(Kind::Int32Value(value)) => (VT_I4, VARIANT_0_0_0 { lVal: value }),
Some(Kind::Int64Value(value)) => (VT_I8, VARIANT_0_0_0 { llVal: value }),
Some(Kind::FloatValue(value)) => (VT_R4, VARIANT_0_0_0 { fltVal: value }),
Some(Kind::DoubleValue(value)) => (VT_R8, VARIANT_0_0_0 { dblVal: value }),
Some(Kind::StringValue(value)) => (
VT_BSTR,
VARIANT_0_0_0 {
bstrVal: allocate_bstr(&value)?,
},
),
Some(Kind::TimestampValue(value)) => (
VT_DATE,
VARIANT_0_0_0 {
date: timestamp_to_date(&value, local_timestamp)?,
},
),
Some(Kind::ArrayValue(array)) => return Self::array(array),
Some(Kind::RawValue(_) | Kind::SparseArrayValue(_)) => {
return Err(Invalid(
"Raw or unexpanded sparse values cannot be written to MXAccess.",
))
}
None => return Err(Invalid("Write value has no value kind.")),
};
variant.Anonymous.Anonymous.vt = tag;
variant.Anonymous.Anonymous.Anonymous = data;
Ok(Self(variant))
}
pub fn by_value(&self) -> VARIANT {
self.0
}
fn array(array: MxArray) -> Result<Self, WriteValueError> {
use WriteValueError::Invalid;
match array.values {
Some(Values::BoolValues(values)) => {
Self::array_elements(VT_BOOL, values.values.into_iter().map(variant_bool))
}
Some(Values::Int32Values(values)) => {
Self::array_elements(VT_I4, values.values.into_iter())
}
Some(Values::Int64Values(values)) => {
Self::array_elements(VT_I8, values.values.into_iter())
}
Some(Values::FloatValues(values)) => {
Self::array_elements(VT_R4, values.values.into_iter())
}
Some(Values::DoubleValues(values)) => {
Self::array_elements(VT_R8, values.values.into_iter())
}
Some(Values::StringValues(values)) => {
Self::build_array(VT_BSTR, values.values.into_iter(), |value, array, index| {
let string = AutomationString::new(&value)?;
// Automation takes a direct BSTR for a BSTR array item and copies it.
put_element(array, index, string.as_raw().cast())?;
Ok(())
})
}
Some(Values::TimestampValues(values)) => {
let dates = values
.values
.iter()
.map(|value| timestamp_to_date(value, false))
.collect::<Result<Vec<_>, _>>()?;
Self::array_elements(VT_DATE, dates.into_iter())
}
Some(Values::RawValues(_)) => Err(Invalid("Raw arrays cannot be written to MXAccess.")),
None => Err(Invalid("Write array has no element values.")),
}
}
fn array_elements<T>(
tag: u16,
values: impl ExactSizeIterator<Item = T>,
) -> Result<Self, WriteValueError> {
Self::build_array(tag, values, |value, array, index| {
put_element(array, index, (&value as *const T).cast())
})
}
fn build_array<T>(
tag: u16,
values: impl ExactSizeIterator<Item = T>,
put: impl Fn(T, *mut SAFEARRAY, i32) -> Result<(), WriteValueError>,
) -> Result<Self, WriteValueError> {
let count = i32::try_from(values.len())
.map_err(|_| WriteValueError::Invalid("Write array is too large."))?;
// The gateway sends flat element lists, as consumed by the .NET worker.
let array = unsafe { SafeArrayCreateVector(tag, 0, count as u32) };
if array.is_null() {
return Err(E_OUTOFMEMORY.into());
}
let mut variant = VARIANT::default();
variant.Anonymous.Anonymous.vt = VT_ARRAY | tag;
variant.Anonymous.Anonymous.Anonymous.parray = array;
let owner = Self(variant);
for (index, value) in values.enumerate() {
put(value, array, index as i32)?;
}
Ok(owner)
}
}
impl Drop for WriteValue {
fn drop(&mut self) {
// SAFETY: the owned VARIANT was initialized here and passed only as [in].
unsafe { VariantClear(&mut self.0) };
}
}
fn put_element(
array: *mut SAFEARRAY,
index: i32,
data: *const c_void,
) -> Result<(), WriteValueError> {
// SAFETY: the callers supply an element representation matching the array tag.
let hr = unsafe { SafeArrayPutElement(array, &index, data) };
if hr < 0 {
Err(hr.into())
} else {
Ok(())
}
}
fn variant_bool(value: bool) -> i16 {
if value {
-1
} else {
0
}
}
fn timestamp_to_date(timestamp: &Timestamp, local_timestamp: bool) -> Result<f64, WriteValueError> {
let invalid =
WriteValueError::Invalid("Write timestamp is outside the supported Automation date range.");
if !(0..1_000_000_000).contains(&timestamp.nanos)
|| !(-62_135_596_800..=253_402_300_799).contains(&timestamp.seconds)
{
return Err(invalid);
}
let mut millis = timestamp.seconds * 1_000 + i64::from(timestamp.nanos / 1_000_000);
if local_timestamp {
const FILETIME_EPOCH_MILLIS: i64 = 11_644_473_600_000;
let ticks = u64::try_from(millis + FILETIME_EPOCH_MILLIS).map_err(|_| {
WriteValueError::Invalid(
"Local write timestamp predates the Windows time zone conversion range.",
)
})? * 10_000;
let mut file = FILETIME {
dwLowDateTime: ticks as u32,
dwHighDateTime: (ticks >> 32) as u32,
};
let mut utc = SYSTEMTIME::default();
let mut local = SYSTEMTIME::default();
// The extended API uses the host's dynamic time zone rules for the UTC instant.
if unsafe { FileTimeToSystemTime(&file, &mut utc) } == 0
|| unsafe { SystemTimeToTzSpecificLocalTimeEx(null(), &utc, &mut local) } == 0
|| unsafe { SystemTimeToFileTime(&local, &mut file) } == 0
{
return Err(invalid);
}
let ticks = (i64::from(file.dwHighDateTime) << 32) | i64::from(file.dwLowDateTime);
millis = ticks / 10_000 - FILETIME_EPOCH_MILLIS;
}
const DAY_MILLIS: i64 = 86_400_000;
let days = millis.div_euclid(DAY_MILLIS) + 25_569;
let fraction = millis.rem_euclid(DAY_MILLIS) as f64 / DAY_MILLIS as f64;
if !(-657_435..=2_958_465).contains(&days) {
return Err(invalid);
}
Ok(days as f64 + if days < 0 { -fraction } else { fraction })
}
// A dispatch argument can wrap the actual VARIANT in VT_VARIANT|VT_BYREF.
pub unsafe fn unwrap_variant(mut value: VARIANT) -> Option<VARIANT> {
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<i32> {
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<MxValue> {
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<Self> {
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<u32> {
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<T: Copy, O>(&self, convert: impl Fn(T) -> Option<O>) -> Option<Vec<O>> {
if unsafe { SafeArrayGetElemsize(self.array) } as usize != size_of::<T>() {
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::<T>().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()
}
}
pub(crate) 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<Timestamp> {
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<Timestamp> {
// 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::<u16>()
.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<Timestamp> {
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<Timestamp> {
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<Timestamp> {
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)]
#[derive(Default)]
pub struct StatusRecord {
success: i16,
category: i32,
source: i32,
detail: i16,
}
impl StatusRecord {
pub fn to_proto(&self) -> MxStatusProxy {
MxStatusProxy {
success: self.success.into(),
category: match self.category {
0..=8 => self.category + 2,
_ => MxStatusCategory::Unknown as i32,
},
detected_by: match self.source {
0..=5 => self.source + 2,
_ => MxStatusSource::Unknown as i32,
},
detail: self.detail.into(),
raw_category: self.category,
raw_detected_by: self.source,
diagnostic_text: status_detail(self.detail).into(),
}
}
}
// MXAccess completion detail codes, shared with the managed converter.
fn status_detail(detail: i16) -> &'static str {
match detail {
16 => "Request timed out",
17 => "Platform communication error",
18 => "Invalid platform ID",
19 => "Invalid engine ID",
20 => "Engine communication error",
21 => "Invalid reference",
22 => "No Galaxy Repository",
23 => "Invalid object ID",
24 => "Object signature mismatch",
25 => "Invalid primitive ID",
26 => "Invalid attribute ID",
27 => "Invalid property ID",
28 => "Index out of range",
29 => "Data out of range",
30 => "Incorrect data type",
31 => "Attribute not readable",
32 => "Attribute not writeable",
33 => "Write access denied",
34 => "Unknown error",
36 => "Wrong data type",
37 => "Wrong number of dimensions",
38 => "Invalid index",
39 => "Index out of order",
40 => "Dimension does not exist",
41 => "Conversion not supported",
42 => "Unable to convert string",
43 => "Overflow",
44 => "Attribute signature mismatch",
47 => "Nmx version mismatch",
48 => "Nmx command not valid",
49 => "Lmx version mismatch",
50 => "Lmx command not valid",
56 => "Secured Write",
57 => "Verified Write",
60 => "User did not have the necessary permissions to write",
61 => "Verifier did not have the necessary permissions to verify",
541 => "Conversion to intended data type is not supported",
542 => "Unable to convert the input string to intended data type",
8017 => "Object must be offscan to modify attributes that have an MxSecurityConfigure security classification",
_ => "",
}
}
pub fn native_data_type(raw: i32) -> i32 {
match raw {
0..=17 => raw + 2,
_ => MxDataType::Unknown as i32,
}
}
pub unsafe fn buffered_array(value: &VARIANT, expected: MxDataType) -> MxArray {
// Buffered timestamp arrays can carry signed 64-bit Windows FILETIME values.
let converted = unsafe { dispatch_value(value) };
let kind = converted.kind;
if let Some(Kind::ArrayValue(mut array)) = kind {
if expected == MxDataType::Time {
if let Some(Values::Int64Values(ticks)) = &array.values {
if let Some(timestamps) = ticks
.values
.iter()
.copied()
.map(file_time_timestamp)
.collect::<Option<Vec<_>>>()
{
array.element_data_type = expected as i32;
array.values = Some(Values::TimestampValues(TimestampArray {
values: timestamps,
}));
}
}
}
return array;
}
let values = match kind {
Some(Kind::BoolValue(value)) => Some(Values::BoolValues(BoolArray {
values: vec![value],
})),
Some(Kind::Int32Value(value)) => Some(Values::Int32Values(Int32Array {
values: vec![value],
})),
Some(Kind::Int64Value(value)) if expected == MxDataType::Time => file_time_timestamp(value)
.map(|value| {
Values::TimestampValues(TimestampArray {
values: vec![value],
})
}),
Some(Kind::Int64Value(value)) => Some(Values::Int64Values(Int64Array {
values: vec![value],
})),
Some(Kind::FloatValue(value)) => Some(Values::FloatValues(FloatArray {
values: vec![value],
})),
Some(Kind::DoubleValue(value)) => Some(Values::DoubleValues(DoubleArray {
values: vec![value],
})),
Some(Kind::StringValue(value)) => Some(Values::StringValues(StringArray {
values: vec![value],
})),
Some(Kind::TimestampValue(value)) => Some(Values::TimestampValues(TimestampArray {
values: vec![value],
})),
_ => None,
};
let element_data_type = if matches!(values, Some(Values::TimestampValues(_))) {
MxDataType::Time as i32
} else {
converted.data_type
};
MxArray {
element_data_type,
variant_type: converted.variant_type,
dimensions: if values.is_some() {
vec![1]
} else {
Vec::new()
},
raw_diagnostic: if converted.raw_diagnostic.is_empty() {
"Buffered MXAccess event argument was not a SAFEARRAY.".into()
} else {
converted.raw_diagnostic
},
values,
..Default::default()
}
}
pub unsafe fn dispatch_statuses(value: &VARIANT) -> Result<Vec<MxStatusProxy>, &'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<Vec<MxStatusProxy>, &'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::<StatusRecord>()
{
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::<StatusRecord>())
|| count > isize::MAX as usize / size_of::<StatusRecord>()
{
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::<StatusRecord>(), count) };
Ok(records.iter().map(StatusRecord::to_proto).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 marshals_write_variants_and_owns_bstr_array_elements() {
let plain_timestamp = Timestamp {
seconds: 1_760_000_000,
nanos: 123_000_000,
};
for kind in [
Kind::BoolValue(true),
Kind::Int32Value(-42),
Kind::Int64Value(1 << 40),
Kind::FloatValue(1.25),
Kind::DoubleValue(-2.5),
Kind::StringValue("one\0é".into()),
Kind::TimestampValue(plain_timestamp),
] {
let value = WriteValue::new(
MxValue {
kind: Some(kind.clone()),
..Default::default()
},
false,
)
.unwrap();
assert_eq!(
unsafe { dispatch_value(&value.by_value()) }.kind,
Some(kind)
);
}
let values = Values::StringValues(StringArray {
values: vec!["".into(), "é".into()],
});
let array = WriteValue::new(
MxValue {
kind: Some(Kind::ArrayValue(MxArray {
dimensions: vec![2],
values: Some(values.clone()),
..Default::default()
})),
..Default::default()
},
false,
)
.unwrap();
let Some(Kind::ArrayValue(result)) = unsafe { dispatch_value(&array.by_value()) }.kind
else {
panic!("expected a write SAFEARRAY");
};
assert_eq!(result.dimensions, [2]);
assert_eq!(result.values, Some(values));
let raw = MxValue {
kind: Some(Kind::RawValue(vec![1])),
..Default::default()
};
assert!(matches!(
WriteValue::new(raw, false),
Err(WriteValueError::Invalid(_))
));
}
#[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]);
}
}
+38
View File
@@ -0,0 +1,38 @@
#[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 alarm;
#[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;
#[cfg(all(windows, target_arch = "x86"))]
#[tokio::main(flavor = "current_thread")]
async fn main() -> std::process::ExitCode {
if std::env::args_os().nth(1).as_deref()
== Some(std::ffi::OsStr::new(alarm::snapshot_worker::ARGUMENT))
{
return alarm::snapshot_worker::run();
}
match session::run().await {
Ok(()) => std::process::ExitCode::SUCCESS,
Err(error) => {
eprintln!("Rust MXAccess worker: {error}");
std::process::ExitCode::from(error.exit_code())
}
}
}
#[cfg(not(windows))]
fn main() -> std::process::ExitCode {
eprintln!("The MXAccess worker requires 32-bit Windows.");
std::process::ExitCode::FAILURE
}
+832
View File
@@ -0,0 +1,832 @@
use crate::alarm::AlarmRuntime;
use crate::automation::{AutomationString, StatusRecord, WriteValue, WriteValueError};
use crate::mxevent::{ComReference, Connection, Publisher, RecordInfoPin, WriteCompletion};
use crate::wire::mxaccess_gateway::v1::{
BulkReadResult, BulkWriteResult, MxCommand, MxCommandReply, MxStatusProxy, MxValue,
SubscribeResult,
};
use std::collections::{BTreeMap, BTreeSet};
use std::time::Duration;
mod items;
use std::ffi::c_void;
use std::io;
use std::os::windows::io::{AsRawHandle, FromRawHandle, OwnedHandle};
use std::ptr::{null, null_mut, NonNull};
use std::sync::{mpsc, Arc};
use std::thread;
use tokio::sync::oneshot;
use windows_sys::core::{BSTR, GUID, HRESULT};
use windows_sys::Win32::Foundation::{E_INVALIDARG, WAIT_OBJECT_0, WAIT_TIMEOUT};
use windows_sys::Win32::System::Com::{
CoCreateInstance, CoInitializeEx, CoUninitialize, CLSCTX_INPROC_SERVER,
COINIT_APARTMENTTHREADED,
};
use windows_sys::Win32::System::Threading::{CreateEventW, SetEvent};
use windows_sys::Win32::System::Variant::VARIANT;
use windows_sys::Win32::UI::WindowsAndMessaging::{
DispatchMessageW, MsgWaitForMultipleObjectsEx, PeekMessageW, TranslateMessage, MSG,
MWMO_INPUTAVAILABLE, PM_REMOVE, QS_ALLINPUT, WM_QUIT,
};
pub const PROG_ID: &str = "LMXProxy.LMXProxyServer.1";
pub const CLASS_ID: &str = "{C30B52F5-2CB5-4760-AF0A-3A344A7EB5DC}";
const CLSID: GUID = GUID::from_u128(0xc30b52f5_2cb5_4760_af0a_3a344a7eb5dc);
const IID: GUID = GUID::from_u128(0xcce67fb7_eafd_4367_9212_617043bf126d);
const IID_V2: GUID = GUID::from_u128(0x020a8a87_69c5_497f_a893_b629e669fbff);
const IID_V3: GUID = GUID::from_u128(0x57d006b6_f25e_4654_a81e_bcbafd60fe59);
const IID_V5: GUID = GUID::from_u128(0xeceff506_a752_46e3_9e31_0a8e257c9926);
const IID_V4: GUID = GUID::from_u128(0x9dc0d5c1_9371_4e84_86f8_7091d316a66c);
pub fn write_completion_wait() -> Duration {
let milliseconds = std::env::var("MXGATEWAY_WORKER_WRITE_COMPLETION_WAIT_MS")
.ok()
.and_then(|value| value.trim().parse::<i32>().ok())
.filter(|&value| value >= 0)
.unwrap_or(1500);
Duration::from_millis(milliseconds as u64)
}
pub enum Command {
Probe,
Alarm(Box<MxCommand>),
Register(String),
Unregister(i32),
AddItem(i32, String),
AddWithContext {
server: i32,
definition: String,
context: String,
buffered: bool,
},
SetBufferedInterval(i32, i32),
Activation {
server: i32,
item: i32,
active: bool,
},
Authenticate(i32, String, String),
UserToId(i32, String),
SubscribeBulk(i32, Vec<String>, bool),
ItemBulk(i32, Vec<i32>, ItemOperation),
ReadBulk(i32, Vec<String>, Duration),
RemoveItem(i32, i32),
Advise {
server: i32,
item: i32,
supervisory: bool,
},
Write(Box<WriteRequest>),
WriteBulk(Vec<WriteRequest>),
UnAdvise(i32, i32),
}
#[derive(Clone, Copy)]
pub enum ItemOperation {
Advise,
UnAdvise,
Remove,
Unsubscribe,
}
pub enum WriteAccess {
Plain(i32),
Secured { current_user: i32, verifier: i32 },
}
pub struct WriteRequest {
pub server: i32,
pub item: i32,
pub value: Option<MxValue>,
pub timestamp: Option<MxValue>,
pub access: WriteAccess,
}
pub enum CommandError {
NotRegistered,
Invalid(&'static str),
HResult(HRESULT),
}
impl From<WriteValueError> for CommandError {
fn from(error: WriteValueError) -> Self {
match error {
WriteValueError::Invalid(message) => Self::Invalid(message),
WriteValueError::HResult(hr) => Self::HResult(hr),
}
}
}
pub enum CommandResult {
Done,
Alarm(
Box<MxCommandReply>,
Option<oneshot::Receiver<Result<(), HRESULT>>>,
),
Handle(i32),
Status(MxStatusProxy),
SubscribeBulk(Vec<SubscribeResult>),
ReadBulk(Vec<BulkReadResult>),
Write(WriteCompletion),
WriteBulk(Vec<BulkWriteResult>),
}
pub(crate) type Outcome = Result<CommandResult, CommandError>;
pub enum StartError {
Io(io::Error),
Com(HRESULT),
}
struct Request {
command: Command,
reply: oneshot::Sender<Outcome>,
}
pub struct Sta {
queue: Option<mpsc::Sender<Request>>,
wake: Arc<OwnedHandle>,
stopped: Option<oneshot::Receiver<usize>>,
}
impl Sta {
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() {
return Err(StartError::Io(io::Error::last_os_error()));
}
let wake = Arc::new(unsafe { OwnedHandle::from_raw_handle(event) });
let (queue, requests) = mpsc::channel();
let (ready, startup) = oneshot::channel();
let (finished, stopped) = oneshot::channel();
let thread_wake = Arc::clone(&wake);
thread::Builder::new()
.name("mxgateway-sta".into())
.spawn(move || run(requests, thread_wake, ready, finished, publisher))
.map_err(StartError::Io)?;
let sta = Self {
queue: Some(queue),
wake,
stopped: Some(stopped),
};
let pin_status = startup
.await
.map_err(|_| StartError::Io(sta_stopped()))?
.map_err(StartError::Com)?;
Ok((sta, pin_status))
}
pub async fn execute(&self, command: Command) -> io::Result<Outcome> {
let (reply, response) = oneshot::channel();
self.queue
.as_ref()
.expect("STA is open")
.send(Request { command, reply })
.map_err(|_| sta_stopped())?;
self.signal()?;
response.await.map_err(|_| sta_stopped())
}
pub async fn shutdown(mut self) -> io::Result<usize> {
self.queue.take();
self.signal()?;
self.stopped
.take()
.expect("STA has a shutdown receiver")
.await
.map_err(|_| sta_stopped())
}
fn signal(&self) -> io::Result<()> {
// SAFETY: both threads hold an owned reference to the same event.
if unsafe { SetEvent(self.wake.as_raw_handle()) } == 0 {
return Err(io::Error::last_os_error());
}
Ok(())
}
}
impl Drop for Sta {
fn drop(&mut self) {
if self.queue.take().is_some() {
let _ = self.signal();
}
}
}
fn sta_stopped() -> io::Error {
io::Error::other("MXAccess STA exited")
}
fn run(
requests: mpsc::Receiver<Request>,
wake: Arc<OwnedHandle>,
ready: oneshot::Sender<Result<String, HRESULT>>,
finished: oneshot::Sender<usize>,
publisher: Publisher,
) {
let apartment = match Apartment::initialize() {
Ok(apartment) => apartment,
Err(error) => {
let _ = ready.send(Err(error));
return;
}
};
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(RecordInfoPin::status(&pin))).is_ok() {
pump(&requests, &wake, &mut proxy);
}
let failures = proxy.cleanup();
drop(proxy);
drop(pin);
drop(apartment);
let _ = finished.send(failures);
}
fn pump(requests: &mpsc::Receiver<Request>, wake: &OwnedHandle, proxy: &mut Proxy) {
let mut alarms = AlarmRuntime::new();
pump_loop(requests, wake, proxy, &mut alarms);
alarms.close(proxy);
}
fn pump_loop(
requests: &mpsc::Receiver<Request>,
wake: &OwnedHandle,
proxy: &mut Proxy,
alarms: &mut AlarmRuntime,
) {
let handle = wake.as_raw_handle();
loop {
alarms.poll(proxy);
// SAFETY: the wait uses one valid event and the current STA's message queue.
let result = unsafe {
MsgWaitForMultipleObjectsEx(
1,
&handle,
alarms.wait_millis(),
QS_ALLINPUT,
MWMO_INPUTAVAILABLE,
)
};
match result {
WAIT_OBJECT_0 => loop {
match requests.try_recv() {
Ok(request) => {
// Fold already queued completions from prior writes into the stream
// before this write arms its handle-only completion match.
if matches!(request.command, Command::Write(_)) && !pump_messages() {
return;
}
let result = match request.command {
Command::Alarm(command) => {
let (reply, startup) = alarms.execute(proxy, *command);
Ok(CommandResult::Alarm(Box::new(reply), startup))
}
command => proxy.execute(command),
};
let _ = request.reply.send(result);
alarms.poll(proxy);
}
Err(mpsc::TryRecvError::Empty) => break,
Err(mpsc::TryRecvError::Disconnected) => return,
}
},
1 if pump_messages() => {}
WAIT_TIMEOUT => {}
_ => return,
}
}
}
pub(crate) fn pump_messages() -> bool {
let mut message = MSG::default();
// SAFETY: MSG lives across PeekMessageW and dispatch on this STA.
while unsafe { PeekMessageW(&mut message, null_mut(), 0, 0, PM_REMOVE) } != 0 {
if message.message == WM_QUIT {
return false;
}
unsafe {
TranslateMessage(&message);
DispatchMessageW(&message);
}
}
true
}
pub(crate) struct Apartment;
impl Apartment {
pub(crate) fn initialize() -> Result<Self, HRESULT> {
// SAFETY: this runs on the new dedicated thread and is balanced by Drop.
check(unsafe { CoInitializeEx(null(), COINIT_APARTMENTTHREADED as u32) })?;
Ok(Self)
}
}
impl Drop for Apartment {
fn drop(&mut self) {
// SAFETY: same thread as successful CoInitializeEx. Proxy has been released.
unsafe { CoUninitialize() };
}
}
// Dual ILMXProxyServer interface. Slots 0–6 are IUnknown and IDispatch.
// The installed type library orders subsequent methods and declares the
// write values as [in] VARIANT by value.
#[repr(C)]
struct ProxyVtable {
query_interface:
unsafe extern "system" fn(*mut c_void, *const GUID, *mut *mut c_void) -> HRESULT,
add_ref: unsafe extern "system" fn(*mut c_void) -> u32,
release: unsafe extern "system" fn(*mut c_void) -> u32,
dispatch_slots: [usize; 4],
register: unsafe extern "system" fn(*mut c_void, BSTR, *mut i32) -> HRESULT,
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,
write: unsafe extern "system" fn(*mut c_void, i32, i32, VARIANT, i32) -> HRESULT,
write_secured: unsafe extern "system" fn(*mut c_void, i32, i32, i32, i32, VARIANT) -> HRESULT,
authenticate_user: unsafe extern "system" fn(*mut c_void, i32, BSTR, BSTR, *mut i32) -> HRESULT,
}
#[repr(C)]
struct ProxyV2Vtable {
base: ProxyVtable,
archestra_user_to_id: unsafe extern "system" fn(*mut c_void, i32, BSTR, *mut i32) -> HRESULT,
}
#[repr(C)]
struct ProxyV3Vtable {
base: ProxyV2Vtable,
add_item2: unsafe extern "system" fn(*mut c_void, i32, BSTR, BSTR, *mut i32) -> HRESULT,
}
#[repr(C)]
struct ProxyV4Vtable {
base: ProxyV3Vtable,
write2: unsafe extern "system" fn(*mut c_void, i32, i32, VARIANT, VARIANT, i32) -> HRESULT,
write_secured2:
unsafe extern "system" fn(*mut c_void, i32, i32, i32, i32, VARIANT, VARIANT) -> HRESULT,
suspend: unsafe extern "system" fn(*mut c_void, i32, i32, *mut StatusRecord) -> HRESULT,
activate: unsafe extern "system" fn(*mut c_void, i32, i32, *mut StatusRecord) -> HRESULT,
advise_supervisory: unsafe extern "system" fn(*mut c_void, i32, i32) -> HRESULT,
}
#[repr(C)]
struct ProxyV5Vtable {
base: ProxyV4Vtable,
add_buffered_item: unsafe extern "system" fn(*mut c_void, i32, BSTR, BSTR, *mut i32) -> HRESULT,
set_buffered_update_interval: unsafe extern "system" fn(*mut c_void, i32, i32) -> HRESULT,
}
pub(crate) struct Proxy {
interface: NonNull<c_void>,
registrations: BTreeSet<i32>,
pub(crate) publisher: Publisher,
connections: Vec<Connection>,
items: BTreeMap<(i32, i32), String>,
}
impl Proxy {
fn activate(publisher: Publisher) -> Result<Self, HRESULT> {
let mut interface = null_mut();
// SAFETY: the output is for the exact IID declared by ProxyVtable.
check(unsafe {
CoCreateInstance(
&CLSID,
null_mut(),
CLSCTX_INPROC_SERVER,
&IID,
&mut interface,
)
})?;
let mut proxy = Self {
interface: NonNull::new(interface).ok_or(0x8000_4003_u32 as i32)?,
registrations: BTreeSet::new(),
publisher,
connections: Vec::new(),
items: BTreeMap::new(),
};
// SAFETY: this STA owns the live proxy and its connection point.
for buffered in [false, true] {
proxy
.connections
.push(unsafe { Connection::attach(interface, proxy.publisher.clone(), buffered) }?);
}
Ok(proxy)
}
pub(crate) fn execute(&mut self, command: Command) -> Outcome {
let object = self.interface.as_ptr();
let table = self.vtable();
let mut handle = 0;
// SAFETY: the proxy and temporary BSTRs are local to this STA.
match command {
Command::Probe => Ok(CommandResult::Done),
Command::Alarm(_) => {
unreachable!("alarm command must run through the STA alarm runtime")
}
Command::Write(request) => self.write(*request, true).map(|completion| {
CommandResult::Write(completion.expect("single write expects a completion"))
}),
Command::WriteBulk(requests) => Ok(CommandResult::WriteBulk(
requests
.into_iter()
.map(|request| self.write_bulk_entry(request))
.collect(),
)),
Command::Register(name) => {
let name = AutomationString::new(&name).map_err(CommandError::HResult)?;
check(unsafe { (table.register)(object, name.as_raw(), &mut handle) })
.map_err(CommandError::HResult)?;
self.registrations.insert(handle);
Ok(CommandResult::Handle(handle))
}
Command::Unregister(server) => {
self.require_registration(server)?;
check(unsafe { (table.unregister)(object, server) })
.map_err(CommandError::HResult)?;
self.registrations.remove(&server);
self.publisher.unregister(server);
self.items.retain(|(handle, _), _| *handle != server);
Ok(CommandResult::Done)
}
Command::AddItem(server, definition_text) => {
self.require_registration(server)?;
let definition =
AutomationString::new(&definition_text).map_err(CommandError::HResult)?;
check(unsafe {
(table.add_item)(object, server, definition.as_raw(), &mut handle)
})
.map_err(CommandError::HResult)?;
self.items.insert((server, handle), definition_text);
Ok(CommandResult::Handle(handle))
}
Command::AddWithContext {
server,
definition,
context,
buffered,
} => {
self.require_registration(server)?;
let item = AutomationString::new(&definition).map_err(CommandError::HResult)?;
let context_value =
AutomationString::new(&context).map_err(CommandError::HResult)?;
let status = if buffered {
let extended = self.query(&IID_V5).map_err(CommandError::HResult)?;
unsafe {
(interface_table::<ProxyV5Vtable>(&extended).add_buffered_item)(
extended.as_ptr(),
server,
item.as_raw(),
context_value.as_raw(),
&mut handle,
)
}
} else {
let extended = self.query(&IID_V3).map_err(CommandError::HResult)?;
// The V3 vtable ends at AddItem2. Only the prefix containing that slot is read.
let table = unsafe { interface_table::<ProxyV3Vtable>(&extended) };
unsafe {
(table.add_item2)(
extended.as_ptr(),
server,
item.as_raw(),
context_value.as_raw(),
&mut handle,
)
}
};
check(status).map_err(CommandError::HResult)?;
// Context is an input to the native resolver; the C# index keys the item definition.
self.items.insert((server, handle), definition);
Ok(CommandResult::Handle(handle))
}
Command::SetBufferedInterval(server, interval) => {
self.require_registration(server)?;
let extended = self.query(&IID_V5).map_err(CommandError::HResult)?;
check(unsafe {
(interface_table::<ProxyV5Vtable>(&extended).set_buffered_update_interval)(
extended.as_ptr(),
server,
interval,
)
})
.map_err(CommandError::HResult)?;
Ok(CommandResult::Done)
}
Command::Activation {
server,
item,
active,
} => {
self.require_registration(server)?;
let extended = self.extended().map_err(CommandError::HResult)?;
let table = extended_table(&extended);
let method = if active {
table.activate
} else {
table.suspend
};
let mut status = StatusRecord::default();
check(unsafe { method(extended.as_ptr(), server, item, &mut status) })
.map_err(CommandError::HResult)?;
Ok(CommandResult::Status(status.to_proto()))
}
Command::Authenticate(server, user, password) => {
self.require_registration(server)?;
let user = AutomationString::new(&user).map_err(CommandError::HResult)?;
let password = AutomationString::new(&password).map_err(CommandError::HResult)?;
check(unsafe {
(table.authenticate_user)(
object,
server,
user.as_raw(),
password.as_raw(),
&mut handle,
)
})
.map_err(CommandError::HResult)?;
Ok(CommandResult::Handle(handle))
}
Command::UserToId(server, guid) => {
self.require_registration(server)?;
let guid = AutomationString::new(&guid).map_err(CommandError::HResult)?;
let extended = self.query(&IID_V2).map_err(CommandError::HResult)?;
check(unsafe {
(interface_table::<ProxyV2Vtable>(&extended).archestra_user_to_id)(
extended.as_ptr(),
server,
guid.as_raw(),
&mut handle,
)
})
.map_err(CommandError::HResult)?;
Ok(CommandResult::Handle(handle))
}
Command::SubscribeBulk(server, tags, advise) => Ok(CommandResult::SubscribeBulk(
self.subscribe_bulk(server, tags, advise),
)),
Command::ItemBulk(server, handles, operation) => Ok(CommandResult::SubscribeBulk(
self.item_bulk(server, handles, operation),
)),
Command::ReadBulk(server, tags, timeout) => Ok(CommandResult::ReadBulk(
self.read_bulk(server, tags, timeout),
)),
Command::RemoveItem(server, item) => {
self.require_registration(server)?;
check(unsafe { (table.remove_item)(object, server, item) })
.map_err(CommandError::HResult)?;
self.publisher.unadvise(server, item);
self.items.remove(&(server, item));
Ok(CommandResult::Done)
}
Command::Advise {
server,
item,
supervisory,
} => {
self.require_registration(server)?;
let extended = if supervisory {
Some(self.extended().map_err(CommandError::HResult)?)
} else {
None
};
// The COM call may publish the initial value before it returns.
let added = self.publisher.advise(server, item);
let status = match extended {
Some(extended) => unsafe {
(extended_table(&extended).advise_supervisory)(
extended.as_ptr(),
server,
item,
)
},
None => unsafe { (table.advise)(object, server, item) },
};
if status < 0 && added {
self.publisher.unadvise(server, item);
}
check(status).map_err(CommandError::HResult)?;
Ok(CommandResult::Done)
}
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(CommandResult::Done)
}
}
}
fn write(
&self,
request: WriteRequest,
observe_completion: bool,
) -> Result<Option<WriteCompletion>, CommandError> {
let WriteRequest {
server,
item,
value,
timestamp,
access,
} = request;
self.require_registration(server)?;
let value = WriteValue::new(
value.ok_or(CommandError::Invalid("Write command value is required."))?,
false,
)?;
let timestamp = timestamp
.map(|time| WriteValue::new(time, true))
.transpose()?;
let extended = timestamp
.as_ref()
.map(|_| self.extended())
.transpose()
.map_err(CommandError::HResult)?;
// Arm before the native call so a reentrant callback is captured as well.
let completion = observe_completion.then(|| self.publisher.expect_write(server, item));
// SAFETY: all arguments match the installed vtables. Each owning VARIANT
// and optional extended interface stay alive on this STA until return.
let status = unsafe {
match (extended.as_ref().zip(timestamp.as_ref()), access) {
(None, WriteAccess::Plain(user)) => (self.vtable().write)(
self.interface.as_ptr(),
server,
item,
value.by_value(),
user,
),
(
None,
WriteAccess::Secured {
current_user,
verifier,
},
) => (self.vtable().write_secured)(
self.interface.as_ptr(),
server,
item,
current_user,
verifier,
value.by_value(),
),
(Some((extended, timestamp)), WriteAccess::Plain(user)) => {
(extended_table(extended).write2)(
extended.as_ptr(),
server,
item,
value.by_value(),
timestamp.by_value(),
user,
)
}
(
Some((extended, timestamp)),
WriteAccess::Secured {
current_user,
verifier,
},
) => (extended_table(extended).write_secured2)(
extended.as_ptr(),
server,
item,
current_user,
verifier,
value.by_value(),
timestamp.by_value(),
),
}
};
if status < 0 {
if observe_completion {
self.publisher.discard_pending_write();
}
return Err(CommandError::HResult(status));
}
Ok(completion)
}
fn write_bulk_entry(&self, request: WriteRequest) -> BulkWriteResult {
let mut result = BulkWriteResult {
server_handle: request.server,
item_handle: request.item,
..Default::default()
};
match self.write(request, false) {
Ok(_) => result.was_successful = true,
Err(CommandError::NotRegistered) => {
result.hresult = Some(E_INVALIDARG);
result.error_message = "Server handle is not registered in this session".into()
}
Err(CommandError::Invalid(message)) => result.error_message = message.into(),
Err(CommandError::HResult(hr)) => {
result.hresult = Some(hr);
result.error_message =
format!("MXAccess command failed (HRESULT 0x{:08X})", hr as u32);
}
}
result
}
fn extended(&self) -> Result<ComReference, HRESULT> {
self.query(&IID_V4)
}
fn query(&self, iid: &GUID) -> Result<ComReference, HRESULT> {
let mut pointer = null_mut();
// SAFETY: this is a live proxy on its STA. The IID matches ProxyV4Vtable.
check(unsafe {
(self.vtable().query_interface)(self.interface.as_ptr(), iid, &mut pointer)
})?;
ComReference::new(pointer)
}
fn require_registration(&self, server: i32) -> Result<(), CommandError> {
if self.registrations.contains(&server) {
Ok(())
} else {
Err(CommandError::NotRegistered)
}
}
fn cleanup(&mut self) -> usize {
self.publisher.discard_pending_write();
let advice = self.publisher.take_advice();
let mut failures = 0;
for mut connection in self.connections.drain(..) {
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) };
if hr < 0 {
failures += 1;
eprintln!(
"MXAccess unregister during shutdown failed (HRESULT 0x{:08X})",
hr as u32
);
}
}
failures
}
fn vtable(&self) -> &ProxyVtable {
// SAFETY: a live COM interface starts with its vtable pointer.
unsafe { &**self.interface.as_ptr().cast::<*const ProxyVtable>() }
}
}
impl Drop for Proxy {
fn drop(&mut self) {
self.connections.clear();
// SAFETY: release the reference returned by CoCreateInstance on this STA.
unsafe { (self.vtable().release)(self.interface.as_ptr()) };
}
}
fn extended_table(reference: &ComReference) -> &ProxyV4Vtable {
// SAFETY: the reference was obtained for IID_V4 by Proxy::extended.
unsafe { interface_table::<ProxyV4Vtable>(reference) }
}
unsafe fn interface_table<V>(reference: &ComReference) -> &V {
// SAFETY: caller selects the vtable matching QueryInterface's IID.
unsafe { &**reference.as_ptr().cast::<*const V>() }
}
fn check(status: HRESULT) -> Result<(), HRESULT> {
if status >= 0 {
Ok(())
} else {
Err(status)
}
}
@@ -0,0 +1,262 @@
use super::*;
use crate::wire::mxaccess_gateway::v1::MxEvent;
use std::time::Instant;
impl Proxy {
pub(super) fn subscribe_bulk(
&mut self,
server: i32,
tags: Vec<String>,
advise: bool,
) -> Vec<SubscribeResult> {
tags.into_iter()
.map(|tag| {
let mut result = SubscribeResult {
server_handle: server,
tag_address: tag,
..Default::default()
};
if result.tag_address.trim().is_empty() {
result.error_message = "Tag address is required.".into();
return result;
}
match self.add_item_handle(server, &result.tag_address) {
Ok(item) => {
result.item_handle = item;
if advise {
if let Err(error) = self.advise_item(server, item) {
result.error_message = error.message();
if let Err(cleanup) =
self.execute(Command::RemoveItem(server, item))
{
result.error_message.push_str(&format!(
"; cleanup RemoveItem failed: {}",
cleanup.message()
));
}
return result;
}
}
result.was_successful = true;
}
Err(error) => result.error_message = error.message(),
}
result
})
.collect()
}
pub(super) fn item_bulk(
&mut self,
server: i32,
items: Vec<i32>,
operation: ItemOperation,
) -> Vec<SubscribeResult> {
items
.into_iter()
.map(|item| {
let mut result = SubscribeResult {
server_handle: server,
item_handle: item,
..Default::default()
};
result.error_message = match operation {
ItemOperation::Advise => self
.advise_item(server, item)
.err()
.map_or_else(String::new, |err| err.message()),
ItemOperation::UnAdvise => self
.execute(Command::UnAdvise(server, item))
.err()
.map_or_else(String::new, |err| err.message()),
ItemOperation::Remove => self
.execute(Command::RemoveItem(server, item))
.err()
.map_or_else(String::new, |err| err.message()),
ItemOperation::Unsubscribe => {
let unadvise = self.execute(Command::UnAdvise(server, item));
let remove = self.execute(Command::RemoveItem(server, item));
[("UnAdvise", unadvise), ("RemoveItem", remove)]
.into_iter()
.filter_map(|(operation, result)| {
result
.err()
.map(|error| format!("{operation} failed: {}", error.message()))
})
.collect::<Vec<_>>()
.join("; ")
}
};
result.was_successful = result.error_message.is_empty();
result
})
.collect()
}
pub(super) fn read_bulk(
&mut self,
server: i32,
tags: Vec<String>,
timeout: Duration,
) -> Vec<BulkReadResult> {
let original_items: BTreeSet<_> = self
.items
.keys()
.filter_map(|&(handle, item)| (handle == server).then_some(item))
.collect();
let mut results = Vec::with_capacity(tags.len());
let mut waiting = Vec::new();
let mut cleanup = BTreeMap::new();
let mut seen_tags = BTreeMap::new();
for tag in tags {
let index = results.len();
let mut result = BulkReadResult {
server_handle: server,
tag_address: tag,
..Default::default()
};
if result.tag_address.trim().is_empty() {
result.error_message = "Tag address is required.".into();
} else if let Some((item, event)) = self
.items
.range((server, i32::MIN)..=(server, i32::MAX))
.filter(|(_, definition)| definition.as_str() == result.tag_address.as_str())
.find_map(|(&(_, item), _)| {
// Only original items count as a cached read for this command.
original_items
.contains(&item)
.then(|| self.publisher.latest_value(server, item))
.flatten()
.map(|event| (item, event))
})
{
result.item_handle = item;
result.was_cached = true;
fill_value(&mut result, event);
} else if let Some(&owner) = seen_tags.get(&result.tag_address) {
// A repeated address shares the same snapshot and outcome.
waiting.push((index, Some(owner)));
} else {
seen_tags.insert(result.tag_address.clone(), index);
match self.add_item_handle(server, &result.tag_address) {
Err(error) => result.error_message = error.message(),
Ok(item) => {
result.item_handle = item;
let already_advised = self.publisher.is_advised(server, item);
if let std::collections::btree_map::Entry::Vacant(entry) =
cleanup.entry(item)
{
let advice = if already_advised {
Ok(())
} else {
self.advise_item(server, item)
};
entry.insert((
!original_items.contains(&item),
!already_advised && advice.is_ok(),
));
if let Err(error) = advice {
result.error_message = error.message();
}
}
if result.error_message.is_empty() {
waiting.push((index, None));
}
}
}
}
results.push(result);
if index % 16 == 15 {
pump_messages();
}
}
let deadline = Instant::now() + timeout;
while pump_messages()
&& waiting.iter().any(|&(index, owner)| {
owner.is_none()
&& self
.publisher
.latest_value(server, results[index].item_handle)
.is_none()
})
{
let remaining = deadline.saturating_duration_since(Instant::now());
if remaining.is_zero() {
break;
}
// The STA remains available to deliver the OnDataChange callbacks.
unsafe {
MsgWaitForMultipleObjectsEx(
0,
null(),
remaining.as_millis().min(u32::MAX as u128 - 1) as u32,
QS_ALLINPUT,
MWMO_INPUTAVAILABLE,
);
}
}
for (index, owner) in waiting {
if let Some(owner) = owner {
let tag = std::mem::take(&mut results[index].tag_address);
results[index] = results[owner].clone();
results[index].tag_address = tag;
} else if let Some(event) = self
.publisher
.latest_value(server, results[index].item_handle)
{
fill_value(&mut results[index], event);
} else {
results[index].error_message = format!(
"ReadBulk timed out after {} ms waiting for first OnDataChange.",
timeout.as_millis()
);
}
}
for (item, (remove, unadvise)) in cleanup {
if unadvise {
let _ = self.execute(Command::UnAdvise(server, item));
}
if remove {
let _ = self.execute(Command::RemoveItem(server, item));
}
}
results
}
fn add_item_handle(&mut self, server: i32, address: &str) -> Result<i32, CommandError> {
match self.execute(Command::AddItem(server, address.into()))? {
CommandResult::Handle(handle) => Ok(handle),
_ => unreachable!("AddItem returns a handle"),
}
}
fn advise_item(&mut self, server: i32, item: i32) -> Result<(), CommandError> {
self.execute(Command::Advise {
server,
item,
supervisory: false,
})
.map(|_| ())
}
}
fn fill_value(result: &mut BulkReadResult, event: MxEvent) {
result.was_successful = true;
result.value = event.value;
result.quality = event.quality;
result.source_timestamp = event.source_timestamp;
result.statuses = event.statuses;
}
impl CommandError {
fn message(&self) -> String {
match self {
Self::NotRegistered => "Server handle is not registered in this session".into(),
Self::Invalid(message) => (*message).into(),
Self::HResult(hr) => format!("MXAccess command failed (HRESULT 0x{:08X})", *hr as u32),
}
}
}
+804
View File
@@ -0,0 +1,804 @@
use std::collections::{BTreeMap, BTreeSet, VecDeque};
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, oneshot, 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::{
buffered_array, dispatch_i32, dispatch_statuses, dispatch_timestamp, dispatch_value,
native_data_type,
};
use crate::wire::mxaccess_gateway::v1::{
mx_event, mx_value, MxDataType, MxEvent, MxEventFamily, MxStatusProxy,
OnBufferedDataChangeEvent, OnDataChangeEvent, OnWriteCompleteEvent, OperationCompleteEvent,
};
const EVENTS_IID: GUID = GUID::from_u128(0x848299b6_dd61_4a0d_a304_3947a564b89c);
const BUFFERED_EVENTS_IID: GUID = GUID::from_u128(0xc70a6fc4_09ef_4f31_8874_a049fee87a95);
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,
AlarmPoll(HRESULT),
Conversion(&'static str),
}
#[derive(Clone)]
pub struct Publisher {
session: Arc<str>,
events: mpsc::Sender<MxEvent>,
faults: watch::Sender<Option<EventFault>>,
state: Arc<Mutex<PublisherState>>,
}
pub type WriteCompletion = oneshot::Receiver<Vec<MxStatusProxy>>;
struct PendingWrite {
server: i32,
item: i32,
completion: oneshot::Sender<Vec<MxStatusProxy>>,
}
#[derive(Default)]
struct PublisherState {
advised: BTreeSet<(i32, i32)>,
last_sequence: u64,
latest_values: BTreeMap<(i32, i32), MxEvent>,
pending_write: Option<PendingWrite>,
private: BTreeMap<i32, Option<VecDeque<MxEvent>>>,
}
pub fn event_channel(
session: &str,
capacity: usize,
) -> (
Publisher,
mpsc::Receiver<MxEvent>,
watch::Receiver<Option<EventFault>>,
) {
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) {
let mut state = self.state.lock().unwrap();
state.advised.remove(&(server, item));
state.latest_values.remove(&(server, item));
}
pub fn private_registration(&self, server: i32, collect: bool) {
self.state
.lock()
.unwrap()
.private
.insert(server, collect.then(VecDeque::new));
}
pub fn private_events(&self, server: i32) -> VecDeque<MxEvent> {
self.state
.lock()
.unwrap()
.private
.get_mut(&server)
.and_then(Option::as_mut)
.map(std::mem::take)
.unwrap_or_default()
}
pub fn unregister(&self, server: i32) {
let mut state = self.state.lock().unwrap();
state.private.remove(&server);
state.advised.retain(|(handle, _)| *handle != server);
state
.latest_values
.retain(|(handle, _), _| *handle != server);
}
pub fn latest_value(&self, server: i32, item: i32) -> Option<MxEvent> {
self.state
.lock()
.unwrap()
.latest_values
.get(&(server, item))
.cloned()
}
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
}
pub(crate) fn is_advised(&self, server: i32, item: i32) -> bool {
self.state.lock().unwrap().advised.contains(&(server, item))
}
pub(crate) fn report_fault(&self, fault: EventFault) {
self.faults.send_if_modified(|current| {
if current.is_some() {
return false;
}
*current = Some(fault);
true
});
}
pub fn expect_write(&self, server: i32, item: i32) -> WriteCompletion {
let (completion, receiver) = oneshot::channel();
self.state.lock().unwrap().pending_write = Some(PendingWrite {
server,
item,
completion,
});
receiver
}
pub fn discard_pending_write(&self) {
self.state.lock().unwrap().pending_write.take();
}
pub(crate) 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;
if event.family() == MxEventFamily::OnDataChange {
let key = (event.server_handle, event.item_handle);
if !state.advised.contains(&key) {
return;
}
if let Some(queue) = state.private.get_mut(&event.server_handle) {
if let Some(queue) = queue {
if queue.len() == self.events.max_capacity() {
self.report_fault(EventFault::Overflow);
} else {
queue.push_back(event);
}
}
return;
}
state.latest_values.insert(key, event.clone());
}
if event.family() == MxEventFamily::OnWriteComplete
&& state.pending_write.as_ref().is_some_and(|pending| {
pending.server == event.server_handle && pending.item == event.item_handle
})
{
let pending = state.pending_write.take().expect("matched pending write");
if !pending.completion.is_closed() {
let _ = pending.completion.send(event.statuses.clone());
}
}
if state.private.contains_key(&event.server_handle) {
return;
}
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<u32>,
}
impl Connection {
// The caller supplies the live LMXProxy interface on the owning STA.
pub unsafe fn attach(
proxy: *mut c_void,
publisher: Publisher,
buffered: bool,
) -> Result<Self, HRESULT> {
let events_iid = if buffered {
BUFFERED_EVENTS_IID
} else {
EVENTS_IID
};
let mut container = null_mut();
let unknown = unsafe { vtable::<IUnknown_Vtbl>(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::<ConnectionPointContainerVtable>(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,
buffered,
reference_count: AtomicU32::new(1),
publisher,
});
let sink = ComReference::new(Box::into_raw(sink).cast())?;
let mut cookie = 0;
let table = unsafe { vtable::<ConnectionPointVtable>(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::<ConnectionPointVtable>(pointer) };
check(unsafe { (table.unadvise)(pointer, cookie) })?;
}
Ok(())
}
}
impl Drop for Connection {
fn drop(&mut self) {
let _ = self.detach();
}
}
pub(crate) struct ComReference(NonNull<c_void>);
impl ComReference {
pub(crate) fn new(pointer: *mut c_void) -> Result<Self, HRESULT> {
NonNull::new(pointer).map(Self).ok_or(E_POINTER)
}
pub(crate) 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::<IUnknown_Vtbl>(pointer).Release)(pointer) };
}
}
unsafe fn vtable<V>(pointer: *mut c_void) -> &'static V {
unsafe { &**pointer.cast::<*const V>() }
}
pub struct RecordInfoPin {
_library: ComReference,
_record: ComReference,
}
impl RecordInfoPin {
pub fn load() -> Result<Self, HRESULT> {
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<Self, HRESULT>) -> 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,
buffered: bool,
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;
};
let sink = unsafe { &*this.cast::<Sink>() };
let events_iid = if sink.buffered {
BUFFERED_EVENTS_IID
} else {
EVENTS_IID
};
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>() };
sink.reference_count.fetch_add(1, Ordering::Relaxed) + 1
}
unsafe extern "system" fn release(this: *mut c_void) -> u32 {
let sink = unsafe { &*this.cast::<Sink>() };
let remaining = sink.reference_count.fetch_sub(1, Ordering::AcqRel) - 1;
if remaining == 0 {
unsafe { drop(Box::from_raw(this.cast::<Sink>())) };
}
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::<Sink>() };
// 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 (self.buffered, member) {
(true, 1) => (
7,
MxEventFamily::OnBufferedDataChange,
mx_event::Body::OnBufferedDataChange(OnBufferedDataChangeEvent::default()),
),
(false, 1) => (
6,
MxEventFamily::OnDataChange,
mx_event::Body::OnDataChange(OnDataChangeEvent {}),
),
(false, 2) => (
3,
MxEventFamily::OnWriteComplete,
mx_event::Body::OnWriteComplete(OnWriteCompleteEvent {}),
),
(false, 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(); 7];
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 self.buffered {
let Some(raw_data_type) = (unsafe { dispatch_i32(&args[4]) }) else {
self.publisher.report_fault(EventFault::Conversion(
"unexpected buffered MXAccess data type",
));
return DISP_E_TYPEMISMATCH;
};
let data_type = native_data_type(raw_data_type);
let mut value = unsafe { dispatch_value(&args[3]) };
if let Some(mx_value::Kind::ArrayValue(array)) = &mut value.kind {
if data_type != MxDataType::NoData as i32 && data_type != MxDataType::Unknown as i32
{
// Inferred Automation scalar/array types remain available on the value.
array.raw_element_data_type = raw_data_type;
}
}
event.value = Some(value);
event.body = Some(mx_event::Body::OnBufferedDataChange(
OnBufferedDataChangeEvent {
data_type,
raw_data_type,
quality_values: Some(unsafe { buffered_array(&args[2], MxDataType::Integer) }),
timestamp_values: Some(unsafe { buffered_array(&args[1], MxDataType::Time) }),
},
));
} else 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::<VARIANT>()]);
let (publisher, mut events, faults) = event_channel("session", 1);
let sink = Sink {
table: &SINK_VTABLE,
buffered: false,
reference_count: AtomicU32::new(1),
publisher,
};
let mut packed = PackedArguments([0; 4 + 6 * size_of::<VARIANT>()]);
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::<VARIANT>();
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, &parameters) }, S_OK);
assert_eq!(unsafe { sink.on_dispatch(1, &parameters) }, 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, &parameters) }, S_OK);
assert!(matches!(
events.try_recv(),
Err(mpsc::error::TryRecvError::Empty)
));
}
#[test]
fn matches_write_completion_by_item_without_consuming_the_stream_event() {
let (publisher, mut events, _) = event_channel("session", 3);
let mut completion = publisher.expect_write(4, 6);
let statuses = vec![MxStatusProxy {
detail: 1008,
..Default::default()
}];
for (server, item) in [(5, 6), (4, 7)] {
publisher.publish(MxEvent {
family: MxEventFamily::OnWriteComplete as i32,
server_handle: server,
item_handle: item,
..Default::default()
});
assert!(matches!(
completion.try_recv(),
Err(oneshot::error::TryRecvError::Empty)
));
}
publisher.publish(MxEvent {
family: MxEventFamily::OnWriteComplete as i32,
server_handle: 4,
item_handle: 6,
statuses: statuses.clone(),
..Default::default()
});
assert_eq!(completion.try_recv().unwrap(), statuses);
assert_eq!(events.try_recv().unwrap().worker_sequence, 1);
assert_eq!(events.try_recv().unwrap().worker_sequence, 2);
assert_eq!(events.try_recv().unwrap().statuses, statuses);
}
#[test]
fn preserves_buffered_samples_quality_and_filetime_timestamps() {
use crate::automation::WriteValue;
use crate::wire::mxaccess_gateway::v1::{
mx_array::Values, Int32Array, Int64Array, MxArray, MxValue,
};
let (publisher, mut events, _) = event_channel("session", 4);
let sink = Sink {
table: &SINK_VTABLE,
buffered: true,
reference_count: AtomicU32::new(1),
publisher,
};
sink.publisher.advise(5, 7);
let array = |values| {
WriteValue::new(
MxValue {
kind: Some(mx_value::Kind::ArrayValue(MxArray {
dimensions: vec![2],
values: Some(values),
..Default::default()
})),
..Default::default()
},
false,
)
.unwrap()
};
let samples = array(Values::Int32Values(Int32Array {
values: vec![40, 41],
}));
let qualities = array(Values::Int32Values(Int32Array {
values: vec![192, 0],
}));
let times = array(Values::Int64Values(Int64Array {
values: vec![116_444_736_000_000_000, 116_444_736_010_000_000],
}));
let mut arguments = [
VARIANT::default(),
times.by_value(),
qualities.by_value(),
samples.by_value(),
integer(2),
integer(7),
integer(5),
];
let parameters = DISPPARAMS {
rgvarg: arguments.as_mut_ptr(),
cArgs: 7,
..Default::default()
};
assert_eq!(unsafe { sink.on_dispatch(1, &parameters) }, S_OK);
let event = events.try_recv().unwrap();
assert_eq!(event.family(), MxEventFamily::OnBufferedDataChange);
let Some(mx_value::Kind::ArrayValue(value)) = event.value.unwrap().kind else {
panic!("missing buffered values");
};
assert_eq!(
value.values,
Some(Values::Int32Values(Int32Array {
values: vec![40, 41]
}))
);
let Some(mx_event::Body::OnBufferedDataChange(body)) = event.body else {
panic!("missing buffered body");
};
assert_eq!(body.data_type, MxDataType::Integer as i32);
assert_eq!(
body.quality_values.unwrap().values,
Some(Values::Int32Values(Int32Array {
values: vec![192, 0]
}))
);
let Some(Values::TimestampValues(times)) = body.timestamp_values.unwrap().values else {
panic!("missing timestamps");
};
assert_eq!(
times
.values
.into_iter()
.map(|value| value.seconds)
.collect::<Vec<_>>(),
[0, 1]
);
}
fn integer(value: i32) -> VARIANT {
let mut result = VARIANT::default();
result.Anonymous.Anonymous.vt = VT_I4;
result.Anonymous.Anonymous.Anonymous.lVal = value;
result
}
}
File diff suppressed because it is too large Load Diff
+150
View File
@@ -0,0 +1,150 @@
use std::io;
use prost::Message;
use tokio::io::{AsyncRead, AsyncReadExt, AsyncWrite, AsyncWriteExt};
// The public contract also defines RPCs the worker never originates.
#[allow(dead_code, clippy::enum_variant_names, clippy::large_enum_variant)]
pub mod mxaccess_gateway {
pub mod v1 {
include!(concat!(env!("OUT_DIR"), "/mxaccess_gateway.v1.rs"));
}
}
#[allow(clippy::large_enum_variant)]
pub mod mxaccess_worker {
pub mod v1 {
include!(concat!(env!("OUT_DIR"), "/mxaccess_worker.v1.rs"));
}
}
pub use mxaccess_worker::v1::{worker_envelope::Body, WorkerEnvelope};
pub const VERSION: u32 = 1;
pub const DEFAULT_MAX: usize = 16 * 1024 * 1024 + 64 * 1024;
pub fn negotiated_max(value: u32) -> io::Result<usize> {
match value {
0 => Ok(DEFAULT_MAX),
1024..=268_435_456 => Ok(value as usize),
_ => Err(invalid(
"GatewayHello frame maximum is outside the supported range",
)),
}
}
pub async fn read(
stream: &mut (impl AsyncRead + Unpin),
session: &str,
max: usize,
) -> io::Result<WorkerEnvelope> {
let mut header = [0; 4];
stream.read_exact(&mut header).await?;
let length = u32::from_le_bytes(header) as usize;
if length == 0 || length > max {
return Err(invalid(
"Worker frame length is outside the supported range",
));
}
let mut payload = vec![0; length];
stream.read_exact(&mut payload).await?;
let frame = WorkerEnvelope::decode(payload.as_slice())
.map_err(|error| io::Error::new(io::ErrorKind::InvalidData, error))?;
if frame.protocol_version != VERSION || frame.session_id != session || frame.body.is_none() {
return Err(invalid(
"Worker envelope has an invalid version, session, or body",
));
}
Ok(frame)
}
pub struct Writer<W> {
stream: W,
sequence: u64,
}
impl<W: AsyncWrite + Unpin> Writer<W> {
pub fn new(stream: W) -> Self {
Self {
stream,
sequence: 0,
}
}
pub async fn send(
&mut self,
session: &str,
correlation: &str,
body: Body,
max: usize,
) -> io::Result<()> {
let frame = WorkerEnvelope {
protocol_version: VERSION,
session_id: session.into(),
sequence: self.sequence + 1,
correlation_id: correlation.into(),
body: Some(body),
};
let length = frame.encoded_len();
if length > max {
return Err(invalid(
"Outgoing worker frame exceeds the negotiated maximum",
));
}
let mut data = Vec::with_capacity(4 + length);
data.extend_from_slice(&(length as u32).to_le_bytes());
frame
.encode(&mut data)
.expect("encoding into a growable buffer");
self.stream.write_all(&data).await?;
self.stream.flush().await?;
self.sequence = frame.sequence;
Ok(())
}
}
fn invalid(message: &'static str) -> io::Error {
io::Error::new(io::ErrorKind::InvalidData, message)
}
#[cfg(test)]
mod tests {
use super::*;
use mxaccess_worker::v1::WorkerHello;
#[tokio::test]
async fn round_trip_keeps_wire_order_after_size_rejection() {
let mut bytes = Vec::new();
let mut writer = Writer::new(&mut bytes);
let hello = Body::WorkerHello(WorkerHello::default());
writer.send("test", "", hello.clone(), 1).await.unwrap_err();
for _ in 0..2 {
writer
.send("test", "corr", hello.clone(), DEFAULT_MAX)
.await
.unwrap();
}
let mut input = bytes.as_slice();
for sequence in 1..=2 {
let frame = read(&mut input, "test", DEFAULT_MAX).await.unwrap();
assert_eq!(frame.sequence, sequence);
assert_eq!(frame.correlation_id, "corr");
assert_eq!(frame.body, Some(hello.clone()));
}
assert!(input.is_empty());
}
#[tokio::test]
async fn rejects_oversized_and_empty_headers_before_reading_payload() {
for length in [0_u32, 1025] {
let header = length.to_le_bytes();
let error = read(&mut header.as_slice(), "test", 1024)
.await
.unwrap_err();
assert_eq!(error.kind(), io::ErrorKind::InvalidData);
}
assert_eq!(negotiated_max(0).unwrap(), DEFAULT_MAX);
assert!(negotiated_max(1023).is_err());
assert!(negotiated_max(268_435_457).is_err());
}
}
@@ -0,0 +1,285 @@
#![cfg(all(windows, target_arch = "x86"))]
#[path = "../src/wire.rs"]
mod wire;
use std::process::{Child, Command};
use std::time::{Duration, SystemTime, UNIX_EPOCH};
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, 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(40);
#[tokio::test]
#[ignore = "requires a registered 32-bit System Platform MXAccess installation"]
async fn activates_registers_and_shuts_down_over_a_windows_pipe() {
let token = format!(
"{}-{}",
std::process::id(),
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_nanos()
);
let pipe_name = format!("mxgw-rust-smoke-{token}");
let pipe = ServerOptions::new()
.first_pipe_instance(true)
.create(format!(r"\\.\pipe\{pipe_name}"))
.unwrap();
let worker_path = std::env::var_os("MXGATEWAY_RUST_SMOKE_WORKER")
.unwrap_or_else(|| env!("CARGO_BIN_EXE_mxgateway-worker-rust").into());
let mut child = ChildGuard(
Command::new(worker_path)
.args([
"--session-id",
&token,
"--pipe-name",
&pipe_name,
"--protocol-version",
"1",
])
.env("MXGATEWAY_WORKER_NONCE", &token)
.env("MXGATEWAY_WORKER_HEARTBEAT_SEND_INTERVAL_MS", "100")
.spawn()
.unwrap(),
);
let session = &token;
timeout(DEADLINE, async {
pipe.connect().await.unwrap();
let (mut reader, sender) = split(pipe);
let mut writer = Writer::new(sender);
writer
.send(
session,
"",
Body::GatewayHello(GatewayHello {
supported_protocol_version: VERSION,
nonce: token.clone(),
max_frame_bytes: 4096,
..Default::default()
}),
DEFAULT_MAX,
)
.await
.unwrap();
let Body::WorkerHello(hello) = read_body(&mut reader, session).await else {
panic!("expected WorkerHello");
};
assert_eq!(hello.nonce, token);
assert_eq!(hello.worker_process_id, child.0.id() as i32);
let ready = read_body(&mut reader, session).await;
let Body::WorkerReady(ready) = ready else {
panic!("expected WorkerReady, got {ready:?}");
};
assert_eq!(ready.worker_process_id, child.0.id() as i32);
let Body::WorkerHeartbeat(heartbeat) = read_body(&mut reader, session).await else {
panic!("expected an idle heartbeat");
};
assert_eq!(heartbeat.worker_process_id, ready.worker_process_id);
let registration = invoke(
&mut reader,
&mut writer,
session,
"register",
MxCommandKind::Register,
mx_command::Payload::Register(RegisterCommand {
client_name: format!("mxgw-rust-smoke-{}", child.0.id()),
}),
)
.await;
let Some(mx_command_reply::Payload::Register(registration)) = registration.payload else {
panic!("missing registration handle");
};
assert!(registration.server_handle > 0);
if let Ok(definition) = std::env::var("MXGATEWAY_RUST_SMOKE_ITEM") {
let add_item = invoke(
&mut reader,
&mut writer,
session,
"add",
MxCommandKind::AddItem,
mx_command::Payload::AddItem(AddItemCommand {
server_handle: registration.server_handle,
item_definition: definition,
}),
)
.await;
let Some(mx_command_reply::Payload::AddItem(item)) = add_item.payload else {
panic!("missing item handle");
};
assert!(item.item_handle > 0);
invoke(
&mut reader,
&mut writer,
session,
"remove",
MxCommandKind::RemoveItem,
mx_command::Payload::RemoveItem(RemoveItemCommand {
server_handle: registration.server_handle,
item_handle: item.item_handle,
}),
)
.await;
}
invoke(
&mut reader,
&mut writer,
session,
"unregister",
MxCommandKind::Unregister,
mx_command::Payload::Unregister(UnregisterCommand {
server_handle: registration.server_handle,
}),
)
.await;
// 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,
"cleanup",
MxCommandKind::Register,
mx_command::Payload::Register(RegisterCommand {
client_name: format!("mxgw-rust-cleanup-{}", child.0.id()),
}),
)
.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,
"",
Body::WorkerShutdown(WorkerShutdown::default()),
4096,
)
.await
.unwrap();
loop {
match read_body(&mut reader, session).await {
Body::WorkerHeartbeat(_) | Body::WorkerEvent(_) => {}
Body::WorkerShutdownAck(ack) => {
let status = ack.status.unwrap();
assert_eq!(status.code(), ProtocolStatusCode::Ok);
assert_eq!(status.message, "Graceful shutdown completed");
break;
}
other => panic!("expected shutdown acknowledgement, got {other:?}"),
}
}
loop {
if let Some(status) = child.0.try_wait().unwrap() {
assert!(status.success(), "{status}");
break;
}
sleep(Duration::from_millis(10)).await;
}
})
.await
.expect("live worker smoke test timed out");
}
async fn invoke(
reader: &mut (impl AsyncRead + Unpin),
writer: &mut Writer<impl AsyncWrite + Unpin>,
session: &str,
correlation: &str,
kind: MxCommandKind,
payload: mx_command::Payload,
) -> MxCommandReply {
writer
.send(
session,
correlation,
Body::WorkerCommand(WorkerCommand {
command: Some(MxCommand {
kind: kind as i32,
payload: Some(payload),
}),
enqueue_timestamp: None,
}),
4096,
)
.await
.unwrap();
loop {
let frame = wire::read(reader, session, 4096).await.unwrap();
match frame.body.unwrap() {
Body::WorkerHeartbeat(_) | Body::WorkerEvent(_) => {}
Body::WorkerCommandReply(result) => {
let reply = result.reply.unwrap();
assert_eq!(frame.correlation_id, correlation);
assert_eq!(reply.correlation_id, correlation);
assert_eq!(reply.kind(), kind);
assert_eq!(
reply.protocol_status.as_ref().unwrap().code(),
ProtocolStatusCode::Ok,
"{reply:?}"
);
return reply;
}
other => panic!("expected command reply, got {other:?}"),
}
}
}
async fn read_body(reader: &mut (impl AsyncRead + Unpin), session: &str) -> Body {
wire::read(reader, session, 4096)
.await
.unwrap()
.body
.unwrap()
}
struct ChildGuard(Child);
impl Drop for ChildGuard {
fn drop(&mut self) {
if matches!(self.0.try_wait(), Ok(None)) {
let _ = self.0.kill();
let _ = self.0.wait();
}
}
}