-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from halzy/halzy/10-docs
feat!: Added HalvesStream, example.
- Loading branch information
Showing
4 changed files
with
180 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
[package] | ||
name = "stream_multiplexer" | ||
version = "0.4.1" | ||
version = "0.5.0" | ||
authors = ["Benjamin Halsted <[email protected]>"] | ||
edition = "2018" | ||
license = "MIT OR Apache-2.0" | ||
|
@@ -17,13 +17,13 @@ byteorder = "1.3" | |
bytes = "0.5" | ||
futures = { version = "0.3", default-features = false, features = ["alloc"] } | ||
thiserror = "1.0" | ||
tokio = { version = "0.2", features = ["full"] } | ||
tokio = { version = "0.2.13", features = ["full"] } | ||
tokio-util = { version = "0.2", features = ["codec"] } | ||
tracing = { version = "0.1", features = ["log"] } | ||
tracing-futures = "0.2" | ||
|
||
[dev-dependencies] | ||
futures = { version = "0.3", default-features = false, features = ["alloc","std"] } | ||
matches = "0.1" | ||
tokio = { version = "0.2", features = ["full", "test-util"] } | ||
tokio = { version = "0.2.13", features = ["full", "test-util"] } | ||
tracing-subscriber = "0.2" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
use futures::stream::TryStream; | ||
use futures::Stream; | ||
use tokio::io::{AsyncRead, AsyncWrite, ReadHalf, WriteHalf}; | ||
use tokio_util::codec::{FramedRead, FramedWrite, LengthDelimitedCodec}; | ||
|
||
use std::pin::Pin; | ||
use std::task::Poll; | ||
|
||
/// Takes a Stream<Item=AsyncRead + AsyncWrite> and provides a | ||
/// Stream<Item=( FramedWrite<WriteHalf, LengthDelimitedCodec>, FramedRead<ReadHalf, LengthDelimitedCodec>)> | ||
#[derive(Debug)] | ||
pub struct HalvesStream<St> { | ||
inner: St, | ||
length_field_length: usize, | ||
} | ||
|
||
impl<St> HalvesStream<St> { | ||
/// Takes a TcpListener to help own the listener while producing TcpStreams | ||
pub fn new(inner: St, length_field_length: usize) -> Self { | ||
Self { | ||
inner, | ||
length_field_length, | ||
} | ||
} | ||
} | ||
|
||
impl<St> Stream for HalvesStream<St> | ||
where | ||
St: TryStream<Error = std::io::Error> + Unpin, | ||
St::Ok: AsyncRead + AsyncWrite, | ||
{ | ||
type Item = Result< | ||
( | ||
FramedWrite<WriteHalf<St::Ok>, LengthDelimitedCodec>, | ||
FramedRead<ReadHalf<St::Ok>, LengthDelimitedCodec>, | ||
), | ||
St::Error, | ||
>; | ||
fn poll_next( | ||
mut self: Pin<&mut Self>, | ||
ctx: &mut std::task::Context, | ||
) -> Poll<Option<Self::Item>> { | ||
match futures::ready!(Pin::new(&mut self.inner).try_poll_next(ctx)) { | ||
None => None.into(), | ||
Some(Err(err)) => Poll::Ready(Some(Err(err))), | ||
Some(Ok(stream)) => { | ||
let (reader, writer) = tokio::io::split(stream); | ||
|
||
// Wrap the writer in a FramedCodec | ||
let framed_write = LengthDelimitedCodec::builder() | ||
.length_field_length(self.length_field_length) | ||
.new_write(writer); | ||
|
||
let framed_read = LengthDelimitedCodec::builder() | ||
.length_field_length(self.length_field_length) | ||
.new_read(reader); | ||
|
||
Poll::Ready(Some(Ok((framed_write, framed_read)))) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters