2019-05-15 07:07:19 +00:00
|
|
|
// Implement simple flow control for h2 client
|
|
|
|
//
|
|
|
|
// See also: hyper/src/proto/h2/mod.rs
|
|
|
|
|
2019-08-22 11:46:31 +00:00
|
|
|
use bytes::Bytes;
|
2019-05-15 07:07:19 +00:00
|
|
|
use failure::*;
|
|
|
|
use futures::{try_ready, Async, Future, Poll};
|
2019-08-22 11:46:31 +00:00
|
|
|
use h2::SendStream;
|
2019-05-15 07:07:19 +00:00
|
|
|
|
|
|
|
pub struct PipeToSendStream {
|
|
|
|
body_tx: SendStream<Bytes>,
|
|
|
|
data: Option<Bytes>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl PipeToSendStream {
|
|
|
|
pub fn new(data: Bytes, tx: SendStream<Bytes>) -> PipeToSendStream {
|
|
|
|
PipeToSendStream {
|
|
|
|
body_tx: tx,
|
|
|
|
data: Some(data),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Future for PipeToSendStream {
|
|
|
|
type Item = ();
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Self::Item, Self::Error> {
|
|
|
|
loop {
|
|
|
|
if self.data != None {
|
2019-05-16 08:24:23 +00:00
|
|
|
// just reserve 1 byte to make sure there's some
|
|
|
|
// capacity available. h2 will handle the capacity
|
|
|
|
// management for the actual body chunk.
|
2019-05-15 07:07:19 +00:00
|
|
|
self.body_tx.reserve_capacity(1);
|
|
|
|
|
|
|
|
if self.body_tx.capacity() == 0 {
|
|
|
|
loop {
|
|
|
|
match try_ready!(self.body_tx.poll_capacity().map_err(Error::from)) {
|
|
|
|
Some(0) => {}
|
|
|
|
Some(_) => break,
|
|
|
|
None => return Err(format_err!("protocol canceled")),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if let Async::Ready(reason) = self.body_tx.poll_reset().map_err(Error::from)? {
|
|
|
|
return Err(format_err!("stream received RST_STREAM: {:?}", reason));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.body_tx
|
|
|
|
.send_data(self.data.take().unwrap(), true)
|
|
|
|
.map_err(Error::from)?;
|
|
|
|
|
|
|
|
return Ok(Async::Ready(()));
|
|
|
|
} else {
|
|
|
|
if let Async::Ready(reason) = self.body_tx.poll_reset().map_err(Error::from)? {
|
|
|
|
return Err(format_err!("stream received RST_STREAM: {:?}", reason));
|
|
|
|
}
|
|
|
|
return Ok(Async::Ready(()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|