2021-06-28 15:58:43 +00:00
|
|
|
mod c;
|
2021-07-05 15:17:12 +00:00
|
|
|
mod model;
|
|
|
|
mod protobuf;
|
2021-07-07 14:24:26 +00:00
|
|
|
mod util;
|
2021-06-28 15:58:43 +00:00
|
|
|
|
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
|
|
|
};
|
2021-07-08 13:23:44 +00:00
|
|
|
use flowy_dispatch::prelude::*;
|
2021-06-28 15:58:43 +00:00
|
|
|
use flowy_sdk::*;
|
2021-07-03 06:14:10 +00:00
|
|
|
use lazy_static::lazy_static;
|
2021-07-08 05:47:11 +00:00
|
|
|
use std::{ffi::CStr, os::raw::c_char};
|
2021-06-28 14:56:15 +00:00
|
|
|
|
2021-07-03 06:14:10 +00:00
|
|
|
lazy_static! {
|
|
|
|
pub static ref FFI_RUNTIME: tokio::runtime::Runtime =
|
|
|
|
tokio::runtime::Builder::new_current_thread()
|
|
|
|
.thread_name("flowy-dart-ffi")
|
|
|
|
.build()
|
|
|
|
.unwrap();
|
|
|
|
}
|
|
|
|
|
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-07-03 06:14:10 +00:00
|
|
|
FlowySDK::init_log(path);
|
2021-07-09 06:02:42 +00:00
|
|
|
|
|
|
|
log::info!("🔥 FlowySDK start running");
|
2021-07-02 12:47:52 +00:00
|
|
|
FlowySDK::init(path);
|
2021-06-28 14:56:15 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
#[no_mangle]
|
|
|
|
pub extern "C" fn async_command(port: i64, input: *const u8, len: usize) {
|
2021-07-08 05:47:11 +00:00
|
|
|
let request: ModuleRequest = FFIRequest::from_u8_pointer(input, len).into();
|
2021-07-03 06:14:10 +00:00
|
|
|
log::trace!(
|
|
|
|
"[FFI]: {} Async Event: {:?} with {} port",
|
2021-07-08 05:47:11 +00:00
|
|
|
&request.id(),
|
|
|
|
&request.event(),
|
2021-07-03 06:14:10 +00:00
|
|
|
port
|
|
|
|
);
|
2021-06-28 14:56:15 +00:00
|
|
|
|
2021-07-07 14:24:26 +00:00
|
|
|
let _ = EventDispatch::async_send(request, move |resp: EventResponse| {
|
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-07-03 14:24:02 +00:00
|
|
|
pub extern "C" fn sync_command(input: *const u8, len: usize) -> *const u8 {
|
2021-07-08 05:47:11 +00:00
|
|
|
let request: ModuleRequest = FFIRequest::from_u8_pointer(input, len).into();
|
|
|
|
log::trace!(
|
|
|
|
"[FFI]: {} Sync Event: {:?}",
|
|
|
|
&request.id(),
|
|
|
|
&request.event(),
|
|
|
|
);
|
2021-07-05 13:23:13 +00:00
|
|
|
let _response = EventDispatch::sync_send(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-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-07-08 13:23:44 +00:00
|
|
|
use flowy_dispatch::prelude::ToBytes;
|
2021-07-02 12:47:52 +00:00
|
|
|
#[inline(always)]
|
2021-07-07 14:24:26 +00:00
|
|
|
async fn post_to_flutter(response: EventResponse, 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);
|
|
|
|
ffi_resp.into_bytes().unwrap()
|
|
|
|
})
|
|
|
|
.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");
|
|
|
|
},
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
2021-07-02 12:47:52 +00:00
|
|
|
}
|