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 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
//! [![docs.rs](https://img.shields.io/docsrs/stream-ws)](https://docs.rs/crate/stream-ws/latest)
//! [![repo](https://img.shields.io/badge/repo-stream--ws-blue?logo=github)](https://github.com/idkidknow/stream-ws)
//! [![crates-io](https://img.shields.io/badge/crates--io-stream--ws-blue)](https://crates.io/crates/stream-ws)
//!
//! A layer over WebSocket enables carrying byte stream, for both native and WebAssembly.
//!
//! Providing methods able to wrap any WebSocket message stream implementation,
//! and impl [`AsyncRead`], [`AsyncBufRead`] and [`AsyncWrite`].
//!
//! # Usage
//!
//! run `cargo add stream-ws` to bring it into your crate.
//!
//! Examples in `examples/`.
//!
//! ## Tungstenite
//!
//! With feature `tungstenite`.
//!
//! For `WebSocketStream` in either crate `tokio-tungstenite` or `async-tungstenite`,
//! use
//!
//! ```rust
//! let stream = stream_ws::tungstenite::WsByteStream::new(inner)
//! ```
//!
//! ## Gloo (for WebAssembly)
//!
//! With feature `gloo`.
//!
//! use
//!
//! ```rust
//! let stream = stream_ws::gloo::WsByteStream::new(inner)
//! ```
//!
//! ## Wrapping underlying stream of other WebSocket implementation
//!
//! Your WebSocket implementation should have a struct `S` satisfying trait bound
//!
//! ```rust
//! Stream<Item = Result<Msg, E>> + Sink<Msg, Error = E> + Unpin
//! ```
//!
//! where `Msg` and `E`
//! are message and error type of the implementation.
//!
//! Create a struct `Handler` and impl [`WsMessageHandle`], which is easy, and then
//! call `Handler::wrap_stream(underlying_stream)` to get a [`WsByteStream`].
//!
//! # Crate features
//!
//! - `tokio`: impl `tokio`'s `AsyncRead`, `AsyncBufRead` and `AsyncWrite` variants
//! - `tungstenite`: handlers for message and error type from crate `tungstenite`
//! - `gloo`: handlers for message and error type from crate `gloo`
#![cfg_attr(docsrs, feature(doc_cfg))]
#[cfg(feature = "gloo")]
#[cfg_attr(docsrs, doc(cfg(feature = "gloo")))]
pub mod gloo;
#[cfg(feature = "tungstenite")]
#[cfg_attr(docsrs, doc(cfg(feature = "tungstenite")))]
pub mod tungstenite;
use futures::{ready, AsyncBufRead, AsyncRead, AsyncWrite, Sink, Stream};
use pin_project::pin_project;
use std::{io, marker::PhantomData, pin::Pin, task::Poll};
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
use tokio::io::{
AsyncBufRead as TokioAsyncBufRead, AsyncRead as TokioAsyncRead, AsyncWrite as TokioAsyncWrite,
};
#[derive(Debug)]
pub enum WsMessageKind {
/// Messages considered as payload carriers
Bytes(Vec<u8>),
Close,
/// Messages that will be ignored
Other,
}
#[derive(Debug)]
pub enum WsErrorKind {
Io(io::Error),
/// Normally closed. Won't be considered as an error.
Closed,
AlreadyClosed,
Other(Box<dyn std::error::Error + Send + Sync>),
}
/// Classify messages and errors of WebSocket.
pub trait WsMessageHandle<Msg, E> {
fn message_into_kind(msg: Msg) -> WsMessageKind;
fn error_into_kind(e: E) -> WsErrorKind;
/// These bytes will be carried by the `Msg` and sent by the underlying stream.
fn message_from_bytes<T: Into<Vec<u8>>>(bytes: T) -> Msg;
fn wrap_stream<S>(inner: S) -> WsByteStream<S, Msg, E, Self>
where
S: Stream<Item = Result<Msg, E>> + Sink<Msg, Error = E> + Unpin,
{
WsByteStream::new(inner)
}
}
/// A wrapper implements [`AsyncRead`], [`AsyncBufRead`] and [`AsyncWrite`],
/// around a established websocket connect which
/// implemented `Stream` and `Sink` traits.
///
/// Bytes are transported on binary messages over WebSocket.
/// Other messages except close messages will be ignored.
///
/// # Assumption:
///
/// The underlying stream should automatically handle ping and pong messages.
#[pin_project]
pub struct WsByteStream<S, Msg, E, H>
where
S: Stream<Item = Result<Msg, E>> + Sink<Msg, Error = E> + Unpin,
H: WsMessageHandle<Msg, E> + ?Sized,
{
#[pin]
inner: S,
state: State,
_marker: PhantomData<H>,
}
#[derive(Debug)]
struct State {
read: ReadState,
write: WriteState,
}
#[derive(Debug)]
enum ReadState {
/// Buf is empty and ready to read from WebSocket.
Pending,
/// Bytes from a message not all being read, are stored in `buf`. The amount of bytes read is `amt_read`.
Ready { buf: Vec<u8>, amt_read: usize },
/// Connection was shut down correctly.
Terminated,
}
#[derive(Debug)]
enum WriteState {
Ready,
Closed,
}
impl<S, Msg, E, H> WsByteStream<S, Msg, E, H>
where
S: Stream<Item = Result<Msg, E>> + Sink<Msg, Error = E> + Unpin,
H: WsMessageHandle<Msg, E> + ?Sized,
{
pub fn new(inner: S) -> Self {
Self {
inner,
state: State {
read: ReadState::Pending,
write: WriteState::Ready,
},
_marker: PhantomData,
}
}
/// This function tries reading next message to fill the buf (set the state to `ReadState::Ready`).
///
/// Returns `None` if the stream has been fused. (The state is set to `ReadState::Terminated`)
fn fill_buf_with_next_msg(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Option<io::Result<()>>> {
let mut this = self.project();
loop {
// Try read from inner stream.
let res = ready!(this.inner.as_mut().poll_next(cx));
let Some(res) = res else {
// `res` is None. Stream has been fused.
this.state.read = ReadState::Terminated;
return Poll::Ready(None);
};
match res {
Ok(msg) => {
let msg = H::message_into_kind(msg);
match msg {
WsMessageKind::Bytes(msg) => {
this.state.read = ReadState::Ready {
buf: msg,
amt_read: 0,
};
return Poll::Ready(Some(Ok(())));
}
WsMessageKind::Close => {
this.state.read = ReadState::Terminated;
return Poll::Ready(None);
}
WsMessageKind::Other => {
// Simply ignore it.
continue;
}
}
}
Err(e) => {
let e = H::error_into_kind(e);
match e {
WsErrorKind::Io(e) => {
return Poll::Ready(Some(Err(e)));
}
WsErrorKind::Closed => {
this.state.read = ReadState::Terminated;
return Poll::Ready(None);
}
WsErrorKind::AlreadyClosed => {
this.state.read = ReadState::Terminated;
let e = io::Error::new(io::ErrorKind::NotConnected, "Already closed");
return Poll::Ready(Some(Err(e)));
}
WsErrorKind::Other(e) => {
return Poll::Ready(Some(Err(io::Error::new(io::ErrorKind::Other, e))));
}
}
}
}
}
}
}
impl<S, Msg, E, H> AsyncRead for WsByteStream<S, Msg, E, H>
where
S: Stream<Item = Result<Msg, E>> + Sink<Msg, Error = E> + Unpin,
H: WsMessageHandle<Msg, E> + ?Sized,
{
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
dst: &mut [u8],
) -> Poll<io::Result<usize>> {
loop {
let this = self.as_mut().project();
match this.state.read {
ReadState::Pending => {
let res = ready!(self.as_mut().fill_buf_with_next_msg(cx));
match res {
Some(Ok(())) => continue, // The state is assumed to be `Ready`
Some(Err(e)) => return Poll::Ready(Err(e)),
None => continue, // The state is assumed to be `Terminated`
}
}
ReadState::Ready {
ref buf,
ref mut amt_read,
} => {
let buf = &buf[*amt_read..];
let len = std::cmp::min(dst.len(), buf.len());
dst[..len].copy_from_slice(&buf[..len]);
if len == buf.len() {
this.state.read = ReadState::Pending;
} else {
*amt_read += len;
}
return Poll::Ready(Ok(len));
}
ReadState::Terminated => {
return Poll::Ready(Ok(0));
}
}
}
}
}
impl<S, Msg, E, H> AsyncBufRead for WsByteStream<S, Msg, E, H>
where
S: Stream<Item = Result<Msg, E>> + Sink<Msg, Error = E> + Unpin,
H: WsMessageHandle<Msg, E> + ?Sized,
{
fn poll_fill_buf(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<io::Result<&[u8]>> {
loop {
let this = self.as_mut().project();
match this.state.read {
ReadState::Pending => {
let res = ready!(self.as_mut().fill_buf_with_next_msg(cx));
match res {
Some(Ok(())) => continue, // The state is assumed to be `Ready`
Some(Err(e)) => return Poll::Ready(Err(e)),
None => continue, // The state is assumed to be `Terminated`
}
}
ReadState::Ready { .. } => {
// Borrow and match again to meet the lifetime requirement.
let this = self.project();
let ReadState::Ready { ref buf, amt_read } = this.state.read else {
unreachable!()
};
return Poll::Ready(Ok(&buf[amt_read..]));
}
ReadState::Terminated => {
return Poll::Ready(Ok(&[]));
}
}
}
}
fn consume(mut self: Pin<&mut Self>, amt: usize) {
let ReadState::Ready {
ref buf,
ref mut amt_read,
} = self.state.read
else {
return;
};
*amt_read = std::cmp::min(buf.len(), *amt_read + amt);
if *amt_read == buf.len() {
self.state.read = ReadState::Pending;
}
}
}
impl<S, Msg, E, H> AsyncWrite for WsByteStream<S, Msg, E, H>
where
S: Stream<Item = Result<Msg, E>> + Sink<Msg, Error = E> + Unpin,
H: WsMessageHandle<Msg, E> + ?Sized,
{
fn poll_write(
mut self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &[u8],
) -> Poll<io::Result<usize>> {
let mut this = self.as_mut().project();
loop {
match this.state.write {
WriteState::Ready => {
if let Err(e) = ready!(this.inner.as_mut().poll_ready(cx)) {
let e = H::error_into_kind(e);
match e {
WsErrorKind::Io(e) => {
return Poll::Ready(Err(e));
}
WsErrorKind::Other(e) => {
return Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, e)));
}
WsErrorKind::Closed => {
this.state.write = WriteState::Closed;
return Poll::Ready(Ok(0));
}
WsErrorKind::AlreadyClosed => {
this.state.write = WriteState::Closed;
let e =
io::Error::new(io::ErrorKind::NotConnected, "Already closed");
return Poll::Ready(Err(e));
}
}
}
// Start sending
let Err(e) = this.inner.as_mut().start_send(H::message_from_bytes(buf)) else {
this.state.write = WriteState::Ready;
return Poll::Ready(Ok(buf.len()));
};
let e = H::error_into_kind(e);
match e {
WsErrorKind::Io(e) => {
return Poll::Ready(Err(e));
}
WsErrorKind::Other(e) => {
return Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, e)));
}
WsErrorKind::Closed => {
this.state.write = WriteState::Closed;
return Poll::Ready(Ok(0));
}
WsErrorKind::AlreadyClosed => {
this.state.write = WriteState::Closed;
let e = io::Error::new(io::ErrorKind::NotConnected, "Already closed");
return Poll::Ready(Err(e));
}
}
}
WriteState::Closed => {
let e = io::Error::new(io::ErrorKind::NotConnected, "Already closed");
return Poll::Ready(Err(e));
}
}
}
}
fn poll_flush(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<io::Result<()>> {
let mut this = self.project();
if let Err(e) = ready!(this.inner.as_mut().poll_flush(cx)) {
let e = H::error_into_kind(e);
match e {
WsErrorKind::Io(e) => {
return Poll::Ready(Err(e));
}
WsErrorKind::Other(e) => {
return Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, e)));
}
WsErrorKind::Closed => {
this.state.write = WriteState::Closed;
return Poll::Ready(Ok(()));
}
WsErrorKind::AlreadyClosed => {
this.state.write = WriteState::Closed;
let e = io::Error::new(io::ErrorKind::NotConnected, "Already closed");
return Poll::Ready(Err(e));
}
}
}
Poll::Ready(Ok(()))
}
fn poll_close(self: Pin<&mut Self>, cx: &mut std::task::Context<'_>) -> Poll<io::Result<()>> {
let mut this = self.project();
this.state.write = WriteState::Closed;
if let Err(e) = ready!(this.inner.as_mut().poll_close(cx)) {
let e = H::error_into_kind(e);
match e {
WsErrorKind::Io(e) => {
return Poll::Ready(Err(e));
}
WsErrorKind::Other(e) => {
return Poll::Ready(Err(io::Error::new(io::ErrorKind::Other, e)));
}
WsErrorKind::Closed => {
return Poll::Ready(Ok(()));
}
WsErrorKind::AlreadyClosed => {
let e = io::Error::new(io::ErrorKind::NotConnected, "Already closed");
return Poll::Ready(Err(e));
}
}
}
Poll::Ready(Ok(()))
}
}
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
impl<S, Msg, E, H> TokioAsyncRead for WsByteStream<S, Msg, E, H>
where
S: Stream<Item = Result<Msg, E>> + Sink<Msg, Error = E> + Unpin,
H: WsMessageHandle<Msg, E> + ?Sized,
{
fn poll_read(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &mut tokio::io::ReadBuf<'_>,
) -> Poll<io::Result<()>> {
let slice = buf.initialize_unfilled();
let n = ready!(AsyncRead::poll_read(self, cx, slice))?;
buf.advance(n);
Poll::Ready(Ok(()))
}
}
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
impl<S, Msg, E, H> TokioAsyncBufRead for WsByteStream<S, Msg, E, H>
where
S: Stream<Item = Result<Msg, E>> + Sink<Msg, Error = E> + Unpin,
H: WsMessageHandle<Msg, E> + ?Sized,
{
fn poll_fill_buf(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<io::Result<&[u8]>> {
AsyncBufRead::poll_fill_buf(self, cx)
}
fn consume(self: Pin<&mut Self>, amt: usize) {
AsyncBufRead::consume(self, amt)
}
}
#[cfg(feature = "tokio")]
#[cfg_attr(docsrs, doc(cfg(feature = "tokio")))]
impl<S, Msg, E, H> TokioAsyncWrite for WsByteStream<S, Msg, E, H>
where
S: Stream<Item = Result<Msg, E>> + Sink<Msg, Error = E> + Unpin,
H: WsMessageHandle<Msg, E> + ?Sized,
{
fn poll_write(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, io::Error>> {
AsyncWrite::poll_write(self, cx, buf)
}
fn poll_flush(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Result<(), io::Error>> {
AsyncWrite::poll_flush(self, cx)
}
fn poll_shutdown(
self: Pin<&mut Self>,
cx: &mut std::task::Context<'_>,
) -> Poll<Result<(), io::Error>> {
AsyncWrite::poll_close(self, cx)
}
}