Update lib to std-future

This commit is contained in:
Gurwinder Singh
2019-08-15 08:25:14 +05:30
committed by Sean McArthur
parent 782f1f712c
commit c8fefd49f1
19 changed files with 1125 additions and 1038 deletions

View File

@@ -1,14 +1,13 @@
use super::{
store, Buffer, Codec, Config, Counts, Frame, Prioritize, Prioritized, Store, Stream, StreamId,
StreamIdOverflow, WindowSize,
};
use crate::codec::{RecvError, UserError};
use crate::frame::{self, Reason};
use super::{
store, Buffer, Codec, Config, Counts, Frame, Prioritize,
Prioritized, Store, Stream, StreamId, StreamIdOverflow, WindowSize,
};
use bytes::Buf;
use http;
use futures::{Async, Poll};
use futures::task::Task;
use std::task::{Context, Poll, Waker};
use tokio_io::AsyncWrite;
use std::io;
@@ -60,7 +59,7 @@ impl Send {
buffer: &mut Buffer<Frame<B>>,
stream: &mut store::Ptr,
counts: &mut Counts,
task: &mut Option<Task>,
task: &mut Option<Waker>,
) -> Result<(), UserError> {
log::trace!(
"send_headers; frame={:?}; init_window={:?}",
@@ -81,7 +80,6 @@ impl Send {
if te != "trailers" {
log::debug!("illegal connection-specific headers found");
return Err(UserError::MalformedHeaders);
}
}
@@ -103,7 +101,8 @@ impl Send {
}
// Queue the frame for sending
self.prioritize.queue_frame(frame.into(), buffer, stream, task);
self.prioritize
.queue_frame(frame.into(), buffer, stream, task);
Ok(())
}
@@ -115,7 +114,7 @@ impl Send {
buffer: &mut Buffer<Frame<B>>,
stream: &mut store::Ptr,
counts: &mut Counts,
task: &mut Option<Task>,
task: &mut Option<Waker>,
) {
let is_reset = stream.state.is_reset();
let is_closed = stream.state.is_closed();
@@ -125,7 +124,7 @@ impl Send {
"send_reset(..., reason={:?}, stream={:?}, ..., \
is_reset={:?}; is_closed={:?}; pending_send.is_empty={:?}; \
state={:?} \
",
",
reason,
stream.id,
is_reset,
@@ -151,7 +150,7 @@ impl Send {
if is_closed && is_empty {
log::trace!(
" -> not sending explicit RST_STREAM ({:?} was closed \
and send queue was flushed)",
and send queue was flushed)",
stream.id
);
return;
@@ -166,7 +165,8 @@ impl Send {
let frame = frame::Reset::new(stream.id, reason);
log::trace!("send_reset -- queueing; frame={:?}", frame);
self.prioritize.queue_frame(frame.into(), buffer, stream, task);
self.prioritize
.queue_frame(frame.into(), buffer, stream, task);
self.prioritize.reclaim_all_capacity(stream, counts);
}
@@ -175,7 +175,7 @@ impl Send {
stream: &mut store::Ptr,
reason: Reason,
counts: &mut Counts,
task: &mut Option<Task>,
task: &mut Option<Waker>,
) {
if stream.state.is_closed() {
// Stream is already closed, nothing more to do
@@ -194,11 +194,13 @@ impl Send {
buffer: &mut Buffer<Frame<B>>,
stream: &mut store::Ptr,
counts: &mut Counts,
task: &mut Option<Task>,
task: &mut Option<Waker>,
) -> Result<(), UserError>
where B: Buf,
where
B: Buf,
{
self.prioritize.send_data(frame, buffer, stream, counts, task)
self.prioritize
.send_data(frame, buffer, stream, counts, task)
}
pub fn send_trailers<B>(
@@ -207,7 +209,7 @@ impl Send {
buffer: &mut Buffer<Frame<B>>,
stream: &mut store::Ptr,
counts: &mut Counts,
task: &mut Option<Task>,
task: &mut Option<Waker>,
) -> Result<(), UserError> {
// TODO: Should this logic be moved into state.rs?
if !stream.state.is_send_streaming() {
@@ -221,7 +223,8 @@ impl Send {
stream.state.send_close();
log::trace!("send_trailers -- queuing; frame={:?}", frame);
self.prioritize.queue_frame(frame.into(), buffer, stream, task);
self.prioritize
.queue_frame(frame.into(), buffer, stream, task);
// Release any excess capacity
self.prioritize.reserve_capacity(0, stream, counts);
@@ -231,15 +234,18 @@ impl Send {
pub fn poll_complete<T, B>(
&mut self,
cx: &mut Context,
buffer: &mut Buffer<Frame<B>>,
store: &mut Store,
counts: &mut Counts,
dst: &mut Codec<T, Prioritized<B>>,
) -> Poll<(), io::Error>
where T: AsyncWrite,
B: Buf,
) -> Poll<io::Result<()>>
where
T: AsyncWrite + Unpin,
B: Buf + Unpin,
{
self.prioritize.poll_complete(buffer, store, counts, dst)
self.prioritize
.poll_complete(cx, buffer, store, counts, dst)
}
/// Request capacity to send data
@@ -247,27 +253,28 @@ impl Send {
&mut self,
capacity: WindowSize,
stream: &mut store::Ptr,
counts: &mut Counts)
{
counts: &mut Counts,
) {
self.prioritize.reserve_capacity(capacity, stream, counts)
}
pub fn poll_capacity(
&mut self,
cx: &Context,
stream: &mut store::Ptr,
) -> Poll<Option<WindowSize>, UserError> {
) -> Poll<Option<Result<WindowSize, UserError>>> {
if !stream.state.is_send_streaming() {
return Ok(Async::Ready(None));
return Poll::Ready(None);
}
if !stream.send_capacity_inc {
stream.wait_send();
return Ok(Async::NotReady);
stream.wait_send(cx);
return Poll::Pending;
}
stream.send_capacity_inc = false;
Ok(Async::Ready(Some(self.capacity(stream))))
Poll::Ready(Some(Ok(self.capacity(stream))))
}
/// Current available stream send capacity
@@ -284,15 +291,16 @@ impl Send {
pub fn poll_reset(
&self,
cx: &Context,
stream: &mut Stream,
mode: PollReset,
) -> Poll<Reason, crate::Error> {
) -> Poll<Result<Reason, crate::Error>> {
match stream.state.ensure_reason(mode)? {
Some(reason) => Ok(reason.into()),
Some(reason) => Poll::Ready(Ok(reason)),
None => {
stream.wait_send();
Ok(Async::NotReady)
},
stream.wait_send(cx);
Poll::Pending
}
}
}
@@ -312,14 +320,18 @@ impl Send {
buffer: &mut Buffer<Frame<B>>,
stream: &mut store::Ptr,
counts: &mut Counts,
task: &mut Option<Task>,
task: &mut Option<Waker>,
) -> Result<(), Reason> {
if let Err(e) = self.prioritize.recv_stream_window_update(sz, stream) {
log::debug!("recv_stream_window_update !!; err={:?}", e);
self.send_reset(
Reason::FLOW_CONTROL_ERROR.into(),
buffer, stream, counts, task);
buffer,
stream,
counts,
task,
);
return Err(e);
}
@@ -344,7 +356,7 @@ impl Send {
buffer: &mut Buffer<Frame<B>>,
store: &mut Store,
counts: &mut Counts,
task: &mut Option<Task>,
task: &mut Option<Waker>,
) -> Result<(), RecvError> {
// Applies an update to the remote endpoint's initial window size.
//
@@ -444,16 +456,14 @@ impl Send {
}
pub fn ensure_next_stream_id(&self) -> Result<StreamId, UserError> {
self.next_stream_id.map_err(|_| UserError::OverflowedStreamId)
self.next_stream_id
.map_err(|_| UserError::OverflowedStreamId)
}
pub fn may_have_created_stream(&self, id: StreamId) -> bool {
if let Ok(next_id) = self.next_stream_id {
// Peer::is_local_init should have been called beforehand
debug_assert_eq!(
id.is_server_initiated(),
next_id.is_server_initiated(),
);
debug_assert_eq!(id.is_server_initiated(), next_id.is_server_initiated(),);
id < next_id
} else {
true