Skip to main content

base_endpoint/tls/
tls_server_endpoint.rs

1// Released under the MIT License.
2// Copyright, 2026, by Samuel Williams.
3
4use super::TlsListener;
5use crate::{BindEndpoint, BindOptions};
6use base_scheduler::{Cancellation, OperationResult};
7
8/// Compose binding with a configured TLS server provider. Binding wraps listeners; each accepted connection performs its own handshake. Wrap a composite endpoint here to support multiple listening addresses.
9#[derive(Clone, Debug)]
10pub struct TlsServerEndpoint<E, T> {
11    endpoint: E,
12    acceptor: T,
13}
14
15impl<E, T> TlsServerEndpoint<E, T> {
16    /// Describe TLS listeners without performing I/O. The acceptor is cloned for each listener; providers can share immutable configuration internally.
17    pub fn new(endpoint: E, acceptor: T) -> Self {
18        Self { endpoint, acceptor }
19    }
20
21    /// The underlying endpoint description.
22    pub fn endpoint(&self) -> &E {
23        &self.endpoint
24    }
25}
26
27impl<S, E, T> BindEndpoint<S> for TlsServerEndpoint<E, T>
28where
29    S: Sync + ?Sized,
30    E: BindEndpoint<S>,
31    T: Clone + Send + Sync,
32{
33    type Listener = TlsListener<E::Listener, T>;
34
35    async fn bind(
36        &self,
37        scheduler: &S,
38        options: BindOptions,
39        cancellation: &Cancellation,
40    ) -> OperationResult<Vec<Self::Listener>> {
41        cancellation.check()?;
42        let listeners = self.endpoint.bind(scheduler, options, cancellation).await?;
43        Ok(listeners
44            .into_iter()
45            .map(|listener| TlsListener::new(listener, self.acceptor.clone()))
46            .collect())
47    }
48}