show login network errors through ui

This commit is contained in:
2025-11-17 18:32:33 -05:00
parent b3c833c667
commit 09a519e5d9
3 changed files with 68 additions and 21 deletions

View File

@@ -6,10 +6,14 @@ use crate::{
transfer::{RecvHandler, recv_uni, send_uni},
},
};
use quinn::{ClientConfig, Connection, Endpoint, crypto::rustls::QuicClientConfig};
use quinn::{
ClientConfig, Connection, Endpoint, IdleTimeout, TransportConfig,
crypto::rustls::QuicClientConfig,
};
use std::{
net::{Ipv6Addr, SocketAddr, SocketAddrV6, ToSocketAddrs},
sync::Arc,
time::Duration,
};
use tokio::sync::mpsc::UnboundedSender;
@@ -22,11 +26,15 @@ pub struct ConnectInfo {
}
pub fn connect(handle: AppHandle, info: ConnectInfo) {
std::thread::spawn(|| {
connect_the(handle, info).unwrap();
std::thread::spawn(move || {
if let Err(msg) = connect_the(handle.clone(), info) {
handle.send(ClientEvent::Err(msg));
}
});
}
type NetResult<T> = Result<T, String>;
type MsgPayload = ClientMsg;
pub struct NetSender {
send: UnboundedSender<MsgPayload>,
@@ -34,7 +42,7 @@ pub struct NetSender {
impl NetSender {
pub fn send(&self, msg: ClientMsg) {
self.send.send(msg).unwrap();
let _ = self.send.send(msg);
}
}
@@ -64,35 +72,50 @@ impl NetSender {
// .map_err(|e| anyhow::anyhow!("failed to connect: {}", e))
// }
async fn connection_no_cert(addr: SocketAddr) -> anyhow::Result<Connection> {
let mut endpoint = Endpoint::client(CLIENT_SOCKET)?;
async fn connection_no_cert(addr: SocketAddr) -> NetResult<Connection> {
let mut endpoint = Endpoint::client(CLIENT_SOCKET).map_err(|e| e.to_string())?;
endpoint.set_default_client_config(ClientConfig::new(Arc::new(QuicClientConfig::try_from(
let quic = QuicClientConfig::try_from(
quinn::rustls::ClientConfig::builder()
.dangerous()
.with_custom_certificate_verifier(SkipServerVerification::new())
.with_no_client_auth(),
)?)));
)
.map_err(|e| e.to_string())?;
let mut transport = TransportConfig::default();
transport.max_idle_timeout(Some(IdleTimeout::try_from(Duration::from_secs(5)).unwrap()));
let mut config = ClientConfig::new(Arc::new(quic));
config.transport_config(transport.into());
endpoint.set_default_client_config(config);
// connect to server
endpoint
.connect(addr, SERVER_NAME)
.unwrap()
.map_err(|e| e.to_string())?
.await
.map_err(|e| anyhow::anyhow!("failed to connect: {e}"))
.map_err(|e| e.to_string())
}
#[tokio::main]
async fn connect_the(handle: AppHandle, info: ConnectInfo) -> anyhow::Result<()> {
async fn connect_the(handle: AppHandle, info: ConnectInfo) -> NetResult<()> {
let (send, mut ui_recv) = tokio::sync::mpsc::unbounded_channel::<MsgPayload>();
let addr = info
.ip
.to_socket_addrs()
.map_err(|e| e.to_string())?
.next()
.ok_or("no addresses found".to_string())?;
let conn = connection_no_cert(addr).await?;
let conn_ = conn.clone();
handle.send(ClientEvent::Connect {
username: info.username,
send: NetSender { send },
});
let addr = info.ip.to_socket_addrs().unwrap().next().unwrap();
let conn = connection_no_cert(addr).await?;
let conn_ = conn.clone();
let recv = ServerRecv { handle };
tokio::spawn(recv_uni(conn_, recv));