pub struct SendStream { /* private fields */ }展开描述
实现§
源代码§impl SendStream
impl SendStream
源代码pub async fn write(&mut self, buf: &[u8]) -> Result<usize, WriteError>
pub async fn write(&mut self, buf: &[u8]) -> Result<usize, WriteError>
将一个缓冲区写入该流,并返回实际写入的字节数
除非此方法出错,否则它会一直等到 buf 中的部分字节可以写入该流,然后尽可能多地写入而不再等待。受拥塞控制与流量控制影响,实际写入长度可能小于 buf.len()。成功时返回已写入前缀的长度。
§取消安全性
该方法是取消安全的。如果它没有完成,不会写入任何字节。
源代码pub async fn write_chunks(
&mut self,
bufs: &mut [Bytes],
) -> Result<Written, WriteError>
pub async fn write_chunks( &mut self, bufs: &mut [Bytes], ) -> Result<Written, WriteError>
源代码pub async fn write_chunk(&mut self, buf: Bytes) -> Result<(), WriteError>
pub async fn write_chunk(&mut self, buf: Bytes) -> Result<(), WriteError>
将单个 Bytes 数据块完整写入该流
待写入的字节以单个廉价可克隆的数据块形式提供。此方法会反复调用 write_chunks,直至所有字节写完或发生错误。
§取消安全性
该方法不是取消安全的。即使未完成,此前轮询中也可能已经写入了部分字节。
源代码pub async fn write_all_chunks(
&mut self,
bufs: &mut [Bytes],
) -> Result<(), WriteError>
pub async fn write_all_chunks( &mut self, bufs: &mut [Bytes], ) -> Result<(), WriteError>
将一段 Bytes 切片完整写入该流
待写入的字节以一组廉价可克隆的数据块形式提供。此方法会反复调用 write_chunks,直至所有字节写完或发生错误。此方法会把 bufs 中的所有数据块都修改为空。
§取消安全性
该方法不是取消安全的。即使未完成,此前轮询中也可能已经写入了部分字节。
源代码pub fn finish(&mut self) -> Result<(), ClosedStream>
pub fn finish(&mut self) -> Result<(), ClosedStream>
通知对端,此后将不会再向该流写入任何数据
在 finish() 之后向 SendStream 写入数据是错误的。在 finish 之后仍可调用 reset() 来丢弃可能仍在缓冲区中的所有流数据。
若需等待对端接收完所有缓冲的流数据,请参阅 stopped()。
如果之前已调用过 finish() 或 reset(),本方法可能失败。该错误无害,仅用于提示调用者可能对流的状态存在错误假设。
源代码pub fn set_priority(&self, priority: i32) -> Result<(), ClosedStream>
pub fn set_priority(&self, priority: i32) -> Result<(), ClosedStream>
设置发送流的优先级
每个发送流的初始优先级为 0。本地缓冲的数据中,优先级较高的流会先于优先级较低的流被发送。修改尚有未发送数据的流的优先级,可能要等到这些数据被发送后才会生效。在每个连接上使用过多不同的优先级等级可能对性能产生负面影响。
源代码pub fn priority(&self) -> Result<i32, ClosedStream>
pub fn priority(&self) -> Result<i32, ClosedStream>
获取发送流的优先级
源代码pub fn stopped(
&self,
) -> impl Future<Output = Result<Option<VarInt>, StoppedError>> + Send + Sync + 'static
pub fn stopped( &self, ) -> impl Future<Output = Result<Option<VarInt>, StoppedError>> + Send + Sync + 'static
当对端停止该流或将该流读取完毕时完成
若对端停止该流,则返回带有停止错误码的 Some。若本地端先 finish() 了流,随后对端确认收完所有流数据(虽然不一定已处理完),则返回 None,此后对端关闭流便不再有意义。
出于多种原因,对端在收到数据后未必立即发送确认。因此,依靠 stopped 来判断对端何时将流读至完成,可能比采用某种应用层响应方式引入更大的延迟。
源代码pub fn poll_write(
self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &[u8],
) -> Poll<Result<usize, WriteError>>
pub fn poll_write( self: Pin<&mut Self>, cx: &mut Context<'_>, buf: &[u8], ) -> Poll<Result<usize, WriteError>>
尝试将 buf 中的字节写入流中。
成功时返回 Poll::Ready(Ok(num_bytes_written))。
若流暂时不可写,该方法返回 Poll::Pending,并安排当前任务(经由 cx.waker().wake_by_ref())在流变为可写或被关闭时收到通知。
trait 实现§
源代码§impl AsyncWrite for SendStream
impl AsyncWrite for SendStream
源代码§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. 更多信息源代码§fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<()>>
fn poll_flush(self: Pin<&mut Self>, _cx: &mut Context<'_>) -> Poll<Result<()>>
源代码§fn poll_shutdown(
self: Pin<&mut Self>,
_cx: &mut Context<'_>,
) -> Poll<Result<()>>
fn poll_shutdown( self: Pin<&mut Self>, _cx: &mut Context<'_>, ) -> Poll<Result<()>>
源代码§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. 更多信息源代码§fn is_write_vectored(&self) -> bool
fn is_write_vectored(&self) -> bool
poll_write_vectored
implementation. 更多信息