lib.rs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. #![allow(clippy::not_unsafe_ptr_arg_deref)]
  2. mod c;
  3. mod model;
  4. mod protobuf;
  5. mod util;
  6. use crate::{
  7. c::{extend_front_four_bytes_into_bytes, forget_rust},
  8. model::{FFIRequest, FFIResponse},
  9. };
  10. use flowy_sdk::*;
  11. use lazy_static::lazy_static;
  12. use lib_dispatch::prelude::*;
  13. use parking_lot::RwLock;
  14. use std::{ffi::CStr, os::raw::c_char, sync::Arc};
  15. lazy_static! {
  16. static ref FLOWY_SDK: RwLock<Option<Arc<FlowySDK>>> = RwLock::new(None);
  17. }
  18. fn dispatch() -> Arc<EventDispatch> { FLOWY_SDK.read().as_ref().unwrap().dispatch() }
  19. #[no_mangle]
  20. pub extern "C" fn init_sdk(path: *mut c_char) -> i64 {
  21. let c_str: &CStr = unsafe { CStr::from_ptr(path) };
  22. let path: &str = c_str.to_str().unwrap();
  23. let server_config = ServerConfig::default();
  24. let config = FlowySDKConfig::new(path, server_config, "appflowy").log_filter("debug");
  25. *FLOWY_SDK.write() = Some(Arc::new(FlowySDK::new(config)));
  26. 0
  27. }
  28. #[no_mangle]
  29. pub extern "C" fn async_command(port: i64, input: *const u8, len: usize) {
  30. let request: ModuleRequest = FFIRequest::from_u8_pointer(input, len).into();
  31. log::trace!(
  32. "[FFI]: {} Async Event: {:?} with {} port",
  33. &request.id,
  34. &request.event,
  35. port
  36. );
  37. let _ = EventDispatch::async_send_with_callback(dispatch(), request, move |resp: EventResponse| {
  38. log::trace!("[FFI]: Post data to dart through {} port", port);
  39. Box::pin(post_to_flutter(resp, port))
  40. });
  41. }
  42. #[no_mangle]
  43. pub extern "C" fn sync_command(input: *const u8, len: usize) -> *const u8 {
  44. let request: ModuleRequest = FFIRequest::from_u8_pointer(input, len).into();
  45. log::trace!("[FFI]: {} Sync Event: {:?}", &request.id, &request.event,);
  46. let _response = EventDispatch::sync_send(dispatch(), request);
  47. // FFIResponse { }
  48. let response_bytes = vec![];
  49. let result = extend_front_four_bytes_into_bytes(&response_bytes);
  50. forget_rust(result)
  51. }
  52. #[no_mangle]
  53. pub extern "C" fn set_stream_port(port: i64) -> i32 {
  54. dart_notify::dart::DartStreamSender::set_port(port);
  55. 0
  56. }
  57. #[inline(never)]
  58. #[no_mangle]
  59. pub extern "C" fn link_me_please() {}
  60. use backend_service::config::ServerConfig;
  61. use lib_dispatch::prelude::ToBytes;
  62. #[inline(always)]
  63. async fn post_to_flutter(response: EventResponse, port: i64) {
  64. let isolate = allo_isolate::Isolate::new(port);
  65. match isolate
  66. .catch_unwind(async {
  67. let ffi_resp = FFIResponse::from(response);
  68. ffi_resp.into_bytes().unwrap().to_vec()
  69. })
  70. .await
  71. {
  72. Ok(_success) => {
  73. log::trace!("[FFI]: Post data to dart success");
  74. },
  75. Err(e) => {
  76. if let Some(msg) = e.downcast_ref::<&str>() {
  77. log::error!("[FFI]: {:?}", msg);
  78. } else {
  79. log::error!("[FFI]: allo_isolate post panic");
  80. }
  81. },
  82. }
  83. }