tokio-xmpp: rewrite for futures-0.3

This commit is contained in:
Astro 2020-03-05 01:25:24 +01:00
commit 23cb34e026
18 changed files with 801 additions and 1222 deletions

View file

@ -1,23 +1,28 @@
//! `XMPPStream` is the common container for all XMPP network connections
use futures::sink::Send;
use futures::{Poll, Sink, StartSend, Stream};
use tokio_codec::Framed;
use tokio_io::{AsyncRead, AsyncWrite};
use futures::{sink::SinkExt, task::Poll, Sink, Stream};
use std::ops::DerefMut;
use std::pin::Pin;
use std::sync::Mutex;
use std::task::Context;
use tokio::io::{AsyncRead, AsyncWrite};
use tokio_util::codec::Framed;
use xmpp_parsers::{Element, Jid};
use crate::stream_start::StreamStart;
use crate::stream_start;
use crate::xmpp_codec::{Packet, XMPPCodec};
use crate::Error;
/// <stream:stream> namespace
pub const NS_XMPP_STREAM: &str = "http://etherx.jabber.org/streams";
/// Wraps a `stream`
pub struct XMPPStream<S> {
pub struct XMPPStream<S: AsyncRead + AsyncWrite + Unpin> {
/// The local Jabber-Id
pub jid: Jid,
/// Codec instance
pub stream: Framed<S, XMPPCodec>,
pub stream: Mutex<Framed<S, XMPPCodec>>,
/// `<stream:features/>` for XMPP version 1.0
pub stream_features: Element,
/// Root namespace
@ -25,68 +30,94 @@ pub struct XMPPStream<S> {
/// This is different for either c2s, s2s, or component
/// connections.
pub ns: String,
/// Stream `id` attribute
pub id: String,
}
impl<S: AsyncRead + AsyncWrite> XMPPStream<S> {
// // TODO: fix this hack
// unsafe impl<S: AsyncRead + AsyncWrite + Unpin> core::marker::Send for XMPPStream<S> {}
// unsafe impl<S: AsyncRead + AsyncWrite + Unpin> Sync for XMPPStream<S> {}
impl<S: AsyncRead + AsyncWrite + Unpin> XMPPStream<S> {
/// Constructor
pub fn new(
jid: Jid,
stream: Framed<S, XMPPCodec>,
ns: String,
id: String,
stream_features: Element,
) -> Self {
XMPPStream {
jid,
stream,
stream: Mutex::new(stream),
stream_features,
ns,
id,
}
}
/// Send a `<stream:stream>` start tag
pub fn start(stream: S, jid: Jid, ns: String) -> StreamStart<S> {
pub async fn start<'a>(stream: S, jid: Jid, ns: String) -> Result<Self, Error> {
let xmpp_stream = Framed::new(stream, XMPPCodec::new());
StreamStart::from_stream(xmpp_stream, jid, ns)
stream_start::start(xmpp_stream, jid, ns).await
}
/// Unwraps the inner stream
// TODO: use this everywhere
pub fn into_inner(self) -> S {
self.stream.into_inner()
self.stream.into_inner().unwrap().into_inner()
}
/// Re-run `start()`
pub fn restart(self) -> StreamStart<S> {
Self::start(self.stream.into_inner(), self.jid, self.ns)
pub async fn restart<'a>(self) -> Result<Self, Error> {
let stream = self.stream.into_inner().unwrap().into_inner();
Self::start(stream, self.jid, self.ns).await
}
}
impl<S: AsyncWrite> XMPPStream<S> {
impl<S: AsyncRead + AsyncWrite + Unpin> XMPPStream<S> {
/// Convenience method
pub fn send_stanza<E: Into<Element>>(self, e: E) -> Send<Self> {
pub fn send_stanza<E: Into<Element>>(&mut self, e: E) -> Send<Self, Packet> {
self.send(Packet::Stanza(e.into()))
}
}
/// Proxy to self.stream
impl<S: AsyncWrite> Sink for XMPPStream<S> {
type SinkItem = <Framed<S, XMPPCodec> as Sink>::SinkItem;
type SinkError = <Framed<S, XMPPCodec> as Sink>::SinkError;
impl<S: AsyncRead + AsyncWrite + Unpin> Sink<Packet> for XMPPStream<S> {
type Error = crate::Error;
fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
self.stream.start_send(item)
fn poll_ready(self: Pin<&mut Self>, _ctx: &mut Context) -> Poll<Result<(), Self::Error>> {
// Pin::new(&mut self.stream).poll_ready(ctx)
// .map_err(|e| e.into())
Poll::Ready(Ok(()))
}
fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
self.stream.poll_complete()
fn start_send(mut self: Pin<&mut Self>, item: Packet) -> Result<(), Self::Error> {
Pin::new(&mut self.stream.lock().unwrap().deref_mut())
.start_send(item)
.map_err(|e| e.into())
}
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
Pin::new(&mut self.stream.lock().unwrap().deref_mut())
.poll_flush(cx)
.map_err(|e| e.into())
}
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), Self::Error>> {
Pin::new(&mut self.stream.lock().unwrap().deref_mut())
.poll_close(cx)
.map_err(|e| e.into())
}
}
/// Proxy to self.stream
impl<S: AsyncRead> Stream for XMPPStream<S> {
type Item = <Framed<S, XMPPCodec> as Stream>::Item;
type Error = <Framed<S, XMPPCodec> as Stream>::Error;
impl<S: AsyncRead + AsyncWrite + Unpin> Stream for XMPPStream<S> {
type Item = Result<Packet, crate::Error>;
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
self.stream.poll()
fn poll_next(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Option<Self::Item>> {
Pin::new(&mut self.stream.lock().unwrap().deref_mut())
.poll_next(cx)
.map(|result| result.map(|result| result.map_err(|e| e.into())))
}
}