mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-15 20:41:26 +03:00
fix connection logic
dont abort after first ip, add timeout logic
This commit is contained in:
committed by
Ferdinand Schober
parent
92398db918
commit
a5727801e3
@@ -36,8 +36,12 @@ pub(crate) enum LanMouseConnectionError {
|
|||||||
NotConnected,
|
NotConnected,
|
||||||
#[error("emulation is disabled on the target device")]
|
#[error("emulation is disabled on the target device")]
|
||||||
TargetEmulationDisabled,
|
TargetEmulationDisabled,
|
||||||
|
#[error("connection timed out")]
|
||||||
|
Timeout,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DEFAULT_CONNECTION_TIMEOUT: Duration = Duration::from_secs(5);
|
||||||
|
|
||||||
async fn connect(
|
async fn connect(
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
cert: Certificate,
|
cert: Certificate,
|
||||||
@@ -52,9 +56,25 @@ async fn connect(
|
|||||||
extended_master_secret: ExtendedMasterSecretType::Require,
|
extended_master_secret: ExtendedMasterSecretType::Require,
|
||||||
..Default::default()
|
..Default::default()
|
||||||
};
|
};
|
||||||
let dtls_conn = DTLSConn::new(conn, config, true, None).await?;
|
let timeout = tokio::time::sleep(DEFAULT_CONNECTION_TIMEOUT);
|
||||||
log::info!("{addr} connected successfully!");
|
tokio::select! {
|
||||||
Ok((Arc::new(dtls_conn), addr))
|
_ = timeout => {
|
||||||
|
log::warn!("connection to {addr} timed out");
|
||||||
|
Err(LanMouseConnectionError::Timeout)
|
||||||
|
}
|
||||||
|
result = DTLSConn::new(conn, config, true, None) => {
|
||||||
|
match result {
|
||||||
|
Ok(dtls_conn) => {
|
||||||
|
log::info!("{addr} connected successfully!");
|
||||||
|
Ok((Arc::new(dtls_conn), addr))
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
log::warn!("failed to connect to {addr}: {e}");
|
||||||
|
Err(e.into())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
async fn connect_any(
|
async fn connect_any(
|
||||||
@@ -65,8 +85,18 @@ async fn connect_any(
|
|||||||
for &addr in addrs {
|
for &addr in addrs {
|
||||||
joinset.spawn_local(connect(addr, cert.clone()));
|
joinset.spawn_local(connect(addr, cert.clone()));
|
||||||
}
|
}
|
||||||
let conn = joinset.join_next().await;
|
loop {
|
||||||
conn.expect("no addrs to connect").expect("failed to join")
|
match joinset.join_next().await {
|
||||||
|
None => return Err(LanMouseConnectionError::NotConnected),
|
||||||
|
Some(r) => match r.expect("join error") {
|
||||||
|
Ok(conn) => return Ok(conn),
|
||||||
|
Err(e) => {
|
||||||
|
log::warn!("failed to connect: {e}");
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
};
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) struct LanMouseConnection {
|
pub(crate) struct LanMouseConnection {
|
||||||
|
|||||||
Reference in New Issue
Block a user