Skip to content

Commit

Permalink
Move TCP_NODELAY and TTL options to TlsListenerBuilder.
Browse files Browse the repository at this point in the history
  • Loading branch information
fiag committed May 24, 2021
1 parent 83a1948 commit ae009b5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
34 changes: 4 additions & 30 deletions src/tls_listener.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,13 +49,13 @@ impl<State> Debug for TlsListener<State> {
}

impl<State> TlsListener<State> {
pub(crate) fn new(connection: TcpConnection, config: TlsListenerConfig) -> Self {
pub(crate) fn new(connection: TcpConnection, config: TlsListenerConfig, tcp_nodelay: Option<bool>, tcp_ttl: Option<u32>) -> Self {
Self {
connection,
config,
server: None,
tcp_nodelay: None,
tcp_ttl: None,
tcp_nodelay,
tcp_ttl,
}
}
/// The primary entrypoint to create a TlsListener. See
Expand Down Expand Up @@ -129,32 +129,6 @@ impl<State> TlsListener<State> {
}
Ok(())
}

/// Set TCP_NODELAY socket option.
pub fn set_nodelay(&mut self, nodelay: bool) {
self.tcp_nodelay = Some(nodelay);
}

/// Get TCP_NODELAY socket option.
pub fn nodelay(&self) -> Option<bool> {
self.tcp_nodelay
}

/// Set TCP_NODELAY socket option.
pub fn with_nodelay(mut self, nodelay: bool) -> Self {
self.set_nodelay(nodelay);
self
}

/// Set TTL option.
pub fn set_ttl(&mut self, ttl: u32) {
self.tcp_ttl = Some(ttl);
}

/// Get TTL option.
pub fn ttl(&self) -> Option<u32> {
self.tcp_ttl
}
}

fn handle_tls<State: Clone + Send + Sync + 'static>(
Expand Down Expand Up @@ -243,7 +217,7 @@ impl<State: Clone + Send + Sync + 'static> Listener<State> for TlsListener<State
}

handle_tls(server.clone(), stream, acceptor.clone())
},
}
};
}
Ok(())
Expand Down
20 changes: 19 additions & 1 deletion src/tls_listener_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ pub struct TlsListenerBuilder<State> {
tls_acceptor: Option<Arc<dyn CustomTlsAcceptor>>,
tcp: Option<TcpListener>,
addrs: Option<Vec<SocketAddr>>,
tcp_nodelay: Option<bool>,
tcp_ttl: Option<u32>,
_state: PhantomData<State>,
}

Expand All @@ -54,6 +56,8 @@ impl<State> Default for TlsListenerBuilder<State> {
tls_acceptor: None,
tcp: None,
addrs: None,
tcp_nodelay: None,
tcp_ttl: None,
_state: PhantomData,
}
}
Expand Down Expand Up @@ -148,6 +152,18 @@ impl<State> TlsListenerBuilder<State> {
self
}

/// Provides a TCP_NODELAY option for this tls listener.
pub fn nodelay(mut self, nodelay: bool) -> Self {
self.tcp_nodelay = Some(nodelay);
self
}

/// Provides a TTL option for this tls listener.
pub fn ttl(mut self, ttl: u32) -> Self {
self.tcp_ttl = Some(ttl);
self
}

/// finishes building a TlsListener from this TlsListenerBuilder.
///
/// # Errors
Expand All @@ -168,6 +184,8 @@ impl<State> TlsListenerBuilder<State> {
tls_acceptor,
tcp,
addrs,
tcp_nodelay,
tcp_ttl,
..
} = self;

Expand All @@ -194,6 +212,6 @@ impl<State> TlsListenerBuilder<State> {
}
};

Ok(TlsListener::new(connection, config))
Ok(TlsListener::new(connection, config, tcp_nodelay, tcp_ttl))
}
}

0 comments on commit ae009b5

Please sign in to comment.