Skip to content

Commit

Permalink
fix a few bad disconnects
Browse files Browse the repository at this point in the history
  • Loading branch information
TudbuT committed Aug 13, 2022
1 parent 629228d commit adb7042
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "qft"
version = "0.1.6"
version = "0.1.7"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
33 changes: 16 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::{
collections::HashMap,
fs::File,
io::{Error, Read, Write, stdout},
io::{stdout, Error, Read, Write},
net::*,
str::FromStr,
thread,
Expand Down Expand Up @@ -59,10 +59,6 @@ impl SafeReadWrite {
buf.insert(0, id);
let buf = buf.as_slice();

self.socket
.set_read_timeout(Some(Duration::from_secs(1)))
.expect("cannot set_read_timeout");

let mut resend = true;
while resend {
match self.socket.send(buf) {
Expand Down Expand Up @@ -108,7 +104,7 @@ impl SafeReadWrite {
while try_again {
match self.socket.recv(buf) {
Ok(x) => {
if x == 0 {
if x < 2 {
continue;
}
if buf[0] <= self.packet_count_in as u8 {
Expand All @@ -125,7 +121,7 @@ impl SafeReadWrite {
return Ok((vec![], 0));
}
}
Err(x) => return Err(x),
Err(_) => {}
}
}
mbuf.remove(0);
Expand All @@ -143,10 +139,6 @@ impl SafeReadWrite {
buf.insert(0, id);
let buf = buf.as_slice();

self.socket
.set_read_timeout(Some(Duration::from_secs(1)))
.expect("cannot set_read_timeout");

let mut resend = true;
while resend {
match self.socket.send(buf) {
Expand Down Expand Up @@ -233,7 +225,8 @@ fn helper(args: &Vec<String>) {

fn sender(args: &Vec<String>) {
let connection = holepunch(args);
let br = u32::from_str_radix(args.get(5).unwrap_or(&"256".to_string()), 10).expect("This is not a correct number");
let br = u32::from_str_radix(args.get(5).unwrap_or(&"256".to_string()), 10)
.expect("This is not a correct number");
let mut buf: Vec<u8> = Vec::new();
buf.resize(br as usize, 0);
let mut buf = buf.leak();
Expand Down Expand Up @@ -265,7 +258,8 @@ fn sender(args: &Vec<String>) {

fn receiver(args: &Vec<String>) {
let connection = holepunch(args);
let br = u32::from_str_radix(args.get(5).unwrap_or(&"256".to_string()), 10).expect("This is not a correct number");
let br = u32::from_str_radix(args.get(5).unwrap_or(&"256".to_string()), 10)
.expect("This is not a correct number");
let mut buf: Vec<u8> = Vec::new();
buf.resize(br as usize, 0);
let mut buf: &[u8] = buf.leak();
Expand Down Expand Up @@ -331,16 +325,21 @@ fn holepunch(args: &Vec<String>) -> UdpSocket {
holepunch
.connect(SocketAddrV4::from_str(bind_addr.as_str()).unwrap())
.expect("connection failed");
holepunch
.set_read_timeout(Some(Duration::from_secs(1)))
.unwrap();
holepunch
.set_write_timeout(Some(Duration::from_secs(1)))
.unwrap();
println!("Waiting...");
holepunch.set_read_timeout(Some(Duration::from_secs(1))).unwrap();
holepunch.set_write_timeout(Some(Duration::from_secs(1))).unwrap();
let mut stop = false;
while !stop {
let m = unix_millis();
thread::sleep(Duration::from_millis(500 - (m % 500)));
println!("CONNECT {}", unix_millis());
holepunch.send(&[0]).expect("connection failed");
if holepunch.recv(&mut [0]).is_ok() {
let result = holepunch.recv(&mut [0]);
if result.is_ok() && result.unwrap() == 1 {
stop = true;
}
}
Expand All @@ -358,7 +357,7 @@ fn print_args(args: &Vec<String>) {
"No arguments. Needed: \n\
| {} helper <bind-port>\n\
| {} sender <helper-address>:<helper-port> <phrase> <filename> [bitrate]\n\
| {} receiver <helper-address>:<helper-port> <phrase> <filename> [bitrate]",
| {} receiver <helper-address>:<helper-port> <phrase> <filename> [bitrate]\n",
f, f, f
);
panic!("No arguments");
Expand Down

0 comments on commit adb7042

Please sign in to comment.