diff --git a/src/client.rs b/src/client.rs index d0c0b52..cd353f8 100644 --- a/src/client.rs +++ b/src/client.rs @@ -57,7 +57,7 @@ use self::encryption::wrap_stream; use protocol::WebSocket; use handshake::HandshakeError; use handshake::client::{ClientHandshake, Request}; -use stream::Mode; +use stream::{NoDelay, Mode}; use error::{Error, Result}; @@ -77,7 +77,8 @@ pub fn connect<'t, Req: Into>>(request: Req) -> Result f, diff --git a/src/stream.rs b/src/stream.rs index 2ef7466..7e8f32a 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -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 NoDelay for TlsStream { + fn set_nodelay(&mut self, nodelay: bool) -> IoResult<()> { + self.get_mut().set_nodelay(nodelay) + } +} + /// Stream, either plain TCP or TLS. pub enum Stream { Plain(S), @@ -42,3 +65,12 @@ impl Write for Stream { } } } + +impl NoDelay for Stream { + 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), + } + } +}