diff options
| author | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-12 20:16:56 +0530 |
|---|---|---|
| committer | Kunal Mohan <kunalmohan99@gmail.com> | 2021-05-15 22:14:34 +0530 |
| commit | c5675e965b9edede26cd26f4658c01ae29b7f2cb (patch) | |
| tree | c298a01bc12b6e69e9285784024406a8daef6926 /src/server | |
| parent | Fix memory overflow error and add panic hook for server (diff) | |
| download | sl0ppij-c5675e965b9edede26cd26f4658c01ae29b7f2cb.tar.gz sl0ppij-c5675e965b9edede26cd26f4658c01ae29b7f2cb.tar.xz sl0ppij-c5675e965b9edede26cd26f4658c01ae29b7f2cb.zip | |
Use ClientToServerMsg and ServerToClientMsg for IPC
Diffstat (limited to 'src/server')
| -rw-r--r-- | src/server/mod.rs | 35 | ||||
| -rw-r--r-- | src/server/route.rs | 28 |
2 files changed, 31 insertions, 32 deletions
diff --git a/src/server/mod.rs b/src/server/mod.rs index 884dbee1..96c0a3ce 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -2,7 +2,6 @@ pub mod route; use daemonize::Daemonize; use interprocess::local_socket::LocalSocketListener; -use serde::{Deserialize, Serialize}; use std::sync::{Arc, RwLock}; use std::thread; use std::{path::PathBuf, sync::mpsc}; @@ -10,11 +9,10 @@ use wasmer::Store; use zellij_tile::data::PluginCapabilities; use crate::cli::CliArgs; -use crate::client::ClientInstruction; use crate::common::thread_bus::{Bus, ThreadSenders}; use crate::common::{ - errors::{ContextType, ServerContext}, - input::{actions::Action, options::Options}, + errors::ContextType, + ipc::{ClientToServerMsg, ServerToClientMsg}, os_input_output::{set_permissions, ServerOsApi}, pty::{pty_thread_main, Pty, PtyInstruction}, screen::{screen_thread_main, ScreenInstruction}, @@ -29,17 +27,25 @@ use route::route_thread_main; /// Instructions related to server-side application including the /// ones sent by client to server -#[derive(Serialize, Deserialize, Debug, Clone)] +#[derive(Debug, Clone)] pub enum ServerInstruction { - TerminalResize(PositionAndSize), - NewClient(PositionAndSize, CliArgs, Options), - Action(Action), + NewClient(PositionAndSize, CliArgs), Render(Option<String>), UnblockInputThread, ClientExit, Error(String), } +impl From<ClientToServerMsg> for ServerInstruction { + fn from(instruction: ClientToServerMsg) -> Self { + match instruction { + ClientToServerMsg::ClientExit => ServerInstruction::ClientExit, + ClientToServerMsg::NewClient(pos, opts) => ServerInstruction::NewClient(pos, opts), + _ => unreachable!(), + } + } +} + pub struct SessionMetaData { pub senders: ThreadSenders, screen_thread: Option<thread::JoinHandle<()>>, @@ -65,6 +71,8 @@ pub fn start_server(os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) { .start() .expect("could not daemonize the server process"); + std::env::set_var(&"ZELLIJ", "0"); + let (to_server, server_receiver): SyncChannelWithContext<ServerInstruction> = mpsc::sync_channel(50); let to_server = SenderWithContext::new(SenderType::SyncSender(to_server)); @@ -140,7 +148,7 @@ pub fn start_server(os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) { loop { let (instruction, mut err_ctx) = server_receiver.recv().unwrap(); - err_ctx.add_call(ContextType::IPCServer(ServerContext::from(&instruction))); + err_ctx.add_call(ContextType::IPCServer((&instruction).into())); match instruction { ServerInstruction::NewClient(full_screen_ws, opts) => { let session_data = @@ -156,21 +164,20 @@ pub fn start_server(os_input: Box<dyn ServerOsApi>, socket_path: PathBuf) { .unwrap(); } ServerInstruction::UnblockInputThread => { - os_input.send_to_client(ClientInstruction::UnblockInputThread); + os_input.send_to_client(ServerToClientMsg::UnblockInputThread); } ServerInstruction::ClientExit => { *sessions.write().unwrap() = None; - os_input.send_to_client(ClientInstruction::Exit); + os_input.send_to_client(ServerToClientMsg::Exit); break; } ServerInstruction::Render(output) => { - os_input.send_to_client(ClientInstruction::Render(output)) + os_input.send_to_client(ServerToClientMsg::Render(output)) } ServerInstruction::Error(backtrace) => { - os_input.send_to_client(ClientInstruction::ServerError(backtrace)); + os_input.send_to_client(ServerToClientMsg::ServerError(backtrace)); break; } - _ => panic!("Received unexpected instruction."), } } drop(std::fs::remove_file(&socket_path)); diff --git a/src/server/route.rs b/src/server/route.rs index bd4b1b60..31b82eb5 100644 --- a/src/server/route.rs +++ b/src/server/route.rs @@ -2,9 +2,9 @@ use std::sync::{Arc, RwLock}; use zellij_tile::data::{Event, PluginCapabilities}; -use crate::common::errors::{ContextType, ServerContext}; use crate::common::input::actions::{Action, Direction}; use crate::common::input::handler::get_mode_info; +use crate::common::ipc::ClientToServerMsg; use crate::common::os_input_output::ServerOsApi; use crate::common::pty::PtyInstruction; use crate::common::screen::ScreenInstruction; @@ -193,23 +193,18 @@ pub fn route_thread_main( capabilities: PluginCapabilities, ) { loop { - let (instruction, mut err_ctx) = os_input.recv_from_client(); - err_ctx.add_call(ContextType::IPCServer(ServerContext::from(&instruction))); + let (instruction, err_ctx) = os_input.recv_from_client(); + err_ctx.update_thread_ctx(); let rlocked_sessions = sessions.read().unwrap(); match instruction { - ServerInstruction::ClientExit => { - to_server.send(instruction).unwrap(); + ClientToServerMsg::ClientExit => { + to_server.send(instruction.into()).unwrap(); break; } - ServerInstruction::Action(action) => { - route_action( - action, - rlocked_sessions.as_ref().unwrap(), - &*os_input, - capabilities, - ); + ClientToServerMsg::Action(action) => { + route_action(action, rlocked_sessions.as_ref().unwrap(), &*os_input); } - ServerInstruction::TerminalResize(new_size) => { + ClientToServerMsg::TerminalResize(new_size) => { rlocked_sessions .as_ref() .unwrap() @@ -217,12 +212,9 @@ pub fn route_thread_main( .send_to_screen(ScreenInstruction::TerminalResize(new_size)) .unwrap(); } - ServerInstruction::NewClient(..) => { + ClientToServerMsg::NewClient(..) => { os_input.add_client_sender(); - to_server.send(instruction).unwrap(); - } - _ => { - to_server.send(instruction).unwrap(); + to_server.send(instruction.into()).unwrap(); } } } |
