From b910178f35e0188d38f605fbcd49714efea08edf Mon Sep 17 00:00:00 2001 From: Alex Emmet <111742636+Alex-Emmet@users.noreply.github.com> Date: Thu, 21 May 2026 23:26:16 +0200 Subject: [PATCH] Reconnection --- communities/src/community.rs | 4 +-- communities/src/community_connection.rs | 32 ++++++++++----------- communities/src/interactables/category.rs | 2 +- communities/src/interactables/text_chat.rs | 2 +- communities/src/interactables/voice_chat.rs | 2 +- omikron-connector/src/omikron_connection.rs | 15 +++++++--- 6 files changed, 32 insertions(+), 25 deletions(-) diff --git a/communities/src/community.rs b/communities/src/community.rs index 835c88a..fb79ba8 100644 --- a/communities/src/community.rs +++ b/communities/src/community.rs @@ -210,7 +210,7 @@ impl Community { for interactable in target_interactables.iter() { if interactable.get_name() == name { if interactable.get_codec() == "category" { - return CommunicationValue::new(CommunicationType::error); + return CommunicationValue::new(CommunicationType::error_internal); } else { // cannot move a value of type dyn Interactable the size of dyn Interactable cannot be statically determined (rustc E0161) return interactable.run_function(cv.clone()).await; @@ -231,7 +231,7 @@ impl Community { .run_function(cv.clone()) .await; } else { - return CommunicationValue::new(CommunicationType::error); + return CommunicationValue::new(CommunicationType::error_internal); } } } diff --git a/communities/src/community_connection.rs b/communities/src/community_connection.rs index 62df251..5ed5ef8 100644 --- a/communities/src/community_connection.rs +++ b/communities/src/community_connection.rs @@ -167,7 +167,7 @@ impl CommunityConnection { }; let Some(community) = self.community.read().await.clone() else { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_internal) .await; return; }; @@ -178,7 +178,7 @@ impl CommunityConnection { let shared_secret = match community_private_key.to_diffie_hellman(&user_pub_key) { Some(secret) => secret, _ => { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_internal) .await; return; } @@ -200,7 +200,7 @@ impl CommunityConnection { let encrypted_challenge = match cipher.encrypt(nonce, challenge_str.as_bytes()) { Ok(data) => data, Err(_) => { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_internal) .await; return; } @@ -223,7 +223,7 @@ impl CommunityConnection { let client_challenge_response_b64 = match cv.get_data(DataTypes::challenge) { Some(data) => data.to_string(), _ => { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_data) .await; return; } @@ -232,38 +232,38 @@ impl CommunityConnection { let challenge_response_bytes = match STANDARD.decode(&client_challenge_response_b64) { Ok(bytes) => bytes, Err(_) => { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_data) .await; return; } }; if challenge_response_bytes.len() < 12 { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_data) .await; return; } let Some(user) = self.auth.read().await.clone() else { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_internal) .await; return; }; let Some(user_pub_bytes) = STANDARD.decode(&user.public_key).ok() else { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_data) .await; return; }; let Some(user_pub_key) = PublicKey::from_bytes(&user_pub_bytes) else { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_public_key) .await; return; }; let Some(community) = self.community.read().await.clone() else { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_internal) .await; return; }; @@ -273,7 +273,7 @@ impl CommunityConnection { let shared_secret = match community_private_key.to_diffie_hellman(&user_pub_key) { Some(secret) => secret, _ => { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_internal) .await; return; } @@ -297,7 +297,7 @@ impl CommunityConnection { let decrypted_bytes = match cipher.decrypt(nonce, ciphertext) { Ok(pt) => pt, Err(_) => { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_challenge) .await; return; } @@ -306,7 +306,7 @@ impl CommunityConnection { let client_response = match String::from_utf8(decrypted_bytes) { Ok(str) => str, Err(_) => { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_data) .await; return; } @@ -315,7 +315,7 @@ impl CommunityConnection { let expected_challenge = self.challenge.read().await.clone(); if client_response != expected_challenge { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_challenge) .await; self.close().await; return; @@ -327,7 +327,7 @@ impl CommunityConnection { } let Some(community) = self.community.read().await.clone() else { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_internal) .await; return; }; @@ -335,7 +335,7 @@ impl CommunityConnection { let user_id = self.get_user_id().await; if user_id == 0 { - self.send_error_response(&cv.get_id(), CommunicationType::error) + self.send_error_response(&cv.get_id(), CommunicationType::error_invalid_user_id) .await; return; } diff --git a/communities/src/interactables/category.rs b/communities/src/interactables/category.rs index 6b6415f..c709014 100644 --- a/communities/src/interactables/category.rs +++ b/communities/src/interactables/category.rs @@ -95,7 +95,7 @@ impl Interactable for Category { v } async fn run_function(&self, _cv: CommunicationValue) -> CommunicationValue { - CommunicationValue::new(CommunicationType::error) + CommunicationValue::new(CommunicationType::error_internal) } fn to_json(&self) -> JsonValue { let mut v = JsonValue::new_object(); diff --git a/communities/src/interactables/text_chat.rs b/communities/src/interactables/text_chat.rs index b4d0392..25eb192 100644 --- a/communities/src/interactables/text_chat.rs +++ b/communities/src/interactables/text_chat.rs @@ -243,7 +243,7 @@ impl Interactable for TextChat { .add_data_str(DataTypes::result, "message_received".to_string()) .add_data(DataTypes::payload, JsonValue::new_object()); } - CommunicationValue::new(CommunicationType::error).with_id(cv.get_id()) + CommunicationValue::new(CommunicationType::error_internal).with_id(cv.get_id()) } fn to_json(&self) -> JsonValue { JsonValue::new_object() diff --git a/communities/src/interactables/voice_chat.rs b/communities/src/interactables/voice_chat.rs index 2a2e3a2..8cf0589 100644 --- a/communities/src/interactables/voice_chat.rs +++ b/communities/src/interactables/voice_chat.rs @@ -164,7 +164,7 @@ impl Interactable for VoiceChat { .add_data_str(DataTypes::result, "user_changed".to_string()) .add_data(DataTypes::payload, response_payload); } - CommunicationValue::new(CommunicationType::error).with_id(cv.get_id()) + CommunicationValue::new(CommunicationType::error_internal).with_id(cv.get_id()) } fn to_json(&self) -> JsonValue { diff --git a/omikron-connector/src/omikron_connection.rs b/omikron-connector/src/omikron_connection.rs index 558e628..e0c6b9c 100755 --- a/omikron-connector/src/omikron_connection.rs +++ b/omikron-connector/src/omikron_connection.rs @@ -536,7 +536,7 @@ impl OmikronConnection { } } - let res = CommunicationValue::new(CommunicationType::error) + let res = CommunicationValue::new(CommunicationType::error_invalid_challenge) .with_id(cv.get_id()) .with_receiver(sender_id); self.send_message(&res).await; @@ -558,7 +558,7 @@ impl OmikronConnection { } } } - let res = CommunicationValue::new(CommunicationType::error) + let res = CommunicationValue::new(CommunicationType::error_invalid_challenge) .with_id(cv.get_id()) .with_receiver(sender_id); self.send_message(&res).await; @@ -1475,7 +1475,7 @@ impl OmikronConnection { for key in keys { if let Some((_, waiting_task)) = WAITING_TASKS.remove(&key) { - let response = CommunicationValue::new(CommunicationType::error) + let response = CommunicationValue::new(CommunicationType::error_internal) .with_id(key) .add_data(DataTypes::message, DataValue::Str(reason.clone())); let _ = (waiting_task.task)(OMIKRON_CONNECTION.clone(), response); @@ -1525,7 +1525,14 @@ impl OmikronConnection { match tokio::time::timeout(timeout, rx.recv()).await { Ok(Some(response_cv)) => { - if response_cv.is_type(CommunicationType::error) { + let resp_type = response_cv.get_type(); + let is_error = resp_type == CommunicationType::error + || resp_type == CommunicationType::error_internal + || resp_type == CommunicationType::error_not_found + || resp_type == CommunicationType::error_invalid_data + || resp_type == CommunicationType::error_invalid_challenge + || resp_type == CommunicationType::error_not_authenticated; + if is_error { let reason = response_cv .get_data(DataTypes::message) .as_str()