2021-11-27 11:19:41 +00:00
|
|
|
#![allow(clippy::not_unsafe_ptr_arg_deref)]
|
2021-06-28 15:58:43 +00:00
|
|
|
mod c;
|
2021-07-05 15:17:12 +00:00
|
|
|
mod model;
|
2023-01-26 07:40:23 +00:00
|
|
|
mod notification;
|
2021-07-05 15:17:12 +00:00
|
|
|
mod protobuf;
|
2021-07-07 14:24:26 +00:00
|
|
|
mod util;
|
2021-06-28 15:58:43 +00:00
|
|
|
|
2023-01-26 07:40:23 +00:00
|
|
|
use crate::notification::DartNotificationSender;
|
2021-07-05 15:17:12 +00:00
|
|
|
use crate::{
|
|
|
|
c::{extend_front_four_bytes_into_bytes, forget_rust},
|
2021-07-07 14:24:26 +00:00
|
|
|
model::{FFIRequest, FFIResponse},
|
2021-07-05 15:17:12 +00:00
|
|
|
};
|
2023-01-08 04:10:53 +00:00
|
|
|
use flowy_core::get_client_server_configuration;
|
|
|
|
use flowy_core::*;
|
2023-01-26 07:40:23 +00:00
|
|
|
use flowy_notification::register_notification_sender;
|
2022-12-20 03:14:42 +00:00
|
|
|
use lazy_static::lazy_static;
|
2022-02-07 02:37:01 +00:00
|
|
|
use lib_dispatch::prelude::ToBytes;
|
2021-11-19 06:38:11 +00:00
|
|
|
use lib_dispatch::prelude::*;
|
2022-12-20 03:14:42 +00:00
|
|
|
use parking_lot::RwLock;
|
2022-01-30 07:17:30 +00:00
|
|
|
use std::{ffi::CStr, os::raw::c_char};
|
2021-07-25 00:13:59 +00:00
|
|
|
|
2022-12-20 03:14:42 +00:00
|
|
|
lazy_static! {
|
2023-02-02 15:02:49 +00:00
|
|
|
static ref APPFLOWY_CORE: RwLock<Option<AppFlowyCore>> = RwLock::new(None);
|
2022-12-20 03:14:42 +00:00
|
|
|
}
|
2021-06-28 14:56:15 +00:00
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn init_sdk(path: *mut c_char) -> i64 {
|
|
|
|
let c_str: &CStr = unsafe { CStr::from_ptr(path) };
|
|
|
|
let path: &str = c_str.to_str().unwrap();
|
2021-09-05 05:50:23 +00:00
|
|
|
|
2021-12-05 08:39:41 +00:00
|
|
|
let server_config = get_client_server_configuration().unwrap();
|
2023-01-17 08:27:17 +00:00
|
|
|
let log_crates = vec!["flowy-ffi".to_string()];
|
2023-02-02 15:02:49 +00:00
|
|
|
let config = AppFlowyCoreConfig::new(path, "appflowy".to_string(), server_config).log_filter("info", log_crates);
|
|
|
|
*APPFLOWY_CORE.write() = Some(AppFlowyCore::new(config));
|
2021-07-14 13:12:52 +00:00
|
|
|
|
2021-11-27 11:19:41 +00:00
|
|
|
0
|
2021-06-28 14:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
2021-12-04 02:27:08 +00:00
|
|
|
pub extern "C" fn async_event(port: i64, input: *const u8, len: usize) {
|
2022-12-01 00:35:50 +00:00
|
|
|
let request: AFPluginRequest = FFIRequest::from_u8_pointer(input, len).into();
|
2021-09-27 15:23:23 +00:00
|
|
|
log::trace!(
|
|
|
|
"[FFI]: {} Async Event: {:?} with {} port",
|
|
|
|
&request.id,
|
|
|
|
&request.event,
|
|
|
|
port
|
|
|
|
);
|
2021-09-04 07:12:53 +00:00
|
|
|
|
2023-02-02 15:02:49 +00:00
|
|
|
let dispatcher = match APPFLOWY_CORE.read().as_ref() {
|
2022-01-30 07:17:30 +00:00
|
|
|
None => {
|
|
|
|
log::error!("sdk not init yet.");
|
|
|
|
return;
|
|
|
|
}
|
2022-11-13 14:23:57 +00:00
|
|
|
Some(e) => e.event_dispatcher.clone(),
|
2022-01-30 07:17:30 +00:00
|
|
|
};
|
2022-12-01 02:59:22 +00:00
|
|
|
let _ = AFPluginDispatcher::async_send_with_callback(dispatcher, request, move |resp: AFPluginEventResponse| {
|
2021-07-03 06:14:10 +00:00
|
|
|
log::trace!("[FFI]: Post data to dart through {} port", port);
|
2021-07-07 14:24:26 +00:00
|
|
|
Box::pin(post_to_flutter(resp, port))
|
|
|
|
});
|
2021-06-28 14:56:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
2021-12-04 02:27:08 +00:00
|
|
|
pub extern "C" fn sync_event(input: *const u8, len: usize) -> *const u8 {
|
2022-12-01 00:35:50 +00:00
|
|
|
let request: AFPluginRequest = FFIRequest::from_u8_pointer(input, len).into();
|
2021-07-10 08:27:20 +00:00
|
|
|
log::trace!("[FFI]: {} Sync Event: {:?}", &request.id, &request.event,);
|
2022-01-30 07:17:30 +00:00
|
|
|
|
2023-02-02 15:02:49 +00:00
|
|
|
let dispatcher = match APPFLOWY_CORE.read().as_ref() {
|
2022-01-30 07:17:30 +00:00
|
|
|
None => {
|
|
|
|
log::error!("sdk not init yet.");
|
|
|
|
return forget_rust(Vec::default());
|
|
|
|
}
|
2022-11-13 14:23:57 +00:00
|
|
|
Some(e) => e.event_dispatcher.clone(),
|
2022-01-30 07:17:30 +00:00
|
|
|
};
|
2022-12-01 00:35:50 +00:00
|
|
|
let _response = AFPluginDispatcher::sync_send(dispatcher, request);
|
2021-07-03 14:24:02 +00:00
|
|
|
|
|
|
|
// FFIResponse { }
|
|
|
|
let response_bytes = vec![];
|
|
|
|
let result = extend_front_four_bytes_into_bytes(&response_bytes);
|
|
|
|
forget_rust(result)
|
|
|
|
}
|
2021-06-28 14:56:15 +00:00
|
|
|
|
2021-07-21 07:43:05 +00:00
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn set_stream_port(port: i64) -> i32 {
|
2023-01-26 07:40:23 +00:00
|
|
|
register_notification_sender(DartNotificationSender::new(port));
|
2021-11-27 11:19:41 +00:00
|
|
|
0
|
2021-07-21 07:43:05 +00:00
|
|
|
}
|
|
|
|
|
2021-06-28 15:58:43 +00:00
|
|
|
#[inline(never)]
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn link_me_please() {}
|
2021-07-02 12:47:52 +00:00
|
|
|
|
|
|
|
#[inline(always)]
|
2022-12-01 02:59:22 +00:00
|
|
|
async fn post_to_flutter(response: AFPluginEventResponse, port: i64) {
|
2021-07-02 12:47:52 +00:00
|
|
|
let isolate = allo_isolate::Isolate::new(port);
|
2021-07-07 14:24:26 +00:00
|
|
|
match isolate
|
|
|
|
.catch_unwind(async {
|
|
|
|
let ffi_resp = FFIResponse::from(response);
|
2021-08-20 14:00:03 +00:00
|
|
|
ffi_resp.into_bytes().unwrap().to_vec()
|
2021-07-07 14:24:26 +00:00
|
|
|
})
|
|
|
|
.await
|
|
|
|
{
|
2021-07-05 13:23:13 +00:00
|
|
|
Ok(_success) => {
|
2021-07-03 06:14:10 +00:00
|
|
|
log::trace!("[FFI]: Post data to dart success");
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-07-03 06:14:10 +00:00
|
|
|
Err(e) => {
|
|
|
|
if let Some(msg) = e.downcast_ref::<&str>() {
|
2021-07-03 13:40:13 +00:00
|
|
|
log::error!("[FFI]: {:?}", msg);
|
2021-07-03 06:14:10 +00:00
|
|
|
} else {
|
|
|
|
log::error!("[FFI]: allo_isolate post panic");
|
|
|
|
}
|
2022-01-23 04:14:00 +00:00
|
|
|
}
|
2021-07-03 06:14:10 +00:00
|
|
|
}
|
2021-07-02 12:47:52 +00:00
|
|
|
}
|