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};
#[derive(Debug, Copy, Clone)]
pub struct DeflateConfig;
pub type DeflateOutput<S> = DeflateDecoder<DeflateEncoder<S>>;
impl UpgradeInfo for DeflateConfig {
type Info = &'static [u8];
type InfoIter = iter::Once<Self::Info>;
fn protocol_info(&self) -> Self::InfoIter {
iter::once(b"/deflate/1.0.0")
}
}
impl<C> InboundUpgrade<C> for DeflateConfig
where
C: AsyncRead + AsyncWrite,
{
type Output = DeflateOutput<Negotiated<C>>;
type Error = io::Error;
type Future = FutureResult<Self::Output, Self::Error>;
fn upgrade_inbound(self, r: Negotiated<C>, _: Self::Info) -> Self::Future {
future::ok(DeflateDecoder::new(DeflateEncoder::new(
r,
Compression::default(),
)))
}
}
impl<C> OutboundUpgrade<C> for DeflateConfig
where
C: AsyncRead + AsyncWrite,
{
type Output = DeflateOutput<Negotiated<C>>;
type Error = io::Error;
type Future = FutureResult<Self::Output, Self::Error>;
fn upgrade_outbound(self, w: Negotiated<C>, _: Self::Info) -> Self::Future {
future::ok(DeflateDecoder::new(DeflateEncoder::new(
w,
Compression::default(),
)))
}
}