Implement native MXAccess writes in the Rust worker
This commit is contained in:
@@ -6,8 +6,8 @@ schemas at build time.
|
||||
|
||||
The worker authenticates the launch nonce, negotiates a frame limit, activates
|
||||
LMXProxy on a dedicated message-pumping STA, and reports readiness and heartbeats.
|
||||
It supports Register, Unregister, AddItem, RemoveItem, Advise, UnAdvise, and the
|
||||
worker diagnostic commands. The worker uses the base MXAccess COM connection
|
||||
It supports Register, Unregister, AddItem, RemoveItem, Advise,
|
||||
AdviseSupervisory, UnAdvise, and the worker diagnostic commands. The worker uses the base MXAccess COM connection
|
||||
point for OnDataChange, OperationComplete, and OnWriteComplete notifications.
|
||||
OnDataChange carries the value, quality, MXAccess status records, source
|
||||
timestamp when available, and worker sequence and timestamp. The bounded queue
|
||||
@@ -17,10 +17,20 @@ 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. Writes, buffered or supervisory advice,
|
||||
bulk commands, and alarms remain to be implemented. Unsupported commands
|
||||
return `InvalidRequest`. The worker serializes commands. If a COM call exceeds
|
||||
the STA timeout, the worker faults the session and exits.
|
||||
Automation type in the diagnostic. The worker serializes commands and faults
|
||||
the session if a COM call exceeds the STA timeout. Buffered advice, bulk
|
||||
subscriptions, snapshot reads, and alarm commands are outside the current
|
||||
implementation.
|
||||
|
||||
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.
|
||||
|
||||
## Build and test
|
||||
|
||||
@@ -62,6 +72,16 @@ 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
|
||||
```
|
||||
|
||||
## COM interface evidence
|
||||
|
||||
The native adapter uses the dual `ILMXProxyServer` interface from the installed
|
||||
@@ -78,3 +98,5 @@ the corresponding native calls and event callbacks.
|
||||
`ev_78053da509335e3e3749b8e6bb020f28c45255b54d910546659c6760eb52160b`
|
||||
- REA event member evidence
|
||||
`ev_97162abcd831b2d84b5b43fb72cd9876252bbe3bb0af5c4673a6954911500e54`
|
||||
- REA write member evidence for `ILMXProxyServer` and `ILMXProxyServer4`
|
||||
`ev_1b4fe7ab436437391b76f9fa3a300f45b58a4e1cff6aa84e30854f653c0a20df`
|
||||
|
||||
@@ -4,16 +4,21 @@ use std::ptr::{null, null_mut};
|
||||
use std::slice;
|
||||
|
||||
use prost_types::Timestamp;
|
||||
use windows_sys::core::BSTR;
|
||||
use windows_sys::Win32::Foundation::{SysStringLen, FILETIME, SYSTEMTIME};
|
||||
use windows_sys::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, SafeArrayGetDim, SafeArrayGetElemsize, SafeArrayGetLBound,
|
||||
SafeArrayGetUBound, SafeArrayGetVartype, SafeArrayPtrOfIndex, SafeArrayUnaccessData,
|
||||
VarDateFromStr,
|
||||
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::Time::{SystemTimeToFileTime, TzSpecificLocalTimeToSystemTime};
|
||||
use windows_sys::Win32::System::Variant::*;
|
||||
|
||||
use crate::wire::mxaccess_gateway::v1::{
|
||||
@@ -26,6 +31,240 @@ 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(×tamp.nanos)
|
||||
|| !(-62_135_596_800..=253_402_300_799).contains(×tamp.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 {
|
||||
@@ -496,6 +735,67 @@ mod tests {
|
||||
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 {
|
||||
|
||||
@@ -1,4 +1,6 @@
|
||||
use crate::mxevent::{Connection, Publisher, RecordInfoPin};
|
||||
use crate::automation::{AutomationString, WriteValue, WriteValueError};
|
||||
use crate::mxevent::{ComReference, Connection, Publisher, RecordInfoPin, WriteCompletion};
|
||||
use crate::wire::mxaccess_gateway::v1::{BulkWriteResult, MxValue};
|
||||
use std::collections::BTreeSet;
|
||||
use std::ffi::c_void;
|
||||
use std::io;
|
||||
@@ -8,12 +10,13 @@ use std::sync::{mpsc, Arc};
|
||||
use std::thread;
|
||||
use tokio::sync::oneshot;
|
||||
use windows_sys::core::{BSTR, GUID, HRESULT};
|
||||
use windows_sys::Win32::Foundation::{SysAllocStringLen, SysFreeString, WAIT_OBJECT_0};
|
||||
use windows_sys::Win32::Foundation::WAIT_OBJECT_0;
|
||||
use windows_sys::Win32::System::Com::{
|
||||
CoCreateInstance, CoInitializeEx, CoUninitialize, CLSCTX_INPROC_SERVER,
|
||||
COINIT_APARTMENTTHREADED,
|
||||
};
|
||||
use windows_sys::Win32::System::Threading::{CreateEventW, SetEvent, INFINITE};
|
||||
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,
|
||||
@@ -23,6 +26,7 @@ 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_V4: GUID = GUID::from_u128(0x9dc0d5c1_9371_4e84_86f8_7091d316a66c);
|
||||
|
||||
pub enum Command {
|
||||
Probe,
|
||||
@@ -30,16 +34,52 @@ pub enum Command {
|
||||
Unregister(i32),
|
||||
AddItem(i32, String),
|
||||
RemoveItem(i32, i32),
|
||||
Advise(i32, i32),
|
||||
Advise {
|
||||
server: i32,
|
||||
item: i32,
|
||||
supervisory: bool,
|
||||
},
|
||||
Write(Box<WriteRequest>),
|
||||
WriteBulk(Vec<WriteRequest>),
|
||||
UnAdvise(i32, i32),
|
||||
}
|
||||
|
||||
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),
|
||||
}
|
||||
|
||||
type Outcome = Result<Option<i32>, CommandError>;
|
||||
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,
|
||||
Handle(i32),
|
||||
Write(WriteCompletion),
|
||||
WriteBulk(Vec<BulkWriteResult>),
|
||||
}
|
||||
|
||||
type Outcome = Result<CommandResult, CommandError>;
|
||||
|
||||
pub enum StartError {
|
||||
Io(io::Error),
|
||||
@@ -171,30 +211,38 @@ fn pump(requests: &mpsc::Receiver<Request>, wake: &OwnedHandle, proxy: &mut Prox
|
||||
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 _ = request.reply.send(proxy.execute(request.command));
|
||||
}
|
||||
Err(mpsc::TryRecvError::Empty) => break,
|
||||
Err(mpsc::TryRecvError::Disconnected) => return,
|
||||
}
|
||||
},
|
||||
1 => {
|
||||
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;
|
||||
}
|
||||
unsafe {
|
||||
TranslateMessage(&message);
|
||||
DispatchMessageW(&message);
|
||||
}
|
||||
}
|
||||
}
|
||||
1 if pump_messages() => {}
|
||||
_ => return,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
struct Apartment;
|
||||
|
||||
impl Apartment {
|
||||
@@ -212,9 +260,9 @@ impl Drop for Apartment {
|
||||
}
|
||||
}
|
||||
|
||||
// First thirteen slots of the installed dual ILMXProxyServer interface. Slots
|
||||
// 0–6 are IUnknown and IDispatch. The next six slots follow the installed
|
||||
// interop assembly, with HRESULT and [out, retval] for the handle results.
|
||||
// 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:
|
||||
@@ -228,6 +276,22 @@ struct ProxyVtable {
|
||||
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: usize,
|
||||
}
|
||||
|
||||
#[repr(C)]
|
||||
struct ProxyV4Vtable {
|
||||
base: ProxyVtable,
|
||||
archestra_user_to_id: usize,
|
||||
add_item2: usize,
|
||||
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: usize,
|
||||
activate: usize,
|
||||
advise_supervisory: unsafe extern "system" fn(*mut c_void, i32, i32) -> HRESULT,
|
||||
}
|
||||
|
||||
struct Proxy {
|
||||
@@ -267,13 +331,22 @@ impl Proxy {
|
||||
let mut handle = 0;
|
||||
// SAFETY: the proxy and temporary BSTRs are local to this STA.
|
||||
match command {
|
||||
Command::Probe => Ok(None),
|
||||
Command::Probe => Ok(CommandResult::Done),
|
||||
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.0, &mut handle) })
|
||||
check(unsafe { (table.register)(object, name.as_raw(), &mut handle) })
|
||||
.map_err(CommandError::HResult)?;
|
||||
self.registrations.insert(handle);
|
||||
Ok(Some(handle))
|
||||
Ok(CommandResult::Handle(handle))
|
||||
}
|
||||
Command::Unregister(server) => {
|
||||
self.require_registration(server)?;
|
||||
@@ -281,44 +354,182 @@ impl Proxy {
|
||||
.map_err(CommandError::HResult)?;
|
||||
self.registrations.remove(&server);
|
||||
self.publisher.unregister(server);
|
||||
Ok(None)
|
||||
Ok(CommandResult::Done)
|
||||
}
|
||||
Command::AddItem(server, definition) => {
|
||||
self.require_registration(server)?;
|
||||
let definition =
|
||||
AutomationString::new(&definition).map_err(CommandError::HResult)?;
|
||||
check(unsafe { (table.add_item)(object, server, definition.0, &mut handle) })
|
||||
.map_err(CommandError::HResult)?;
|
||||
Ok(Some(handle))
|
||||
check(unsafe {
|
||||
(table.add_item)(object, server, definition.as_raw(), &mut handle)
|
||||
})
|
||||
.map_err(CommandError::HResult)?;
|
||||
Ok(CommandResult::Handle(handle))
|
||||
}
|
||||
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);
|
||||
Ok(None)
|
||||
Ok(CommandResult::Done)
|
||||
}
|
||||
Command::Advise(server, item) => {
|
||||
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 = unsafe { (table.advise)(object, 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(None)
|
||||
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(None)
|
||||
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.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> {
|
||||
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_V4, &mut pointer)
|
||||
})?;
|
||||
ComReference::new(pointer)
|
||||
}
|
||||
|
||||
fn require_registration(&self, server: i32) -> Result<(), CommandError> {
|
||||
if self.registrations.contains(&server) {
|
||||
Ok(())
|
||||
@@ -328,6 +539,7 @@ impl Proxy {
|
||||
}
|
||||
|
||||
fn cleanup(&mut self) -> usize {
|
||||
self.publisher.discard_pending_write();
|
||||
let advice = self.publisher.take_advice();
|
||||
let mut failures = 0;
|
||||
if let Some(mut connection) = self.connection.take() {
|
||||
@@ -378,25 +590,9 @@ impl Drop for Proxy {
|
||||
}
|
||||
}
|
||||
|
||||
struct AutomationString(BSTR);
|
||||
|
||||
impl AutomationString {
|
||||
fn new(value: &str) -> Result<Self, 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() {
|
||||
return Err(0x8007_000E_u32 as i32);
|
||||
}
|
||||
Ok(Self(string))
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for AutomationString {
|
||||
fn drop(&mut self) {
|
||||
// SAFETY: this BSTR came from SysAllocStringLen.
|
||||
unsafe { SysFreeString(self.0) };
|
||||
}
|
||||
fn extended_table(reference: &ComReference) -> &ProxyV4Vtable {
|
||||
// SAFETY: the reference was obtained for IID_V4 by Proxy::extended.
|
||||
unsafe { &**reference.as_ptr().cast::<*const ProxyV4Vtable>() }
|
||||
}
|
||||
|
||||
fn check(status: HRESULT) -> Result<(), HRESULT> {
|
||||
|
||||
@@ -7,7 +7,7 @@ use std::sync::{
|
||||
};
|
||||
use std::time::SystemTime;
|
||||
|
||||
use tokio::sync::{mpsc, watch};
|
||||
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,
|
||||
@@ -19,7 +19,7 @@ use windows_sys::Win32::System::Variant::VARIANT;
|
||||
|
||||
use crate::automation::{dispatch_i32, dispatch_statuses, dispatch_timestamp, dispatch_value};
|
||||
use crate::wire::mxaccess_gateway::v1::{
|
||||
mx_event, MxEvent, MxEventFamily, OnDataChangeEvent, OnWriteCompleteEvent,
|
||||
mx_event, MxEvent, MxEventFamily, MxStatusProxy, OnDataChangeEvent, OnWriteCompleteEvent,
|
||||
OperationCompleteEvent,
|
||||
};
|
||||
|
||||
@@ -44,10 +44,19 @@ pub struct Publisher {
|
||||
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,
|
||||
pending_write: Option<PendingWrite>,
|
||||
}
|
||||
|
||||
pub fn event_channel(
|
||||
@@ -111,11 +120,35 @@ impl Publisher {
|
||||
});
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
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::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());
|
||||
}
|
||||
}
|
||||
match self.events.try_send(event) {
|
||||
Ok(()) => state.last_sequence += 1,
|
||||
Err(mpsc::error::TrySendError::Full(_)) => self.report_fault(EventFault::Overflow),
|
||||
@@ -194,14 +227,14 @@ impl Drop for Connection {
|
||||
}
|
||||
}
|
||||
|
||||
struct ComReference(NonNull<c_void>);
|
||||
pub(crate) struct ComReference(NonNull<c_void>);
|
||||
|
||||
impl ComReference {
|
||||
fn new(pointer: *mut c_void) -> Result<Self, HRESULT> {
|
||||
pub(crate) fn new(pointer: *mut c_void) -> Result<Self, HRESULT> {
|
||||
NonNull::new(pointer).map(Self).ok_or(E_POINTER)
|
||||
}
|
||||
|
||||
fn as_ptr(&self) -> *mut c_void {
|
||||
pub(crate) fn as_ptr(&self) -> *mut c_void {
|
||||
self.0.as_ptr()
|
||||
}
|
||||
}
|
||||
@@ -536,6 +569,39 @@ mod tests {
|
||||
));
|
||||
}
|
||||
|
||||
#[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);
|
||||
}
|
||||
|
||||
fn integer(value: i32) -> VARIANT {
|
||||
let mut result = VARIANT::default();
|
||||
result.Anonymous.Anonymous.vt = VT_I4;
|
||||
|
||||
@@ -10,14 +10,17 @@ use tokio::sync::{mpsc, watch};
|
||||
use tokio::time::{interval, sleep, timeout, Instant, MissedTickBehavior};
|
||||
|
||||
use crate::automation::int32;
|
||||
use crate::mxaccess::{Command, CommandError, Sta, StartError, CLASS_ID, PROG_ID};
|
||||
use crate::mxaccess::{
|
||||
Command, CommandError, CommandResult, Sta, StartError, WriteAccess, WriteRequest, CLASS_ID,
|
||||
PROG_ID,
|
||||
};
|
||||
use crate::mxevent::{event_channel, EventFault, Publisher};
|
||||
use crate::wire::{
|
||||
self,
|
||||
mxaccess_gateway::v1::{
|
||||
mx_command, mx_command_reply, AddItemReply, DrainEventsReply, MxCommand, MxCommandKind,
|
||||
MxCommandReply, MxEvent, ProtocolStatus, ProtocolStatusCode, RegisterReply, SessionState,
|
||||
SessionStateReply, WorkerInfoReply,
|
||||
mx_command, mx_command_reply, AddItemReply, BulkWriteReply, DrainEventsReply, MxCommand,
|
||||
MxCommandKind, MxCommandReply, MxEvent, ProtocolStatus, ProtocolStatusCode, RegisterReply,
|
||||
SessionState, SessionStateReply, WorkerInfoReply,
|
||||
},
|
||||
mxaccess_worker::v1::{
|
||||
GatewayHello, WorkerCommand, WorkerCommandReply, WorkerEvent, WorkerFault,
|
||||
@@ -70,6 +73,7 @@ struct Options {
|
||||
sta_grace: Duration,
|
||||
command_ceiling: Duration,
|
||||
event_capacity: usize,
|
||||
write_completion_wait: Duration,
|
||||
}
|
||||
|
||||
impl Options {
|
||||
@@ -117,6 +121,13 @@ impl Options {
|
||||
heartbeat_interval: env_duration("MXGATEWAY_WORKER_HEARTBEAT_SEND_INTERVAL_MS", 5000),
|
||||
sta_grace: env_duration("MXGATEWAY_WORKER_HEARTBEAT_GRACE_MS", 15000),
|
||||
command_ceiling: env_duration("MXGATEWAY_WORKER_HEARTBEAT_STUCK_CEILING_MS", 75000),
|
||||
write_completion_wait: Duration::from_millis(
|
||||
std::env::var("MXGATEWAY_WORKER_WRITE_COMPLETION_WAIT_MS")
|
||||
.ok()
|
||||
.and_then(|value| value.parse::<u32>().ok())
|
||||
.unwrap_or(1500)
|
||||
.into(),
|
||||
),
|
||||
event_capacity: std::env::var("MXGATEWAY_EVENT_QUEUE_CAPACITY")
|
||||
.ok()
|
||||
.and_then(|value| value.parse().ok())
|
||||
@@ -355,7 +366,7 @@ async fn command_loop<W: AsyncWrite + Unpin>(
|
||||
let kind = command.as_ref().map_or(0, |command| command.kind);
|
||||
let execution = timeout(
|
||||
options.command_ceiling,
|
||||
execute(sta, command, &options.session, &correlation, events, max),
|
||||
execute(sta, command, options, &correlation, events, max),
|
||||
).await;
|
||||
let mut reply = match execution {
|
||||
Ok(Ok(reply)) => reply,
|
||||
@@ -419,7 +430,7 @@ async fn command_loop<W: AsyncWrite + Unpin>(
|
||||
async fn execute(
|
||||
sta: &Sta,
|
||||
command: Option<MxCommand>,
|
||||
session: &str,
|
||||
options: &Options,
|
||||
correlation: &str,
|
||||
events: &mut EventStream,
|
||||
max: usize,
|
||||
@@ -429,7 +440,7 @@ async fn execute(
|
||||
use MxCommandKind as Kind;
|
||||
|
||||
let mut reply = command_reply(
|
||||
session,
|
||||
&options.session,
|
||||
correlation,
|
||||
command.as_ref().map_or(0, |command| command.kind),
|
||||
);
|
||||
@@ -455,12 +466,128 @@ async fn execute(
|
||||
(Kind::RemoveItem, Some(Payload::RemoveItem(request))) => {
|
||||
Command::RemoveItem(request.server_handle, request.item_handle)
|
||||
}
|
||||
(Kind::Advise, Some(Payload::Advise(request))) => {
|
||||
Command::Advise(request.server_handle, request.item_handle)
|
||||
(Kind::Advise, Some(Payload::Advise(request))) => Command::Advise {
|
||||
server: request.server_handle,
|
||||
item: request.item_handle,
|
||||
supervisory: false,
|
||||
},
|
||||
(Kind::AdviseSupervisory, Some(Payload::AdviseSupervisory(request))) => Command::Advise {
|
||||
server: request.server_handle,
|
||||
item: request.item_handle,
|
||||
supervisory: true,
|
||||
},
|
||||
(Kind::Write, Some(Payload::Write(request))) => Command::Write(Box::new(WriteRequest {
|
||||
server: request.server_handle,
|
||||
item: request.item_handle,
|
||||
value: request.value,
|
||||
timestamp: None,
|
||||
access: WriteAccess::Plain(request.user_id),
|
||||
})),
|
||||
(Kind::WriteSecured, Some(Payload::WriteSecured(request))) => {
|
||||
Command::Write(Box::new(WriteRequest {
|
||||
server: request.server_handle,
|
||||
item: request.item_handle,
|
||||
value: request.value,
|
||||
timestamp: None,
|
||||
access: WriteAccess::Secured {
|
||||
current_user: request.current_user_id,
|
||||
verifier: request.verifier_user_id,
|
||||
},
|
||||
}))
|
||||
}
|
||||
(Kind::Write2, Some(Payload::Write2(request))) if request.timestamp_value.is_some() => {
|
||||
Command::Write(Box::new(WriteRequest {
|
||||
server: request.server_handle,
|
||||
item: request.item_handle,
|
||||
value: request.value,
|
||||
timestamp: request.timestamp_value,
|
||||
access: WriteAccess::Plain(request.user_id),
|
||||
}))
|
||||
}
|
||||
(Kind::WriteSecured2, Some(Payload::WriteSecured2(request)))
|
||||
if request.timestamp_value.is_some() =>
|
||||
{
|
||||
Command::Write(Box::new(WriteRequest {
|
||||
server: request.server_handle,
|
||||
item: request.item_handle,
|
||||
value: request.value,
|
||||
timestamp: request.timestamp_value,
|
||||
access: WriteAccess::Secured {
|
||||
current_user: request.current_user_id,
|
||||
verifier: request.verifier_user_id,
|
||||
},
|
||||
}))
|
||||
}
|
||||
(Kind::Write2, Some(Payload::Write2(_)))
|
||||
| (Kind::WriteSecured2, Some(Payload::WriteSecured2(_))) => {
|
||||
set_status(
|
||||
&mut reply,
|
||||
ProtocolStatusCode::InvalidRequest,
|
||||
"Write command timestamp value is required.",
|
||||
);
|
||||
return Ok(reply);
|
||||
}
|
||||
(Kind::UnAdvise, Some(Payload::UnAdvise(request))) => {
|
||||
Command::UnAdvise(request.server_handle, request.item_handle)
|
||||
}
|
||||
(Kind::WriteBulk, Some(Payload::WriteBulk(request))) => Command::WriteBulk(
|
||||
request
|
||||
.entries
|
||||
.into_iter()
|
||||
.map(|entry| WriteRequest {
|
||||
server: request.server_handle,
|
||||
item: entry.item_handle,
|
||||
value: entry.value,
|
||||
timestamp: None,
|
||||
access: WriteAccess::Plain(entry.user_id),
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
(Kind::Write2Bulk, Some(Payload::Write2Bulk(request))) => Command::WriteBulk(
|
||||
request
|
||||
.entries
|
||||
.into_iter()
|
||||
.map(|entry| WriteRequest {
|
||||
server: request.server_handle,
|
||||
item: entry.item_handle,
|
||||
value: entry.value,
|
||||
timestamp: entry.timestamp_value,
|
||||
access: WriteAccess::Plain(entry.user_id),
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
(Kind::WriteSecuredBulk, Some(Payload::WriteSecuredBulk(request))) => Command::WriteBulk(
|
||||
request
|
||||
.entries
|
||||
.into_iter()
|
||||
.map(|entry| WriteRequest {
|
||||
server: request.server_handle,
|
||||
item: entry.item_handle,
|
||||
value: entry.value,
|
||||
timestamp: None,
|
||||
access: WriteAccess::Secured {
|
||||
current_user: entry.current_user_id,
|
||||
verifier: entry.verifier_user_id,
|
||||
},
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
(Kind::WriteSecured2Bulk, Some(Payload::WriteSecured2Bulk(request))) => Command::WriteBulk(
|
||||
request
|
||||
.entries
|
||||
.into_iter()
|
||||
.map(|entry| WriteRequest {
|
||||
server: request.server_handle,
|
||||
item: entry.item_handle,
|
||||
value: entry.value,
|
||||
timestamp: entry.timestamp_value,
|
||||
access: WriteAccess::Secured {
|
||||
current_user: entry.current_user_id,
|
||||
verifier: entry.verifier_user_id,
|
||||
},
|
||||
})
|
||||
.collect(),
|
||||
),
|
||||
(Kind::Ping, Some(Payload::Ping(request))) => {
|
||||
reply.diagnostic_message = request.message;
|
||||
return Ok(reply);
|
||||
@@ -499,8 +626,18 @@ async fn execute(
|
||||
return Ok(reply);
|
||||
}
|
||||
};
|
||||
if let Command::WriteBulk(requests) = &native {
|
||||
let timestamped = matches!(kind, Kind::Write2Bulk | Kind::WriteSecured2Bulk);
|
||||
if requests
|
||||
.iter()
|
||||
.any(|request| request.value.is_none() || (timestamped && request.timestamp.is_none()))
|
||||
{
|
||||
set_status(&mut reply, ProtocolStatusCode::InvalidRequest, "Bulk write entries require a value and any timestamp specified by their command kind.");
|
||||
return Ok(reply);
|
||||
}
|
||||
}
|
||||
match sta.execute(native).await.map_err(Error::sta)? {
|
||||
Ok(Some(handle)) => {
|
||||
Ok(CommandResult::Handle(handle)) => {
|
||||
reply.return_value = Some(int32(handle));
|
||||
reply.payload = Some(match kind {
|
||||
Kind::Register => Reply::Register(RegisterReply {
|
||||
@@ -512,7 +649,29 @@ async fn execute(
|
||||
_ => unreachable!("only Register and AddItem return a handle"),
|
||||
});
|
||||
}
|
||||
Ok(None) => {}
|
||||
Ok(CommandResult::Done) => {}
|
||||
Ok(CommandResult::Write(completion)) => {
|
||||
if !options.write_completion_wait.is_zero() {
|
||||
// Wait outside the STA so it can pump the COM callback.
|
||||
if let Ok(Ok(statuses)) = timeout(options.write_completion_wait, completion).await {
|
||||
reply.statuses = statuses;
|
||||
}
|
||||
}
|
||||
events.publisher.discard_pending_write();
|
||||
}
|
||||
Ok(CommandResult::WriteBulk(results)) => {
|
||||
let bulk = BulkWriteReply { results };
|
||||
reply.payload = Some(match kind {
|
||||
Kind::WriteBulk => Reply::WriteBulk(bulk),
|
||||
Kind::Write2Bulk => Reply::Write2Bulk(bulk),
|
||||
Kind::WriteSecuredBulk => Reply::WriteSecuredBulk(bulk),
|
||||
Kind::WriteSecured2Bulk => Reply::WriteSecured2Bulk(bulk),
|
||||
_ => unreachable!("bulk write outcome belongs to a bulk write command"),
|
||||
});
|
||||
}
|
||||
Err(CommandError::Invalid(message)) => {
|
||||
set_status(&mut reply, ProtocolStatusCode::InvalidRequest, message);
|
||||
}
|
||||
Err(CommandError::NotRegistered) => {
|
||||
set_status(
|
||||
&mut reply,
|
||||
|
||||
Reference in New Issue
Block a user