lib.rs 2.6 KB

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