update rust api to match ffi

This commit is contained in:
dignifiedquire
2020-05-22 22:04:20 +02:00
parent 014d2946b2
commit c53a3d5cb4
5 changed files with 32 additions and 33 deletions

View File

@@ -487,9 +487,8 @@ pub unsafe extern "C" fn dc_start_io(context: *mut dc_context_t) {
}
let ffi_context = &*context;
with_inner_async!(ffi_context, ctx, { ctx.run() }).unwrap_or(())
with_inner_async!(ffi_context, ctx, { ctx.start_io() }).unwrap_or(())
}
#[no_mangle]
pub unsafe extern "C" fn dc_is_io_running(context: *mut dc_context_t) -> libc::c_int {
if context.is_null() {
@@ -497,7 +496,7 @@ pub unsafe extern "C" fn dc_is_io_running(context: *mut dc_context_t) -> libc::c
}
let ffi_context = &*context;
with_inner_async!(ffi_context, ctx, { ctx.is_running() }).unwrap_or_default() as libc::c_int
with_inner_async!(ffi_context, ctx, { ctx.is_io_running() }).unwrap_or_default() as libc::c_int
}
#[no_mangle]
@@ -698,7 +697,7 @@ pub unsafe extern "C" fn dc_stop_io(context: *mut dc_context_t) {
let ffi_context = &*context;
with_inner_async!(ffi_context, ctx, async move {
ctx.stop().await;
ctx.stop_io().await;
})
.unwrap_or(())
}

View File

@@ -277,10 +277,8 @@ async fn start(args: Vec<String>) -> Result<(), Error> {
let events = context.get_event_emitter();
async_std::task::spawn(async move {
loop {
if let Some(event) = events.recv().await {
receive_event(event);
}
while let Some(event) = events.recv().await {
receive_event(event);
}
});
@@ -322,7 +320,7 @@ async fn start(args: Vec<String>) -> Result<(), Error> {
}
Err(ReadlineError::Interrupted) | Err(ReadlineError::Eof) => {
println!("Exiting...");
context.stop().await;
context.stop_io().await;
break;
}
Err(err) => {
@@ -355,10 +353,10 @@ async fn handle_cmd(
match arg0 {
"connect" => {
ctx.run().await;
ctx.start_io().await;
}
"disconnect" => {
ctx.stop().await;
ctx.stop_io().await;
}
"configure" => {
ctx.configure().await?;
@@ -381,7 +379,7 @@ async fn handle_cmd(
print!("\x1b[1;1H\x1b[2J");
}
"getqr" | "getbadqr" => {
ctx.run().await;
ctx.start_io().await;
if let Some(mut qr) =
dc_get_securejoin_qr(&ctx, ChatId::new(arg1.parse().unwrap_or_default())).await
{
@@ -400,7 +398,7 @@ async fn handle_cmd(
}
}
"joinqr" => {
ctx.run().await;
ctx.start_io().await;
if !arg0.is_empty() {
dc_join_securejoin(&ctx, arg1).await;
}

View File

@@ -39,11 +39,9 @@ async fn main() {
println!("info: {:#?}", info);
let events = ctx.get_event_emitter();
async_std::task::spawn(async move {
loop {
if let Some(event) = events.recv().await {
cb(event);
}
let events_spawn = async_std::task::spawn(async move {
while let Some(event) = events.recv().await {
cb(event);
}
});
@@ -62,7 +60,7 @@ async fn main() {
ctx.configure().await.unwrap();
println!("------ RUN ------");
ctx.clone().run().await;
ctx.clone().start_io().await;
println!("--- SENDING A MESSAGE ---");
let contact_id = Contact::create(&ctx, "dignifiedquire", "dignifiedquire@gmail.com")
@@ -89,6 +87,7 @@ async fn main() {
async_std::task::sleep(duration).await;
println!("stopping");
ctx.stop().await;
ctx.stop_io().await;
println!("closing");
events_spawn.await;
}

View File

@@ -135,21 +135,24 @@ impl Context {
Ok(ctx)
}
pub async fn run(&self) {
assert!(!self.is_running().await, "context is already running");
/// Starts the IO scheduler.
pub async fn start_io(&self) {
info!(self, "starting IO");
assert!(!self.is_io_running().await, "context is already running");
let l = &mut *self.inner.scheduler.write().await;
l.run(self.clone()).await;
l.start(self.clone()).await;
}
pub async fn is_running(&self) -> bool {
self.inner.is_running().await
/// Returns if the IO scheduler is running.
pub async fn is_io_running(&self) -> bool {
self.inner.is_io_running().await
}
pub async fn stop(&self) {
info!(self, "stopping context");
self.inner.stop().await;
info!(self, "stopped context");
/// Stops the IO scheduler.
pub async fn stop_io(&self) {
info!(self, "stopping IO");
self.inner.stop_io().await;
}
/// Returns a reference to the underlying SQL instance.
@@ -491,12 +494,12 @@ impl Context {
}
impl InnerContext {
async fn is_running(&self) -> bool {
async fn is_io_running(&self) -> bool {
self.scheduler.read().await.is_running()
}
async fn stop(&self) {
assert!(self.is_running().await, "context is already stopped");
async fn stop_io(&self) {
assert!(self.is_io_running().await, "context is already stopped");
let token = {
let lock = &*self.scheduler.read().await;
lock.pre_stop().await

View File

@@ -276,7 +276,7 @@ async fn smtp_loop(ctx: Context, started: Sender<()>, smtp_handlers: SmtpConnect
impl Scheduler {
/// Start the scheduler, panics if it is already running.
pub async fn run(&mut self, ctx: Context) {
pub async fn start(&mut self, ctx: Context) {
let (mvbox, mvbox_handlers) = ImapConnectionState::new();
let (sentbox, sentbox_handlers) = ImapConnectionState::new();
let (smtp, smtp_handlers) = SmtpConnectionState::new();