aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorKunal Mohan <kunalmohan99@gmail.com>2021-02-18 11:27:19 +0530
committerKunal Mohan <kunalmohan99@gmail.com>2021-05-04 20:47:49 +0530
commit77682d9ab51d45a1ab765bfd7eb0b527d41782e7 (patch)
tree3fd30d82aa94a4e89326bd3efc6796ec7e42b5cc /src
parentIntroduce serlializable ClientInstruction and Rename ApiCommand (diff)
downloadsl0ppij-77682d9ab51d45a1ab765bfd7eb0b527d41782e7.tar.gz
sl0ppij-77682d9ab51d45a1ab765bfd7eb0b527d41782e7.tar.xz
sl0ppij-77682d9ab51d45a1ab765bfd7eb0b527d41782e7.zip
Use ipmpsc crate for IPC
Diffstat (limited to 'src')
-rw-r--r--src/common/mod.rs39
-rw-r--r--src/common/pty_bus.rs23
-rw-r--r--src/main.rs8
-rw-r--r--src/server/mod.rs145
4 files changed, 90 insertions, 125 deletions
diff --git a/src/common/mod.rs b/src/common/mod.rs
index 0bf235f1..34249f86 100644
--- a/src/common/mod.rs
+++ b/src/common/mod.rs
@@ -18,7 +18,7 @@ use std::{collections::HashMap, fs};
use crate::panes::PaneId;
use directories_next::ProjectDirs;
use input::handler::InputMode;
-use interprocess::local_socket::LocalSocketStream;
+use ipmpsc::{Sender as IpcSender, SharedRingBuffer};
use serde::{Deserialize, Serialize};
use termion::input::TermRead;
use wasm_vm::PluginEnv;
@@ -132,37 +132,32 @@ thread_local!(
/// stack in the form of an [`ErrorContext`].
static OPENCALLS: RefCell<ErrorContext> = RefCell::default()
);
+#[derive(Clone)]
pub struct IpcSenderWithContext {
err_ctx: ErrorContext,
- sender: BufWriter<LocalSocketStream>,
+ sender: IpcSender,
}
impl IpcSenderWithContext {
- pub fn new() -> Self {
+ pub fn new(buffer: SharedRingBuffer) -> Self {
Self {
err_ctx: ErrorContext::new(),
- sender: BufWriter::new(LocalSocketStream::connect(ZELLIJ_IPC_PIPE).unwrap()),
+ sender: IpcSender::new(buffer),
}
}
- pub fn update(&mut self, ctx: ErrorContext) {
- self.err_ctx = ctx;
+ // This is expensive. Use this only if a buffer is not available.
+ // Otherwise clone the buffer and use `new()`
+ pub fn to_server() -> Self {
+ Self::new(SharedRingBuffer::open(ZELLIJ_IPC_PIPE).unwrap())
}
- pub fn send(&mut self, msg: ServerInstruction) -> std::io::Result<()> {
- let command = bincode::serialize(&(self.err_ctx, msg)).unwrap();
- let x = self.sender.write_all(&command);
- self.sender.flush();
- x
+ pub fn update(&mut self, ctx: ErrorContext) {
+ self.err_ctx = ctx;
}
-}
-impl std::clone::Clone for IpcSenderWithContext {
- fn clone(&self) -> Self {
- Self {
- err_ctx: self.err_ctx,
- sender: BufWriter::new(LocalSocketStream::connect(ZELLIJ_IPC_PIPE).unwrap()),
- }
+ pub fn send<T: Serialize>(&mut self, msg: T) -> ipmpsc::Result<()> {
+ self.sender.send(&(self.err_ctx, msg))
}
}
@@ -215,7 +210,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs, config: Config) {
let send_app_instructions =
SenderWithContext::new(SenderType::SyncSender(send_app_instructions));
- let pty_thread = start_server(
+ let ipc_thread = start_server(
os_input.clone(),
opts.clone(),
command_is_executing.clone(),
@@ -555,7 +550,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs, config: Config) {
}
});
- let mut send_server_instructions = IpcSenderWithContext::new();
+ let mut send_server_instructions = IpcSenderWithContext::to_server();
#[warn(clippy::never_loop)]
loop {
@@ -572,7 +567,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs, config: Config) {
}
AppInstruction::Error(backtrace) => {
let _ = send_server_instructions.send(ServerInstruction::Quit);
- //let _ = pty_thread.join();
+ //let _ = ipc_thread.join();
let _ = send_screen_instructions.send(ScreenInstruction::Quit);
let _ = screen_thread.join();
let _ = send_plugin_instructions.send(PluginInstruction::Quit);
@@ -599,7 +594,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs, config: Config) {
}
let _ = send_server_instructions.send(ServerInstruction::Quit);
- //let _ = pty_thread.join().unwrap();
+ //let _ = ipc_thread.join().unwrap();
let _ = send_screen_instructions.send(ScreenInstruction::Quit);
screen_thread.join().unwrap();
let _ = send_plugin_instructions.send(PluginInstruction::Quit);
diff --git a/src/common/pty_bus.rs b/src/common/pty_bus.rs
index 4cbd5675..52144454 100644
--- a/src/common/pty_bus.rs
+++ b/src/common/pty_bus.rs
@@ -193,12 +193,16 @@ pub struct PtyBus {
pub send_server_instructions: IpcSenderWithContext,
}
-fn stream_terminal_bytes(pid: RawFd, os_input: Box<dyn OsApi>, debug: bool) -> JoinHandle<()> {
+fn stream_terminal_bytes(
+ pid: RawFd,
+ os_input: Box<dyn OsApi>,
+ mut send_server_instructions: IpcSenderWithContext,
+ debug: bool,
+) -> JoinHandle<()> {
let mut err_ctx = OPENCALLS.with(|ctx| *ctx.borrow());
task::spawn({
async move {
err_ctx.add_call(ContextType::AsyncTask);
- let mut send_server_instructions = IpcSenderWithContext::new();
send_server_instructions.update(err_ctx);
let mut vte_parser = vte::Parser::new();
let mut vte_event_sender = VteEventSender::new(pid, send_server_instructions.clone());
@@ -286,8 +290,12 @@ impl PtyBus {
pub fn spawn_terminal(&mut self, file_to_open: Option<PathBuf>) -> RawFd {
let (pid_primary, pid_secondary): (RawFd, RawFd) =
self.os_input.spawn_terminal(file_to_open);
- let task_handle =
- stream_terminal_bytes(pid_primary, self.os_input.clone(), self.debug_to_file);
+ let task_handle = stream_terminal_bytes(
+ pid_primary,
+ self.os_input.clone(),
+ self.send_server_instructions.clone(),
+ self.debug_to_file,
+ );
self.task_handles.insert(pid_primary, task_handle);
self.id_to_child_pid.insert(pid_primary, pid_secondary);
pid_primary
@@ -307,7 +315,12 @@ impl PtyBus {
)))
.unwrap();
for id in new_pane_pids {
- let task_handle = stream_terminal_bytes(id, self.os_input.clone(), self.debug_to_file);
+ let task_handle = stream_terminal_bytes(
+ id,
+ self.os_input.clone(),
+ self.send_server_instructions.clone(),
+ self.debug_to_file,
+ );
self.task_handles.insert(id, task_handle);
}
}
diff --git a/src/main.rs b/src/main.rs
index 78696a8d..2ad38135 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -62,13 +62,13 @@ pub fn main() {
if let Some(split_dir) = opts.split {
match split_dir {
'h' => {
- let mut send_server_instructions = IpcSenderWithContext::new();
+ let mut send_server_instructions = IpcSenderWithContext::to_server();
send_server_instructions
.send(ServerInstruction::SplitHorizontally)
.unwrap();
}
'v' => {
- let mut send_server_instructions = IpcSenderWithContext::new();
+ let mut send_server_instructions = IpcSenderWithContext::to_server();
send_server_instructions
.send(ServerInstruction::SplitVertically)
.unwrap();
@@ -76,12 +76,12 @@ pub fn main() {
_ => {}
};
} else if opts.move_focus {
- let mut send_server_instructions = IpcSenderWithContext::new();
+ let mut send_server_instructions = IpcSenderWithContext::to_server();
send_server_instructions
.send(ServerInstruction::MoveFocus)
.unwrap();
} else if let Some(file_to_open) = opts.open_file {
- let mut send_server_instructions = IpcSenderWithContext::new();
+ let mut send_server_instructions = IpcSenderWithContext::to_server();
send_server_instructions
.send(ServerInstruction::OpenFile(file_to_open))
.unwrap();
diff --git a/src/server/mod.rs b/src/server/mod.rs
index dd65e81f..27b24736 100644
--- a/src/server/mod.rs
+++ b/src/server/mod.rs
@@ -12,7 +12,7 @@ use crate::pty_bus::{PtyBus, PtyInstruction};
use crate::screen::ScreenInstruction;
use crate::utils::consts::ZELLIJ_IPC_PIPE;
use crate::wasm_vm::PluginInstruction;
-use interprocess::local_socket::{LocalSocketListener, LocalSocketStream};
+use ipmpsc::{Receiver, SharedRingBuffer};
use std::io::{BufReader, Read};
use std::path::PathBuf;
use std::sync::mpsc::channel;
@@ -32,8 +32,7 @@ pub fn start_server(
);
std::fs::remove_file(ZELLIJ_IPC_PIPE).ok();
- let listener =
- LocalSocketListener::bind(ZELLIJ_IPC_PIPE).expect("could not listen on ipc socket");
+ let server_buffer = SharedRingBuffer::create(ZELLIJ_IPC_PIPE, 8192).unwrap();
// Don't use default layouts in tests, but do everywhere else
#[cfg(not(test))]
@@ -42,7 +41,7 @@ pub fn start_server(
let default_layout = None;
let maybe_layout = opts.layout.or(default_layout);
- let send_server_instructions = IpcSenderWithContext::new();
+ let send_server_instructions = IpcSenderWithContext::new(server_buffer.clone());
let mut pty_bus = PtyBus::new(
receive_pty_instructions,
@@ -120,98 +119,56 @@ pub fn start_server(
thread::Builder::new()
.name("ipc_server".to_string())
.spawn({
- move || {
- let mut km = 0;
- for stream in listener.incoming() {
- match stream {
- Ok(stream) => {
- let send_app_instructions = send_app_instructions.clone();
- let send_pty_instructions = send_pty_instructions.clone();
- let nm = format!("{}", km);
- thread::Builder::new()
- .name(nm)
- .spawn(move || {
- handle_stream(
- send_pty_instructions,
- send_app_instructions,
- stream,
- km,
- );
- })
- .unwrap();
- km += 1;
- }
- Err(err) => {
- panic!("err {:?}", err);
- }
+ let recv_server_instructions = Receiver::new(server_buffer);
+ move || loop {
+ let (mut err_ctx, decoded): (ErrorContext, ServerInstruction) =
+ recv_server_instructions.recv().unwrap();
+ err_ctx.add_call(ContextType::IPCServer);
+ send_pty_instructions.update(err_ctx);
+ send_app_instructions.update(err_ctx);
+
+ match decoded {
+ ServerInstruction::OpenFile(file_name) => {
+ let path = PathBuf::from(file_name);
+ send_pty_instructions
+ .send(PtyInstruction::SpawnTerminal(Some(path)))
+ .unwrap();
+ }
+ ServerInstruction::SplitHorizontally => {
+ send_pty_instructions
+ .send(PtyInstruction::SpawnTerminalHorizontally(None))
+ .unwrap();
+ }
+ ServerInstruction::SplitVertically => {
+ send_pty_instructions
+ .send(PtyInstruction::SpawnTerminalVertically(None))
+ .unwrap();
+ }
+ ServerInstruction::MoveFocus => {
+ send_app_instructions
+ .send(AppInstruction::ToScreen(ScreenInstruction::MoveFocus))
+ .unwrap();
+ }
+ ServerInstruction::ToPty(instruction) => {
+ send_pty_instructions.send(instruction).unwrap();
+ }
+ ServerInstruction::ToScreen(instruction) => {
+ send_app_instructions
+ .send(AppInstruction::ToScreen(instruction))
+ .unwrap();
+ }
+ ServerInstruction::ClosePluginPane(pid) => {
+ send_app_instructions
+ .send(AppInstruction::ToPlugin(PluginInstruction::Unload(pid)))
+ .unwrap();
+ }
+ ServerInstruction::Quit => {
+ let _ = send_pty_instructions.send(PtyInstruction::Quit);
+ let _ = pty_thread.join();
+ break;
}
}
}
})
- .unwrap();
- pty_thread
-}
-
-fn handle_stream(
- mut send_pty_instructions: SenderWithContext<PtyInstruction>,
- mut send_app_instructions: SenderWithContext<AppInstruction>,
- mut stream: LocalSocketStream,
- km: u32,
-) {
- let mut reader = BufReader::new(stream);
- let mut buffer = [0; 65535]; // TODO: more accurate
- loop {
- let bytes = reader
- .read(&mut buffer)
- .expect("failed to parse ipc message");
- let (mut err_ctx, decoded): (ErrorContext, ServerInstruction) =
- match bincode::deserialize(&buffer[..bytes]) {
- Ok(d) => d,
- Err(_) => break,
- };
- err_ctx.add_call(ContextType::IPCServer);
- send_pty_instructions.update(err_ctx);
- send_app_instructions.update(err_ctx);
-
- match decoded {
- ServerInstruction::OpenFile(file_name) => {
- let path = PathBuf::from(file_name);
- send_pty_instructions
- .send(PtyInstruction::SpawnTerminal(Some(path)))
- .unwrap();
- }
- ServerInstruction::SplitHorizontally => {
- send_pty_instructions
- .send(PtyInstruction::SpawnTerminalHorizontally(None))
- .unwrap();
- }
- ServerInstruction::SplitVertically => {
- send_pty_instructions
- .send(PtyInstruction::SpawnTerminalVertically(None))
- .unwrap();
- }
- ServerInstruction::MoveFocus => {
- send_app_instructions
- .send(AppInstruction::ToScreen(ScreenInstruction::MoveFocus))
- .unwrap();
- }
- ServerInstruction::ToPty(instruction) => {
- send_pty_instructions.send(instruction).unwrap();
- }
- ServerInstruction::ToScreen(instruction) => {
- send_app_instructions
- .send(AppInstruction::ToScreen(instruction))
- .unwrap();
- }
- ServerInstruction::ClosePluginPane(pid) => {
- send_app_instructions
- .send(AppInstruction::ToPlugin(PluginInstruction::Unload(pid)))
- .unwrap();
- }
- ServerInstruction::Quit => {
- let _ = send_pty_instructions.send(PtyInstruction::Quit);
- break;
- }
- }
- }
+ .unwrap()
}