Struct ws_stream_tungstenite::WsStream
source · pub struct WsStream<S>{ /* private fields */ }
Expand description
Takes a WebSocketStream
and implements futures 0.3 AsyncRead
/AsyncWrite
/AsyncBufRead
.
Will always create an entire Websocket message from every write. Tungstenite buffers messages up to
write_buffer_size
in their tungstenite::protocol::WebSocketConfig
. If you want small messages to be sent out,
either make sure this buffer is small enough or flush the writer.
On the other hand the max_write_buffer_size
from tokio is the maximum size we can send in one go, otherwise
tungstenite returns an error. Our AsyncWrite
implementation never sends data that exceeds this buffer or
max_message_size
.
However you still must respect the max_message_size
of the receiving end.
§Errors
Errors returned directly are generally io errors from the underlying stream. Only fatal errors are returned in band, so consider them fatal and drop the WsStream object.
Other errors are returned out of band through pharos:
On reading, eg. AsyncRead::poll_read
:
WsErr::Protocol
: The remote made a websocket protocol violation. The connection will be closed gracefully indicating to the remote what went wrong. You can just keep callingpoll_read
untilNone
is returned.- tungstenite returned a utf8 error. Pharos will return it as a Tungstenite error. This means the remote
send a text message, which is not supported, so the connection will be gracefully closed. You can just keep calling
poll_read
untilNone
is returned. WsErr::ReceivedText
: This means the remote send a text message, which is not supported, so the connection will be gracefully closed. You can just keep callingpoll_read
untilNone
is returned.
On writing, eg. AsyncWrite::*
all errors are fatal.
When a Protocol error is encountered during writing, it indicates that either ws_stream_tungstenite or tungstenite have a bug so it will panic.
Implementations§
Trait Implementations§
source§impl<S> AsyncBufRead for WsStream<S>
impl<S> AsyncBufRead for WsStream<S>
source§impl<S> AsyncRead for WsStream<S>
impl<S> AsyncRead for WsStream<S>
source§impl<S> AsyncWrite for WsStream<S>
Available on crate feature tokio_io
only.
impl<S> AsyncWrite for WsStream<S>
tokio_io
only.source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>
Will always flush the underlying socket. Will always create an entire Websocket message from every write, so call with a sufficiently large buffer if you have performance problems. Don’t call with a buffer larger than the max message size accepted by the remote endpoint.
source§fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
source§fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>
source§fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize, Error>>
fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize, Error>>
poll_write
, except that it writes from a slice of buffers. Read moresource§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
poll_write_vectored
implementation. Read moresource§impl<S> AsyncWrite for WsStream<S>
impl<S> AsyncWrite for WsStream<S>
source§fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8]
) -> Poll<Result<usize>>
fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>
buf
into the object. Read moresource§fn poll_write_vectored(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
bufs: &[IoSlice<'_>]
) -> Poll<Result<usize>>
fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize>>
bufs
into the object using vectored
IO operations. Read moresource§impl<S> Observable<WsEvent> for WsStream<S>
impl<S> Observable<WsEvent> for WsStream<S>
Auto Trait Implementations§
impl<S> !RefUnwindSafe for WsStream<S>
impl<S> Send for WsStream<S>
impl<S> !Sync for WsStream<S>
impl<S> Unpin for WsStream<S>
impl<S> !UnwindSafe for WsStream<S>
Blanket Implementations§
source§impl<R> AsyncBufReadExt for Rwhere
R: AsyncBufRead + ?Sized,
impl<R> AsyncBufReadExt for Rwhere
R: AsyncBufRead + ?Sized,
source§fn fill_buf(&mut self) -> FillBuf<'_, Self>where
Self: Unpin,
fn fill_buf(&mut self) -> FillBuf<'_, Self>where
Self: Unpin,
source§fn consume_unpin(&mut self, amt: usize)where
Self: Unpin,
fn consume_unpin(&mut self, amt: usize)where
Self: Unpin,
source§fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntil<'a, Self>where
Self: Unpin,
fn read_until<'a>(
&'a mut self,
byte: u8,
buf: &'a mut Vec<u8>
) -> ReadUntil<'a, Self>where
Self: Unpin,
buf
until the delimiter byte
or EOF is reached.
This method is the async equivalent to BufRead::read_until
. Read moresource§fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self>where
Self: Unpin,
fn read_line<'a>(&'a mut self, buf: &'a mut String) -> ReadLine<'a, Self>where
Self: Unpin,
buf
until a newline (the 0xA byte) or EOF is reached,
This method is the async equivalent to BufRead::read_line
. Read moresource§impl<R> AsyncReadExt for R
impl<R> AsyncReadExt for R
source§fn chain<R>(self, next: R) -> Chain<Self, R>
fn chain<R>(self, next: R) -> Chain<Self, R>
source§fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
fn read<'a>(&'a mut self, buf: &'a mut [u8]) -> Read<'a, Self>where
Self: Unpin,
buf
in asynchronous
manner, returning a future type. Read moresource§fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectored<'a, Self>where
Self: Unpin,
fn read_vectored<'a>(
&'a mut self,
bufs: &'a mut [IoSliceMut<'a>]
) -> ReadVectored<'a, Self>where
Self: Unpin,
AsyncRead
into bufs
using vectored
IO operations. Read moresource§fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
fn read_exact<'a>(&'a mut self, buf: &'a mut [u8]) -> ReadExact<'a, Self>where
Self: Unpin,
buf
,
returning an error if end of file (EOF) is hit sooner. Read moresource§fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
fn read_to_end<'a>(&'a mut self, buf: &'a mut Vec<u8>) -> ReadToEnd<'a, Self>where
Self: Unpin,
AsyncRead
. Read moresource§fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String
) -> ReadToString<'a, Self>where
Self: Unpin,
fn read_to_string<'a>(
&'a mut self,
buf: &'a mut String
) -> ReadToString<'a, Self>where
Self: Unpin,
AsyncRead
. Read moresource§impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
impl<W> AsyncWriteExt for Wwhere
W: AsyncWrite + ?Sized,
source§fn flush(&mut self) -> Flush<'_, Self>where
Self: Unpin,
fn flush(&mut self) -> Flush<'_, Self>where
Self: Unpin,
AsyncWrite
. Read moresource§fn close(&mut self) -> Close<'_, Self>where
Self: Unpin,
fn close(&mut self) -> Close<'_, Self>where
Self: Unpin,
AsyncWrite
.source§fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>where
Self: Unpin,
fn write<'a>(&'a mut self, buf: &'a [u8]) -> Write<'a, Self>where
Self: Unpin,
buf
into the object. Read moresource§fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectored<'a, Self>where
Self: Unpin,
fn write_vectored<'a>(
&'a mut self,
bufs: &'a [IoSlice<'a>]
) -> WriteVectored<'a, Self>where
Self: Unpin,
bufs
into the object using vectored
IO operations. Read more