Created function for wasm/native sleep, used clippy suggestions

This commit is contained in:
G2-Games 2024-01-31 21:41:41 -06:00
parent 630a8c95eb
commit 0d9646c5a0
4 changed files with 27 additions and 49 deletions

View file

@ -4,16 +4,12 @@ use once_cell::sync::Lazy;
use std::error::Error;
use std::time::Duration;
#[cfg(target_family = "wasm")]
use gloo::{
timers::future::TimeoutFuture,
console::log,
};
// USB stuff
//use nusb::transfer::{Control, ControlIn, ControlOut, ControlType, Recipient, RequestBuffer};
use cross_usb::{UsbDevice, UsbInterface};
use cross_usb::usb::{ControlIn, ControlOut, ControlType, Device, Interface, Recipient};
use super::utils::cross_sleep;
//use nusb::{Device, DeviceInfo, Interface};
const DEFAULT_TIMEOUT: Duration = Duration::new(10000, 0);
@ -257,14 +253,10 @@ impl NetMD {
}
// Back off while trying again
let sleep_time = Self::READ_REPLY_RETRY_INTERVAL as u64
* (u64::pow(2, attempt as u32 / 10) - 1);
let sleep_time = Self::READ_REPLY_RETRY_INTERVAL
* (u32::pow(2, attempt / 10) - 1);
#[cfg(not(target_family = "wasm"))]
std::thread::sleep(std::time::Duration::from_millis(sleep_time));
#[cfg(target_family = "wasm")]
TimeoutFuture::new(sleep_time as u32).await;
cross_sleep(sleep_time).await;
}
if let Some(value) = override_length {

View file

@ -1,9 +1,10 @@
#![cfg_attr(debug_assertions, allow(dead_code))]
use std::{error::Error, thread::sleep, time::Duration};
use std::error::Error;
use num_derive::FromPrimitive;
use num_traits::FromPrimitive;
use super::interface::{NetMDInterface, MDTrack};
use super::utils::cross_sleep;
#[derive(FromPrimitive)]
#[derive(PartialEq)]
@ -59,7 +60,7 @@ pub async fn device_status(interface: &mut NetMDInterface) -> Result<DeviceStatu
pub async fn prepare_download(interface: &mut NetMDInterface) -> Result<(), Box<dyn Error>>{
while ![OperatingStatus::DiscBlank, OperatingStatus::Ready].contains(&device_status(interface).await?.state.unwrap_or(OperatingStatus::NoDisc)) {
sleep(Duration::from_millis(200));
cross_sleep(200).await;
}
let _ = interface.session_key_forget().await;

View file

@ -14,18 +14,7 @@ use std::error::Error;
use lazy_static::lazy_static;
#[cfg(target_family = "wasm")]
use gloo::{
timers::future::TimeoutFuture,
console::log,
};
// Blocking stuff - can't use on WASM
#[cfg(not(arget_family = "wasm"))]
use std::{
thread::sleep,
time::Duration,
};
use super::utils::cross_sleep;
#[derive(Copy, Clone)]
enum Action {
@ -398,15 +387,10 @@ impl NetMDInterface {
NetmdStatus::NotImplemented => return Err("Not implemented".into()),
NetmdStatus::Rejected => return Err("Rejected".into()),
NetmdStatus::Interim if !accept_interim => {
let sleep_time = Self::INTERIM_RESPONSE_RETRY_INTERVAL as u64
* (u64::pow(2, current_attempt as u32) - 1);
#[cfg(not(target_family = "wasm"))]
sleep(Duration::from_millis(sleep_time));
#[cfg(target_family = "wasm")]
TimeoutFuture::new(sleep_time as u32).await;
let sleep_time = Self::INTERIM_RESPONSE_RETRY_INTERVAL
* (u32::pow(2, current_attempt as u32) - 1);
cross_sleep(sleep_time).await;
current_attempt += 1;
continue; // Retry!
@ -1004,7 +988,7 @@ impl NetMDInterface {
let wchar_value = match wchar {
true => {
new_title = sanitize_full_width_title(&title, false);
new_title = sanitize_full_width_title(title, false);
1
}
false => {
@ -1055,7 +1039,7 @@ impl NetMDInterface {
let new_title: Vec<u8>;
let (wchar_value, descriptor) = match wchar {
true => {
new_title = sanitize_full_width_title(&title, false);
new_title = sanitize_full_width_title(title, false);
(3, Descriptor::AudioUTOC4TD)
}
false => {
@ -1520,11 +1504,7 @@ impl NetMDInterface {
}
// Sharps are slow
#[cfg(not(target_family = "wasm"))]
sleep(Duration::from_millis(200));
#[cfg(target_family = "wasm")]
TimeoutFuture::new(200).await;
cross_sleep(200).await;
let total_bytes = pkt_size + 24; //framesizedict[wireformat] * frames + pktcount * 24;
@ -1544,11 +1524,7 @@ impl NetMDInterface {
)?;
// Sharps are slow
#[cfg(not(target_family = "wasm"))]
sleep(Duration::from_millis(200));
#[cfg(target_family = "wasm")]
TimeoutFuture::new(200).await;
cross_sleep(200).await;
let mut _written_bytes = 0;
for (packet_count, (key, iv, data)) in packets.into_iter().enumerate() {

View file

@ -8,6 +8,15 @@ use unicode_normalization::UnicodeNormalization;
extern crate kana;
use kana::*;
/// Sleep for a specified number of milliseconds on any platform
pub async fn cross_sleep(millis: u32) {
#[cfg(not(target_family = "wasm"))]
std::thread::sleep(std::time::Duration::from_millis(millis as u64));
#[cfg(target_family = "wasm")]
gloo::timers::future::TimeoutFuture::new(millis).await;
}
pub fn bcd_to_int(mut bcd: i32) -> i32 {
let mut value = 0;
let mut nibble = 0;
@ -90,7 +99,7 @@ fn check(string: String) -> Option<String> {
}
pub fn sanitize_half_width_title(title: &str) -> Vec<u8> {
let mut string_title = wide2ascii(&title);
let mut string_title = wide2ascii(title);
string_title = nowidespace(&string_title);
string_title = hira2kata(&string_title);
string_title = combine(&string_title);
@ -107,7 +116,7 @@ pub fn sanitize_half_width_title(title: &str) -> Vec<u8> {
let sjis_string = SHIFT_JIS.encode(&new_title).0;
if validate_shift_jis(sjis_string.clone().into()) {
return agressive_sanitize_title(&title).into();
return agressive_sanitize_title(title).into();
}
sjis_string.into()