Depot API & executor launch (#173)

* feat: depot api downloads

* feat: frontend fixes and experimental webview store

* feat: sync downloader

* feat: cleanup and fixes

* feat: encrypted database and fixed resuming

* feat: launch option selector

* fix: autostart when no options

* fix: clippy

* fix: clippy x2

* feat: executor launch

* feat: executor launch

* feat: not installed error handling

* feat: better offline handling

* feat: dependency popup

* fix: cancelation and resuming issues

* feat: dedup by platform

* feat: new ui for additional components and fix dl manager clog

* feat: auto-queue dependencies

* feat: depot scanning and ranking

* feat: new library fetching stack

* In-app store page (Windows + macOS) (#176)

* feat: async store loading

* feat: fix overscroll behaviour

* fix: query params in server protocol

* fix: clippy
This commit is contained in:
DecDuck
2026-01-20 00:40:48 +00:00
committed by GitHub
parent 55fdaf51e1
commit fc69ae30ab
72 changed files with 3430 additions and 2732 deletions
@@ -0,0 +1,144 @@
use std::{
collections::HashMap,
sync::RwLock,
time::{Duration, Instant},
};
use futures_util::StreamExt;
use remote::{
error::RemoteAccessError,
requests::{generate_url, make_authenticated_get},
utils::DROP_CLIENT_ASYNC,
};
use serde::Deserialize;
use tauri::Url;
use crate::util::semaphore::{SyncSemaphore, SyncSemaphorePermit};
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct DepotManifestContent {
version_id: String,
//compression: String,
}
#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
struct DepotManifest {
content: HashMap<String, Vec<DepotManifestContent>>,
}
struct Depot {
endpoint: String,
manifest: Option<DepotManifest>,
latest_speed: Option<usize>, // bytes per second
current_downloads: SyncSemaphore,
}
pub struct DepotManager {
depots: RwLock<Vec<Depot>>,
}
#[derive(Deserialize)]
struct ServersideDepot {
endpoint: String,
}
const SPEEDTEST_TIMEOUT: Duration = Duration::from_secs(4);
impl DepotManager {
pub fn new() -> Self {
Self {
depots: RwLock::new(Vec::new()),
}
}
pub async fn sync_depots(&self) -> Result<(), RemoteAccessError> {
let depots = make_authenticated_get(generate_url(&["/api/v1/client/depots"], &[])?).await?;
let depots: Vec<ServersideDepot> = depots.json().await?;
let mut new_depots = depots
.into_iter()
.map(|depot| Depot {
endpoint: if depot.endpoint.ends_with("/") {
depot.endpoint
} else {
format!("{}/", depot.endpoint)
},
manifest: None,
latest_speed: None,
current_downloads: SyncSemaphore::new(),
})
.collect::<Vec<Depot>>();
for depot in &mut new_depots {
let manifest_url = Url::parse(&depot.endpoint)?.join("manifest.json")?;
let manifest = DROP_CLIENT_ASYNC.get(manifest_url).send().await?;
let manifest: DepotManifest = manifest.json().await?;
depot.manifest.replace(manifest);
let speedtest_url = Url::parse(&depot.endpoint)?.join("speedtest")?;
let speedtest = DROP_CLIENT_ASYNC.get(speedtest_url).send().await?;
let mut stream = speedtest.bytes_stream();
let start = Instant::now();
let mut total_length = 0;
while let Some(chunk) = stream.next().await {
let length = chunk?.len();
total_length += length;
if SPEEDTEST_TIMEOUT <= start.elapsed() {
break;
}
}
let elapsed = start.elapsed().as_millis() as usize;
let speed = if elapsed == 0 { usize::MAX } else { (total_length / elapsed) * 1000 };
depot.latest_speed.replace(speed);
}
let mut depot_lock = self.depots.write().unwrap();
*depot_lock = new_depots;
Ok(())
}
pub fn next_depot(
&self,
game_id: &str,
version_id: &str,
) -> Result<(String, SyncSemaphorePermit), RemoteAccessError> {
let lock = self.depots.read().unwrap();
let best_depot = lock
.iter()
.filter(|v| {
let manifest = match &v.manifest {
Some(v) => v,
None => return false,
};
let versions = match manifest.content.get(game_id) {
Some(v) => v,
None => return false,
};
let _version = match versions.iter().find(|v| v.version_id == version_id) {
Some(v) => v,
None => return false,
};
true
})
.max_by(|x, y| {
let x_speed = x.latest_speed.unwrap_or(0) / x.current_downloads.permits();
let y_speed = y.latest_speed.unwrap_or(0) / y.current_downloads.permits();
x_speed.cmp(&y_speed)
})
.ok_or(RemoteAccessError::NoDepots)?;
Ok((
best_depot.endpoint.clone(),
best_depot.current_downloads.acquire(),
))
}
}
@@ -1,18 +1,19 @@
use core::panic;
use std::{
collections::HashMap,
sync::{
Arc, Mutex,
mpsc::{Receiver, Sender, channel},
},
thread::{JoinHandle, spawn},
sync::{Arc, Mutex},
time::Duration,
};
use database::DownloadableMetadata;
use log::{debug, error, info, warn};
use tauri::AppHandle;
use tauri::{AppHandle, async_runtime::JoinHandle};
use tokio::sync::mpsc::{Receiver, Sender};
use tokio::{sync::mpsc, time::timeout};
use utils::{app_emit, lock, send};
use crate::{
depot_manager::DepotManager,
download_manager_frontend::DownloadStatus,
error::ApplicationDownloadError,
frontend_updates::{QueueUpdateEvent, QueueUpdateEventQueueData, StatsUpdateEvent},
@@ -83,10 +84,11 @@ pub struct DownloadManagerBuilder {
impl DownloadManagerBuilder {
pub fn build(app_handle: AppHandle) -> DownloadManager {
let queue = Queue::new();
let (command_sender, command_receiver) = channel();
let (command_sender, command_receiver) = mpsc::channel(1500);
let active_progress = Arc::new(Mutex::new(None));
let status = Arc::new(Mutex::new(DownloadManagerStatus::Empty));
let depot_manager = Arc::new(DepotManager::new());
let manager = Self {
download_agent_registry: HashMap::new(),
download_queue: queue.clone(),
@@ -100,74 +102,87 @@ impl DownloadManagerBuilder {
active_control_flag: None,
};
let terminator = spawn(|| manager.manage_queue());
let terminator = tauri::async_runtime::spawn(async move {
let result = manager.manage_queue().await;
info!("download manager exited with result: {:?}", result);
});
DownloadManager::new(terminator, queue, active_progress, command_sender)
DownloadManager::new(terminator, queue, active_progress, command_sender, depot_manager)
}
fn set_status(&self, status: DownloadManagerStatus) {
*lock!(self.status) = status;
}
fn remove_and_cleanup_front_download(&mut self, meta: &DownloadableMetadata) -> DownloadAgent {
async fn remove_and_cleanup_front_download(
&mut self,
meta: &DownloadableMetadata,
) -> DownloadAgent {
self.download_queue.pop_front();
let download_agent = self.download_agent_registry.remove(meta).unwrap();
self.cleanup_current_download();
self.cleanup_current_download().await;
download_agent
}
// CAREFUL WITH THIS FUNCTION
// Make sure the download thread is terminated
fn cleanup_current_download(&mut self) {
async fn cleanup_current_download(&mut self) {
self.active_control_flag = None;
*lock!(self.progress) = None;
let mut download_thread_lock = lock!(self.current_download_thread);
if let Some(unfinished_thread) = download_thread_lock.take()
&& !unfinished_thread.is_finished()
{
unfinished_thread.join().unwrap();
if let Some(unfinished_thread) = {
let mut download_thread_lock = lock!(self.current_download_thread);
download_thread_lock.take()
} {
let _ = unfinished_thread.await;
}
drop(download_thread_lock);
}
fn stop_and_wait_current_download(&self) -> bool {
async fn stop_and_wait_current_download(&self) -> bool {
self.set_status(DownloadManagerStatus::Paused);
if let Some(current_flag) = &self.active_control_flag {
current_flag.set(DownloadThreadControlFlag::Stop);
}
let mut download_thread_lock = lock!(self.current_download_thread);
if let Some(current_download_thread) = download_thread_lock.take() {
return current_download_thread.join().is_ok();
};
if let Some(current_download_thread) = {
let mut download_thread_lock = lock!(self.current_download_thread);
download_thread_lock.take()
} {
let result = timeout(Duration::from_secs(4), async {
current_download_thread.await.is_ok()
})
.await;
if let Ok(result) = result {
return result;
};
panic!("failed to cleanup download: timeout after 4 seconds");
};
}
true
}
fn manage_queue(mut self) -> Result<(), ()> {
async fn manage_queue(mut self) -> Result<(), ()> {
loop {
let signal = match self.command_receiver.recv() {
Ok(signal) => signal,
Err(_) => return Err(()),
let signal = match self.command_receiver.recv().await {
Some(signal) => signal,
None => return Err(()),
};
match signal {
DownloadManagerSignal::Go => {
self.manage_go_signal();
self.manage_go_signal().await;
}
DownloadManagerSignal::Stop => {
self.manage_stop_signal();
}
DownloadManagerSignal::Completed(meta) => {
self.manage_completed_signal(meta);
self.manage_completed_signal(meta).await;
}
DownloadManagerSignal::Queue(download_agent) => {
self.manage_queue_signal(download_agent);
self.manage_queue_signal(download_agent).await;
}
DownloadManagerSignal::Error(e) => {
self.manage_error_signal(e);
self.manage_error_signal(e).await;
}
DownloadManagerSignal::UpdateUIQueue => {
self.push_ui_queue_update();
@@ -176,16 +191,16 @@ impl DownloadManagerBuilder {
self.push_ui_stats_update(kbs, time);
}
DownloadManagerSignal::Finish => {
self.stop_and_wait_current_download();
self.stop_and_wait_current_download().await;
return Ok(());
}
DownloadManagerSignal::Cancel(meta) => {
self.manage_cancel_signal(&meta);
self.manage_cancel_signal(&meta).await;
}
}
}
}
fn manage_queue_signal(&mut self, download_agent: DownloadAgent) {
async fn manage_queue_signal(&mut self, download_agent: DownloadAgent) {
debug!("got signal Queue");
let meta = download_agent.metadata();
@@ -203,7 +218,7 @@ impl DownloadManagerBuilder {
send!(self.sender, DownloadManagerSignal::UpdateUIQueue);
}
fn manage_go_signal(&mut self) {
async fn manage_go_signal(&mut self) {
debug!("got signal Go");
if self.download_agent_registry.is_empty() {
debug!(
@@ -249,18 +264,19 @@ impl DownloadManagerBuilder {
let mut download_thread_lock = lock!(self.current_download_thread);
let app_handle = self.app_handle.clone();
*download_thread_lock = Some(spawn(move || {
*download_thread_lock = Some(tauri::async_runtime::spawn(async move {
loop {
let download_result = match download_agent.download(&app_handle) {
// Ok(true) is for completed and exited properly
Ok(v) => v,
Err(e) => {
error!("download {:?} has error {}", download_agent.metadata(), &e);
download_agent.on_error(&app_handle, &e);
send!(sender, DownloadManagerSignal::Error(e));
return;
}
};
let download_result =
match download_agent.download(&app_handle).await {
// Ok(true) is for completed and exited properly
Ok(v) => v,
Err(e) => {
error!("download {:?} has error {}", download_agent.metadata(), &e);
download_agent.on_error(&app_handle, &e);
send!(sender, DownloadManagerSignal::Error(e));
return;
}
};
// If the download gets canceled
// immediately return, on_cancelled gets called for us earlier
@@ -291,7 +307,7 @@ impl DownloadManagerBuilder {
}
if validate_result {
download_agent.on_complete(&app_handle);
download_agent.on_complete(&app_handle).await;
send!(
sender,
DownloadManagerSignal::Completed(download_agent.metadata())
@@ -307,40 +323,35 @@ impl DownloadManagerBuilder {
active_control_flag.set(DownloadThreadControlFlag::Go);
}
fn manage_stop_signal(&mut self) {
debug!("got signal Stop");
if let Some(active_control_flag) = self.active_control_flag.clone() {
self.set_status(DownloadManagerStatus::Paused);
active_control_flag.set(DownloadThreadControlFlag::Stop);
}
}
fn manage_completed_signal(&mut self, meta: DownloadableMetadata) {
debug!("got signal Completed");
async fn manage_completed_signal(&mut self, meta: DownloadableMetadata) {
if let Some(interface) = self.download_queue.read().front()
&& interface == &meta
{
self.remove_and_cleanup_front_download(&meta);
self.remove_and_cleanup_front_download(&meta).await;
}
self.push_ui_queue_update();
send!(self.sender, DownloadManagerSignal::Go);
}
fn manage_error_signal(&mut self, error: ApplicationDownloadError) {
debug!("got signal Error");
async fn manage_error_signal(&mut self, error: ApplicationDownloadError) {
info!("got signal Error");
if let Some(metadata) = self.download_queue.read().front()
&& let Some(current_agent) = self.download_agent_registry.get(metadata)
{
current_agent.on_error(&self.app_handle, &error);
self.stop_and_wait_current_download();
self.remove_and_cleanup_front_download(metadata);
self.stop_and_wait_current_download().await;
self.remove_and_cleanup_front_download(metadata).await;
}
self.push_ui_queue_update();
self.set_status(DownloadManagerStatus::Error);
}
fn manage_cancel_signal(&mut self, meta: &DownloadableMetadata) {
debug!("got signal Cancel");
async fn manage_cancel_signal(&mut self, meta: &DownloadableMetadata) {
// If the current download is the one we're tryna cancel
if let Some(current_metadata) = self.download_queue.read().front()
&& current_metadata == meta
@@ -348,13 +359,13 @@ impl DownloadManagerBuilder {
{
self.set_status(DownloadManagerStatus::Paused);
current_download.on_cancelled(&self.app_handle);
self.stop_and_wait_current_download();
self.stop_and_wait_current_download().await;
self.set_status(DownloadManagerStatus::Empty);
self.download_queue.pop_front();
self.cleanup_current_download();
self.cleanup_current_download().await;
self.download_agent_registry.remove(meta);
debug!("current download queue: {:?}", self.download_queue.read());
}
// else just cancel it
else if let Some(download_agent) = self.download_agent_registry.get(meta) {
@@ -370,8 +381,8 @@ impl DownloadManagerBuilder {
);
}
}
self.sender.send(DownloadManagerSignal::Go).unwrap();
self.push_ui_queue_update();
send!(self.sender, DownloadManagerSignal::Go);
}
fn push_ui_stats_update(&self, kbs: usize, time: usize) {
let event_data = StatsUpdateEvent { speed: kbs, time };
@@ -1,26 +1,25 @@
use std::{
any::Any,
collections::VecDeque,
fmt::Debug,
sync::{
Mutex, MutexGuard,
mpsc::{SendError, Sender},
},
thread::JoinHandle,
sync::{Arc, Mutex, MutexGuard},
};
use database::DownloadableMetadata;
use log::{debug, info};
use serde::Serialize;
use tauri::async_runtime::JoinHandle;
use tokio::sync::mpsc::Sender;
use tokio::sync::mpsc::error::SendError;
use utils::{lock, send};
use crate::error::ApplicationDownloadError;
use crate::{depot_manager::DepotManager, error::ApplicationDownloadError};
use super::{
download_manager_builder::{CurrentProgressObject, DownloadAgent},
util::queue::Queue,
};
#[derive(Debug)]
pub enum DownloadManagerSignal {
/// Resumes (or starts) the `DownloadManager`
Go,
@@ -79,38 +78,47 @@ pub enum DownloadStatus {
/// The actual download queue may be accessed through the .`edit()` function,
/// which provides raw access to the underlying queue.
/// THIS EDITING IS BLOCKING!!!
#[derive(Debug)]
pub struct DownloadManager {
terminator: Mutex<Option<JoinHandle<Result<(), ()>>>>,
terminator: Mutex<Option<JoinHandle<()>>>,
download_queue: Queue,
progress: CurrentProgressObject,
command_sender: Sender<DownloadManagerSignal>,
depot_manager: Arc<DepotManager>,
}
impl Debug for DownloadManager {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("DownloadManager").finish()
}
}
#[allow(dead_code)]
impl DownloadManager {
pub fn new(
terminator: JoinHandle<Result<(), ()>>,
terminator: JoinHandle<()>,
download_queue: Queue,
progress: CurrentProgressObject,
command_sender: Sender<DownloadManagerSignal>,
depot_manager: Arc<DepotManager>,
) -> Self {
Self {
terminator: Mutex::new(Some(terminator)),
download_queue,
progress,
command_sender,
depot_manager
}
}
pub fn queue_download(
pub async fn queue_download(
&self,
download: DownloadAgent,
) -> Result<(), SendError<DownloadManagerSignal>> {
info!("creating download with meta {:?}", download.metadata());
self.command_sender
.send(DownloadManagerSignal::Queue(download))?;
self.command_sender.send(DownloadManagerSignal::Go)
.send(DownloadManagerSignal::Queue(download))
.await?;
self.command_sender.send(DownloadManagerSignal::Go).await
}
pub fn edit(&self) -> MutexGuard<'_, VecDeque<DownloadableMetadata>> {
self.download_queue.edit()
@@ -122,7 +130,7 @@ impl DownloadManager {
let progress_object = (*lock!(self.progress)).clone()?;
Some(progress_object.get_progress())
}
pub fn rearrange_string(&self, meta: &DownloadableMetadata, new_index: usize) {
pub async fn rearrange_string(&self, meta: &DownloadableMetadata, new_index: usize) {
let mut queue = self.edit();
let current_index =
get_index_from_id(&mut queue, meta).expect("Failed to get meta index from id");
@@ -132,10 +140,10 @@ impl DownloadManager {
queue.insert(new_index, to_move);
send!(self.command_sender, DownloadManagerSignal::UpdateUIQueue);
}
pub fn cancel(&self, meta: DownloadableMetadata) {
pub async fn cancel(&self, meta: DownloadableMetadata) {
send!(self.command_sender, DownloadManagerSignal::Cancel(meta));
}
pub fn rearrange(&self, current_index: usize, new_index: usize) {
pub async fn rearrange(&self, current_index: usize, new_index: usize) {
if current_index == new_index {
return;
}
@@ -147,10 +155,11 @@ impl DownloadManager {
debug!("moving download at index {current_index} to index {new_index}");
let mut queue = self.edit();
let to_move = queue.remove(current_index).expect("Failed to get");
queue.insert(new_index, to_move);
drop(queue);
{
let mut queue = self.edit();
let to_move = queue.remove(current_index).expect("Failed to get");
queue.insert(new_index, to_move);
}
if needs_pause {
send!(self.command_sender, DownloadManagerSignal::Go);
@@ -158,20 +167,23 @@ impl DownloadManager {
send!(self.command_sender, DownloadManagerSignal::UpdateUIQueue);
send!(self.command_sender, DownloadManagerSignal::Go);
}
pub fn pause_downloads(&self) {
pub async fn pause_downloads(&self) {
send!(self.command_sender, DownloadManagerSignal::Stop);
}
pub fn resume_downloads(&self) {
pub async fn resume_downloads(&self) {
send!(self.command_sender, DownloadManagerSignal::Go);
}
pub fn ensure_terminated(&self) -> Result<Result<(), ()>, Box<dyn Any + Send>> {
pub async fn ensure_terminated(&self) -> Result<(), tauri::Error> {
send!(self.command_sender, DownloadManagerSignal::Finish);
let terminator = lock!(self.terminator).take();
terminator.unwrap().join()
terminator.unwrap().await
}
pub fn get_sender(&self) -> Sender<DownloadManagerSignal> {
self.command_sender.clone()
}
pub fn clone_depot_manager(&self) -> Arc<DepotManager> {
self.depot_manager.clone()
}
}
/// Takes in the locked value from .`edit()` and attempts to
@@ -1,5 +1,6 @@
use std::sync::Arc;
use std::{fmt::Debug, sync::Arc};
use async_trait::async_trait;
use database::DownloadableMetadata;
use tauri::AppHandle;
@@ -16,8 +17,9 @@ use super::{
*
* But the download manager manages the queue state
*/
pub trait Downloadable: Send + Sync {
fn download(&self, app_handle: &AppHandle) -> Result<bool, ApplicationDownloadError>;
#[async_trait]
pub trait Downloadable: Send + Sync + Debug {
async fn download(&self, app_handle: &AppHandle) -> Result<bool, ApplicationDownloadError>;
fn validate(&self, app_handle: &AppHandle) -> Result<bool, ApplicationDownloadError>;
fn progress(&self) -> Arc<ProgressObject>;
@@ -26,6 +28,6 @@ pub trait Downloadable: Send + Sync {
fn metadata(&self) -> DownloadableMetadata;
fn on_queued(&self, app_handle: &AppHandle);
fn on_error(&self, app_handle: &AppHandle, error: &ApplicationDownloadError);
fn on_complete(&self, app_handle: &AppHandle);
async fn on_complete(&self, app_handle: &AppHandle);
fn on_cancelled(&self, app_handle: &AppHandle);
}
@@ -43,6 +43,7 @@ pub enum ApplicationDownloadError {
Lock,
IoError(Arc<io::Error>),
DownloadError(RemoteAccessError),
InvalidCommand,
}
impl Display for ApplicationDownloadError {
@@ -69,6 +70,7 @@ impl Display for ApplicationDownloadError {
ApplicationDownloadError::DownloadError(error) => {
write!(f, "Download failed with error {error:?}")
}
ApplicationDownloadError::InvalidCommand => write!(f, "Invalid command state"),
}
}
}
@@ -16,6 +16,7 @@ pub mod downloadable;
pub mod error;
pub mod frontend_updates;
pub mod util;
pub mod depot_manager;
pub static DOWNLOAD_MANAGER: DownloadManagerWrapper = DownloadManagerWrapper::new();
@@ -28,7 +29,7 @@ impl DownloadManagerWrapper {
DOWNLOAD_MANAGER
.0
.set(DownloadManagerBuilder::build(app_handle))
.expect("Failed to initialise download manager");
.expect("failed to initialise download manager");
}
}
@@ -2,3 +2,4 @@ pub mod download_thread_control_flag;
pub mod progress_object;
pub mod queue;
pub mod rolling_progress_updates;
pub mod semaphore;
@@ -2,14 +2,13 @@ use std::{
sync::{
Arc, Mutex,
atomic::{AtomicUsize, Ordering},
mpsc::Sender,
},
time::{Duration, Instant},
time::Instant,
};
use atomic_instant_full::AtomicInstant;
use throttle_my_fn::throttle;
use utils::{lock, send};
use tokio::sync::mpsc::Sender;
use crate::download_manager_frontend::DownloadManagerSignal;
@@ -46,7 +45,7 @@ impl ProgressHandle {
pub fn add(&self, amount: usize) {
self.progress
.fetch_add(amount, std::sync::atomic::Ordering::AcqRel);
calculate_update(&self.progress_object);
tauri::async_runtime::spawn(calculate_update(self.progress_object.clone()));
}
pub fn skip(&self, amount: usize) {
self.progress
@@ -112,14 +111,17 @@ impl ProgressObject {
}
}
#[throttle(1, Duration::from_millis(20))]
pub fn calculate_update(progress: &ProgressObject) {
pub async fn calculate_update(progress: Arc<ProgressObject>) {
let last_update_time = progress
.last_update_time
.swap(Instant::now(), Ordering::SeqCst);
.load(Ordering::SeqCst);
let time_since_last_update = Instant::now()
.duration_since(last_update_time)
.as_millis_f64();
if time_since_last_update < 250.0 {
return;
}
progress.last_update_time.swap(Instant::now(), Ordering::SeqCst);
let current_bytes_downloaded = progress.sum();
let max = progress.get_max();
@@ -135,25 +137,24 @@ pub fn calculate_update(progress: &ProgressObject) {
let bytes_remaining = max.saturating_sub(current_bytes_downloaded); // bytes
progress.update_window(kilobytes_per_second as usize);
push_update(progress, bytes_remaining);
push_update(&progress, bytes_remaining).await;
}
#[throttle(1, Duration::from_millis(250))]
pub fn push_update(progress: &ProgressObject, bytes_remaining: usize) {
pub async fn push_update(progress: &ProgressObject, bytes_remaining: usize) {
let average_speed = progress.rolling.get_average();
let time_remaining = (bytes_remaining / 1000) / average_speed.max(1);
update_ui(progress, average_speed, time_remaining);
update_queue(progress);
update_ui(progress, average_speed, time_remaining).await;
update_queue(progress).await;
}
fn update_ui(progress_object: &ProgressObject, kilobytes_per_second: usize, time_remaining: usize) {
async fn update_ui(progress_object: &ProgressObject, kilobytes_per_second: usize, time_remaining: usize) {
send!(
progress_object.sender,
DownloadManagerSignal::UpdateUIStats(kilobytes_per_second, time_remaining)
);
}
fn update_queue(progress: &ProgressObject) {
async fn update_queue(progress: &ProgressObject) {
send!(progress.sender, DownloadManagerSignal::UpdateUIQueue)
}
@@ -0,0 +1,28 @@
use std::sync::{Arc, atomic::{AtomicUsize, Ordering}};
pub struct SyncSemaphore {
inner: Arc<AtomicUsize>
}
impl SyncSemaphore {
pub fn new() -> Self {
Self { inner: Arc::new(AtomicUsize::new(0)) }
}
pub fn acquire(&self) -> SyncSemaphorePermit {
self.inner.fetch_add(1, Ordering::Relaxed);
SyncSemaphorePermit(self.inner.clone())
}
pub fn permits(&self) -> usize {
self.inner.fetch_add(0, Ordering::Relaxed)
}
}
pub struct SyncSemaphorePermit(Arc<AtomicUsize>);
impl Drop for SyncSemaphorePermit {
fn drop(&mut self) {
self.0.fetch_sub(1, Ordering::Relaxed);
}
}