Skip to content

Commit

Permalink
Try to rebind the socket if connection timed out
Browse files Browse the repository at this point in the history
  • Loading branch information
madeye authored and Max Lv committed Mar 17, 2022
1 parent 46e5d10 commit b7d0ce5
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ use tokio::net::{TcpListener, TcpStream};

use anyhow::{anyhow, Result};
use futures::future::try_join;
use log::info;
use log::{error, info};
use quinn::ConnectionError;
use quinn::Endpoint;
use structopt::{self, StructOpt};

Expand Down Expand Up @@ -115,7 +116,27 @@ async fn transfer(
let new_conn = endpoint
.connect(*remote, &host)?
.await
.map_err(|e| anyhow!("failed to connect: {}", e))?;
.map_err(|e| {
if e == ConnectionError::TimedOut {
let socket = if cfg!(target_os = "windows") {
std::net::UdpSocket::bind("0.0.0.0:0").unwrap()
} else {
std::net::UdpSocket::bind("[::]:0").unwrap()
};
let addr = socket.local_addr().unwrap();
let ret = endpoint.rebind(socket);
match ret {
Ok(_) => {
info!("rebinding to: {}", addr);
}
Err(e) => {
error!("rebind fail: {:?}", e);
}
}
}
anyhow!("failed to connect: {:?}", e)
})
.unwrap();

let quinn::NewConnection {
connection: conn, ..
Expand Down

0 comments on commit b7d0ce5

Please sign in to comment.