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
use_prelude!();
use std::sync::{Arc, Mutex, Weak};
use ffi_sdk::OnConnectingResponseHandle;
pub use serde_cbor::Value as InfoValue;
#[derive(Clone, Debug, Deserialize, Serialize)]
pub struct ConnectingPeer {
pub peer_info: Option<HashMap<String, InfoValue>>,
pub identity_service_info: Option<HashMap<String, InfoValue>>,
}
pub struct OnConnecting {
ditto: Arc<ffi_sdk::BoxedDitto>,
on_connecting_callback:
Mutex<Option<Box<dyn Fn(ConnectingPeer, Box<dyn FnOnce(bool)>) + Send + Sync + 'static>>>,
}
impl OnConnecting {
pub(crate) fn new(ditto: Arc<ffi_sdk::BoxedDitto>) -> Self {
Self {
ditto,
on_connecting_callback: Mutex::new(None),
}
}
#[allow(unused)]
pub(crate) unsafe extern "C" fn on_connecting_wrapper(
ctx: *mut c_void,
cbor: c_slice::Ref<'_, u8>,
handle: repr_c::Box<OnConnectingResponseHandle>,
) {
let weak_ctx: &Weak<_> =
&*::core::mem::ManuallyDrop::new(Weak::from_raw(ctx.cast::<Self>()));
if let Some(strong_ctx) = weak_ctx.upgrade() {
if let Some(callback) = strong_ctx.on_connecting_callback.lock().unwrap().as_ref() {
match serde_cbor::from_slice(cbor.as_slice()) {
Ok(connecting_peer) => {
return callback(
connecting_peer,
Box::new(move |decision| {
ffi_sdk::ditto_handle_on_connecting_response(handle, decision)
}),
);
}
Err(e) => {
::log::error!("invalid CBOR passed into ConnectFilter callback! {:?}", e);
}
}
}
}
}
pub(crate) fn set_on_connecting(
self: &Arc<Self>,
callback: impl Fn(ConnectingPeer, Box<dyn FnOnce(bool)>) + Send + Sync + 'static,
) {
*self.on_connecting_callback.lock().unwrap() = Some(Box::new(callback));
unsafe {
ffi_sdk::ditto_register_on_connecting(
&self.ditto,
Arc::downgrade(self).into_raw() as *mut _,
Some(Self::retain),
Some(Self::release),
Some(ffi_sdk::OnConnectingCallback(<unsafe extern "C" fn(
_,
c_slice::Ref<'_, u8>,
repr_c::Box<OnConnectingResponseHandle>,
)>::into(
Self::on_connecting_wrapper
))),
)
}
}
pub(crate) extern "C" fn retain(ctx: *mut c_void) {
let ptr = ctx.cast::<Self>();
unsafe { Arc::increment_strong_count(ptr) }; }
pub(crate) extern "C" fn release(ctx: *mut c_void) {
let ptr = ctx.cast::<Self>();
unsafe { Arc::decrement_strong_count(ptr) }; }
}