mirror of
https://github.com/fluencelabs/rust-libp2p
synced 2025-06-29 09:41:34 +00:00
Switch to stable futures (#1196)
* Switch to stable futures * Remove from_fn * Fix secio * Fix core --lib tests
This commit is contained in:
@ -18,21 +18,22 @@
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
|
||||
// DEALINGS IN THE SOFTWARE.
|
||||
|
||||
use flate2::read::DeflateDecoder;
|
||||
use flate2::write::DeflateEncoder;
|
||||
use flate2::Compression;
|
||||
use std::io;
|
||||
|
||||
use futures::future::{self, FutureResult};
|
||||
use libp2p_core::{upgrade::Negotiated, InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
||||
use std::iter;
|
||||
use tokio_io::{AsyncRead, AsyncWrite};
|
||||
use futures::{prelude::*, ready};
|
||||
use libp2p_core::{Negotiated, InboundUpgrade, OutboundUpgrade, UpgradeInfo};
|
||||
use std::{io, iter, pin::Pin, task::Context, task::Poll};
|
||||
|
||||
#[derive(Debug, Copy, Clone)]
|
||||
pub struct DeflateConfig;
|
||||
pub struct DeflateConfig {
|
||||
compression: flate2::Compression,
|
||||
}
|
||||
|
||||
/// Output of the deflate protocol.
|
||||
pub type DeflateOutput<S> = DeflateDecoder<DeflateEncoder<S>>;
|
||||
impl Default for DeflateConfig {
|
||||
fn default() -> Self {
|
||||
DeflateConfig {
|
||||
compression: flate2::Compression::fast(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl UpgradeInfo for DeflateConfig {
|
||||
type Info = &'static [u8];
|
||||
@ -49,13 +50,10 @@ where
|
||||
{
|
||||
type Output = DeflateOutput<Negotiated<C>>;
|
||||
type Error = io::Error;
|
||||
type Future = FutureResult<Self::Output, Self::Error>;
|
||||
type Future = future::Ready<Result<Self::Output, Self::Error>>;
|
||||
|
||||
fn upgrade_inbound(self, r: Negotiated<C>, _: Self::Info) -> Self::Future {
|
||||
future::ok(DeflateDecoder::new(DeflateEncoder::new(
|
||||
r,
|
||||
Compression::default(),
|
||||
)))
|
||||
future::ok(DeflateOutput::new(r, self.compression))
|
||||
}
|
||||
}
|
||||
|
||||
@ -65,12 +63,195 @@ where
|
||||
{
|
||||
type Output = DeflateOutput<Negotiated<C>>;
|
||||
type Error = io::Error;
|
||||
type Future = FutureResult<Self::Output, Self::Error>;
|
||||
type Future = future::Ready<Result<Self::Output, Self::Error>>;
|
||||
|
||||
fn upgrade_outbound(self, w: Negotiated<C>, _: Self::Info) -> Self::Future {
|
||||
future::ok(DeflateDecoder::new(DeflateEncoder::new(
|
||||
w,
|
||||
Compression::default(),
|
||||
)))
|
||||
future::ok(DeflateOutput::new(w, self.compression))
|
||||
}
|
||||
}
|
||||
|
||||
/// Decodes and encodes traffic using DEFLATE.
|
||||
pub struct DeflateOutput<S> {
|
||||
/// Inner stream where we read compressed data from and write compressed data to.
|
||||
inner: S,
|
||||
/// Internal object used to hold the state of the compression.
|
||||
compress: flate2::Compress,
|
||||
/// Internal object used to hold the state of the decompression.
|
||||
decompress: flate2::Decompress,
|
||||
/// Temporary buffer between `compress` and `inner`. Stores compressed bytes that need to be
|
||||
/// sent out once `inner` is ready to accept more.
|
||||
write_out: Vec<u8>,
|
||||
/// Temporary buffer between `decompress` and `inner`. Stores compressed bytes that need to be
|
||||
/// given to `decompress`.
|
||||
read_interm: Vec<u8>,
|
||||
/// When we read from `inner` and `Ok(0)` is returned, we set this to `true` so that we don't
|
||||
/// read from it again.
|
||||
inner_read_eof: bool,
|
||||
}
|
||||
|
||||
impl<S> DeflateOutput<S> {
|
||||
fn new(inner: S, compression: flate2::Compression) -> Self {
|
||||
DeflateOutput {
|
||||
inner,
|
||||
compress: flate2::Compress::new(compression, false),
|
||||
decompress: flate2::Decompress::new(false),
|
||||
write_out: Vec::with_capacity(256),
|
||||
read_interm: Vec::with_capacity(256),
|
||||
inner_read_eof: false,
|
||||
}
|
||||
}
|
||||
|
||||
/// Tries to write the content of `self.write_out` to `self.inner`.
|
||||
/// Returns `Ready(Ok(()))` if `self.write_out` is empty.
|
||||
fn flush_write_out(&mut self, cx: &mut Context) -> Poll<Result<(), io::Error>>
|
||||
where S: AsyncWrite + Unpin
|
||||
{
|
||||
loop {
|
||||
if self.write_out.is_empty() {
|
||||
return Poll::Ready(Ok(()))
|
||||
}
|
||||
|
||||
match AsyncWrite::poll_write(Pin::new(&mut self.inner), cx, &self.write_out) {
|
||||
Poll::Ready(Ok(0)) => return Poll::Ready(Err(io::ErrorKind::WriteZero.into())),
|
||||
Poll::Ready(Ok(n)) => self.write_out = self.write_out.split_off(n),
|
||||
Poll::Ready(Err(err)) => return Poll::Ready(Err(err)),
|
||||
Poll::Pending => return Poll::Pending,
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> AsyncRead for DeflateOutput<S>
|
||||
where S: AsyncRead + Unpin
|
||||
{
|
||||
fn poll_read(mut self: Pin<&mut Self>, cx: &mut Context, buf: &mut [u8]) -> Poll<Result<usize, io::Error>> {
|
||||
// We use a `this` variable because the compiler doesn't allow multiple mutable borrows
|
||||
// across a `Deref`.
|
||||
let this = &mut *self;
|
||||
|
||||
loop {
|
||||
// Read from `self.inner` into `self.read_interm` if necessary.
|
||||
if this.read_interm.is_empty() && !this.inner_read_eof {
|
||||
unsafe {
|
||||
this.read_interm.reserve(256);
|
||||
this.read_interm.set_len(this.read_interm.capacity());
|
||||
this.inner.initializer().initialize(&mut this.read_interm);
|
||||
}
|
||||
|
||||
match AsyncRead::poll_read(Pin::new(&mut this.inner), cx, &mut this.read_interm) {
|
||||
Poll::Ready(Ok(0)) => {
|
||||
this.inner_read_eof = true;
|
||||
this.read_interm.clear();
|
||||
}
|
||||
Poll::Ready(Ok(n)) => {
|
||||
this.read_interm.truncate(n)
|
||||
},
|
||||
Poll::Ready(Err(err)) => {
|
||||
this.read_interm.clear();
|
||||
return Poll::Ready(Err(err))
|
||||
},
|
||||
Poll::Pending => {
|
||||
this.read_interm.clear();
|
||||
return Poll::Pending
|
||||
},
|
||||
}
|
||||
}
|
||||
debug_assert!(!this.read_interm.is_empty() || this.inner_read_eof);
|
||||
|
||||
let before_out = this.decompress.total_out();
|
||||
let before_in = this.decompress.total_in();
|
||||
let ret = this.decompress.decompress(&this.read_interm, buf, if this.inner_read_eof { flate2::FlushDecompress::Finish } else { flate2::FlushDecompress::None })?;
|
||||
|
||||
// Remove from `self.read_interm` the bytes consumed by the decompressor.
|
||||
let consumed = (this.decompress.total_in() - before_in) as usize;
|
||||
this.read_interm = this.read_interm.split_off(consumed);
|
||||
|
||||
let read = (this.decompress.total_out() - before_out) as usize;
|
||||
if read != 0 || ret == flate2::Status::StreamEnd {
|
||||
return Poll::Ready(Ok(read))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe fn initializer(&self) -> futures::io::Initializer {
|
||||
futures::io::Initializer::nop()
|
||||
}
|
||||
}
|
||||
|
||||
impl<S> AsyncWrite for DeflateOutput<S>
|
||||
where S: AsyncWrite + Unpin
|
||||
{
|
||||
fn poll_write(mut self: Pin<&mut Self>, cx: &mut Context, buf: &[u8])
|
||||
-> Poll<Result<usize, io::Error>>
|
||||
{
|
||||
// We use a `this` variable because the compiler doesn't allow multiple mutable borrows
|
||||
// across a `Deref`.
|
||||
let this = &mut *self;
|
||||
|
||||
// We don't want to accumulate too much data in `self.write_out`, so we only proceed if it
|
||||
// is empty.
|
||||
ready!(this.flush_write_out(cx))?;
|
||||
|
||||
// We special-case this, otherwise an empty buffer would make the loop below infinite.
|
||||
if buf.is_empty() {
|
||||
return Poll::Ready(Ok(0));
|
||||
}
|
||||
|
||||
// Unfortunately, the compressor might be in a "flushing mode", not accepting any input
|
||||
// data. We don't want to return `Ok(0)` in that situation, as that would be wrong.
|
||||
// Instead, we invoke the compressor in a loop until it accepts some of our data.
|
||||
loop {
|
||||
let before_in = this.compress.total_in();
|
||||
this.write_out.reserve(256); // compress_vec uses the Vec's capacity
|
||||
let ret = this.compress.compress_vec(buf, &mut this.write_out, flate2::FlushCompress::None)?;
|
||||
let written = (this.compress.total_in() - before_in) as usize;
|
||||
|
||||
if written != 0 || ret == flate2::Status::StreamEnd {
|
||||
return Poll::Ready(Ok(written));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn poll_flush(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), io::Error>> {
|
||||
// We use a `this` variable because the compiler doesn't allow multiple mutable borrows
|
||||
// across a `Deref`.
|
||||
let this = &mut *self;
|
||||
|
||||
ready!(this.flush_write_out(cx))?;
|
||||
this.compress.compress_vec(&[], &mut this.write_out, flate2::FlushCompress::Sync)?;
|
||||
|
||||
loop {
|
||||
ready!(this.flush_write_out(cx))?;
|
||||
|
||||
debug_assert!(this.write_out.is_empty());
|
||||
// We ask the compressor to flush everything into `self.write_out`.
|
||||
this.write_out.reserve(256); // compress_vec uses the Vec's capacity
|
||||
this.compress.compress_vec(&[], &mut this.write_out, flate2::FlushCompress::None)?;
|
||||
if this.write_out.is_empty() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
AsyncWrite::poll_flush(Pin::new(&mut this.inner), cx)
|
||||
}
|
||||
|
||||
fn poll_close(mut self: Pin<&mut Self>, cx: &mut Context) -> Poll<Result<(), io::Error>> {
|
||||
// We use a `this` variable because the compiler doesn't allow multiple mutable borrows
|
||||
// across a `Deref`.
|
||||
let this = &mut *self;
|
||||
|
||||
loop {
|
||||
ready!(this.flush_write_out(cx))?;
|
||||
|
||||
// We ask the compressor to flush everything into `self.write_out`.
|
||||
debug_assert!(this.write_out.is_empty());
|
||||
this.write_out.reserve(256); // compress_vec uses the Vec's capacity
|
||||
this.compress.compress_vec(&[], &mut this.write_out, flate2::FlushCompress::Finish)?;
|
||||
if this.write_out.is_empty() {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
AsyncWrite::poll_close(Pin::new(&mut this.inner), cx)
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user