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;
|
|
|
|
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-06-28 15:58:43 +00:00
|
|
|
use flowy_sdk::*;
|
2022-01-30 01:10:26 +00:00
|
|
|
use lazy_static::lazy_static;
|
2021-11-19 06:38:11 +00:00
|
|
|
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()
|
|
|
|
}
|
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();
|
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-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) {
|
2021-07-08 05:47:11 +00:00
|
|
|
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);
|
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 {
|
2021-07-08 05:47:11 +00:00
|
|
|
let request: ModuleRequest = 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 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-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 {
|
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;
|
2021-11-19 06:38:11 +00:00
|
|
|
use lib_dispatch::prelude::ToBytes;
|
2021-09-28 07:29:29 +00:00
|
|
|
|
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);
|
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
|
|
|
}
|