Skip to content

Commit

Permalink
Create some auxiliary methods to shorten up the code
Browse files Browse the repository at this point in the history
  • Loading branch information
rakivo committed Dec 22, 2024
1 parent cb48d13 commit 87f195d
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 38 deletions.
26 changes: 13 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@ name = "droppa"
path = "back/main.rs"

[dependencies]
paste = { version = "=1.0.15" default-features = false }
qrcodegen = { version = "=1.8.0" default-features = false }
serde_json = { version = "=1.0.133" default-features = false }
actix-files = { version = "=0.6.6" default-features = false }
env_logger = { version = "=0.11.5", default-features = false }
futures-util = { version = "=0.3.31", default-features = false }
actix-multipart = { version = "=0.5.0", default-features = false }
actix-web = { version = "=4.9.0", default-features = false, features = ["macros"] }
zip = { version = "=2.2.1", default-features = false, features = ["deflate"] }
tokio-stream = { version = "=0.1.17", features = ["sync"] }
tokio = { version = "=1.42.0", features = ["sync"] }
serde = { version = "=1.0.216", features = ["derive"] }
dashmap = { version = "=6.1.0", features = ["inline"] }
paste = { version = "=1.0.15", default-features = false }
qrcodegen = { version = "=1.8.0", default-features = false }
serde_json = { version = "=1.0.133", default-features = false }
actix-files = { version = "=0.6.6", default-features = false }
env_logger = { version = "=0.11.5", default-features = false }
futures-util = { version = "=0.3.31", default-features = false }
actix-multipart = { version = "=0.5.0", default-features = false }
actix-web = { version = "=4.9.0", default-features = false, features = ["macros"] }
zip = { version = "=2.2.1", default-features = false, features = ["deflate"] }
tokio-stream = { version = "=0.1.17", features = ["sync"] }
tokio = { version = "=1.42.0", features = ["sync"] }
serde = { version = "=1.0.216", features = ["derive"] }
dashmap = { version = "=6.1.0", features = ["inline"] }
47 changes: 22 additions & 25 deletions back/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fs;
use std::future::Future;
use std::net::{IpAddr, UdpSocket};
use std::sync::{Arc, Mutex, MutexGuard};
use std::io::{Cursor, Write, BufWriter};
Expand Down Expand Up @@ -31,8 +32,8 @@ macro_rules! atomic_type {

macro_rules! lock_fn {
($($field: tt), *) => { $(paste::paste! {
#[inline]
#[track_caller]
#[inline(always)]
fn [<lock_ $field>](&self) -> MutexGuard::<[<$field:camel>]> {
self.$field.lock().unwrap()
}
Expand Down Expand Up @@ -168,6 +169,22 @@ struct Server {
}

impl Server {
#[inline(always)]
fn lock_sender(&self, mobile: bool) -> impl Future::<Output = tokio::sync::MutexGuard::<Option::<watch::Sender::<String>>>> {
if mobile {
self.mobile_files_progress_sender.lock()
} else {
self.desktop_files_progress_sender.lock()
}
}

#[inline(always)]
async fn sender_send(&self, json: String, mobile: bool) {
if let Err(e) = self.lock_sender(mobile).await.as_ref().expect("SENDER IS NOT INITIALIZED").send(json) {
eprintln!("[FATAL] could not send JSON: {e}")
}
}

lock_fn! { files }
}

Expand Down Expand Up @@ -342,15 +359,9 @@ async fn stream_progress(state: Data::<Server>, mobile: bool) -> impl Responder
};

{
let files_progress_sender = &mut if mobile {
state.mobile_files_progress_sender.lock()
} else {
state.desktop_files_progress_sender.lock()
}.await;

let files_progress_sender = &mut state.lock_sender(mobile).await;
if files_progress_sender.is_some() {
files_progress_sender.as_ref().unwrap().send("Connection replaced".to_owned()).unwrap();

state.sender_send("Connection replaced".to_owned(), mobile);
**files_progress_sender = Some(ptx);
return HttpResponse::Ok()
.append_header(("Content-Type", "text/event-stream"))
Expand All @@ -372,14 +383,7 @@ async fn stream_progress(state: Data::<Server>, mobile: bool) -> impl Responder
}).collect::<Vec::<_>>();

let json = serde_json::to_string(&data).unwrap();

if let Err(e) = if mobile {
state.mobile_files_progress_sender.lock()
} else {
state.desktop_files_progress_sender.lock()
}.await.as_ref().unwrap().send(json) {
eprintln!("[FATAL] could not send JSON: {e}")
}
state.sender_send(json, mobile);

let state = Data::clone(&state);
actix_rt::spawn(async move {
Expand All @@ -394,14 +398,7 @@ async fn stream_progress(state: Data::<Server>, mobile: bool) -> impl Responder
}).collect::<Vec::<_>>();

let json = serde_json::to_string(&data).unwrap();

if let Err(e) = if mobile {
state.mobile_files_progress_sender.lock()
} else {
state.desktop_files_progress_sender.lock()
}.await.as_ref().unwrap().send(json) {
eprintln!("[FATAL] could not send JSON: {e}")
}
state.sender_send(json, mobile)
}
});

Expand Down

0 comments on commit 87f195d

Please sign in to comment.