This commit is contained in:
John Smith
2022-02-15 13:40:17 -05:00
parent 125901fcd8
commit 7458d0d991
12 changed files with 191 additions and 103 deletions

View File

@@ -119,7 +119,14 @@ pub extern "C" fn startup_veilid_core(port: i64, config: FfiStr) {
move |update: veilid_core::VeilidUpdate| -> veilid_core::SystemPinBoxFuture<()> {
let sink = sink.clone();
Box::pin(async move {
sink.item_json(update);
match update {
veilid_core::VeilidUpdate::Shutdown => {
sink.close();
}
_ => {
sink.item_json(update);
}
}
})
},
);
@@ -159,6 +166,16 @@ pub extern "C" fn shutdown_veilid_core(port: i64) {
});
}
#[no_mangle]
pub extern "C" fn debug(port: i64, command: FfiStr) {
let command = command.into_opt_string().unwrap_or_default();
DartIsolateWrapper::new(port).spawn_result(async move {
let veilid_api = get_veilid_api().await?;
let out = veilid_api.debug(command).await?;
APIResult::Ok(out)
});
}
#[no_mangle]
pub extern "C" fn veilid_version_string() -> *mut c_char {
veilid_core::veilid_version_string().into_ffi_value()

View File

@@ -13,12 +13,12 @@ pub struct DartIsolateWrapper {
}
const MESSAGE_OK: i32 = 0;
const MESSAGE_ERR: i32 = 1;
//const MESSAGE_ERR: i32 = 1;
const MESSAGE_OK_JSON: i32 = 2;
const MESSAGE_ERR_JSON: i32 = 3;
const MESSAGE_STREAM_ITEM: i32 = 4;
//const MESSAGE_STREAM_ITEM: i32 = 4;
const MESSAGE_STREAM_ITEM_JSON: i32 = 5;
const MESSAGE_STREAM_ABORT: i32 = 6;
//const MESSAGE_STREAM_ABORT: i32 = 6;
const MESSAGE_STREAM_ABORT_JSON: i32 = 7;
const MESSAGE_STREAM_CLOSE: i32 = 8;
@@ -29,6 +29,17 @@ impl DartIsolateWrapper {
}
}
pub fn spawn_result<F, T, E>(self, future: F)
where
F: Future<Output = Result<T, E>> + Send + 'static,
T: IntoDart,
E: Serialize,
{
async_std::task::spawn(async move {
self.result(future.await);
});
}
pub fn spawn_result_json<F, T, E>(self, future: F)
where
F: Future<Output = Result<T, E>> + Send + 'static,
@@ -40,10 +51,10 @@ impl DartIsolateWrapper {
});
}
pub fn result<T: IntoDart, E: IntoDart>(&self, result: Result<T, E>) -> bool {
pub fn result<T: IntoDart, E: Serialize>(&self, result: Result<T, E>) -> bool {
match result {
Ok(v) => self.ok(v),
Err(e) => self.err(e),
Err(e) => self.err_json(e),
}
}
pub fn result_json<T: Serialize, E: Serialize>(&self, result: Result<T, E>) -> bool {
@@ -64,10 +75,10 @@ impl DartIsolateWrapper {
])
}
pub fn err<E: IntoDart>(&self, error: E) -> bool {
self.isolate
.post(vec![MESSAGE_ERR.into_dart(), error.into_dart()])
}
// pub fn err<E: IntoDart>(&self, error: E) -> bool {
// self.isolate
// .post(vec![MESSAGE_ERR.into_dart(), error.into_dart()])
// }
pub fn err_json<E: Serialize>(&self, error: E) -> bool {
self.isolate.post(vec![
@@ -89,14 +100,14 @@ impl DartIsolateStream {
}
}
pub fn item<T: IntoDart>(&self, value: T) -> bool {
let isolate = self.isolate.lock();
if let Some(isolate) = &*isolate {
isolate.post(vec![MESSAGE_STREAM_ITEM.into_dart(), value.into_dart()])
} else {
false
}
}
// pub fn item<T: IntoDart>(&self, value: T) -> bool {
// let isolate = self.isolate.lock();
// if let Some(isolate) = &*isolate {
// isolate.post(vec![MESSAGE_STREAM_ITEM.into_dart(), value.into_dart()])
// } else {
// false
// }
// }
pub fn item_json<T: Serialize>(&self, value: T) -> bool {
let isolate = self.isolate.lock();
@@ -110,14 +121,14 @@ impl DartIsolateStream {
}
}
pub fn abort<E: IntoDart>(self, error: E) -> bool {
let mut isolate = self.isolate.lock();
if let Some(isolate) = isolate.take() {
isolate.post(vec![MESSAGE_STREAM_ABORT.into_dart(), error.into_dart()])
} else {
false
}
}
// pub fn abort<E: IntoDart>(self, error: E) -> bool {
// let mut isolate = self.isolate.lock();
// if let Some(isolate) = isolate.take() {
// isolate.post(vec![MESSAGE_STREAM_ABORT.into_dart(), error.into_dart()])
// } else {
// false
// }
// }
pub fn abort_json<E: Serialize>(self, error: E) -> bool {
let mut isolate = self.isolate.lock();