Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

do not explicit copy data before sending #234

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 37 additions & 24 deletions h3-quinn/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ impl From<quinn::SendDatagramError> for SendDatagramError {

impl<B> quic::Connection<B> for Connection
where
B: Buf,
B: Buf + Send + 'static,
{
type SendStream = SendStream<B>;
type RecvStream = RecvStream;
Expand Down Expand Up @@ -277,7 +277,7 @@ pub struct OpenStreams {

impl<B> quic::OpenStreams<B> for OpenStreams
where
B: Buf,
B: Buf + Send + 'static,
{
type RecvStream = RecvStream;
type SendStream = SendStream<B>;
Expand Down Expand Up @@ -348,7 +348,7 @@ where

impl<B> quic::BidiStream<B> for BidiStream<B>
where
B: Buf,
B: Buf + Send + 'static,
{
type SendStream = SendStream<B>;
type RecvStream = RecvStream;
Expand Down Expand Up @@ -380,7 +380,7 @@ impl<B: Buf> quic::RecvStream for BidiStream<B> {

impl<B> quic::SendStream<B> for BidiStream<B>
where
B: Buf,
B: Buf + Send + 'static,
{
type Error = SendStreamError;

Expand All @@ -406,7 +406,7 @@ where
}
impl<B> quic::SendStreamUnframed<B> for BidiStream<B>
where
B: Buf,
B: Buf + Send + 'static,
{
fn poll_send<D: Buf>(
&mut self,
Expand Down Expand Up @@ -543,11 +543,17 @@ impl Error for ReadError {
pub struct SendStream<B: Buf> {
stream: Option<quinn::SendStream>,
writing: Option<WriteBuf<B>>,
write_fut: WriteFuture,
write_fut: WriteFuture<B>,
}

type WriteFuture =
ReusableBoxFuture<'static, (quinn::SendStream, Result<usize, quinn::WriteError>)>;
type WriteFuture<B> = ReusableBoxFuture<
'static,
(
quinn::SendStream,
Result<usize, quinn::WriteError>,
WriteBuf<B>,
),
>;

impl<B> SendStream<B>
where
Expand All @@ -564,29 +570,35 @@ where

impl<B> quic::SendStream<B> for SendStream<B>
where
B: Buf,
B: Buf + Send + 'static,
{
type Error = SendStreamError;

fn poll_ready(&mut self, cx: &mut task::Context<'_>) -> Poll<Result<(), Self::Error>> {
if let Some(ref mut data) = self.writing {
while data.has_remaining() {
loop {
if let Some(data) = self.writing.take() {
if let Some(mut stream) = self.stream.take() {
let chunk = data.chunk().to_owned(); // FIXME - avoid copy
self.write_fut.set(async move {
let ret = stream.write(&chunk).await;
(stream, ret)
let chunk = data.chunk();
let ret = stream.write(chunk).await;
(stream, ret, data)
});
}

let (stream, res) = ready!(self.write_fut.poll(cx));
self.stream = Some(stream);
match res {
Ok(cnt) => data.advance(cnt),
Err(err) => {
return Poll::Ready(Err(SendStreamError::Write(err)));
}
}
let (stream, res, mut data2) = ready!(self.write_fut.poll(cx));
self.stream = Some(stream);
match res {
Ok(cnt) => data2.advance(cnt),
Err(err) => {
self.writing = Some(data2);
return Poll::Ready(Err(SendStreamError::Write(err)));
}
};
if !data2.has_remaining() {
break;
} else {
self.writing = Some(data2);
continue;
}
}
self.writing = None;
Expand All @@ -610,7 +622,8 @@ where
}

fn send_data<D: Into<WriteBuf<B>>>(&mut self, data: D) -> Result<(), Self::Error> {
if self.writing.is_some() {
// writing is moved into the write_fut, so stream should be present
if self.writing.is_some() || self.stream.is_none() {
return Err(Self::Error::NotReady);
}
self.writing = Some(data.into());
Expand All @@ -630,7 +643,7 @@ where

impl<B> quic::SendStreamUnframed<B> for SendStream<B>
where
B: Buf,
B: Buf + Send + 'static,
{
fn poll_send<D: Buf>(
&mut self,
Expand Down
Loading