add delay option

This commit is contained in:
Daniella / Tove 2023-08-19 16:30:50 +02:00
parent 9b7577ce65
commit a0989bdc73
Signed by: TudbuT
GPG key ID: 7D63D5634B7C417F
2 changed files with 52 additions and 11 deletions

View file

@ -116,6 +116,39 @@ pub fn gui() -> Result<(), iui::UIError> {
LayoutStrategy::Compact, LayoutStrategy::Compact,
); );
let mut delay = VerticalBox::new(&ui);
let mut delay_slider = Slider::new(&ui, 100, 3_000);
let delayb = Ref::new(&delay_slider);
let mut delay_box = Entry::new(&ui);
delay_slider.set_value(&ui, 500);
delay_box.set_value(&ui, "500");
// We know that ui.main() will wait until the UI is dead, so these are safe.
let sb = RefMut::new(&mut delay_slider);
let bb = RefMut::new(&mut delay_box);
let uib = Ref::new(&ui);
let uib1 = uib.clone();
delay_box.on_changed(&ui, move |val| {
sb.get().set_value(
uib.get(),
u16::from_str_radix(val.as_str(), 10).unwrap_or(256) as i32,
);
});
delay_slider.on_changed(&ui, move |val| {
bb.get().set_value(uib1.get(), val.to_string().as_str());
});
delay.set_padded(&ui, true);
delay.append(&ui, delay_slider, LayoutStrategy::Compact);
delay.append(&ui, delay_box, LayoutStrategy::Compact);
vbox.append(
&ui,
wrap(
&ui,
"Delay in µs: (higher = more reliable, lower = faster)",
delay,
),
LayoutStrategy::Compact,
);
let mut speed = VerticalBox::new(&ui); let mut speed = VerticalBox::new(&ui);
let mut speed_slider = Slider::new(&ui, 100, 3_000); let mut speed_slider = Slider::new(&ui, 100, 3_000);
let speedb = Ref::new(&speed_slider); let speedb = Ref::new(&speed_slider);
@ -183,6 +216,8 @@ pub fn gui() -> Result<(), iui::UIError> {
args.push(a); args.push(a);
let a = pathb.get().clone(); let a = pathb.get().clone();
args.push(a); args.push(a);
let a = delayb.get().value(uib.get()).to_string();
args.push(a);
let a = speedb.get().value(uib.get()).to_string(); let a = speedb.get().value(uib.get()).to_string();
args.push(a); args.push(a);
let a = skipb.get().value(uib.get()); let a = skipb.get().value(uib.get());

View file

@ -52,12 +52,12 @@ impl SafeReadWrite {
} }
} }
pub fn write_safe(&mut self, buf: &[u8]) -> Result<(), Error> { pub fn write_safe(&mut self, buf: &[u8], delay: u64) -> Result<(), Error> {
self.write_flush_safe(buf, false) self.write_flush_safe(buf, false, delay)
} }
pub fn write_flush_safe(&mut self, buf: &[u8], flush: bool) -> Result<(), Error> { pub fn write_flush_safe(&mut self, buf: &[u8], flush: bool, delay: u64) -> Result<(), Error> {
self.internal_write_safe(buf, Write, flush, false) self.internal_write_safe(buf, Write, flush, false, delay)
} }
pub fn read_safe(&mut self, buf: &[u8]) -> Result<(Vec<u8>, usize), Error> { pub fn read_safe(&mut self, buf: &[u8]) -> Result<(Vec<u8>, usize), Error> {
@ -129,7 +129,7 @@ impl SafeReadWrite {
} }
pub fn end(mut self) -> UdpSocket { pub fn end(mut self) -> UdpSocket {
let _ = self.internal_write_safe(&mut [], End, true, true); let _ = self.internal_write_safe(&mut [], End, true, true, 3000);
self.socket self.socket
} }
@ -140,6 +140,7 @@ impl SafeReadWrite {
packet: SafeReadWritePacket, packet: SafeReadWritePacket,
flush: bool, flush: bool,
exit_on_lost: bool, exit_on_lost: bool,
delay: u64,
) -> Result<(), Error> { ) -> Result<(), Error> {
if buf.len() > 0xfffc { if buf.len() > 0xfffc {
panic!( panic!(
@ -169,7 +170,7 @@ impl SafeReadWrite {
continue; continue;
} }
} }
thread::sleep(Duration::from_micros(1200)); thread::sleep(Duration::from_micros(delay));
self.last_transmitted.insert(idn, vbuf); self.last_transmitted.insert(idn, vbuf);
break; break;
} }
@ -392,13 +393,18 @@ pub fn helper(args: &Vec<String>) {
pub fn sender<F: Fn(f32)>(args: &Vec<String>, on_progress: F) { pub fn sender<F: Fn(f32)>(args: &Vec<String>, on_progress: F) {
let connection = holepunch(args); let connection = holepunch(args);
let br = args let dly = args
.get(5) .get(5)
.map(|s| u64::from_str_radix(s, 10))
.unwrap_or(Ok(500))
.expect("bad delay operand");
let br = args
.get(6)
.map(|s| u32::from_str_radix(s, 10)) .map(|s| u32::from_str_radix(s, 10))
.unwrap_or(Ok(256)) .unwrap_or(Ok(256))
.expect("bad bitrate argument"); .expect("bad bitrate argument");
let begin = args let begin = args
.get(6) .get(7)
.map(|s| u64::from_str_radix(s, 10)) .map(|s| u64::from_str_radix(s, 10))
.unwrap_or(Ok(0)) .unwrap_or(Ok(0))
.expect("bad begin operand"); .expect("bad begin operand");
@ -421,7 +427,7 @@ pub fn sender<F: Fn(f32)>(args: &Vec<String>, on_progress: F) {
let mut bytes_sent: u64 = 0; let mut bytes_sent: u64 = 0;
let mut last_update = unix_millis(); let mut last_update = unix_millis();
let len = file.metadata().expect("bad metadata").len(); let len = file.metadata().expect("bad metadata").len();
sc.write_safe(&len.to_be_bytes()) sc.write_safe(&len.to_be_bytes(), 3000)
.expect("unable to send file length"); .expect("unable to send file length");
println!("Length: {}", &len); println!("Length: {}", &len);
let mut time = unix_millis(); let mut time = unix_millis();
@ -434,7 +440,7 @@ pub fn sender<F: Fn(f32)>(args: &Vec<String>, on_progress: F) {
return; return;
} }
sc.write_safe(&buf[..read]).expect("send error"); sc.write_safe(&buf[..read], dly).expect("send error");
bytes_sent += read as u64; bytes_sent += read as u64;
if (bytes_sent % (br * 20) as u64) < (br as u64) { if (bytes_sent % (br * 20) as u64) < (br as u64) {
let elapsed = unix_millis() - time; let elapsed = unix_millis() - time;
@ -624,7 +630,7 @@ fn print_args(args: &Vec<String>) {
println!( println!(
"No arguments. Needed: \n\ "No arguments. Needed: \n\
| {} helper <bind-port>\n\ | {} helper <bind-port>\n\
| {} sender <helper-address>:<helper-port> <phrase> <filename> [bitrate] [skip]\n\ | {} sender <helper-address>:<helper-port> <phrase> <filename> [send-dly] [bitrate] [skip]\n\
| {} receiver <helper-address>:<helper-port> <phrase> <filename> [bitrate] [skip]\n\ | {} receiver <helper-address>:<helper-port> <phrase> <filename> [bitrate] [skip]\n\
| {} gui\n\ | {} gui\n\
| {} version\n", | {} version\n",