From aef52b06907e9ce7a933b33112ec69ecc4def507 Mon Sep 17 00:00:00 2001
From: Kunal Mohan <kunalmohan99@gmail.com>
Date: Sat, 13 Feb 2021 15:50:56 +0530
Subject: [PATCH] Use interprocess crate, BufReader and BufWriter

---
 src/common/mod.rs | 36 ++++++++++++++++++++----------------
 src/server/mod.rs | 15 +++++++++------
 2 files changed, 29 insertions(+), 22 deletions(-)

diff --git a/src/common/mod.rs b/src/common/mod.rs
index 62d7e702c..4e0eeec1e 100644
--- a/src/common/mod.rs
+++ b/src/common/mod.rs
@@ -9,19 +9,21 @@ pub mod setup;
 pub mod utils;
 pub mod wasm_vm;
 
-use std::io::Write;
-use std::os::unix::net::UnixStream;
+use std::io::{BufWriter, Write};
 use std::path::{Path, PathBuf};
 use std::sync::mpsc;
 use std::thread;
 use std::{collections::HashMap, fs};
-use std::{
-    collections::HashSet,
-    env,
-    io::Write,
-    str::FromStr,
-    sync::{Arc, Mutex},
-};
+
+use crate::panes::PaneId;
+use directories_next::ProjectDirs;
+use input::handler::InputMode;
+use interprocess::local_socket::LocalSocketStream;
+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::layout::Layout;
@@ -125,14 +127,14 @@ thread_local!(
 );
 pub struct IpcSenderWithContext {
     err_ctx: ErrorContext,
-    sender: UnixStream,
+    sender: BufWriter<LocalSocketStream>,
 }
 
 impl IpcSenderWithContext {
     pub fn new() -> Self {
         Self {
             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<()> {
         eprintln!("IpcSender sent {:?}", msg);
         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 {
         Self {
             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;
             }
             AppInstruction::Error(backtrace) => {
-                //let _ = send_server_instructions.send(ApiCommand::Quit);
-                //let _ = ipc_thread.join();
+                let _ = send_server_instructions.send(ApiCommand::Quit);
+                let _ = ipc_thread.join();
                 //IpcSenderWithContext::new().send(ApiCommand::Quit);
                 let _ = send_screen_instructions.send(ScreenInstruction::Quit);
                 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 _ = ipc_thread.join().unwrap();
+    let _ = ipc_thread.join().unwrap();
     //IpcSenderWithContext::new().send(ApiCommand::Quit);
     let _ = send_screen_instructions.send(ScreenInstruction::Quit);
     screen_thread.join().unwrap();
diff --git a/src/server/mod.rs b/src/server/mod.rs
index aa8aad8d4..eb87753c1 100644
--- a/src/server/mod.rs
+++ b/src/server/mod.rs
@@ -12,7 +12,8 @@ use crate::pty_bus::{PtyBus, PtyInstruction};
 use crate::screen::ScreenInstruction;
 use crate::utils::consts::ZELLIJ_IPC_PIPE;
 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::sync::mpsc::channel;
 use std::thread;
@@ -31,8 +32,8 @@ pub fn start_server(
 	);
 
 	std::fs::remove_file(ZELLIJ_IPC_PIPE).ok();
-	let listener = std::os::unix::net::UnixListener::bind(ZELLIJ_IPC_PIPE)
-		.expect("could not listen on ipc socket");
+	let listener =
+		LocalSocketListener::bind(ZELLIJ_IPC_PIPE).expect("could not listen on ipc socket");
 
 	// Don't use default layouts in tests, but do everywhere else
 	#[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(
 	mut send_pty_instructions: SenderWithContext<PtyInstruction>,
 	mut send_app_instructions: SenderWithContext<AppInstruction>,
-	mut stream: std::os::unix::net::UnixStream,
+	mut stream: LocalSocketStream,
 	km: u32,
 ) {
+	//let mut reader = BufReader::new(stream);
 	let mut buffer = [0; 65535]; // TODO: more accurate
 	loop {
 		let bytes = stream