diff --git a/src/sessions.rs b/src/sessions.rs index 0cc870a7..d05287e4 100644 --- a/src/sessions.rs +++ b/src/sessions.rs @@ -6,7 +6,7 @@ use zellij_utils::{ consts::ZELLIJ_SOCK_DIR, envs, interprocess::local_socket::LocalSocketStream, - ipc::{ClientToServerMsg, IpcSenderWithContext}, + ipc::{ClientToServerMsg, IpcReceiverWithContext, IpcSenderWithContext, ServerToClientMsg}, }; pub(crate) fn get_sessions() -> Result, io::ErrorKind> { @@ -56,14 +56,18 @@ fn assert_socket(name: &str) -> bool { let path = &*ZELLIJ_SOCK_DIR.join(name); match LocalSocketStream::connect(path) { Ok(stream) => { - IpcSenderWithContext::new(stream).send(ClientToServerMsg::ClientExited); - true + let mut sender = IpcSenderWithContext::new(stream); + sender.send(ClientToServerMsg::ConnStatus); + + let mut receiver: IpcReceiverWithContext = sender.get_receiver(); + let (instruction, _) = receiver.recv(); + matches!(instruction, ServerToClientMsg::Connected) } Err(e) if e.kind() == io::ErrorKind::ConnectionRefused => { drop(fs::remove_file(path)); false } - Err(_) => true, + Err(_) => false, } } diff --git a/zellij-client/src/lib.rs b/zellij-client/src/lib.rs index 9bc8828a..0e4f0e20 100644 --- a/zellij-client/src/lib.rs +++ b/zellij-client/src/lib.rs @@ -35,6 +35,7 @@ pub(crate) enum ClientInstruction { UnblockInputThread, Exit(ExitReason), SwitchToMode(InputMode), + Connected, } impl From for ClientInstruction { @@ -46,6 +47,7 @@ impl From for ClientInstruction { ServerToClientMsg::SwitchToMode(input_mode) => { ClientInstruction::SwitchToMode(input_mode) } + ServerToClientMsg::Connected => ClientInstruction::Connected, } } } @@ -58,6 +60,7 @@ impl From<&ClientInstruction> for ClientContext { ClientInstruction::Render(_) => ClientContext::Render, ClientInstruction::UnblockInputThread => ClientContext::UnblockInputThread, ClientInstruction::SwitchToMode(_) => ClientContext::SwitchToMode, + ClientInstruction::Connected => ClientContext::Connected, } } } @@ -323,6 +326,7 @@ pub fn start_client( .send(InputInstruction::SwitchToMode(input_mode)) .unwrap(); } + _ => {} } } diff --git a/zellij-server/src/lib.rs b/zellij-server/src/lib.rs index 9ee0bbfe..82f759db 100644 --- a/zellij-server/src/lib.rs +++ b/zellij-server/src/lib.rs @@ -71,6 +71,7 @@ pub enum ServerInstruction { KillSession, DetachSession(ClientId), AttachClient(ClientAttributes, Options, ClientId), + ConnStatus(ClientId), } impl From<&ServerInstruction> for ServerContext { @@ -85,6 +86,7 @@ impl From<&ServerInstruction> for ServerContext { ServerInstruction::KillSession => ServerContext::KillSession, ServerInstruction::DetachSession(..) => ServerContext::DetachSession, ServerInstruction::AttachClient(..) => ServerContext::AttachClient, + ServerInstruction::ConnStatus(..) => ServerContext::ConnStatus, } } } @@ -523,6 +525,10 @@ pub fn start_server(mut os_input: Box, socket_path: PathBuf) { } break; } + ServerInstruction::ConnStatus(client_id) => { + os_input.send_to_client(client_id, ServerToClientMsg::Connected); + remove_client!(client_id, os_input, session_state); + } } } diff --git a/zellij-server/src/route.rs b/zellij-server/src/route.rs index fc14175a..033a571b 100644 --- a/zellij-server/src/route.rs +++ b/zellij-server/src/route.rs @@ -461,6 +461,10 @@ pub(crate) fn route_thread_main( ClientToServerMsg::KillSession => { to_server.send(ServerInstruction::KillSession).unwrap(); } + ClientToServerMsg::ConnStatus => { + let _ = to_server.send(ServerInstruction::ConnStatus(client_id)); + break; + } } } } diff --git a/zellij-utils/src/errors.rs b/zellij-utils/src/errors.rs index ea8033e5..7ac0df85 100644 --- a/zellij-utils/src/errors.rs +++ b/zellij-utils/src/errors.rs @@ -311,6 +311,7 @@ pub enum ClientContext { Render, ServerError, SwitchToMode, + Connected, } /// Stack call representations corresponding to the different types of [`ServerInstruction`]s. @@ -325,4 +326,5 @@ pub enum ServerContext { KillSession, DetachSession, AttachClient, + ConnStatus, } diff --git a/zellij-utils/src/ipc.rs b/zellij-utils/src/ipc.rs index 2716dd3c..231c5ecd 100644 --- a/zellij-utils/src/ipc.rs +++ b/zellij-utils/src/ipc.rs @@ -69,6 +69,7 @@ pub enum ClientToServerMsg { Action(Action), ClientExited, KillSession, + ConnStatus, } // Types of messages sent from the server to the client @@ -82,6 +83,7 @@ pub enum ServerToClientMsg { UnblockInputThread, Exit(ExitReason), SwitchToMode(InputMode), + Connected, } #[derive(Serialize, Deserialize, Debug, Clone)]