mssf_core/api/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
//! Dynamically load SF libs and c functions.
//! SF shared lib provides these functions, and we dynamically load them here so that user of this crate
//! does not need to worry about installing SF lib and linking, which can be complex.
//!

use mssf_com::{
    FabricClient::{IFabricClientConnectionEventHandler, IFabricServiceNotificationEventHandler},
    FabricCommon::{
        IFabricAsyncOperationCallback, IFabricAsyncOperationContext, IFabricStringResult,
    },
    FabricRuntime::IFabricStoreEventHandler,
    FabricTypes::{FABRIC_CLIENT_ROLE, FABRIC_LOCAL_STORE_KIND, FABRIC_REPLICATOR_SETTINGS},
};
use windows_core::{Interface, Param};

lazy_static::lazy_static! {
    static ref LIB_TABLE: LibTable = LibTable::create();
    /// All SF APIs entrypoints needed for mssf.
    /// These APIs are lazy loaded at the first time use after app starts.
    pub static ref API_TABLE: ApiTable = ApiTable::create(&LIB_TABLE);
}

/// Contains all the SF shared libs needs to be loaded for mssf.
pub struct LibTable {
    fabric_runtime: libloading::Library,
    fabric_common: libloading::Library,
    fabric_client: libloading::Library,
}

impl LibTable {
    fn create() -> Self {
        Self {
            fabric_runtime: load_lib("FabricRuntime"),
            fabric_common: load_lib("FabricCommon"),
            fabric_client: load_lib("FabricClient"),
        }
    }
}

fn load_lib(name: &str) -> libloading::Library {
    unsafe { libloading::Library::new(libloading::library_filename(name)) }
        .unwrap_or_else(|e| panic!("cannot load lib {name} :{e}"))
}

fn load_fn<T>(lib: &'static libloading::Library, name: &str) -> libloading::Symbol<'static, T> {
    unsafe { lib.get(name.as_bytes()) }.unwrap_or_else(|e| panic!("cannot load fn {name} :{e}"))
}

/// Contains all SF APIs loaded from SF libs needed for mssf.
/// More APIs can be added here when mssf needs them.
pub struct ApiTable {
    fabric_get_last_error_message_fn: libloading::Symbol<
        'static,
        unsafe extern "system" fn(message: *mut *mut core::ffi::c_void) -> crate::HRESULT,
    >,
    fabric_create_client3_fn: libloading::Symbol<
        'static,
        unsafe extern "system" fn(
            connectionstringssize: u16,
            connectionstrings: *const windows_core::PCWSTR,
            __midl__fabricclientmodule0002: *mut core::ffi::c_void,
            __midl__fabricclientmodule0003: *mut core::ffi::c_void,
            iid: *const windows_core::GUID,
            fabricclient: *mut *mut core::ffi::c_void,
        ) -> crate::HRESULT,
    >,
    fabric_create_local_client3_fn: libloading::Symbol<
        'static,
        unsafe extern "system" fn(
            __midl__fabricclientmodule0004: *mut core::ffi::c_void,
            __midl__fabricclientmodule0005: *mut core::ffi::c_void,
            iid: *const windows_core::GUID,
            fabricclient: *mut *mut core::ffi::c_void,
        ) -> crate::HRESULT,
    >,

    fabric_create_local_client4_fn: libloading::Symbol<
        'static,
        unsafe extern "system" fn(
            __midl__fabricclientmodule0006: *mut core::ffi::c_void,
            __midl__fabricclientmodule0007: *mut core::ffi::c_void,
            clientrole: FABRIC_CLIENT_ROLE,
            iid: *const windows_core::GUID,
            fabricclient: *mut *mut core::ffi::c_void,
        ) -> crate::HRESULT,
    >,

    fabric_create_runtime_fn: libloading::Symbol<
        'static,
        unsafe extern "system" fn(
            riid: *const windows_core::GUID,
            fabricruntime: *mut *mut core::ffi::c_void,
        ) -> crate::HRESULT,
    >,

    fabric_get_activation_context_fn: libloading::Symbol<
        'static,
        unsafe extern "system" fn(
            riid: *const windows_core::GUID,
            activationcontext: *mut *mut core::ffi::c_void,
        ) -> crate::HRESULT,
    >,

    fabric_begin_get_node_context_fn: libloading::Symbol<
        'static,
        unsafe extern "system" fn(
            timeoutmilliseconds: u32,
            callback: *mut core::ffi::c_void,
            context: *mut *mut core::ffi::c_void,
        ) -> crate::HRESULT,
    >,

    fabric_end_get_node_context_fn: libloading::Symbol<
        'static,
        unsafe extern "system" fn(
            context: *mut core::ffi::c_void,
            nodecontext: *mut *mut core::ffi::c_void,
        ) -> crate::HRESULT,
    >,
    fabric_get_node_context_fn: libloading::Symbol<
        'static,
        unsafe extern "system" fn(nodecontext: *mut *mut core::ffi::c_void) -> crate::HRESULT,
    >,
    fabric_create_key_value_store_replica_fn: libloading::Symbol<
        'static,
        unsafe extern "system" fn(
            riid: *const windows_core::GUID,
            storename: windows_core::PCWSTR,
            partitionid: windows_core::GUID,
            replicaid: i64,
            replicatorsettings: *const FABRIC_REPLICATOR_SETTINGS,
            localstorekind: FABRIC_LOCAL_STORE_KIND,
            localstoresettings: *const core::ffi::c_void,
            storeeventhandler: *mut core::ffi::c_void,
            keyvaluestore: *mut *mut core::ffi::c_void,
        ) -> crate::HRESULT,
    >,
}

impl ApiTable {
    fn create(lib_table: &'static LibTable) -> Self {
        Self {
            fabric_get_last_error_message_fn: load_fn(
                &lib_table.fabric_common,
                "FabricGetLastErrorMessage",
            ),
            fabric_create_client3_fn: load_fn(&lib_table.fabric_client, "FabricCreateClient3"),
            fabric_create_local_client3_fn: load_fn(
                &lib_table.fabric_client,
                "FabricCreateLocalClient3",
            ),
            fabric_create_local_client4_fn: load_fn(
                &lib_table.fabric_client,
                "FabricCreateLocalClient4",
            ),
            fabric_create_runtime_fn: load_fn(&lib_table.fabric_runtime, "FabricCreateRuntime"),
            fabric_get_activation_context_fn: load_fn(
                &lib_table.fabric_runtime,
                "FabricGetActivationContext",
            ),
            fabric_begin_get_node_context_fn: load_fn(
                &lib_table.fabric_runtime,
                "FabricBeginGetNodeContext",
            ),
            fabric_end_get_node_context_fn: load_fn(
                &lib_table.fabric_runtime,
                "FabricEndGetNodeContext",
            ),
            fabric_get_node_context_fn: load_fn(&lib_table.fabric_runtime, "FabricGetNodeContext"),
            fabric_create_key_value_store_replica_fn: load_fn(
                &lib_table.fabric_runtime,
                "FabricCreateKeyValueStoreReplica",
            ),
        }
    }

    pub fn fabric_get_last_error_message(&self) -> crate::Result<IFabricStringResult> {
        let mut result = std::ptr::null_mut::<core::ffi::c_void>();
        unsafe { (self.fabric_get_last_error_message_fn)(std::ptr::addr_of_mut!(result)) }.ok()?;
        assert!(!result.is_null());
        Ok(unsafe { IFabricStringResult::from_raw(result) })
    }

    pub fn fabric_create_client3<T: Interface>(
        &self,
        connectionstrings: &[windows_core::PCWSTR],
        service_notification_handler: Option<&IFabricServiceNotificationEventHandler>,
        client_connection_handler: Option<&IFabricClientConnectionEventHandler>,
    ) -> crate::Result<T> {
        let mut result = std::ptr::null_mut::<core::ffi::c_void>();
        unsafe {
            (self.fabric_create_client3_fn)(
                connectionstrings.len().try_into().unwrap(),
                connectionstrings.as_ptr(),
                service_notification_handler.param().abi(),
                client_connection_handler.param().abi(),
                &T::IID,
                std::ptr::addr_of_mut!(result),
            )
        }
        .ok()?;
        Ok(unsafe { T::from_raw(result) })
    }

    pub fn fabric_create_local_client3<T: Interface>(
        &self,
        service_notification_handler: Option<&IFabricServiceNotificationEventHandler>,
        client_connection_handler: Option<&IFabricClientConnectionEventHandler>,
    ) -> crate::Result<T> {
        let mut result = std::ptr::null_mut::<core::ffi::c_void>();
        unsafe {
            (self.fabric_create_local_client3_fn)(
                service_notification_handler.param().abi(),
                client_connection_handler.param().abi(),
                &T::IID,
                std::ptr::addr_of_mut!(result),
            )
        }
        .ok()?;
        Ok(unsafe { T::from_raw(result) })
    }

    pub fn fabric_create_local_client4<T: Interface>(
        &self,
        service_notification_handler: Option<&IFabricServiceNotificationEventHandler>,
        client_connection_handler: Option<&IFabricClientConnectionEventHandler>,
        clientrole: FABRIC_CLIENT_ROLE,
    ) -> crate::Result<T> {
        let mut result = std::ptr::null_mut::<core::ffi::c_void>();
        unsafe {
            (self.fabric_create_local_client4_fn)(
                service_notification_handler.param().abi(),
                client_connection_handler.param().abi(),
                clientrole,
                &T::IID,
                std::ptr::addr_of_mut!(result),
            )
        }
        .ok()?;
        Ok(unsafe { T::from_raw(result) })
    }

    pub fn fabric_create_runtime<T: Interface>(&self) -> crate::Result<T> {
        let mut result = std::ptr::null_mut::<core::ffi::c_void>();
        unsafe { (self.fabric_create_runtime_fn)(&T::IID, std::ptr::addr_of_mut!(result)) }.ok()?;
        Ok(unsafe { T::from_raw(result) })
    }

    pub fn fabric_get_activation_context<T: Interface>(&self) -> crate::Result<T> {
        let mut result = std::ptr::null_mut::<core::ffi::c_void>();
        unsafe { (self.fabric_get_activation_context_fn)(&T::IID, std::ptr::addr_of_mut!(result)) }
            .ok()?;
        Ok(unsafe { T::from_raw(result) })
    }

    pub fn fabric_begin_get_node_context(
        &self,
        timeoutmilliseconds: u32,
        callback: Option<&IFabricAsyncOperationCallback>,
    ) -> crate::Result<IFabricAsyncOperationContext> {
        let mut result = std::ptr::null_mut::<core::ffi::c_void>();
        unsafe {
            (self.fabric_begin_get_node_context_fn)(
                timeoutmilliseconds,
                callback.param().abi(),
                std::ptr::addr_of_mut!(result),
            )
        }
        .ok()?;
        Ok(unsafe { IFabricAsyncOperationContext::from_raw(result) })
    }

    pub fn fabric_end_get_node_context<T: Interface>(
        &self,
        context: Option<&IFabricAsyncOperationContext>,
    ) -> crate::Result<T> {
        let mut result = std::ptr::null_mut::<core::ffi::c_void>();
        unsafe {
            (self.fabric_end_get_node_context_fn)(
                context.param().abi(),
                std::ptr::addr_of_mut!(result),
            )
        }
        .ok()?;
        Ok(unsafe { T::from_raw(result) })
    }

    pub fn fabric_get_node_context<T: Interface>(&self) -> crate::Result<T> {
        let mut result = std::ptr::null_mut::<core::ffi::c_void>();
        unsafe { (self.fabric_get_node_context_fn)(std::ptr::addr_of_mut!(result)) }.ok()?;
        Ok(unsafe { T::from_raw(result) })
    }

    #[allow(clippy::too_many_arguments)]
    pub fn fabric_create_key_value_store_replica<T: Interface>(
        &self,
        storename: windows_core::PCWSTR,
        partitionid: windows_core::GUID,
        replicaid: i64,
        replicatorsettings: *const FABRIC_REPLICATOR_SETTINGS,
        localstorekind: FABRIC_LOCAL_STORE_KIND,
        localstoresettings: *const core::ffi::c_void,
        storeeventhandler: Option<&IFabricStoreEventHandler>,
    ) -> crate::Result<T> {
        let mut result = std::ptr::null_mut::<core::ffi::c_void>();
        unsafe {
            (self.fabric_create_key_value_store_replica_fn)(
                &T::IID,
                storename,
                partitionid,
                replicaid,
                replicatorsettings,
                localstorekind,
                localstoresettings,
                storeeventhandler.param().abi(),
                std::ptr::addr_of_mut!(result),
            )
        }
        .ok()?;
        Ok(unsafe { T::from_raw(result) })
    }
}