persistence + proper disconnect

This commit is contained in:
2025-11-28 17:29:33 -05:00
parent 029d62cb53
commit 7557507f27
16 changed files with 413 additions and 67 deletions

View File

@@ -2,7 +2,7 @@
use crate::{
app::App,
net::NetSender,
net::{NetCtrlMsg, NetHandle, NetSender, NetState},
rsc::{CLIENT_DATA, ClientData},
ui::*,
};
@@ -11,7 +11,7 @@ use arboard::Clipboard;
use input::Input;
use iris::prelude::*;
use openworm::{
net::{ClientMsg, Msg, ServerMsg, install_crypto_provider},
net::{ClientMsg, NetMsg, ServerMsg, install_crypto_provider},
rsc::DataDir,
};
use render::Renderer;
@@ -53,7 +53,8 @@ pub struct Client {
data: ClientData,
handle: AppHandle,
error: Option<WidgetId<WidgetPtr>>,
msgs: Vec<Msg>,
net: NetState,
msgs: Vec<NetMsg>,
ime: usize,
last_click: Instant,
}
@@ -78,6 +79,7 @@ impl Client {
dir,
channel: None,
focus: None,
net: Default::default(),
username: "<unknown>".to_string(),
clipboard: Clipboard::new().unwrap(),
error: None,
@@ -94,6 +96,13 @@ impl Client {
ClientEvent::Connect { send, username } => {
self.username = username;
send.send(ClientMsg::RequestMsgs);
let NetState::Connecting(th) = self.net.take() else {
panic!("invalid state");
};
self.net = NetState::Connected(NetHandle {
send: send.clone(),
thread: th,
});
main_view(self, send).set_root(&mut self.ui);
}
ClientEvent::ServerMsg(msg) => match msg {
@@ -213,6 +222,10 @@ impl Client {
}
pub fn exit(&mut self) {
if let Some(handle) = self.net.take_connection() {
handle.send.send(NetCtrlMsg::Exit);
let _ = handle.thread.join();
}
self.dir.save(CLIENT_DATA, &self.data);
}
}