2020-03-15 23:34:46 +00:00
|
|
|
//! `XMPPStream` provides encoding/decoding for XMPP
|
2018-08-02 17:58:19 +00:00
|
|
|
|
2018-08-01 22:19:06 +00:00
|
|
|
use futures::sink::Send;
|
2020-03-05 00:25:24 +00:00
|
|
|
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;
|
2019-10-22 23:32:41 +00:00
|
|
|
use xmpp_parsers::{Element, Jid};
|
2017-06-05 00:50:22 +00:00
|
|
|
|
2020-03-18 00:12:48 +00:00
|
|
|
use crate::stream_features::StreamFeatures;
|
2020-05-29 22:14:32 +00:00
|
|
|
use crate::stream_start;
|
2018-12-18 18:04:31 +00:00
|
|
|
use crate::xmpp_codec::{Packet, XMPPCodec};
|
2020-03-05 00:25:24 +00:00
|
|
|
use crate::Error;
|
2017-06-05 00:50:22 +00:00
|
|
|
|
2020-03-15 23:34:46 +00:00
|
|
|
/// Wraps a binary stream (tokio's `AsyncRead + AsyncWrite`) to decode
|
|
|
|
/// and encode XMPP packets.
|
|
|
|
///
|
|
|
|
/// Implements `Sink + Stream`
|
2020-03-05 00:25:24 +00:00
|
|
|
pub struct XMPPStream<S: AsyncRead + AsyncWrite + Unpin> {
|
2018-08-02 17:58:19 +00:00
|
|
|
/// The local Jabber-Id
|
2017-06-13 23:55:56 +00:00
|
|
|
pub jid: Jid,
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Codec instance
|
2020-03-05 00:25:24 +00:00
|
|
|
pub stream: Mutex<Framed<S, XMPPCodec>>,
|
2018-08-02 17:58:19 +00:00
|
|
|
/// `<stream:features/>` for XMPP version 1.0
|
2020-03-18 00:12:48 +00:00
|
|
|
pub stream_features: StreamFeatures,
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Root namespace
|
|
|
|
///
|
|
|
|
/// This is different for either c2s, s2s, or component
|
|
|
|
/// connections.
|
2017-07-18 23:02:45 +00:00
|
|
|
pub ns: String,
|
2020-03-05 00:25:24 +00:00
|
|
|
/// Stream `id` attribute
|
|
|
|
pub id: String,
|
2017-06-05 00:50:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 00:25:24 +00:00
|
|
|
impl<S: AsyncRead + AsyncWrite + Unpin> XMPPStream<S> {
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Constructor
|
2018-12-18 18:04:31 +00:00
|
|
|
pub fn new(
|
|
|
|
jid: Jid,
|
|
|
|
stream: Framed<S, XMPPCodec>,
|
|
|
|
ns: String,
|
2020-03-05 00:25:24 +00:00
|
|
|
id: String,
|
2018-12-18 18:04:31 +00:00
|
|
|
stream_features: Element,
|
|
|
|
) -> Self {
|
|
|
|
XMPPStream {
|
|
|
|
jid,
|
2020-03-05 00:25:24 +00:00
|
|
|
stream: Mutex::new(stream),
|
2020-03-18 00:12:48 +00:00
|
|
|
stream_features: StreamFeatures::new(stream_features),
|
2018-12-18 18:04:31 +00:00
|
|
|
ns,
|
2020-03-05 00:25:24 +00:00
|
|
|
id,
|
2018-12-18 18:04:31 +00:00
|
|
|
}
|
2017-06-13 23:55:56 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Send a `<stream:stream>` start tag
|
2020-03-05 00:25:24 +00:00
|
|
|
pub async fn start<'a>(stream: S, jid: Jid, ns: String) -> Result<Self, Error> {
|
2018-07-31 21:34:04 +00:00
|
|
|
let xmpp_stream = Framed::new(stream, XMPPCodec::new());
|
2020-03-05 00:25:24 +00:00
|
|
|
stream_start::start(xmpp_stream, jid, ns).await
|
2017-06-05 00:50:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Unwraps the inner stream
|
2017-06-05 00:50:22 +00:00
|
|
|
pub fn into_inner(self) -> S {
|
2020-03-05 00:25:24 +00:00
|
|
|
self.stream.into_inner().unwrap().into_inner()
|
2017-06-05 00:50:22 +00:00
|
|
|
}
|
|
|
|
|
2018-08-02 17:58:19 +00:00
|
|
|
/// Re-run `start()`
|
2020-03-05 00:25:24 +00:00
|
|
|
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
|
2017-06-05 23:38:48 +00:00
|
|
|
}
|
2017-06-05 00:50:22 +00:00
|
|
|
}
|
|
|
|
|
2020-03-05 00:25:24 +00:00
|
|
|
impl<S: AsyncRead + AsyncWrite + Unpin> XMPPStream<S> {
|
2018-08-01 22:19:06 +00:00
|
|
|
/// Convenience method
|
2020-03-05 00:25:24 +00:00
|
|
|
pub fn send_stanza<E: Into<Element>>(&mut self, e: E) -> Send<Self, Packet> {
|
2018-08-01 22:19:06 +00:00
|
|
|
self.send(Packet::Stanza(e.into()))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-05 23:29:20 +00:00
|
|
|
/// Proxy to self.stream
|
2020-03-05 00:25:24 +00:00
|
|
|
impl<S: AsyncRead + AsyncWrite + Unpin> Sink<Packet> for XMPPStream<S> {
|
|
|
|
type Error = crate::Error;
|
|
|
|
|
|
|
|
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(()))
|
|
|
|
}
|
|
|
|
|
2020-12-25 01:25:17 +00:00
|
|
|
fn start_send(
|
2021-02-15 23:20:40 +00:00
|
|
|
#[cfg_attr(rustc_least_1_46, allow(unused_mut))] mut self: Pin<&mut Self>,
|
2020-12-25 01:25:17 +00:00
|
|
|
item: Packet,
|
|
|
|
) -> Result<(), Self::Error> {
|
2020-03-05 00:25:24 +00:00
|
|
|
Pin::new(&mut self.stream.lock().unwrap().deref_mut())
|
|
|
|
.start_send(item)
|
|
|
|
.map_err(|e| e.into())
|
|
|
|
}
|
2017-06-05 00:50:22 +00:00
|
|
|
|
2020-12-25 01:25:17 +00:00
|
|
|
fn poll_flush(
|
2021-02-15 23:20:40 +00:00
|
|
|
#[cfg_attr(rustc_least_1_46, allow(unused_mut))] mut self: Pin<&mut Self>,
|
2020-12-25 01:25:17 +00:00
|
|
|
cx: &mut Context,
|
|
|
|
) -> Poll<Result<(), Self::Error>> {
|
2020-03-05 00:25:24 +00:00
|
|
|
Pin::new(&mut self.stream.lock().unwrap().deref_mut())
|
|
|
|
.poll_flush(cx)
|
|
|
|
.map_err(|e| e.into())
|
2017-06-05 00:50:22 +00:00
|
|
|
}
|
|
|
|
|
2020-12-25 01:25:17 +00:00
|
|
|
fn poll_close(
|
2021-02-15 23:20:40 +00:00
|
|
|
#[cfg_attr(rustc_least_1_46, allow(unused_mut))] mut self: Pin<&mut Self>,
|
2020-12-25 01:25:17 +00:00
|
|
|
cx: &mut Context,
|
|
|
|
) -> Poll<Result<(), Self::Error>> {
|
2020-03-05 00:25:24 +00:00
|
|
|
Pin::new(&mut self.stream.lock().unwrap().deref_mut())
|
|
|
|
.poll_close(cx)
|
|
|
|
.map_err(|e| e.into())
|
2017-06-05 00:50:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-05 23:29:20 +00:00
|
|
|
/// Proxy to self.stream
|
2020-03-05 00:25:24 +00:00
|
|
|
impl<S: AsyncRead + AsyncWrite + Unpin> Stream for XMPPStream<S> {
|
|
|
|
type Item = Result<Packet, crate::Error>;
|
2017-06-05 00:50:22 +00:00
|
|
|
|
2020-12-25 01:25:17 +00:00
|
|
|
fn poll_next(
|
2021-02-15 23:20:40 +00:00
|
|
|
#[cfg_attr(rustc_least_1_46, allow(unused_mut))] mut self: Pin<&mut Self>,
|
2020-12-25 01:25:17 +00:00
|
|
|
cx: &mut Context,
|
|
|
|
) -> Poll<Option<Self::Item>> {
|
2020-03-05 00:25:24 +00:00
|
|
|
Pin::new(&mut self.stream.lock().unwrap().deref_mut())
|
|
|
|
.poll_next(cx)
|
|
|
|
.map(|result| result.map(|result| result.map_err(|e| e.into())))
|
2017-06-05 00:50:22 +00:00
|
|
|
}
|
|
|
|
}
|