update to tokio 0.3 (#491)

This commit is contained in:
João Oliveira
2020-10-23 18:45:09 +01:00
committed by GitHub
parent 676a068fd4
commit cbbdd305b1
11 changed files with 68 additions and 61 deletions

View File

@@ -3,13 +3,10 @@ use crate::codec::UserError::*;
use crate::frame::{self, Frame, FrameSize};
use crate::hpack;
use bytes::{
buf::{BufExt, BufMutExt},
Buf, BufMut, BytesMut,
};
use bytes::{buf::BufMutExt, Buf, BufMut, BytesMut};
use std::pin::Pin;
use std::task::{Context, Poll};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use std::io::{self, Cursor};
@@ -193,12 +190,26 @@ where
match self.next {
Some(Next::Data(ref mut frame)) => {
tracing::trace!(queued_data_frame = true);
let mut buf = (&mut self.buf).chain(frame.payload_mut());
ready!(Pin::new(&mut self.inner).poll_write_buf(cx, &mut buf))?;
if self.buf.has_remaining() {
let n =
ready!(Pin::new(&mut self.inner).poll_write(cx, self.buf.bytes()))?;
self.buf.advance(n);
}
let buf = frame.payload_mut();
if !self.buf.has_remaining() && buf.has_remaining() {
let n = ready!(Pin::new(&mut self.inner).poll_write(cx, buf.bytes()))?;
buf.advance(n);
}
}
_ => {
tracing::trace!(queued_data_frame = false);
ready!(Pin::new(&mut self.inner).poll_write_buf(cx, &mut self.buf))?;
let n = ready!(
Pin::new(&mut self.inner).poll_write(cx, &mut self.buf.bytes())
)?;
self.buf.advance(n);
}
}
}
@@ -290,25 +301,13 @@ impl<T, B> FramedWrite<T, B> {
}
impl<T: AsyncRead + Unpin, B> AsyncRead for FramedWrite<T, B> {
unsafe fn prepare_uninitialized_buffer(&self, buf: &mut [std::mem::MaybeUninit<u8>]) -> bool {
self.inner.prepare_uninitialized_buffer(buf)
}
fn poll_read(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut [u8],
) -> Poll<io::Result<usize>> {
buf: &mut ReadBuf,
) -> Poll<io::Result<()>> {
Pin::new(&mut self.inner).poll_read(cx, buf)
}
fn poll_read_buf<Buf: BufMut>(
mut self: Pin<&mut Self>,
cx: &mut Context<'_>,
buf: &mut Buf,
) -> Poll<io::Result<usize>> {
Pin::new(&mut self.inner).poll_read_buf(cx, buf)
}
}
// We never project the Pin to `B`.

View File

@@ -127,7 +127,7 @@ use std::pin::Pin;
use std::task::{Context, Poll};
use std::time::Duration;
use std::{convert, fmt, io, mem};
use tokio::io::{AsyncRead, AsyncWrite};
use tokio::io::{AsyncRead, AsyncWrite, ReadBuf};
use tracing_futures::{Instrument, Instrumented};
/// In progress HTTP/2.0 connection handshake future.
@@ -1158,8 +1158,10 @@ where
let mut rem = PREFACE.len() - self.pos;
while rem > 0 {
let n = ready!(Pin::new(self.inner_mut()).poll_read(cx, &mut buf[..rem]))
let mut buf = ReadBuf::new(&mut buf[..rem]);
ready!(Pin::new(self.inner_mut()).poll_read(cx, &mut buf))
.map_err(crate::Error::from_io)?;
let n = buf.filled().len();
if n == 0 {
return Poll::Ready(Err(crate::Error::from_io(io::Error::new(
io::ErrorKind::UnexpectedEof,
@@ -1167,7 +1169,7 @@ where
))));
}
if PREFACE[self.pos..self.pos + n] != buf[..n] {
if &PREFACE[self.pos..self.pos + n] != buf.filled() {
proto_err!(conn: "read_preface: invalid preface");
// TODO: Should this just write the GO_AWAY frame directly?
return Poll::Ready(Err(Reason::PROTOCOL_ERROR.into()));