1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
//! See the type-level documentation for [`WsTransport`](struct.WsTransport.html).

#![cfg_attr(feature = "strict", deny(warnings))]
#![cfg_attr(feature = "strict", deny(missing_docs, missing_debug_implementations))]
#![cfg_attr(feature = "clippy", feature(plugin))]
#![cfg_attr(feature = "clippy", plugin(clippy))]
#![cfg_attr(feature = "clippy", allow(doc_markdown))]
#![doc(html_root_url = "https://docs.rs/websocket-transport/0.1.0")]

#[macro_use]
extern crate futures;
extern crate websocket;

use futures::{Async, AsyncSink, Poll, Sink, StartSend, Stream};
use std::fmt;
use std::str::Utf8Error;
use websocket::message::OwnedMessage;

/// An easy wrapper around an async WebSocket which implements
/// [`Stream`](https://docs.rs/futures/0.1.15/futures/stream/trait.Stream.html)
/// and
/// [`Sink`](https://docs.rs/futures/0.1.15/futures/sink/trait.Sink.html)
/// for `String`.
///
/// This type automatically takes care of:
///
/// - receiving and responding to `Ping`s, as the `Stream` is polled
/// - attempting to convert `Binary` messages to UTF-8 `String`s
///
/// It can be wrapped around
/// [`Client`](https://docs.rs/websocket/0.20.2/websocket/client/async/type.Client.html)
/// or any other type which implements
/// [`Stream`](https://docs.rs/futures/0.1.15/futures/stream/trait.Stream.html)
/// and
/// [`Sink`](https://docs.rs/futures/0.1.15/futures/sink/trait.Sink.html) for
/// [`OwnedMessage`](https://docs.rs/websocket/0.20.2/websocket/message/enum.OwnedMessage.html).
pub struct WsTransport<T> {
    inner: T,
    sending: Option<OwnedMessage>,
    flushing: bool,
}

impl<T> WsTransport<T> {
    /// Wrap around an inner async WebSocket transport.
    ///
    /// `T` can be
    /// [`Client`](https://docs.rs/websocket/0.20.2/websocket/client/async/type.Client.html)
    /// or any other type which implements
    /// [`Stream`](https://docs.rs/futures/0.1.15/futures/stream/trait.Stream.html)
    /// and
    /// [`Sink`](https://docs.rs/futures/0.1.15/futures/sink/trait.Sink.html) for
    /// [`OwnedMessage`](https://docs.rs/websocket/0.20.2/websocket/message/enum.OwnedMessage.html).
    pub fn new(inner: T) -> Self {
        WsTransport {
            inner: inner,
            sending: None,
            flushing: false,
        }
    }
}

impl<T> fmt::Debug for WsTransport<T>
where
    T: fmt::Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.debug_struct("WsTransport")
            .field("ws", &Omitted)
            .field("sending", &self.sending)
            .field("flushing", &self.flushing)
            .finish()
    }
}

impl<T> Stream for WsTransport<T>
where
    T: Stream<Item = OwnedMessage>,
    T: Sink<SinkItem = OwnedMessage, SinkError = <T as Stream>::Error>,
    T::Error: From<Utf8Error>,
{
    type Item = String;
    type Error = T::Error;

    fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
        loop {
            if let Some(msg) = self.sending.take() {
                if let AsyncSink::NotReady(msg) = self.inner.start_send(msg)? {
                    self.sending = Some(msg);
                    return Ok(Async::NotReady);
                }
                self.flushing = true;
            }

            if self.flushing {
                try_ready!(self.inner.poll_complete());
                self.flushing = false;
            }

            let item = try_ready!(self.inner.poll());
            match item {
                None => return Ok(None.into()),
                Some(OwnedMessage::Text(text)) => {
                    return Ok(Some(text).into());
                }
                Some(OwnedMessage::Binary(bytes)) => {
                    let text = String::from_utf8(bytes).map_err(|err| err.utf8_error().into())?;
                    return Ok(Some(text).into());
                }
                Some(OwnedMessage::Ping(data)) => {
                    self.sending = Some(OwnedMessage::Pong(data));
                }
                Some(OwnedMessage::Close(_)) | Some(OwnedMessage::Pong(_)) => (),
            }
        }
    }
}

impl<T> Sink for WsTransport<T>
where
    T: Sink<SinkItem = OwnedMessage>,
{
    type SinkItem = String;
    type SinkError = T::SinkError;

    fn start_send(&mut self, item: Self::SinkItem) -> StartSend<Self::SinkItem, Self::SinkError> {
        Ok(match self.inner.start_send(OwnedMessage::Text(item))? {
            AsyncSink::Ready => AsyncSink::Ready,
            AsyncSink::NotReady(msg) => AsyncSink::NotReady(match msg {
                OwnedMessage::Text(item) => item,
                _ => unreachable!("websocket-transport: inner Sink broke its contract"),
            }),
        })
    }

    fn poll_complete(&mut self) -> Poll<(), Self::SinkError> {
        self.inner.poll_complete()
    }

    fn close(&mut self) -> Poll<(), Self::SinkError> {
        self.inner.close()
    }
}

struct Omitted;

impl fmt::Debug for Omitted {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "...")
    }
}