migrate gui to a feature flag, drastically speed up send times

This commit is contained in:
Daniella / Tove 2023-08-19 16:14:35 +02:00
parent 69081acc51
commit 9b7577ce65
Signed by: TudbuT
GPG key ID: 7D63D5634B7C417F
4 changed files with 63 additions and 30 deletions

2
Cargo.lock generated
View file

@ -355,7 +355,7 @@ dependencies = [
[[package]] [[package]]
name = "qft" name = "qft"
version = "0.5.5" version = "0.5.6"
dependencies = [ dependencies = [
"iui", "iui",
"rand", "rand",

View file

@ -1,11 +1,14 @@
[package] [package]
name = "qft" name = "qft"
version = "0.5.5" version = "0.5.6"
edition = "2021" edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies] [dependencies]
iui.git = "https://github.com/rust-native-ui/libui-rs" iui = { git = "https://github.com/rust-native-ui/libui-rs", optional = true }
rand = "0" rand = { version = "0.8", optional = true }
time = "0" time = "0.3"
[features]
gui = [ "dep:iui", "dep:rand" ]

View file

@ -117,7 +117,7 @@ pub fn gui() -> Result<(), iui::UIError> {
); );
let mut speed = VerticalBox::new(&ui); let mut speed = VerticalBox::new(&ui);
let mut speed_slider = Slider::new(&ui, 100, 10_000); let mut speed_slider = Slider::new(&ui, 100, 3_000);
let speedb = Ref::new(&speed_slider); let speedb = Ref::new(&speed_slider);
let mut speed_box = Entry::new(&ui); let mut speed_box = Entry::new(&ui);
speed_slider.set_value(&ui, 256); speed_slider.set_value(&ui, 256);

View file

@ -1,3 +1,4 @@
#[cfg(feature = "gui")]
mod gui; mod gui;
use std::{ use std::{
@ -168,6 +169,7 @@ impl SafeReadWrite {
continue; continue;
} }
} }
thread::sleep(Duration::from_micros(1200));
self.last_transmitted.insert(idn, vbuf); self.last_transmitted.insert(idn, vbuf);
break; break;
} }
@ -186,7 +188,17 @@ impl SafeReadWrite {
} }
let mut is_catching_up = false; let mut is_catching_up = false;
loop { loop {
match self.socket.recv(&mut buf).ok() { match (
if !wait {
self.socket.set_nonblocking(true).unwrap()
} else {
()
},
self.socket.recv(&mut buf).ok(),
self.socket.set_nonblocking(false).unwrap(),
)
.1
{
Some(x) => { Some(x) => {
if x != 3 { if x != 3 {
continue; continue;
@ -205,33 +217,38 @@ impl SafeReadWrite {
} }
if buf[2] == ResendRequest as u8 { if buf[2] == ResendRequest as u8 {
let mut n = u16::from_be_bytes([buf[0], buf[1]]); let mut n = u16::from_be_bytes([buf[0], buf[1]]);
thread::sleep(Duration::from_millis(100));
while let Some(_) = self.socket.recv(&mut buf).ok() {}
if !is_catching_up && !env::var("QFT_HIDE_DROPS").is_ok() { if !is_catching_up && !env::var("QFT_HIDE_DROPS").is_ok() {
println!("\r\x1b[KA packet dropped: {}", &n); println!("\r\x1b[KA packet dropped: {}", &n);
} }
wait = true; if !is_catching_up {
is_catching_up = true; wait = true;
while n <= idn && !(idn == 0xffff && n == 0) { is_catching_up = true;
let buf = self.last_transmitted.get(&n); while n <= idn && !(idn == 0xffff && n == 0) {
if let Some(buf) = buf { let buf = self.last_transmitted.get(&n);
loop { if let Some(buf) = buf {
// resend until success loop {
match self.socket.send(&buf.as_slice()) { // resend until success
Ok(x) => { match self.socket.send(&buf.as_slice()) {
if x != buf.len() { Ok(x) => {
if x != buf.len() {
continue;
}
}
Err(_) => {
continue; continue;
} }
} };
Err(_) => { thread::sleep(Duration::from_millis(4));
continue; break;
} }
}; } else {
break; break;
} }
} else { // do NOT remove from last_transmitted yet, wait for Ack to do that.
break; n += 1;
} }
// do NOT remove from last_transmitted yet, wait for Ack to do that.
n += 1;
} }
} }
} }
@ -253,6 +270,7 @@ impl SafeReadWrite {
continue; continue;
} }
} }
thread::sleep(Duration::from_millis(4));
break; break;
} }
start = unix_millis(); start = unix_millis();
@ -279,10 +297,13 @@ fn main() {
panic!("no args"); panic!("no args");
} }
if args.len() == 1 { if args.len() == 1 {
#[cfg(feature = "gui")]
match gui::gui() { match gui::gui() {
Ok(_) => (), Ok(_) => (),
Err(_) => print_args(&args), Err(_) => print_args(&args),
} }
#[cfg(not(feature = "gui"))]
print_args(&args)
} }
match args match args
.get(1) .get(1)
@ -292,7 +313,10 @@ fn main() {
"helper" => helper(&args), "helper" => helper(&args),
"sender" => sender(&args, |_| {}), "sender" => sender(&args, |_| {}),
"receiver" => receiver(&args, |_| {}), "receiver" => receiver(&args, |_| {}),
#[cfg(feature = "gui")]
"gui" => gui::gui().expect("can't use gui"), "gui" => gui::gui().expect("can't use gui"),
#[cfg(not(feature = "gui"))]
"gui" => println!("Feature 'gui' was not enabled during compilation. GUI not available."),
"version" => println!("QFT version: {}", env!("CARGO_PKG_VERSION")), "version" => println!("QFT version: {}", env!("CARGO_PKG_VERSION")),
_ => print_args(&args), _ => print_args(&args),
} }
@ -416,8 +440,11 @@ pub fn sender<F: Fn(f32)>(args: &Vec<String>, on_progress: F) {
let elapsed = unix_millis() - time; let elapsed = unix_millis() - time;
let elapsed = if elapsed == 0 { 1 } else { elapsed }; let elapsed = if elapsed == 0 { 1 } else { elapsed };
print!("\r\x1b[KSent {} bytes; Speed: {} kb/s", print!(
bytes_sent, br as usize * 20 / elapsed as usize ); "\r\x1b[KSent {} bytes; Speed: {} kb/s",
bytes_sent,
br as usize * 20 / elapsed as usize
);
stdout().flush().unwrap(); stdout().flush().unwrap();
time = unix_millis(); time = unix_millis();
} }
@ -489,8 +516,11 @@ pub fn receiver<F: Fn(f32)>(args: &Vec<String>, on_progress: F) {
let elapsed = unix_millis() - time; let elapsed = unix_millis() - time;
let elapsed = if elapsed == 0 { 1 } else { elapsed }; let elapsed = if elapsed == 0 { 1 } else { elapsed };
print!("\r\x1b[KReceived {} bytes; Speed: {} kb/s", print!(
bytes_received, br as usize * 20 / elapsed as usize ); "\r\x1b[KReceived {} bytes; Speed: {} kb/s",
bytes_received,
br as usize * 20 / elapsed as usize
);
stdout().flush().unwrap(); stdout().flush().unwrap();
time = unix_millis(); time = unix_millis();
} }