stream: set TCP_NODELAY on connect

Signed-off-by: Alexey Galakhov <agalakhov@snapview.de>
pull/13/head
Alexey Galakhov 8 years ago
parent f696771069
commit 686bc51b84
  1. 5
      src/client.rs
  2. 32
      src/stream.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<'t>>>(request: Req) -> Result<WebSocket<Aut
let request: Request = request.into();
let mode = url_mode(&request.url)?;
let addrs = request.url.to_socket_addrs()?;
let stream = connect_to_some(addrs, &request.url, mode)?;
let mut stream = connect_to_some(addrs, &request.url, mode)?;
stream.set_nodelay(true)?;
client(request, stream)
.map_err(|e| match e {
HandshakeError::Failure(f) => f,

@ -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),
}
}
}

Loading…
Cancel
Save