use bytes::Buf; use futures_core::Stream; use futures_sink::Sink; use std::pin::Pin; use std::task::{Context, Poll}; use tokio::io::{AsyncRead, AsyncWrite}; use tokio_util::codec::length_delimited;
impl<T, B> Codec<T, B> where
T: AsyncRead + AsyncWrite + Unpin,
B: Buf,
{ /// Returns a new `Codec` with the default max frame size #[inline] pubfn new(io: T) -> Self { Self::with_max_recv_frame_size(io, frame::DEFAULT_MAX_FRAME_SIZE as usize)
}
/// Returns a new `Codec` with the given maximum frame size pubfn with_max_recv_frame_size(io: T, max_frame_size: usize) -> Self { // Wrap with writer let framed_write = FramedWrite::new(io);
// Delimit the frames let delimited = length_delimited::Builder::new()
.big_endian()
.length_field_length(3)
.length_adjustment(9)
.num_skip(0) // Don't skip the header
.new_read(framed_write);
letmut inner = FramedRead::new(delimited);
// Use FramedRead's method since it checks the value is within range.
inner.set_max_frame_size(max_frame_size);
Codec { inner }
}
}
impl<T, B> Codec<T, B> { /// Updates the max received frame size. /// /// The change takes effect the next time a frame is decoded. In other /// words, if a frame is currently in process of being decoded with a frame /// size greater than `val` but less than the max frame size in effect /// before calling this function, then the frame will be allowed. #[inline] pubfn set_max_recv_frame_size(&mutself, val: usize) { self.inner.set_max_frame_size(val)
}
/// Returns the current max received frame size setting. /// /// This is the largest size this codec will accept from the wire. Larger /// frames will be rejected. #[cfg(feature = "unstable")] #[inline] pubfn max_recv_frame_size(&self) -> usize { self.inner.max_frame_size()
}
/// Returns the max frame size that can be sent to the peer. pubfn max_send_frame_size(&self) -> usize { self.inner.get_ref().max_frame_size()
}
/// Set the peer's max frame size. pubfn set_max_send_frame_size(&mutself, val: usize) { self.framed_write().set_max_frame_size(val)
}
/// Set the peer's header table size size. pubfn set_send_header_table_size(&mutself, val: usize) { self.framed_write().set_header_table_size(val)
}
/// Set the decoder header table size size. pubfn set_recv_header_table_size(&mutself, val: usize) { self.inner.set_header_table_size(val)
}
/// Set the max header list size that can be received. pubfn set_max_recv_header_list_size(&mutself, val: usize) { self.inner.set_max_header_list_size(val);
}
/// Get a reference to the inner stream. #[cfg(feature = "unstable")] pubfn get_ref(&self) -> &T { self.inner.get_ref().get_ref()
}
/// Get a mutable reference to the inner stream. pubfn get_mut(&mutself) -> &mut T { self.inner.get_mut().get_mut()
}
/// Takes the data payload value that was fully written to the socket pub(crate) fn take_last_data_frame(&mutself) -> Option<Data<B>> { self.framed_write().take_last_data_frame()
}
impl<T, B> Codec<T, B> where
T: AsyncWrite + Unpin,
B: Buf,
{ /// Returns `Ready` when the codec can buffer a frame pubfn poll_ready(&mutself, cx: &mut Context) -> Poll<io::Result<()>> { self.framed_write().poll_ready(cx)
}
/// Buffer a frame. /// /// `poll_ready` must be called first to ensure that a frame may be /// accepted. /// /// TODO: Rename this to avoid conflicts with Sink::buffer pubfn buffer(&mutself, item: Frame<B>) -> Result<(), UserError> { self.framed_write().buffer(item)
}
/// Flush buffered data to the wire pubfn flush(&mutself, cx: &mut Context) -> Poll<io::Result<()>> { self.framed_write().flush(cx)
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.