lib.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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 server_config = ServerConfig::default();
  23. let config = FlowySDKConfig::new(path, server_config).log_filter("debug");
  24. *FLOWY_SDK.write() = Some(Arc::new(FlowySDK::new(config)));
  25. return 1;
  26. }
  27. #[no_mangle]
  28. pub extern "C" fn async_command(port: i64, input: *const u8, len: usize) {
  29. let request: ModuleRequest = FFIRequest::from_u8_pointer(input, len).into();
  30. log::trace!(
  31. "[FFI]: {} Async Event: {:?} with {} port",
  32. &request.id,
  33. &request.event,
  34. port
  35. );
  36. let _ = EventDispatch::async_send_with_callback(dispatch(), request, move |resp: EventResponse| {
  37. log::trace!("[FFI]: Post data to dart through {} port", port);
  38. Box::pin(post_to_flutter(resp, port))
  39. });
  40. }
  41. #[no_mangle]
  42. pub extern "C" fn sync_command(input: *const u8, len: usize) -> *const u8 {
  43. let request: ModuleRequest = FFIRequest::from_u8_pointer(input, len).into();
  44. log::trace!("[FFI]: {} Sync Event: {:?}", &request.id, &request.event,);
  45. let _response = EventDispatch::sync_send(dispatch(), request);
  46. // FFIResponse { }
  47. let response_bytes = vec![];
  48. let result = extend_front_four_bytes_into_bytes(&response_bytes);
  49. forget_rust(result)
  50. }
  51. #[no_mangle]
  52. pub extern "C" fn set_stream_port(port: i64) -> i32 {
  53. flowy_dart_notify::dart::DartStreamSender::set_port(port);
  54. return 0;
  55. }
  56. #[inline(never)]
  57. #[no_mangle]
  58. pub extern "C" fn link_me_please() {}
  59. use flowy_dispatch::prelude::ToBytes;
  60. use flowy_net::config::ServerConfig;
  61. #[inline(always)]
  62. async fn post_to_flutter(response: EventResponse, port: i64) {
  63. let isolate = allo_isolate::Isolate::new(port);
  64. match isolate
  65. .catch_unwind(async {
  66. let ffi_resp = FFIResponse::from(response);
  67. ffi_resp.into_bytes().unwrap().to_vec()
  68. })
  69. .await
  70. {
  71. Ok(_success) => {
  72. log::trace!("[FFI]: Post data to dart success");
  73. },
  74. Err(e) => {
  75. if let Some(msg) = e.downcast_ref::<&str>() {
  76. log::error!("[FFI]: {:?}", msg);
  77. } else {
  78. log::error!("[FFI]: allo_isolate post panic");
  79. }
  80. },
  81. }
  82. }