tokio-xmpp: add option to split client
Adds an option to split the `tokio-xmpp::Client` into a Sender and Receiver. To enable this, a client worker is added which drives the stream in the background.
This commit is contained in:
parent
d84cb261e4
commit
10f1b3663c
8 changed files with 287 additions and 63 deletions
48
tokio-xmpp/src/client/receiver.rs
Normal file
48
tokio-xmpp/src/client/receiver.rs
Normal file
|
|
@ -0,0 +1,48 @@
|
|||
// Copyright (c) 2025 xmpp-rs contributors.
|
||||
//
|
||||
// This Source Code Form is subject to the terms of the Mozilla Public
|
||||
// License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
|
||||
use crate::Client;
|
||||
use crate::Event;
|
||||
use core::{pin::Pin, task::Context};
|
||||
use futures::StreamExt;
|
||||
use futures::{task::Poll, Stream};
|
||||
use std::sync::Arc;
|
||||
use tokio::sync::Mutex;
|
||||
use xmpp_parsers::{jid::Jid, stream_features::StreamFeatures};
|
||||
|
||||
/// Read half of a [`Client`](crate::Client).
|
||||
#[derive(Debug)]
|
||||
pub struct ClientReceiver(pub(super) Arc<Mutex<Client>>);
|
||||
|
||||
impl ClientReceiver {
|
||||
/// Return the bound JID.
|
||||
///
|
||||
/// See the documentation of [`Client::bound_jid`](crate::Client::bound_jid) for more
|
||||
/// information.
|
||||
pub async fn bound_jid(&self) -> Option<Jid> {
|
||||
self.0.lock().await.bound_jid.clone()
|
||||
}
|
||||
|
||||
/// Return the received stream features.
|
||||
///
|
||||
/// See the documentation of [`Client::get_stream_features`](crate::Client::get_stream_features)
|
||||
/// for more information.
|
||||
pub async fn get_stream_features(&self) -> Option<StreamFeatures> {
|
||||
self.0.lock().await.features.clone()
|
||||
}
|
||||
}
|
||||
|
||||
impl Stream for ClientReceiver {
|
||||
type Item = Event;
|
||||
|
||||
fn poll_next(self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
|
||||
let Ok(mut client) = self.0.try_lock() else {
|
||||
return Poll::Pending;
|
||||
};
|
||||
|
||||
client.poll_next_unpin(cx)
|
||||
}
|
||||
}
|
||||
Loading…
Reference in a new issue