allow writing to a muxed tab

This commit is contained in:
Wez Furlong committed 2019-06-09 13:00:36 -07:00
1 parent 661daa4f77
commit ee54864d6a
5 files changed
+58 -2

No files matched your search

+1
View File
@@ -71,4 +71,5 @@ impl Client {
GetCoarseTabRenderableDataResponse
);
rpc!(spawn, Spawn, SpawnResponse);
rpc!(write_to_tab, WriteToTab, UnitResponse);
}
+11
View File
@@ -215,8 +215,13 @@ pdu! {
GetCoarseTabRenderableDataResponse: 6,
Spawn: 7,
SpawnResponse: 8,
WriteToTab: 9,
UnitResponse: 10,
}
#[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct UnitResponse {}
#[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct Ping {}
#[derive(Deserialize, Serialize, PartialEq, Debug)]
@@ -268,6 +273,12 @@ pub struct SpawnResponse {
pub tab_id: TabId,
}
#[derive(Deserialize, Serialize, PartialEq, Debug)]
pub struct WriteToTab {
pub tab_id: TabId,
pub data: Vec<u8>,
}
#[cfg(test)]
mod test {
use super::*;
+1 -1
View File
@@ -3,7 +3,7 @@ use crate::mux::tab::Tab;
use crate::server::client::Client;
use crate::server::codec::Spawn;
use crate::server::tab::ClientTab;
use failure::{bail, Fallible};
use failure::Fallible;
use portable_pty::{CommandBuilder, PtySize};
use std::rc::Rc;
use std::sync::{Arc, Mutex};
+14
View File
@@ -105,6 +105,19 @@ impl ClientSession {
.encode(&mut self.stream, decoded.serial)?;
}
Pdu::WriteToTab(WriteToTab { tab_id, data }) => {
Future::with_executor(self.executor.clone_executor(), move || {
let mux = Mux::get().unwrap();
let tab = mux
.get_tab(tab_id)
.ok_or_else(|| format_err!("no such tab {}", tab_id))?;
tab.writer().write_all(&data)?;
Ok(())
})
.wait()?;
Pdu::UnitResponse(UnitResponse {}).encode(&mut self.stream, decoded.serial)?;
}
Pdu::Spawn(spawn) => {
let result = Future::with_executor(self.executor.clone_executor(), move || {
let mux = Mux::get().unwrap();
@@ -123,6 +136,7 @@ impl ClientSession {
| Pdu::ListTabsResponse { .. }
| Pdu::GetCoarseTabRenderableDataResponse { .. }
| Pdu::SpawnResponse { .. }
| Pdu::UnitResponse { .. }
| Pdu::Invalid { .. } => {}
}
}
+31 -1
View File
@@ -1,6 +1,7 @@
use crate::mux::domain::DomainId;
use crate::mux::renderable::Renderable;
use crate::mux::tab::{alloc_tab_id, Tab, TabId};
use crate::server::codec::WriteToTab;
use crate::server::domain::ClientInner;
use failure::{bail, Fallible};
use portable_pty::PtySize;
@@ -18,16 +19,23 @@ pub struct ClientTab {
local_tab_id: TabId,
remote_tab_id: TabId,
renderable: RefCell<RenderableState>,
writer: RefCell<TabWriter>,
}
impl ClientTab {
pub fn new(client: &Arc<ClientInner>, remote_tab_id: TabId) -> Self {
let local_tab_id = alloc_tab_id();
let writer = TabWriter {
client: Arc::clone(client),
remote_tab_id,
};
Self {
client: Arc::clone(client),
remote_tab_id,
local_tab_id,
renderable: RefCell::new(RenderableState { dirty: true }),
writer: RefCell::new(writer),
}
}
}
@@ -53,7 +61,7 @@ impl Tab for ClientTab {
}
fn writer(&self) -> RefMut<dyn std::io::Write> {
panic!("ClientTab::writer not impl");
self.writer.borrow_mut()
}
fn resize(&self, size: PtySize) -> Fallible<()> {
@@ -118,3 +126,25 @@ impl Renderable for RenderableState {
(24, 80)
}
}
struct TabWriter {
client: Arc<ClientInner>,
remote_tab_id: TabId,
}
impl std::io::Write for TabWriter {
fn write(&mut self, data: &[u8]) -> Result<usize, std::io::Error> {
let mut client = self.client.client.lock().unwrap();
client
.write_to_tab(WriteToTab {
tab_id: self.remote_tab_id,
data: data.to_vec(),
})
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, format!("{}", e)))?;
Ok(data.len())
}
fn flush(&mut self) -> Result<(), std::io::Error> {
Ok(())
}
}