Use interprocess crate, BufReader and BufWriter

This commit is contained in:
Kunal Mohan 2021-02-13 15:50:56 +05:30
parent 2943dc7b3b
commit aef52b0690
2 changed files with 29 additions and 22 deletions

View File

@ -9,19 +9,21 @@ pub mod setup;
pub mod utils; pub mod utils;
pub mod wasm_vm; pub mod wasm_vm;
use std::io::Write; use std::io::{BufWriter, Write};
use std::os::unix::net::UnixStream;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use std::sync::mpsc; use std::sync::mpsc;
use std::thread; use std::thread;
use std::{collections::HashMap, fs}; use std::{collections::HashMap, fs};
use std::{
collections::HashSet, use crate::panes::PaneId;
env, use directories_next::ProjectDirs;
io::Write, use input::handler::InputMode;
str::FromStr, use interprocess::local_socket::LocalSocketStream;
sync::{Arc, Mutex}, use serde::{Deserialize, Serialize};
}; use termion::input::TermRead;
use wasm_vm::PluginEnv;
use wasmer::{ChainableNamedResolver, Instance, Module, Store, Value};
use wasmer_wasi::{Pipe, WasiState};
use crate::cli::CliArgs; use crate::cli::CliArgs;
use crate::layout::Layout; use crate::layout::Layout;
@ -125,14 +127,14 @@ thread_local!(
); );
pub struct IpcSenderWithContext { pub struct IpcSenderWithContext {
err_ctx: ErrorContext, err_ctx: ErrorContext,
sender: UnixStream, sender: BufWriter<LocalSocketStream>,
} }
impl IpcSenderWithContext { impl IpcSenderWithContext {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
err_ctx: ErrorContext::new(), err_ctx: ErrorContext::new(),
sender: UnixStream::connect(ZELLIJ_IPC_PIPE).unwrap(), sender: BufWriter::new(LocalSocketStream::connect(ZELLIJ_IPC_PIPE).unwrap()),
} }
} }
@ -143,7 +145,9 @@ impl IpcSenderWithContext {
pub fn send(&mut self, msg: ApiCommand) -> std::io::Result<()> { pub fn send(&mut self, msg: ApiCommand) -> std::io::Result<()> {
eprintln!("IpcSender sent {:?}", msg); eprintln!("IpcSender sent {:?}", msg);
let command = bincode::serialize(&(self.err_ctx, msg)).unwrap(); let command = bincode::serialize(&(self.err_ctx, msg)).unwrap();
self.sender.write_all(&command) let x = self.sender.write_all(&command);
self.sender.flush();
x
} }
} }
@ -151,7 +155,7 @@ impl std::clone::Clone for IpcSenderWithContext {
fn clone(&self) -> Self { fn clone(&self) -> Self {
Self { Self {
err_ctx: self.err_ctx, err_ctx: self.err_ctx,
sender: UnixStream::connect(ZELLIJ_IPC_PIPE).unwrap(), sender: BufWriter::new(LocalSocketStream::connect(ZELLIJ_IPC_PIPE).unwrap()),
} }
} }
} }
@ -561,8 +565,8 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs, config: Config) {
break; break;
} }
AppInstruction::Error(backtrace) => { AppInstruction::Error(backtrace) => {
//let _ = send_server_instructions.send(ApiCommand::Quit); let _ = send_server_instructions.send(ApiCommand::Quit);
//let _ = ipc_thread.join(); let _ = ipc_thread.join();
//IpcSenderWithContext::new().send(ApiCommand::Quit); //IpcSenderWithContext::new().send(ApiCommand::Quit);
let _ = send_screen_instructions.send(ScreenInstruction::Quit); let _ = send_screen_instructions.send(ScreenInstruction::Quit);
let _ = screen_thread.join(); let _ = screen_thread.join();
@ -591,7 +595,7 @@ pub fn start(mut os_input: Box<dyn OsApi>, opts: CliArgs, config: Config) {
} }
let _ = send_server_instructions.send(ApiCommand::Quit); let _ = send_server_instructions.send(ApiCommand::Quit);
//let _ = ipc_thread.join().unwrap(); let _ = ipc_thread.join().unwrap();
//IpcSenderWithContext::new().send(ApiCommand::Quit); //IpcSenderWithContext::new().send(ApiCommand::Quit);
let _ = send_screen_instructions.send(ScreenInstruction::Quit); let _ = send_screen_instructions.send(ScreenInstruction::Quit);
screen_thread.join().unwrap(); screen_thread.join().unwrap();

View File

@ -12,7 +12,8 @@ use crate::pty_bus::{PtyBus, PtyInstruction};
use crate::screen::ScreenInstruction; use crate::screen::ScreenInstruction;
use crate::utils::consts::ZELLIJ_IPC_PIPE; use crate::utils::consts::ZELLIJ_IPC_PIPE;
use crate::wasm_vm::PluginInstruction; use crate::wasm_vm::PluginInstruction;
use std::io::Read; use interprocess::local_socket::{LocalSocketListener, LocalSocketStream};
use std::io::{BufReader, Read};
use std::path::PathBuf; use std::path::PathBuf;
use std::sync::mpsc::channel; use std::sync::mpsc::channel;
use std::thread; use std::thread;
@ -31,8 +32,8 @@ pub fn start_server(
); );
std::fs::remove_file(ZELLIJ_IPC_PIPE).ok(); std::fs::remove_file(ZELLIJ_IPC_PIPE).ok();
let listener = std::os::unix::net::UnixListener::bind(ZELLIJ_IPC_PIPE) let listener =
.expect("could not listen on ipc socket"); LocalSocketListener::bind(ZELLIJ_IPC_PIPE).expect("could not listen on ipc socket");
// Don't use default layouts in tests, but do everywhere else // Don't use default layouts in tests, but do everywhere else
#[cfg(not(test))] #[cfg(not(test))]
@ -146,18 +147,20 @@ pub fn start_server(
} }
} }
let _ = pty_thread.join(); //let _ = pty_thread.join();
} }
}) })
.unwrap() .unwrap();
pty_thread
} }
fn handle_stream( fn handle_stream(
mut send_pty_instructions: SenderWithContext<PtyInstruction>, mut send_pty_instructions: SenderWithContext<PtyInstruction>,
mut send_app_instructions: SenderWithContext<AppInstruction>, mut send_app_instructions: SenderWithContext<AppInstruction>,
mut stream: std::os::unix::net::UnixStream, mut stream: LocalSocketStream,
km: u32, km: u32,
) { ) {
//let mut reader = BufReader::new(stream);
let mut buffer = [0; 65535]; // TODO: more accurate let mut buffer = [0; 65535]; // TODO: more accurate
loop { loop {
let bytes = stream let bytes = stream