mirror of
https://github.com/feschber/lan-mouse.git
synced 2026-04-13 21:41:28 +03:00
improve error handling
This commit is contained in:
@@ -36,7 +36,7 @@ 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")]
|
#[error("Connection timed out")]
|
||||||
Timeout,
|
Timeout,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -45,10 +45,14 @@ const DEFAULT_CONNECTION_TIMEOUT: Duration = Duration::from_secs(5);
|
|||||||
async fn connect(
|
async fn connect(
|
||||||
addr: SocketAddr,
|
addr: SocketAddr,
|
||||||
cert: Certificate,
|
cert: Certificate,
|
||||||
) -> Result<(Arc<dyn Conn + Sync + Send>, SocketAddr), LanMouseConnectionError> {
|
) -> Result<(Arc<dyn Conn + Sync + Send>, SocketAddr), (SocketAddr, LanMouseConnectionError)> {
|
||||||
log::info!("connecting to {addr} ...");
|
log::info!("connecting to {addr} ...");
|
||||||
let conn = Arc::new(UdpSocket::bind("0.0.0.0:0").await?);
|
let conn = Arc::new(
|
||||||
conn.connect(addr).await?;
|
UdpSocket::bind("0.0.0.0:0")
|
||||||
|
.await
|
||||||
|
.map_err(|e| (addr, e.into()))?,
|
||||||
|
);
|
||||||
|
conn.connect(addr).await.map_err(|e| (addr, e.into()))?;
|
||||||
let config = Config {
|
let config = Config {
|
||||||
certificates: vec![cert],
|
certificates: vec![cert],
|
||||||
server_name: "ignored".to_owned(),
|
server_name: "ignored".to_owned(),
|
||||||
@@ -58,21 +62,10 @@ async fn connect(
|
|||||||
};
|
};
|
||||||
let timeout = tokio::time::sleep(DEFAULT_CONNECTION_TIMEOUT);
|
let timeout = tokio::time::sleep(DEFAULT_CONNECTION_TIMEOUT);
|
||||||
tokio::select! {
|
tokio::select! {
|
||||||
_ = timeout => {
|
_ = timeout => Err((addr, LanMouseConnectionError::Timeout)),
|
||||||
log::warn!("connection to {addr} timed out");
|
result = DTLSConn::new(conn, config, true, None) => match result {
|
||||||
Err(LanMouseConnectionError::Timeout)
|
Ok(dtls_conn) => Ok((Arc::new(dtls_conn), addr)),
|
||||||
}
|
Err(e) => Err((addr, e.into())),
|
||||||
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())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -90,9 +83,8 @@ async fn connect_any(
|
|||||||
None => return Err(LanMouseConnectionError::NotConnected),
|
None => return Err(LanMouseConnectionError::NotConnected),
|
||||||
Some(r) => match r.expect("join error") {
|
Some(r) => match r.expect("join error") {
|
||||||
Ok(conn) => return Ok(conn),
|
Ok(conn) => return Ok(conn),
|
||||||
Err(e) => {
|
Err((a, e)) => {
|
||||||
log::warn!("failed to connect: {e}");
|
log::warn!("failed to connect to {a}: `{e}`")
|
||||||
continue;
|
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|||||||
Reference in New Issue
Block a user