|
|
|
@ -6,6 +6,11 @@ |
|
|
|
|
|
|
|
|
|
use std::io::{Read, Write, Result as IoResult}; |
|
|
|
|
|
|
|
|
|
use std::net::TcpStream; |
|
|
|
|
|
|
|
|
|
#[cfg(feature="tls")] |
|
|
|
|
use native_tls::TlsStream; |
|
|
|
|
|
|
|
|
|
/// Stream mode, either plain TCP or TLS.
|
|
|
|
|
#[derive(Clone, Copy)] |
|
|
|
|
pub enum Mode { |
|
|
|
@ -13,6 +18,24 @@ pub enum Mode { |
|
|
|
|
Tls, |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Trait to switch TCP_NODELAY.
|
|
|
|
|
pub trait NoDelay { |
|
|
|
|
fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()>; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl NoDelay for TcpStream { |
|
|
|
|
fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> { |
|
|
|
|
TcpStream::set_nodelay(self, nodelay) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
#[cfg(feature="tls")] |
|
|
|
|
impl<S: Read + Write + NoDelay> NoDelay for TlsStream<S> { |
|
|
|
|
fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> { |
|
|
|
|
self.get_mut().set_nodelay(nodelay) |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/// Stream, either plain TCP or TLS.
|
|
|
|
|
pub enum Stream<S, T> { |
|
|
|
|
Plain(S), |
|
|
|
@ -42,3 +65,12 @@ impl<S: Write, T: Write> Write for Stream<S, T> { |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
impl<S: NoDelay, T: NoDelay> NoDelay for Stream<S, T> { |
|
|
|
|
fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> { |
|
|
|
|
match *self { |
|
|
|
|
Stream::Plain(ref mut s) => s.set_nodelay(nodelay), |
|
|
|
|
Stream::Tls(ref mut s) => s.set_nodelay(nodelay), |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|