AppFlowy/frontend/rust-lib/dart-ffi/src/lib.rs

100 lines
2.9 KiB
Rust
Raw Normal View History

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;
mod model;
mod protobuf;
mod util;
2021-06-28 15:58:43 +00:00
use crate::{
c::{extend_front_four_bytes_into_bytes, forget_rust},
model::{FFIRequest, FFIResponse},
};
2021-06-28 15:58:43 +00:00
use flowy_sdk::*;
2022-01-30 01:10:26 +00:00
use lazy_static::lazy_static;
use lib_dispatch::prelude::*;
2022-01-30 01:10:26 +00:00
use parking_lot::RwLock;
use std::{ffi::CStr, os::raw::c_char, sync::Arc};
2021-07-25 00:13:59 +00:00
2022-01-30 01:10:26 +00:00
lazy_static! {
static ref FLOWY_SDK: RwLock<Option<Arc<FlowySDK>>> = RwLock::new(None);
}
2022-01-30 02:33:21 +00:00
fn get_dispatcher() -> Arc<EventDispatcher> {
2022-01-30 01:10:26 +00:00
FLOWY_SDK.read().as_ref().unwrap().dispatcher()
}
#[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();
2022-01-07 15:00:23 +00:00
let config = FlowySDKConfig::new(path, server_config, "appflowy").log_filter("debug");
2022-01-30 01:10:26 +00:00
*FLOWY_SDK.write() = Some(Arc::new(FlowySDK::new(config)));
2021-11-27 11:19:41 +00:00
0
}
#[no_mangle]
2021-12-04 02:27:08 +00:00
pub extern "C" fn async_event(port: i64, input: *const u8, len: usize) {
let request: ModuleRequest = 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
2022-01-30 02:33:21 +00:00
let _ = EventDispatcher::async_send_with_callback(get_dispatcher(), request, move |resp: EventResponse| {
2021-07-03 06:14:10 +00:00
log::trace!("[FFI]: Post data to dart through {} port", port);
Box::pin(post_to_flutter(resp, port))
});
}
#[no_mangle]
2021-12-04 02:27:08 +00:00
pub extern "C" fn sync_event(input: *const u8, len: usize) -> *const u8 {
let request: ModuleRequest = FFIRequest::from_u8_pointer(input, len).into();
log::trace!("[FFI]: {} Sync Event: {:?}", &request.id, &request.event,);
2022-01-30 02:33:21 +00:00
let _response = EventDispatcher::sync_send(get_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-07-21 07:43:05 +00:00
#[no_mangle]
pub extern "C" fn set_stream_port(port: i64) -> i32 {
2021-11-19 07:00:51 +00:00
dart_notify::dart::DartStreamSender::set_port(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
2021-12-05 08:39:41 +00:00
use backend_service::configuration::get_client_server_configuration;
use lib_dispatch::prelude::ToBytes;
2021-07-02 12:47:52 +00:00
#[inline(always)]
async fn post_to_flutter(response: EventResponse, port: i64) {
2021-07-02 12:47:52 +00:00
let isolate = allo_isolate::Isolate::new(port);
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()
})
.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
}