1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
use crate::{
client::{Client, ClientProviders, DefaultProviders},
provider::*,
};
/// A builder for configuring [`Client`] providers
#[derive(Debug)]
pub struct Builder<Providers>(pub(crate) Providers);
impl Default for Builder<DefaultProviders> {
fn default() -> Self {
Self(Default::default())
}
}
impl<Providers: ClientProviders> Builder<Providers> {
impl_provider_method!(
/// Sets the connection ID provider for the [`Client`]
///
/// # Examples
///
/// Uses the default connection ID provider with the default configuration.
///
/// ```rust,no_run
/// # use std::{error::Error, time::Duration};
/// use s2n_quic::{Client, provider::connection_id};
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn Error>> {
/// let client = Client::builder()
/// .with_connection_id(connection_id::Default::default())?
/// .start()?;
/// #
/// # Ok(())
/// # }
/// ```
///
/// Sets a custom connection ID provider for the client
///
/// ```rust,no_run
/// # use std::{error::Error, time::Duration};
/// use s2n_quic::{Client, provider::connection_id};
/// use rand::prelude::*;
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn Error>> {
/// struct MyConnectionIdFormat;
///
/// impl connection_id::Generator for MyConnectionIdFormat {
/// fn generate(&mut self, _conn_info: &connection_id::ConnectionInfo) -> connection_id::LocalId {
/// let mut id = [0u8; 16];
/// rand::thread_rng().fill_bytes(&mut id);
/// connection_id::LocalId::try_from_bytes(&id[..]).unwrap()
/// }
/// }
///
/// impl connection_id::Validator for MyConnectionIdFormat {
/// fn validate(&self, _conn_info: &connection_id::ConnectionInfo, packet: &[u8]) -> Option<usize> {
/// // this connection id format is always 16 bytes
/// Some(16)
/// }
/// }
///
/// let client = Client::builder()
/// .with_connection_id(MyConnectionIdFormat)?
/// .start()?;
/// #
/// # Ok(())
/// # }
/// ```
with_connection_id,
connection_id,
ClientProviders
);
impl_provider_method!(
/// Sets the stateless reset token provider for the [`Client`]
///
/// # Examples
///
/// Sets a custom stateless reset token provider for the client
///
/// ```rust,ignore
/// # use std::error::Error;
/// use s2n_quic::Client;
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn Error>> {
/// let client = Client::builder()
/// .with_stateless_reset_token(MyStatelessResetTokenGenerator::new())?
/// .start()?;
/// #
/// # Ok(())
/// # }
/// ```
with_stateless_reset_token,
stateless_reset_token,
ClientProviders
);
impl_provider_method!(
/// Sets the limits provider for the [`Client`]
///
/// # Examples
///
/// Sets the max idle time, while inheriting the remaining default limits
///
/// ```rust,no_run
/// # use std::{error::Error, time::Duration};
/// use s2n_quic::{Client, provider::limits};
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn Error>> {
/// let client = Client::builder()
/// .with_limits(limits::Default::default())?
/// .start()?;
/// #
/// # Ok(())
/// # }
/// ```
with_limits,
limits,
ClientProviders
);
impl_provider_method!(
/// Sets the event provider for the [`Client`]
///
/// # Examples
///
/// Sets a custom event subscriber for the client
///
/// ```rust,ignore
/// # use std::{error::Error, time::Duration};
/// use s2n_quic::Client;
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn Error>> {
/// let client = Client::builder()
/// .with_event(MyEventLogger::new())?
/// .start()?;
/// #
/// # Ok(())
/// # }
/// ```
with_event,
event,
ClientProviders
);
impl_provider_method!(
/// Sets the IO provider for the [`Client`]
///
/// # Examples
///
/// Opens a socket on an address chosen by the operating system
///
/// ```rust,no_run
/// # use std::error::Error;
/// use s2n_quic::Client;
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn Error>> {
/// let client = Client::builder()
/// .with_io("0.0.0.0:0")?
/// .start()?;
/// #
/// # Ok(())
/// # }
/// ```
///
/// Configures a socket with the provided `Builder`
///
/// ```rust,no_run
/// # use std::error::Error;
/// use s2n_quic::{Client, provider::io::tokio::Builder as IoBuilder};
/// use std::net::ToSocketAddrs;
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn Error>> {
/// let addr = "0.0.0.0:0".to_socket_addrs()?.next().unwrap();
///
/// let io = IoBuilder::default()
/// .with_receive_address(addr)?
/// .build()?;
///
/// let client = Client::builder()
/// .with_io(io)?
/// .start()?;
/// #
/// # Ok(())
/// # }
/// ```
with_io,
io,
ClientProviders
);
impl_provider_method!(
/// Sets the TLS provider for the [`Client`]
///
/// # Examples
///
/// The default TLS provider and configuration will be used with the
/// path to the private key.
///
/// ```rust,no_run
/// # use std::{error::Error, path::Path};
/// # use s2n_quic::Client;
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn Error>> {
/// let client = Client::builder()
/// .with_tls(Path::new("./certs/cert.pem"))?
/// .start()?;
/// #
/// # Ok(())
/// # }
/// ```
///
/// Sets the TLS provider to a TLS client builder
///
/// ```rust,ignore
/// # use std::{error::Error, path::Path};
/// # use s2n_quic::Client;
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn Error>> {
/// let tls = s2n_tls::Client::builder()
/// .with_certificate(Path::new("./certs/cert.pem"))?
/// .with_security_policy(s2n::tls::security_policy::S2N_20190802)?;
///
/// let client = Client::builder()
/// .with_tls(tls)?
/// .start()?;
/// #
/// # Ok(())
/// # }
/// ```
with_tls,
tls,
ClientProviders
);
#[cfg(any(test, feature = "unstable-provider-packet-interceptor"))]
impl_provider_method!(
/// Sets the packet interceptor provider for the [`Client`]
with_packet_interceptor,
packet_interceptor,
ClientProviders
);
#[cfg(any(test, feature = "unstable-provider-random"))]
impl_provider_method!(
/// Sets the random provider for the [`Client`]
with_random,
random,
ClientProviders
);
#[cfg(feature = "unstable-provider-datagram")]
impl_provider_method!(
/// Sets the datagram provider for the [`Client`]
with_datagram,
datagram,
ClientProviders
);
impl_provider_method!(
/// Sets the congestion controller provider for the [`Client`]
with_congestion_controller,
congestion_controller,
ClientProviders
);
/// Starts the [`Client`] with the configured providers
///
/// # Examples
///
/// ```rust,no_run
/// # use std::{error::Error, path::Path};
/// # use s2n_quic::Client;
/// #
/// # #[tokio::main]
/// # async fn main() -> Result<(), Box<dyn Error>> {
/// let client = Client::builder()
/// .with_tls(Path::new("./certs/cert.pem"))?
/// .with_io("0.0.0.0:0")?
/// .start()?;
/// #
/// # Ok(())
/// # }
/// ```
pub fn start(self) -> Result<Client, StartError> {
self.0.build().start()
}
}