Refactor codec::length_delimited (#575)

This patch refactors `length_delimited` to be implemented as a `Codec` and
use the default `Framed` wrapper types.

The original implementation did not do this in order to support vectored writes in the
write half. However, this implementation would be more efficient with small frames anyway.

If vectored writes are to be explored in the future, then it should be done holistically.

Signed-off-by: Eliza Weisman <[email protected]>
This commit is contained in:
Eliza Weisman
2018-08-30 14:50:32 -07:00
committed by Carl Lerche
parent 97618746de
commit 673fdb5cb3
3 changed files with 640 additions and 401 deletions
+14 -11
View File
@@ -135,19 +135,20 @@ pub mod codec {
//! # Getting started
//!
//! If implementing a protocol from scratch, using length delimited framing
//! is an easy way to get started. [`Framed::new()`] will adapt a
//! full-duplex byte stream with a length delimited framer using default
//! configuration values.
//! is an easy way to get started. [`Codec::new()`] will return a length
//! delimited codec using default configuration values. This can then be
//! used to construct a framer to adapt a full-duplex byte stream into a
//! stream of frames.
//!
//! ```
//! # extern crate tokio;
//! use tokio::io::{AsyncRead, AsyncWrite};
//! use tokio::codec::length_delimited;
//! use tokio::codec::*;
//!
//! fn bind_transport<T: AsyncRead + AsyncWrite>(io: T)
//! -> length_delimited::Framed<T>
//! -> Framed<T, LengthDelimitedCodec>
//! {
//! length_delimited::Framed::new(io)
//! Framed::new(io, LengthDelimitedCodec::new())
//! }
//! # pub fn main() {}
//! ```
@@ -170,13 +171,13 @@ pub mod codec {
//! # extern crate futures;
//! #
//! use tokio::io::{AsyncRead, AsyncWrite};
//! use tokio::codec::length_delimited;
//! use bytes::BytesMut;
//! use tokio::codec::*;
//! use bytes::Bytes;
//! use futures::{Sink, Future};
//!
//! fn write_frame<T: AsyncRead + AsyncWrite>(io: T) {
//! let mut transport = length_delimited::Framed::new(io);
//! let frame = BytesMut::from("hello world");
//! let mut transport = Framed::new(io, LengthDelimitedCodec::new());
//! let frame = Bytes::from("hello world");
//!
//! transport.send(frame).wait().unwrap();
//! }
@@ -454,7 +455,7 @@ pub mod codec {
//! # use tokio::codec::length_delimited;
//! # use bytes::BytesMut;
//! # fn write_frame<T: AsyncWrite>(io: T) {
//! # let _: length_delimited::FramedWrite<T, BytesMut> =
//! # let _ =
//! length_delimited::Builder::new()
//! .length_field_length(2)
//! .new_write(io);
@@ -478,6 +479,8 @@ pub mod codec {
//! [`BytesMut`]: https://docs.rs/bytes/0.4/bytes/struct.BytesMut.html
pub use ::length_delimited::*;
}
pub use self::length_delimited::LengthDelimitedCodec;
}
pub mod io {