Skip to content

Commit b7d0ce5

Browse files
madeyeMax Lv
authored and
Max Lv
committed
Try to rebind the socket if connection timed out
1 parent 46e5d10 commit b7d0ce5

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

src/client.rs

+23-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ use tokio::net::{TcpListener, TcpStream};
55

66
use anyhow::{anyhow, Result};
77
use futures::future::try_join;
8-
use log::info;
8+
use log::{error, info};
9+
use quinn::ConnectionError;
910
use quinn::Endpoint;
1011
use structopt::{self, StructOpt};
1112

@@ -115,7 +116,27 @@ async fn transfer(
115116
let new_conn = endpoint
116117
.connect(*remote, &host)?
117118
.await
118-
.map_err(|e| anyhow!("failed to connect: {}", e))?;
119+
.map_err(|e| {
120+
if e == ConnectionError::TimedOut {
121+
let socket = if cfg!(target_os = "windows") {
122+
std::net::UdpSocket::bind("0.0.0.0:0").unwrap()
123+
} else {
124+
std::net::UdpSocket::bind("[::]:0").unwrap()
125+
};
126+
let addr = socket.local_addr().unwrap();
127+
let ret = endpoint.rebind(socket);
128+
match ret {
129+
Ok(_) => {
130+
info!("rebinding to: {}", addr);
131+
}
132+
Err(e) => {
133+
error!("rebind fail: {:?}", e);
134+
}
135+
}
136+
}
137+
anyhow!("failed to connect: {:?}", e)
138+
})
139+
.unwrap();
119140

120141
let quinn::NewConnection {
121142
connection: conn, ..

0 commit comments

Comments
 (0)