yumush
Clone
https://git.tahinli.com/Tahinli/yumush.git
0 watching·0 forks·Rust·AGPL-3.0·10.6 MiB·02871e1·Tahinli·1 hour ago·415 commits
| 1 | use std::{net::SocketAddr, path::Path, sync::Arc, time::Duration}; |
| 2 | |
| 3 | use bitcode::{DecodeOwned, Encode}; |
| 4 | use common::{ |
| 5 | call::Call, |
| 6 | constant::{ATTACHMENT_CHUNK_BYTES, MAXIMUM_ATTACHMENT_LIMIT_BYTES}, |
| 7 | error::{Error, ReleaseProblem}, |
| 8 | frame::{self, DOWN_MAX, Sending, UP_MAX}, |
| 9 | model::{Attachment, Id, Staged, User}, |
| 10 | open::{Blob, Open, Stage, Upload}, |
| 11 | session::{Hello, Welcome}, |
| 12 | transport::{ALPN, Close, transport}, |
| 13 | update::{FILE_MAX, target_of}, |
| 14 | validate::{validate_attachment_name, validate_attachment_size}, |
| 15 | }; |
| 16 | use quinn::rustls::{ |
| 17 | DigitallySignedStruct, SignatureScheme, |
| 18 | client::danger::{HandshakeSignatureValid, ServerCertVerified, ServerCertVerifier}, |
| 19 | pki_types::{CertificateDer, ServerName, UnixTime}, |
| 20 | }; |
| 21 | use quinn::{Endpoint, crypto::rustls::QuicClientConfig, rustls}; |
| 22 | use tokio::{ |
| 23 | io::AsyncReadExt, |
| 24 | sync::{mpsc, oneshot, watch}, |
| 25 | }; |
| 26 | use tracing::{info, warn}; |
| 27 | |
| 28 | use super::{Fail, LinkError, NetworkEvent, SignIn, media::Media, receive}; |
| 29 | |
| 30 | const CHANNEL_SIZE: usize = 32; |
| 31 | const BACKOFF_BASE_MILLIS: u64 = 250; |
| 32 | const BACKOFF_CAP_MILLIS: u64 = 8_000; |
| 33 | const UPDATE_CHECK: Duration = Duration::from_secs(5); |
| 34 | |
| 35 | pub fn start(server: crate::Server) -> (super::NetworkHandle, mpsc::Receiver<NetworkEvent>) { |
| 36 | let (sign_in, sign_in_receiver) = mpsc::channel(CHANNEL_SIZE); |
| 37 | let (server, server_receiver) = watch::channel(server); |
| 38 | let (event_sender, event_receiver) = mpsc::channel(CHANNEL_SIZE); |
| 39 | let runtime = tokio::runtime::Builder::new_current_thread() |
| 40 | .enable_all() |
| 41 | .build() |
| 42 | .expect("network runtime"); |
| 43 | let media = Media::new(runtime.handle().clone()); |
| 44 | |
| 45 | std::thread::Builder::new() |
| 46 | .name("network".to_string()) |
| 47 | .spawn({ |
| 48 | let media = media.clone(); |
| 49 | move || { |
| 50 | runtime.block_on(actor( |
| 51 | server_receiver, |
| 52 | sign_in_receiver, |
| 53 | event_sender, |
| 54 | media, |
| 55 | )) |
| 56 | } |
| 57 | }) |
| 58 | .expect("network thread"); |
| 59 | |
| 60 | ( |
| 61 | super::NetworkHandle { |
| 62 | sign_in, |
| 63 | server, |
| 64 | media, |
| 65 | fetches: Arc::default(), |
| 66 | }, |
| 67 | event_receiver, |
| 68 | ) |
| 69 | } |
| 70 | |
| 71 | #[derive(Default)] |
| 72 | struct Endpoints { |
| 73 | v6: Option<Endpoint>, |
| 74 | v4: Option<Endpoint>, |
| 75 | } |
| 76 | |
| 77 | impl Endpoints { |
| 78 | fn get(&mut self, address: SocketAddr) -> Result<Endpoint, LinkError> { |
| 79 | let (slot, bind) = match address { |
| 80 | SocketAddr::V6(_) => (&mut self.v6, "[::]:0"), |
| 81 | SocketAddr::V4(_) => (&mut self.v4, "0.0.0.0:0"), |
| 82 | }; |
| 83 | if let Some(endpoint) = slot { |
| 84 | return Ok(endpoint.clone()); |
| 85 | } |
| 86 | |
| 87 | let bind: SocketAddr = bind.parse().map_err(|_| LinkError::Unreachable)?; |
| 88 | let endpoint = common::transport::endpoint(bind, quinn::EndpointConfig::default(), None) |
| 89 | .map_err(|_| LinkError::Unreachable)?; |
| 90 | *slot = Some(endpoint.clone()); |
| 91 | |
| 92 | Ok(endpoint) |
| 93 | } |
| 94 | |
| 95 | fn forget(&mut self, address: SocketAddr) { |
| 96 | match address { |
| 97 | SocketAddr::V6(_) => self.v6 = None, |
| 98 | SocketAddr::V4(_) => self.v4 = None, |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | async fn backoff(attempt: &mut u32) { |
| 104 | let base = (BACKOFF_BASE_MILLIS << (*attempt).min(10)).min(BACKOFF_CAP_MILLIS); |
| 105 | let nanos = std::time::SystemTime::now() |
| 106 | .duration_since(std::time::UNIX_EPOCH) |
| 107 | .map_or(0, |elapsed| elapsed.subsec_nanos() as u64); |
| 108 | let jitter = base * (80 + nanos % 41) / 100; |
| 109 | |
| 110 | tokio::time::sleep(Duration::from_millis(jitter)).await; |
| 111 | *attempt += 1; |
| 112 | } |
| 113 | |
| 114 | pub(crate) async fn dial<T>( |
| 115 | server: &crate::Server, |
| 116 | alpn: &[u8], |
| 117 | work: impl AsyncFnOnce(&quinn::Connection) -> T, |
| 118 | ) -> Result<T, LinkError> { |
| 119 | let mut endpoints = Endpoints::default(); |
| 120 | let connection = connect(server, &mut endpoints, alpn).await?; |
| 121 | let done = work(&connection).await; |
| 122 | connection.close(0u32.into(), b""); |
| 123 | for endpoint in [endpoints.v4, endpoints.v6].into_iter().flatten() { |
| 124 | endpoint.wait_idle().await; |
| 125 | } |
| 126 | Ok(done) |
| 127 | } |
| 128 | |
| 129 | async fn connect( |
| 130 | server: &crate::Server, |
| 131 | endpoints: &mut Endpoints, |
| 132 | alpn: &[u8], |
| 133 | ) -> Result<quinn::Connection, LinkError> { |
| 134 | let mut addresses: Vec<SocketAddr> = tokio::net::lookup_host(server.address.as_str()) |
| 135 | .await |
| 136 | .map_err(|_| LinkError::Unreachable)? |
| 137 | .collect(); |
| 138 | addresses.sort_by_key(|address| address.is_ipv4()); |
| 139 | |
| 140 | let mut last = LinkError::Unreachable; |
| 141 | for address in addresses { |
| 142 | let endpoint = match endpoints.get(address) { |
| 143 | Ok(endpoint) => endpoint, |
| 144 | Err(error) => { |
| 145 | last = error; |
| 146 | continue; |
| 147 | } |
| 148 | }; |
| 149 | let connecting = |
| 150 | match endpoint.connect_with(config(server.any_cert, alpn)?, address, server.host()) { |
| 151 | Ok(connecting) => connecting, |
| 152 | Err(_) => { |
| 153 | endpoints.forget(address); |
| 154 | last = LinkError::Unreachable; |
| 155 | continue; |
| 156 | } |
| 157 | }; |
| 158 | match connecting.await { |
| 159 | Ok(connection) => return Ok(connection), |
| 160 | Err(error) => { |
| 161 | endpoints.forget(address); |
| 162 | last = error.into(); |
| 163 | if last == LinkError::Outdated { |
| 164 | return Err(last); |
| 165 | } |
| 166 | } |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | Err(last) |
| 171 | } |
| 172 | |
| 173 | fn tls_error(_: impl std::fmt::Display) -> LinkError { |
| 174 | LinkError::Tls |
| 175 | } |
| 176 | |
| 177 | #[derive(Debug)] |
| 178 | struct AcceptAnyCertificate { |
| 179 | provider: Arc<rustls::crypto::CryptoProvider>, |
| 180 | } |
| 181 | |
| 182 | impl ServerCertVerifier for AcceptAnyCertificate { |
| 183 | fn verify_server_cert( |
| 184 | &self, |
| 185 | _end_entity: &CertificateDer<'_>, |
| 186 | _intermediates: &[CertificateDer<'_>], |
| 187 | _server_name: &ServerName<'_>, |
| 188 | _ocsp_response: &[u8], |
| 189 | _now: UnixTime, |
| 190 | ) -> Result<ServerCertVerified, rustls::Error> { |
| 191 | Ok(ServerCertVerified::assertion()) |
| 192 | } |
| 193 | |
| 194 | fn verify_tls12_signature( |
| 195 | &self, |
| 196 | message: &[u8], |
| 197 | cert: &CertificateDer<'_>, |
| 198 | dss: &DigitallySignedStruct, |
| 199 | ) -> Result<HandshakeSignatureValid, rustls::Error> { |
| 200 | rustls::crypto::verify_tls12_signature( |
| 201 | message, |
| 202 | cert, |
| 203 | dss, |
| 204 | &self.provider.signature_verification_algorithms, |
| 205 | ) |
| 206 | } |
| 207 | |
| 208 | fn verify_tls13_signature( |
| 209 | &self, |
| 210 | message: &[u8], |
| 211 | cert: &CertificateDer<'_>, |
| 212 | dss: &DigitallySignedStruct, |
| 213 | ) -> Result<HandshakeSignatureValid, rustls::Error> { |
| 214 | rustls::crypto::verify_tls13_signature( |
| 215 | message, |
| 216 | cert, |
| 217 | dss, |
| 218 | &self.provider.signature_verification_algorithms, |
| 219 | ) |
| 220 | } |
| 221 | |
| 222 | fn supported_verify_schemes(&self) -> Vec<SignatureScheme> { |
| 223 | self.provider |
| 224 | .signature_verification_algorithms |
| 225 | .supported_schemes() |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | fn config(any_cert: bool, alpn: &[u8]) -> Result<quinn::ClientConfig, LinkError> { |
| 230 | let provider = Arc::new(rustls::crypto::ring::default_provider()); |
| 231 | let verifier: Arc<dyn ServerCertVerifier> = if any_cert { |
| 232 | Arc::new(AcceptAnyCertificate { |
| 233 | provider: provider.clone(), |
| 234 | }) |
| 235 | } else { |
| 236 | Arc::new(rustls_platform_verifier::Verifier::new(provider.clone()).map_err(tls_error)?) |
| 237 | }; |
| 238 | |
| 239 | let mut tls = rustls::ClientConfig::builder_with_provider(provider) |
| 240 | .with_protocol_versions(&[&rustls::version::TLS13]) |
| 241 | .map_err(tls_error)? |
| 242 | .dangerous() |
| 243 | .with_custom_certificate_verifier(verifier) |
| 244 | .with_no_client_auth(); |
| 245 | tls.alpn_protocols = vec![alpn.to_vec()]; |
| 246 | |
| 247 | let crypto = QuicClientConfig::try_from(tls).map_err(tls_error)?; |
| 248 | let mut client_config = quinn::ClientConfig::new(Arc::new(crypto)); |
| 249 | client_config.transport_config(Arc::new(transport())); |
| 250 | |
| 251 | Ok(client_config) |
| 252 | } |
| 253 | |
| 254 | async fn exchange<R: DecodeOwned>( |
| 255 | connection: &quinn::Connection, |
| 256 | request: &impl Encode, |
| 257 | ) -> Result<(R, quinn::RecvStream), Fail> { |
| 258 | let (send_stream, mut receive_stream) = connection.open_bi().await?; |
| 259 | let mut send_stream = Sending::new(send_stream); |
| 260 | frame::write(&mut send_stream, request, UP_MAX).await?; |
| 261 | send_stream.finish()?; |
| 262 | |
| 263 | let reply = frame::read::<Result<R, Error>>(&mut receive_stream, DOWN_MAX).await??; |
| 264 | |
| 265 | Ok((reply, receive_stream)) |
| 266 | } |
| 267 | |
| 268 | async fn send_body<R: DecodeOwned>( |
| 269 | connection: &quinn::Connection, |
| 270 | header: &impl Encode, |
| 271 | body: impl AsyncFnOnce(&mut Sending) -> Result<(), Fail>, |
| 272 | ) -> Result<R, Fail> { |
| 273 | let (send_stream, mut receive_stream) = connection.open_bi().await?; |
| 274 | let mut send_stream = Sending::new(send_stream); |
| 275 | frame::write(&mut send_stream, header, UP_MAX).await?; |
| 276 | |
| 277 | let write = async move { |
| 278 | body(&mut send_stream).await?; |
| 279 | send_stream.finish()?; |
| 280 | Ok::<(), Fail>(()) |
| 281 | }; |
| 282 | let read = frame::read::<Result<R, Error>>(&mut receive_stream, DOWN_MAX); |
| 283 | let (sent, reply) = tokio::join!(write, read); |
| 284 | |
| 285 | match (sent, reply) { |
| 286 | (_, Ok(reply)) => Ok(reply?), |
| 287 | (Err(error), _) => Err(error), |
| 288 | (_, Err(error)) => Err(error.into()), |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | pub(super) async fn call<C: Call>( |
| 293 | connection: &quinn::Connection, |
| 294 | call: C, |
| 295 | ) -> Result<C::Reply, Fail> { |
| 296 | Ok( |
| 297 | exchange(connection, &Open::Call(Box::new(call.into_request()))) |
| 298 | .await? |
| 299 | .0, |
| 300 | ) |
| 301 | } |
| 302 | |
| 303 | async fn hello(connection: &quinn::Connection, token: Option<String>) -> Result<Welcome, Fail> { |
| 304 | let hello = Hello { |
| 305 | token, |
| 306 | device: super::device_name(), |
| 307 | install: crate::update::install_id(), |
| 308 | }; |
| 309 | |
| 310 | Ok(exchange(connection, &hello).await?.0) |
| 311 | } |
| 312 | |
| 313 | pub(super) async fn upload( |
| 314 | connection: &quinn::Connection, |
| 315 | community_id: Id, |
| 316 | path: &Path, |
| 317 | maximum: Option<u64>, |
| 318 | ) -> Result<Attachment, Fail> { |
| 319 | let name = path |
| 320 | .file_name() |
| 321 | .map(|name| name.to_string_lossy().into_owned()) |
| 322 | .unwrap_or_default(); |
| 323 | validate_attachment_name(&name)?; |
| 324 | |
| 325 | let mut file = tokio::fs::File::open(path).await?; |
| 326 | let size = file.metadata().await?.len(); |
| 327 | if let Some(maximum) = maximum { |
| 328 | validate_attachment_size(size, maximum)?; |
| 329 | } |
| 330 | |
| 331 | let header = Open::Upload(Upload { |
| 332 | community_id, |
| 333 | name, |
| 334 | size, |
| 335 | }); |
| 336 | send_body(connection, &header, async |sending: &mut Sending| { |
| 337 | let mut buffer = vec![0u8; ATTACHMENT_CHUNK_BYTES]; |
| 338 | loop { |
| 339 | let read = file.read(&mut buffer).await?; |
| 340 | if read == 0 { |
| 341 | return Ok(()); |
| 342 | } |
| 343 | sending.get().write_all(&buffer[..read]).await?; |
| 344 | } |
| 345 | }) |
| 346 | .await |
| 347 | } |
| 348 | |
| 349 | pub(super) async fn stage( |
| 350 | connection: &quinn::Connection, |
| 351 | path: &Path, |
| 352 | progress: &watch::Sender<u64>, |
| 353 | ) -> Result<Staged, Fail> { |
| 354 | let bytes = tokio::fs::read(path).await?; |
| 355 | if bytes.len() as u64 > FILE_MAX { |
| 356 | return Err(Error::Release(ReleaseProblem::TooLarge).into()); |
| 357 | } |
| 358 | let (bytes, target) = tokio::task::spawn_blocking(move || { |
| 359 | let target = target_of(&bytes); |
| 360 | (bytes, target) |
| 361 | }) |
| 362 | .await |
| 363 | .map_err(|_| Fail::Protocol)?; |
| 364 | if target.is_none() { |
| 365 | return Err(Error::Release(ReleaseProblem::NotClient).into()); |
| 366 | } |
| 367 | |
| 368 | let header = Open::Stage(Stage { |
| 369 | size: bytes.len() as u64, |
| 370 | }); |
| 371 | send_body(connection, &header, async |sending: &mut Sending| { |
| 372 | for (index, chunk) in bytes.chunks(ATTACHMENT_CHUNK_BYTES).enumerate() { |
| 373 | sending.get().write_all(chunk).await?; |
| 374 | progress.send_replace(((index * ATTACHMENT_CHUNK_BYTES) + chunk.len()) as u64); |
| 375 | } |
| 376 | Ok(()) |
| 377 | }) |
| 378 | .await |
| 379 | } |
| 380 | |
| 381 | pub(super) async fn backup_download( |
| 382 | connection: &quinn::Connection, |
| 383 | name: &str, |
| 384 | path: &Path, |
| 385 | ) -> Result<(), Fail> { |
| 386 | let (length, mut receive_stream) = |
| 387 | exchange::<u64>(connection, &Open::Fetch(Blob::Backup(name.to_string()))).await?; |
| 388 | |
| 389 | let part = path.with_added_extension("part"); |
| 390 | |
| 391 | let copied = async { |
| 392 | let mut file = tokio::fs::File::create(&part).await?; |
| 393 | let copied = tokio::io::copy(&mut receive_stream, &mut file).await?; |
| 394 | if copied != length { |
| 395 | return Err(Fail::Protocol); |
| 396 | } |
| 397 | file.sync_all().await?; |
| 398 | tokio::fs::rename(&part, path).await?; |
| 399 | Ok(()) |
| 400 | } |
| 401 | .await; |
| 402 | if copied.is_err() { |
| 403 | let _ = tokio::fs::remove_file(&part).await; |
| 404 | } |
| 405 | |
| 406 | copied |
| 407 | } |
| 408 | |
| 409 | pub(super) async fn download(connection: &quinn::Connection, blob: &Blob) -> Result<Vec<u8>, Fail> { |
| 410 | let (length, mut receive_stream) = |
| 411 | exchange::<u64>(connection, &Open::Fetch(blob.clone())).await?; |
| 412 | |
| 413 | if length > MAXIMUM_ATTACHMENT_LIMIT_BYTES { |
| 414 | return Err(Fail::Protocol); |
| 415 | } |
| 416 | |
| 417 | let mut bytes = vec![0u8; length as usize]; |
| 418 | receive_stream.read_exact(&mut bytes).await?; |
| 419 | |
| 420 | Ok(bytes) |
| 421 | } |
| 422 | |
| 423 | fn rejects(token: &Option<String>, pending: bool, error: &Fail) -> bool { |
| 424 | let Fail::Server(error) = error else { |
| 425 | return false; |
| 426 | }; |
| 427 | |
| 428 | token.is_some() && (pending || matches!(error, Error::Unauthenticated | Error::NotFound)) |
| 429 | } |
| 430 | |
| 431 | async fn halt(sign_in: &mut mpsc::Receiver<SignIn>) { |
| 432 | while sign_in.recv().await.is_some() {} |
| 433 | } |
| 434 | |
| 435 | async fn actor( |
| 436 | mut server: watch::Receiver<crate::Server>, |
| 437 | mut sign_in: mpsc::Receiver<SignIn>, |
| 438 | events: mpsc::Sender<NetworkEvent>, |
| 439 | media: Arc<Media>, |
| 440 | ) { |
| 441 | let mut token: Option<String> = None; |
| 442 | let mut pending: Option<oneshot::Sender<Result<Option<User>, Fail>>> = None; |
| 443 | let mut endpoints = Endpoints::default(); |
| 444 | let mut attempt = 0u32; |
| 445 | 'session: loop { |
| 446 | let connection = loop { |
| 447 | let address = server.borrow_and_update().clone(); |
| 448 | let result = tokio::select! { |
| 449 | result = connect(&address, &mut endpoints, ALPN) => result, |
| 450 | _ = server.changed() => { |
| 451 | attempt = 0; |
| 452 | token = None; |
| 453 | continue; |
| 454 | } |
| 455 | }; |
| 456 | if matches!(result, Err(LinkError::Outdated)) { |
| 457 | let _ = |
| 458 | tokio::time::timeout(UPDATE_CHECK, crate::update::check(address, true)).await; |
| 459 | } |
| 460 | match result { |
| 461 | Ok(connection) => break connection, |
| 462 | Err(error) => { |
| 463 | warn!("connect failed: {error}"); |
| 464 | |
| 465 | if events |
| 466 | .send(NetworkEvent::Disconnected(error)) |
| 467 | .await |
| 468 | .is_err() |
| 469 | { |
| 470 | return; |
| 471 | } |
| 472 | |
| 473 | tokio::select! { |
| 474 | _ = backoff(&mut attempt) => {} |
| 475 | _ = server.changed() => { |
| 476 | attempt = 0; |
| 477 | token = None; |
| 478 | } |
| 479 | } |
| 480 | } |
| 481 | }; |
| 482 | }; |
| 483 | |
| 484 | let welcome = match hello(&connection, token.clone()).await { |
| 485 | Ok(welcome) => welcome, |
| 486 | Err(error) => { |
| 487 | warn!("hello failed: {error}"); |
| 488 | if rejects(&token, pending.is_some(), &error) { |
| 489 | token = None; |
| 490 | Close::Normal.on(&connection); |
| 491 | match pending.take() { |
| 492 | Some(reply) => { |
| 493 | let _ = reply.send(Err(error)); |
| 494 | } |
| 495 | None => { |
| 496 | media.reset(); |
| 497 | if events.send(NetworkEvent::SessionLost).await.is_err() { |
| 498 | return; |
| 499 | } |
| 500 | } |
| 501 | } |
| 502 | continue 'session; |
| 503 | } |
| 504 | |
| 505 | let reason = connection |
| 506 | .close_reason() |
| 507 | .map_or(LinkError::Reset, LinkError::from); |
| 508 | Close::Protocol.on(&connection); |
| 509 | if reason.terminal() { |
| 510 | if let Some(reply) = pending.take() { |
| 511 | let _ = reply.send(Err(error)); |
| 512 | } |
| 513 | let _ = events.send(NetworkEvent::Disconnected(reason)).await; |
| 514 | halt(&mut sign_in).await; |
| 515 | return; |
| 516 | } |
| 517 | |
| 518 | tokio::select! { |
| 519 | _ = backoff(&mut attempt) => {} |
| 520 | _ = server.changed() => { |
| 521 | attempt = 0; |
| 522 | token = None; |
| 523 | } |
| 524 | } |
| 525 | continue 'session; |
| 526 | } |
| 527 | }; |
| 528 | |
| 529 | attempt = 0; |
| 530 | info!("connected"); |
| 531 | crate::update::set_token(token.clone().filter(|_| welcome.user.is_some())); |
| 532 | tokio::spawn(crate::update::check(server.borrow().clone(), false)); |
| 533 | media.ready(connection.clone(), events.clone()); |
| 534 | if events.send(NetworkEvent::Connected).await.is_err() { |
| 535 | return; |
| 536 | } |
| 537 | if let Some(reply) = pending.take() { |
| 538 | let _ = reply.send(Ok(welcome.user)); |
| 539 | } |
| 540 | |
| 541 | tokio::spawn(receive::events( |
| 542 | connection.clone(), |
| 543 | events.clone(), |
| 544 | media.clone(), |
| 545 | )); |
| 546 | tokio::spawn(receive::datagrams(connection.clone(), media.clone())); |
| 547 | |
| 548 | tokio::select! { |
| 549 | sign = sign_in.recv() => match sign { |
| 550 | Some(SignIn { token: next, reply }) => { |
| 551 | token = next; |
| 552 | pending = Some(reply); |
| 553 | media.reset(); |
| 554 | Close::Normal.on(&connection); |
| 555 | continue 'session; |
| 556 | } |
| 557 | None => break 'session, |
| 558 | }, |
| 559 | |
| 560 | _ = server.changed() => { |
| 561 | attempt = 0; |
| 562 | token = None; |
| 563 | media.reset(); |
| 564 | Close::Normal.on(&connection); |
| 565 | continue 'session; |
| 566 | } |
| 567 | |
| 568 | error = connection.closed() => { |
| 569 | warn!("disconnected: {error}"); |
| 570 | media.lost(); |
| 571 | |
| 572 | let event = LinkError::from(error); |
| 573 | let terminal = event.terminal(); |
| 574 | |
| 575 | if events.send(NetworkEvent::Disconnected(event)).await.is_err() { |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | if terminal { |
| 580 | halt(&mut sign_in).await; |
| 581 | return; |
| 582 | } |
| 583 | |
| 584 | backoff(&mut attempt).await; |
| 585 | continue 'session; |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | } |