From f5f78c84d552f35f86410b2271944e74e44fe612 Mon Sep 17 00:00:00 2001 From: lc Date: Thu, 13 Nov 2025 23:06:40 +0800 Subject: [PATCH] remove unwraps --- examples/webrtc.rs | 14 ++++++++------ src/webrtc.rs | 14 +++++++++++++- 2 files changed, 21 insertions(+), 7 deletions(-) diff --git a/examples/webrtc.rs b/examples/webrtc.rs index 317a7f5..3b9162c 100644 --- a/examples/webrtc.rs +++ b/examples/webrtc.rs @@ -102,13 +102,15 @@ async fn read_loop(mut stream: hbb_common::Stream) -> Result<()> { println!("WebRTC stream closed; Exit the read_loop"); return Ok(()); }; - if res.is_err() { - println!("WebRTC stream read error: {}; Exit the read_loop", res.err().unwrap()); - return Ok(()); + match res { + Err(e) => { + println!("WebRTC stream read error: {}; Exit the read_loop", e); + return Ok(()); + } + Ok(data) => { + println!("Message from stream: {}", String::from_utf8(data.to_vec())?); + } } - println!("Message from stream: {}", - String::from_utf8(res.unwrap().to_vec())? - ); } } diff --git a/src/webrtc.rs b/src/webrtc.rs index 3a3ed01..e805970 100644 --- a/src/webrtc.rs +++ b/src/webrtc.rs @@ -105,7 +105,19 @@ impl WebRTCStream { let contains = lock.contains_key(&key); if contains { log::debug!("Start webrtc with cached peer"); - return Ok(lock.get(&key).unwrap().clone()); + return Ok(lock[&key].clone()); + } + // ...existing code... + Self::get_remote_offer(remote_endpoint)? + }; + + let key = remote_offer.clone(); + let mut lock = SESSIONS.lock().await; + if let Some(cached_stream) = lock.get(&key) { + if !key.is_empty() { + log::debug!("Start webrtc with cached peer"); + return Ok(cached_stream.clone()); + } } // Create a SettingEngine and enable Detach