Struct lightws::stream::Stream

source ·
pub struct Stream<IO, Role, Guard = Direct> { /* private fields */ }
Expand description

Websocket stream.

Depending on IO, Stream implements std::io::Read and std::io::Write or tokio::io::AsyncRead and tokio::io::AsyncWrite.

Role decides whether to mask payload data. It is reserved to provide extra infomation to apply optimizations.

See also: Stream::read, Stream::write.

Implementations§

source§

impl<IO, Role, Guard> Stream<IO, Role, Guard>
where Role: RoleHelper,

source

pub fn mask_key(&self) -> Mask

Get mask for upcoming writes.

source

pub fn set_mask_key(&mut self, key: [u8; 4]) -> Result<(), CtrlError>

Set mask for upcoming writes. An attempt to set mask during a write will fail with CtrlError::SetMaskInWrite.

source§

impl<IO, Role, Guard> Stream<IO, Role, Guard>

Check status.

source

pub const fn is_pinged(&self) -> bool

Check if a Ping frame is received.

source

pub const fn is_ping_completed(&self) -> bool

Check if a Ping frame is completely read.

source

pub const fn ping_data(&self) -> &[u8]

Get the most recent ping.

source

pub const fn is_read_eof(&self) -> bool

Check if EOF is reached.

source

pub const fn is_read_close(&self) -> bool

Check if a Close frame is received.

source

pub const fn is_read_end(&self) -> bool

Check if a Close frame is received or EOF is reached.

source

pub const fn is_write_zero(&self) -> bool

Check if a WriteZero error occurred.

source

pub const fn is_read_partial_head(&self) -> bool

Check if a frame head is partially read.

source

pub const fn is_write_partial_head(&self) -> bool

Check if frame head is partially written.

source§

impl<Role: RoleHelper> Stream<TcpStream, Role>

source

pub fn try_clone(&self) -> Result<Self>

Creates a new independently owned handle to the underlying IO source.

Caution: states are not shared among instances!

source§

impl<IO, Role> Stream<IO, Role>

source

pub const fn new(io: IO, role: Role) -> Self

Create websocket stream from IO source directly, without a handshake.

source

pub fn guard(self) -> Stream<IO, Role, Guarded>

Convert to a guarded stream.

Trait Implementations§

source§

impl<IO, Role, Guard> AsMut<IO> for Stream<IO, Role, Guard>

source§

fn as_mut(&mut self) -> &mut IO

Converts this type into a mutable reference of the (usually inferred) input type.
source§

impl<IO, Role, Guard> AsRef<IO> for Stream<IO, Role, Guard>

source§

fn as_ref(&self) -> &IO

Converts this type into a shared reference of the (usually inferred) input type.
source§

impl<IO, Role> AsyncRead for Stream<IO, Role>
where IO: AsyncRead + Unpin, Stream<IO, Role>: Unpin, Role: RoleHelper,

source§

fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_> ) -> Poll<Result<()>>

Async version of Stream::read.

source§

impl<IO, Role> AsyncRead for Stream<IO, Role, Guarded>
where IO: AsyncRead + Unpin, Stream<IO, Role, Guarded>: Unpin, Role: RoleHelper,

source§

fn poll_read( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &mut ReadBuf<'_> ) -> Poll<Result<()>>

Async version of Stream::read. Continue to read if frame head is not complete.

source§

impl<IO, Role> AsyncWrite for Stream<IO, Role>
where IO: AsyncWrite + Unpin, Stream<IO, Role>: Unpin, Role: RoleHelper,

source§

fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>

Async version of Stream::write.

source§

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

This is a no-op since we do not buffer any data.

source§

fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Shutdown the underlying IO source.

source§

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize, Error>>

Like poll_write, except that it writes from a slice of buffers. Read more
source§

fn is_write_vectored(&self) -> bool

Determines if this writer has an efficient poll_write_vectored implementation. Read more
source§

impl<IO, Role> AsyncWrite for Stream<IO, Role, Guarded>
where IO: AsyncWrite + Unpin, Stream<IO, Role, Guarded>: Unpin, Role: RoleHelper,

source§

fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8] ) -> Poll<Result<usize>>

Async version of Stream::write. Continue to write if frame head is not completely written.

source§

fn poll_flush(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

This is a no-op since we do not buffer any data.

source§

fn poll_shutdown(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Result<()>>

Shutdown the underlying IO source.

source§

fn poll_write_vectored( self: Pin<&mut Self>, cx: &mut Context<'_>, bufs: &[IoSlice<'_>] ) -> Poll<Result<usize, Error>>

Like poll_write, except that it writes from a slice of buffers. Read more
source§

fn is_write_vectored(&self) -> bool

Determines if this writer has an efficient poll_write_vectored implementation. Read more
source§

impl<IO, Role, Guard> Debug for Stream<IO, Role, Guard>

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<IO: Read, Role: RoleHelper> Read for Stream<IO, Role>

source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Read some data from the underlying IO source, returns Ok(0) until a complete frame head is present. Caller should ensure the available buffer size is larger than 14 before a read.

Read a control frame(like Ping) returns Ok(0), which could be detected via Stream::is_pinged.

Any read after receiving a Close frame or reaching EOF will return Ok(0), which could be checked via Stream::is_read_end, Stream::is_read_close, Stream::is_read_eof.

source§

fn read_to_end(&mut self, _: &mut Vec<u8>) -> Result<usize>

This is NOT supported!

source§

fn read_exact(&mut self, _: &mut [u8]) -> Result<()>

This is NOT supported!

source§

fn read_to_string(&mut self, _: &mut String) -> Result<usize>

This is NOT supported!

1.36.0 · source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
source§

impl<IO: Read, Role: RoleHelper> Read for Stream<IO, Role, Guarded>

source§

fn read(&mut self, buf: &mut [u8]) -> Result<usize>

Wrap read in a loop. Continue to read if frame head is not complete.

source§

fn read_to_end(&mut self, buf: &mut Vec<u8>) -> Result<usize>

Override default implement, extend reserved buffer size, so that there is enough space to accommodate frame head.

1.36.0 · source§

fn read_vectored(&mut self, bufs: &mut [IoSliceMut<'_>]) -> Result<usize, Error>

Like read, except that it reads into a slice of buffers. Read more
source§

fn is_read_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Reader has an efficient read_vectored implementation. Read more
1.0.0 · source§

fn read_to_string(&mut self, buf: &mut String) -> Result<usize, Error>

Read all bytes until EOF in this source, appending them to buf. Read more
1.6.0 · source§

fn read_exact(&mut self, buf: &mut [u8]) -> Result<(), Error>

Read the exact number of bytes required to fill buf. Read more
source§

fn read_buf(&mut self, buf: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Pull some bytes from this source into the specified buffer. Read more
source§

fn read_buf_exact(&mut self, cursor: BorrowedCursor<'_>) -> Result<(), Error>

🔬This is a nightly-only experimental API. (read_buf)
Read the exact number of bytes required to fill cursor. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adaptor for this instance of Read. Read more
1.0.0 · source§

fn bytes(self) -> Bytes<Self>
where Self: Sized,

Transforms this Read instance to an Iterator over its bytes. Read more
1.0.0 · source§

fn chain<R>(self, next: R) -> Chain<Self, R>
where R: Read, Self: Sized,

Creates an adapter which will chain this stream with another. Read more
1.0.0 · source§

fn take(self, limit: u64) -> Take<Self>
where Self: Sized,

Creates an adapter which will read at most limit bytes from it. Read more
source§

impl<IO: Write, Role: RoleHelper> Write for Stream<IO, Role>

source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Write some data to the underlying IO source, returns Ok(0) until the frame head is completely written.

if WriteZero occurs, it will also return Ok(0), which could be detected via Stream::is_write_zero.

Frame head will be generated automatically, according to the length of the provided buffer.

A standard client should mask payload data before sending it.

source§

fn flush(&mut self) -> Result<()>

The writer does not buffer any data, simply flush the underlying IO source.

source§

fn write_all(&mut self, _: &[u8]) -> Result<()>

This is NOT supported!

1.36.0 · source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more
source§

impl<IO: Write, Role: RoleHelper> Write for Stream<IO, Role, Guarded>

source§

fn write(&mut self, buf: &[u8]) -> Result<usize>

Wrap write in a loop. Continue to write if frame head is not completely written.

source§

fn flush(&mut self) -> Result<()>

The writer does not buffer any data, simply flush the underlying IO source.

1.36.0 · source§

fn write_vectored(&mut self, bufs: &[IoSlice<'_>]) -> Result<usize, Error>

Like write, except that it writes from a slice of buffers. Read more
source§

fn is_write_vectored(&self) -> bool

🔬This is a nightly-only experimental API. (can_vector)
Determines if this Writer has an efficient write_vectored implementation. Read more
1.0.0 · source§

fn write_all(&mut self, buf: &[u8]) -> Result<(), Error>

Attempts to write an entire buffer into this writer. Read more
source§

fn write_all_vectored(&mut self, bufs: &mut [IoSlice<'_>]) -> Result<(), Error>

🔬This is a nightly-only experimental API. (write_all_vectored)
Attempts to write multiple buffers into this writer. Read more
1.0.0 · source§

fn write_fmt(&mut self, fmt: Arguments<'_>) -> Result<(), Error>

Writes a formatted string into this writer, returning any error encountered. Read more
1.0.0 · source§

fn by_ref(&mut self) -> &mut Self
where Self: Sized,

Creates a “by reference” adapter for this instance of Write. Read more

Auto Trait Implementations§

§

impl<IO, Role, Guard> RefUnwindSafe for Stream<IO, Role, Guard>
where Guard: RefUnwindSafe, IO: RefUnwindSafe, Role: RefUnwindSafe,

§

impl<IO, Role, Guard> Send for Stream<IO, Role, Guard>
where Guard: Send, IO: Send, Role: Send,

§

impl<IO, Role, Guard> Sync for Stream<IO, Role, Guard>
where Guard: Sync, IO: Sync, Role: Sync,

§

impl<IO, Role, Guard> Unpin for Stream<IO, Role, Guard>
where Guard: Unpin, IO: Unpin, Role: Unpin,

§

impl<IO, Role, Guard> UnwindSafe for Stream<IO, Role, Guard>
where Guard: UnwindSafe, IO: UnwindSafe, Role: UnwindSafe,

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

§

fn vzip(self) -> V