Move TCP_NODELAY and TTL options to TlsListenerBuilder.

This commit is contained in:
fiag 2021-05-24 11:59:37 +08:00
parent 83a1948dfd
commit ae009b5d89
2 changed files with 23 additions and 31 deletions

View File

@ -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
@ -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>(
@ -243,7 +217,7 @@ impl<State: Clone + Send + Sync + 'static> Listener<State> for TlsListener<State
}
handle_tls(server.clone(), stream, acceptor.clone())
},
}
};
}
Ok(())

View File

@ -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>,
}
@ -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,
}
}
@ -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
@ -168,6 +184,8 @@ impl<State> TlsListenerBuilder<State> {
tls_acceptor,
tcp,
addrs,
tcp_nodelay,
tcp_ttl,
..
} = self;
@ -194,6 +212,6 @@ impl<State> TlsListenerBuilder<State> {
}
};
Ok(TlsListener::new(connection, config))
Ok(TlsListener::new(connection, config, tcp_nodelay, tcp_ttl))
}
}