Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions src/listener/tcp_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ pub struct TcpListener<State> {
listener: Option<net::TcpListener>,
server: Option<Server<State>>,
info: Option<ListenInfo>,
tcp_nodelay: bool,
}

impl<State> TcpListener<State> {
Expand All @@ -31,6 +32,7 @@ impl<State> TcpListener<State> {
listener: None,
server: None,
info: None,
tcp_nodelay: true,
}
}

Expand All @@ -40,12 +42,25 @@ impl<State> TcpListener<State> {
listener: Some(tcp_listener.into()),
server: None,
info: None,
tcp_nodelay: true,
}
}

/// Gets the value of the TCP_NODELAY option for tcp connections.
pub fn tcp_nodelay(&self) -> bool {
self.tcp_nodelay
}

/// Set the TCP_NODELAY option for tcp connections.
pub fn set_tcp_nodelay(&mut self, tcp_nodelay: bool) -> &mut Self {
self.tcp_nodelay = tcp_nodelay;
self
}
}

fn handle_tcp<State: Clone + Send + Sync + 'static>(app: Server<State>, stream: TcpStream) {
fn handle_tcp<State: Clone + Send + Sync + 'static>(app: Server<State>, stream: TcpStream, tcp_nodelay: bool) {
task::spawn(async move {
stream.set_nodelay(tcp_nodelay).ok();
let local_addr = stream.local_addr().ok();
let peer_addr = stream.peer_addr().ok();

Expand Down Expand Up @@ -111,7 +126,7 @@ where
}

Ok(stream) => {
handle_tcp(server.clone(), stream);
handle_tcp(server.clone(), stream, self.tcp_nodelay);
}
};
}
Expand Down