From f235c9d064fbb2cc7d51a0c921ee632103dd9567 Mon Sep 17 00:00:00 2001 From: mat Date: Sat, 29 Oct 2022 13:54:19 -0500 Subject: [PATCH] don't panic if connection closes on join --- azalea-client/src/client.rs | 83 +++++++++++++++++-------------------- 1 file changed, 38 insertions(+), 45 deletions(-) diff --git a/azalea-client/src/client.rs b/azalea-client/src/client.rs index 2ca92354..05461f0f 100644 --- a/azalea-client/src/client.rs +++ b/azalea-client/src/client.rs @@ -182,55 +182,48 @@ impl Client { .await?; let (conn, game_profile) = loop { - let packet_result = conn.read().await; - match packet_result { - Ok(packet) => match packet { - ClientboundLoginPacket::Hello(p) => { - debug!("Got encryption request"); - let e = azalea_crypto::encrypt(&p.public_key, &p.nonce).unwrap(); + let packet = conn.read().await?; + match packet { + ClientboundLoginPacket::Hello(p) => { + debug!("Got encryption request"); + let e = azalea_crypto::encrypt(&p.public_key, &p.nonce).unwrap(); - if let Some(access_token) = &account.access_token { - conn.authenticate( - access_token, - &account - .uuid - .expect("Uuid must be present if access token is present."), - e.secret_key, - p, - ) - .await?; - } - - conn.write( - ServerboundKeyPacket { - nonce_or_salt_signature: NonceOrSaltSignature::Nonce( - e.encrypted_nonce, - ), - key_bytes: e.encrypted_public_key, - } - .get(), + if let Some(access_token) = &account.access_token { + conn.authenticate( + access_token, + &account + .uuid + .expect("Uuid must be present if access token is present."), + e.secret_key, + p, ) .await?; + } - conn.set_encryption_key(e.secret_key); - } - ClientboundLoginPacket::LoginCompression(p) => { - debug!("Got compression request {:?}", p.compression_threshold); - conn.set_compression_threshold(p.compression_threshold); - } - ClientboundLoginPacket::GameProfile(p) => { - debug!("Got profile {:?}", p.game_profile); - break (conn.game(), p.game_profile); - } - ClientboundLoginPacket::LoginDisconnect(p) => { - debug!("Got disconnect {:?}", p); - } - ClientboundLoginPacket::CustomQuery(p) => { - debug!("Got custom query {:?}", p); - } - }, - Err(e) => { - panic!("Error: {e:?}"); + conn.write( + ServerboundKeyPacket { + nonce_or_salt_signature: NonceOrSaltSignature::Nonce(e.encrypted_nonce), + key_bytes: e.encrypted_public_key, + } + .get(), + ) + .await?; + + conn.set_encryption_key(e.secret_key); + } + ClientboundLoginPacket::LoginCompression(p) => { + debug!("Got compression request {:?}", p.compression_threshold); + conn.set_compression_threshold(p.compression_threshold); + } + ClientboundLoginPacket::GameProfile(p) => { + debug!("Got profile {:?}", p.game_profile); + break (conn.game(), p.game_profile); + } + ClientboundLoginPacket::LoginDisconnect(p) => { + debug!("Got disconnect {:?}", p); + } + ClientboundLoginPacket::CustomQuery(p) => { + debug!("Got custom query {:?}", p); } } };