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

@@ -21,6 +21,7 @@ pub use app::AppHandle;
pub enum ClientEvent { pub enum ClientEvent {
Connect { send: NetSender, username: String }, Connect { send: NetSender, username: String },
ServerMsg(ServerMsg), ServerMsg(ServerMsg),
Err(String),
} }
pub struct Client { pub struct Client {
@@ -34,6 +35,7 @@ pub struct Client {
dir: DataDir, dir: DataDir,
data: ClientData, data: ClientData,
handle: AppHandle, handle: AppHandle,
error: Option<WidgetId<WidgetPtr>>,
} }
impl Client { impl Client {
@@ -52,6 +54,7 @@ impl Client {
focus: None, focus: None,
username: "<unknown>".to_string(), username: "<unknown>".to_string(),
clipboard: Clipboard::new().unwrap(), clipboard: Clipboard::new().unwrap(),
error: None,
}; };
ui::init(&mut s); ui::init(&mut s);
s s
@@ -80,6 +83,11 @@ impl Client {
} }
} }
}, },
ClientEvent::Err(msg) => {
if let Some(err) = &self.error {
self.ui[err].inner = Some(ui::error(&mut self.ui, &msg));
}
}
} }
} }

View File

@@ -27,7 +27,7 @@ pub fn init(client: &mut Client) {
login_screen(client).set_root(&mut client.ui); login_screen(client).set_root(&mut client.ui);
} }
pub fn main_view(client: &mut Client, network: NetSender) -> WidgetId<AnyWidget> { pub fn main_view(client: &mut Client, network: NetSender) -> WidgetId {
let msg_panel = msg_panel(client, network); let msg_panel = msg_panel(client, network);
let side_bar = rect(Color::BLACK.brighter(0.05)).width(80); let side_bar = rect(Color::BLACK.brighter(0.05)).width(80);
@@ -37,9 +37,22 @@ pub fn main_view(client: &mut Client, network: NetSender) -> WidgetId<AnyWidget>
.any() .any()
} }
fn login_screen(client: &mut Client) -> WidgetId<AnyWidget> { pub fn error(ui: &mut Ui, msg: &str) -> WidgetId {
text(msg)
.size(20)
.color(Color::RED.brighter(0.3))
.pad(10)
.add(ui)
.any()
}
fn login_screen(client: &mut Client) -> WidgetId {
let Client { let Client {
ui, handle, data, .. ui,
handle,
data,
error,
..
} = client; } = client;
let mut field = |name| text(name).editable().size(20).add(ui); let mut field = |name| text(name).editable().size(20).add(ui);
@@ -69,7 +82,7 @@ fn login_screen(client: &mut Client) -> WidgetId<AnyWidget> {
connect(handle.clone(), ConnectInfo { ip, username }); connect(handle.clone(), ConnectInfo { ip, username });
}) })
.height(40); .height(40);
( let modal = (
text("login").size(30), text("login").size(30),
fbx(ip fbx(ip
.id_on(Edited, |id, client: &mut Client, _| { .id_on(Edited, |id, client: &mut Client, _| {
@@ -89,9 +102,12 @@ fn login_screen(client: &mut Client) -> WidgetId<AnyWidget> {
.pad(15) .pad(15)
.background(rect(Color::BLACK.brighter(0.2)).radius(15)) .background(rect(Color::BLACK.brighter(0.2)).radius(15))
.width(400) .width(400)
.align(Align::Center) .align(Align::Center);
.add(ui)
.any() let err = WidgetPtr::default().add(ui);
client.error = Some(err.clone());
(modal, err.align(Align::Top)).stack().add(ui).any()
} }
pub fn msg_widget(msg: Msg) -> impl WidgetLike<FnTag> { pub fn msg_widget(msg: Msg) -> impl WidgetLike<FnTag> {

View File

@@ -6,10 +6,14 @@ use crate::{
transfer::{RecvHandler, recv_uni, send_uni}, 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::{ use std::{
net::{Ipv6Addr, SocketAddr, SocketAddrV6, ToSocketAddrs}, net::{Ipv6Addr, SocketAddr, SocketAddrV6, ToSocketAddrs},
sync::Arc, sync::Arc,
time::Duration,
}; };
use tokio::sync::mpsc::UnboundedSender; use tokio::sync::mpsc::UnboundedSender;
@@ -22,11 +26,15 @@ pub struct ConnectInfo {
} }
pub fn connect(handle: AppHandle, info: ConnectInfo) { pub fn connect(handle: AppHandle, info: ConnectInfo) {
std::thread::spawn(|| { std::thread::spawn(move || {
connect_the(handle, info).unwrap(); if let Err(msg) = connect_the(handle.clone(), info) {
handle.send(ClientEvent::Err(msg));
}
}); });
} }
type NetResult<T> = Result<T, String>;
type MsgPayload = ClientMsg; type MsgPayload = ClientMsg;
pub struct NetSender { pub struct NetSender {
send: UnboundedSender<MsgPayload>, send: UnboundedSender<MsgPayload>,
@@ -34,7 +42,7 @@ pub struct NetSender {
impl NetSender { impl NetSender {
pub fn send(&self, msg: ClientMsg) { 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)) // .map_err(|e| anyhow::anyhow!("failed to connect: {}", e))
// } // }
async fn connection_no_cert(addr: SocketAddr) -> anyhow::Result<Connection> { async fn connection_no_cert(addr: SocketAddr) -> NetResult<Connection> {
let mut endpoint = Endpoint::client(CLIENT_SOCKET)?; 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() quinn::rustls::ClientConfig::builder()
.dangerous() .dangerous()
.with_custom_certificate_verifier(SkipServerVerification::new()) .with_custom_certificate_verifier(SkipServerVerification::new())
.with_no_client_auth(), .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 // connect to server
endpoint endpoint
.connect(addr, SERVER_NAME) .connect(addr, SERVER_NAME)
.unwrap() .map_err(|e| e.to_string())?
.await .await
.map_err(|e| anyhow::anyhow!("failed to connect: {e}")) .map_err(|e| e.to_string())
} }
#[tokio::main] #[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 (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 { handle.send(ClientEvent::Connect {
username: info.username, username: info.username,
send: NetSender { send }, 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 }; let recv = ServerRecv { handle };
tokio::spawn(recv_uni(conn_, recv)); tokio::spawn(recv_uni(conn_, recv));