This commit is contained in:
Astro 2018-08-02 19:58:19 +02:00
commit b929a3c71e
7 changed files with 57 additions and 3 deletions

View file

@ -1,3 +1,5 @@
//! `XMPPStream` is the common container for all XMPP network connections
use futures::{Poll, Stream, Sink, StartSend};
use futures::sink::Send;
use tokio_io::{AsyncRead, AsyncWrite};
@ -8,16 +10,26 @@ use jid::Jid;
use xmpp_codec::{XMPPCodec, Packet};
use stream_start::StreamStart;
/// <stream:stream> namespace
pub const NS_XMPP_STREAM: &str = "http://etherx.jabber.org/streams";
/// Wraps a `stream`
pub struct XMPPStream<S> {
/// The local Jabber-Id
pub jid: Jid,
/// Codec instance
pub stream: Framed<S, XMPPCodec>,
/// `<stream:features/>` for XMPP version 1.0
pub stream_features: Element,
/// Root namespace
///
/// This is different for either c2s, s2s, or component
/// connections.
pub ns: String,
}
impl<S: AsyncRead + AsyncWrite> XMPPStream<S> {
/// Constructor
pub fn new(jid: Jid,
stream: Framed<S, XMPPCodec>,
ns: String,
@ -25,15 +37,18 @@ impl<S: AsyncRead + AsyncWrite> XMPPStream<S> {
XMPPStream { jid, stream, stream_features, ns }
}
/// Send a `<stream:stream>` start tag
pub fn start(stream: S, jid: Jid, ns: String) -> StreamStart<S> {
let xmpp_stream = Framed::new(stream, XMPPCodec::new());
StreamStart::from_stream(xmpp_stream, jid, ns)
}
/// Unwraps the inner stream
pub fn into_inner(self) -> S {
self.stream.into_inner()
}
/// Re-run `start()`
pub fn restart(self) -> StreamStart<S> {
Self::start(self.stream.into_inner(), self.jid, self.ns)
}