improve holepunching drastically

This commit is contained in:
Daniella 2022-08-15 21:21:01 +02:00
parent 5b1f0ec982
commit 921b959f14
4 changed files with 40 additions and 20 deletions

2
Cargo.lock generated
View file

@ -4,4 +4,4 @@ version = 3
[[package]] [[package]]
name = "qft" name = "qft"
version = "0.2.1" version = "0.3.0"

View file

@ -1,6 +1,6 @@
[package] [package]
name = "qft" name = "qft"
version = "0.2.1" version = "0.3.0"
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

View file

@ -28,11 +28,10 @@ Helpers are there to help with holepunching.
- P2 gets P1's public IP and port - P2 gets P1's public IP and port
- P1 gets P2's public IP and port - P1 gets P2's public IP and port
- P1 and P2 disconnect\* from the helper - P1 and P2 disconnect\* from the helper
- P1 and P2 start a loop: - P1 and P2 start a loop (slightly simplified):
- wait until their system clock is at .0 or .5 of a second - fire a packet at eachother multiple times
- fire a packet at eachother at the same time - try to receive as many packets from the other one
- try to receive the packet from the other one - if none are received, loop again
- if none is received, loop again
- if one is received, exit the loop - if one is received, exit the loop
- Connection between P1 and P2 is established. - Connection between P1 and P2 is established.
@ -73,11 +72,9 @@ bitrate before, the default is 256). It will skip those bytes and continue where
## Troubleshooting ## Troubleshooting
### It constantly says `CONNECTING` ### It says `Connecting...` but doesn't connect
One of your ends didn't correctly connect to the helper. Stop the transfer on both ends One of your ends didn't correctly connect to the helper. Stop the transfer on both ends
and try again. If it still doesn't work, make SURE the time and date on both ends are within an and try again.
error of <0.1 seconds! Holepunching strongly relies on the time and date matching. (If you have any
suggestion on how I can mitigate this reliance on time, please open an issue!)
## [Relevant XKCD](https://xkcd.com/949) ## [Relevant XKCD](https://xkcd.com/949)

View file

@ -1,5 +1,6 @@
use std::{ use std::{
collections::HashMap, collections::HashMap,
env,
fs::{File, OpenOptions}, fs::{File, OpenOptions},
io::{stdout, Error, Read, Seek, SeekFrom, Write}, io::{stdout, Error, Read, Seek, SeekFrom, Write},
net::*, net::*,
@ -355,12 +356,13 @@ fn holepunch(args: &Vec<String>) -> UdpSocket {
holepunch holepunch
.set_write_timeout(Some(Duration::from_secs(1))) .set_write_timeout(Some(Duration::from_secs(1)))
.unwrap(); .unwrap();
if env::var("QFT_USE_TIMED_HOLEPUNCH").is_ok() {
println!("Waiting..."); println!("Waiting...");
let mut stop = false; let mut stop = false;
while !stop { while !stop {
thread::sleep(Duration::from_millis(500 - (unix_millis() % 500))); thread::sleep(Duration::from_millis(500 - (unix_millis() % 500)));
println!("CONNECT {}", unix_millis()); println!("CONNECT {}", unix_millis());
holepunch.send(&[0]).expect("connection failed"); let _ = holepunch.send(&[0]);
let result = holepunch.recv(&mut [0, 0]); let result = holepunch.recv(&mut [0, 0]);
if result.is_ok() && result.unwrap() == 1 { if result.is_ok() && result.unwrap() == 1 {
holepunch.send(&[0, 0]).expect("connection failed"); holepunch.send(&[0, 0]).expect("connection failed");
@ -370,6 +372,27 @@ fn holepunch(args: &Vec<String>) -> UdpSocket {
} }
} }
} }
} else {
println!("Connecting...");
let mut stop = false;
while !stop {
thread::sleep(Duration::from_millis(500 - (unix_millis() % 500)));
for _ in 0..40 {
let m = unix_millis();
let _ = holepunch.send(&[0]);
thread::sleep(Duration::from_millis((50 - (unix_millis() - m)).max(0)));
}
let mut result = Ok(1);
while result.is_ok() && result.unwrap() == 1 {
result = holepunch.recv(&mut [0, 0]);
}
holepunch.send(&[0, 0]).expect("connection failed");
result = holepunch.recv(&mut [0, 0]);
if result.is_ok() && result.unwrap() == 2 {
stop = true;
}
}
}
println!("Holepunch and connection successful."); println!("Holepunch and connection successful.");
return holepunch; return holepunch;
} }