1
1
mirror of https://github.com/wez/wezterm.git synced 2024-11-23 06:54:45 +03:00

ssh: adopt dispatch helper in a couple more places

This commit is contained in:
Wez Furlong 2021-10-19 20:15:34 -07:00
parent 5ada8e20cc
commit 561eaa69be
3 changed files with 49 additions and 50 deletions

View File

@ -3,7 +3,7 @@ use crate::sessioninner::{ChannelId, ChannelInfo, DescriptorState};
use crate::sessionwrap::SessionWrap;
use filedescriptor::{socketpair, FileDescriptor};
use portable_pty::{ExitStatus, PtySize};
use smol::channel::{bounded, Receiver, Sender, TryRecvError};
use smol::channel::{bounded, Receiver, TryRecvError};
use std::collections::HashMap;
use std::collections::VecDeque;
use std::io::{Read, Write};
@ -15,14 +15,12 @@ pub(crate) struct NewPty {
pub size: PtySize,
pub command_line: Option<String>,
pub env: Option<HashMap<String, String>>,
pub reply: Sender<(SshPty, SshChildProcess)>,
}
#[derive(Debug)]
pub(crate) struct ResizePty {
pub channel: ChannelId,
pub size: PtySize,
pub reply: Option<Sender<()>>,
}
#[derive(Debug)]
@ -49,11 +47,13 @@ impl portable_pty::MasterPty for SshPty {
self.tx
.as_ref()
.unwrap()
.try_send(SessionRequest::ResizePty(ResizePty {
channel: self.channel,
size,
reply: None,
}))?;
.try_send(SessionRequest::ResizePty(
ResizePty {
channel: self.channel,
size,
},
None,
))?;
*self.size.lock().unwrap() = size;
Ok(())
@ -165,7 +165,11 @@ impl portable_pty::Child for SshChildProcess {
}
impl crate::sessioninner::SessionInner {
pub fn new_pty(&mut self, sess: &mut SessionWrap, newpty: &NewPty) -> anyhow::Result<()> {
pub fn new_pty(
&mut self,
sess: &mut SessionWrap,
newpty: NewPty,
) -> anyhow::Result<(SshPty, SshChildProcess)> {
sess.set_blocking(true);
let mut channel = sess.open_session()?;
@ -182,7 +186,7 @@ impl crate::sessioninner::SessionInner {
}
*/
channel.request_pty(newpty)?;
channel.request_pty(&newpty)?;
if let Some(env) = &newpty.env {
for (key, val) in env {
@ -254,21 +258,17 @@ impl crate::sessioninner::SessionInner {
],
};
newpty.reply.try_send((ssh_pty, child))?;
self.channels.insert(channel_id, info);
Ok(())
Ok((ssh_pty, child))
}
pub fn resize_pty(&mut self, resize: &ResizePty) -> anyhow::Result<()> {
pub fn resize_pty(&mut self, resize: ResizePty) -> anyhow::Result<()> {
let info = self
.channels
.get_mut(&resize.channel)
.ok_or_else(|| anyhow::anyhow!("invalid channel id {}", resize.channel))?;
info.channel.resize_pty(resize)?;
if let Some(reply) = resize.reply.as_ref() {
reply.try_send(())?;
}
info.channel.resize_pty(&resize)?;
Ok(())
}
}

View File

@ -47,9 +47,9 @@ impl SessionSender {
#[derive(Debug)]
pub(crate) enum SessionRequest {
NewPty(NewPty),
ResizePty(ResizePty),
Exec(Exec),
NewPty(NewPty, Sender<anyhow::Result<(SshPty, SshChildProcess)>>),
ResizePty(ResizePty, Option<Sender<anyhow::Result<()>>>),
Exec(Exec, Sender<anyhow::Result<ExecResult>>),
Sftp(SftpRequest),
SignalChannel(SignalChannel),
}
@ -64,7 +64,6 @@ pub(crate) struct SignalChannel {
pub(crate) struct Exec {
pub command_line: String,
pub env: Option<HashMap<String, String>>,
pub reply: Sender<ExecResult>,
}
#[derive(Clone)]
@ -115,15 +114,17 @@ impl Session {
) -> anyhow::Result<(SshPty, SshChildProcess)> {
let (reply, rx) = bounded(1);
self.tx
.send(SessionRequest::NewPty(NewPty {
term: term.to_string(),
size,
command_line: command_line.map(|s| s.to_string()),
env,
.send(SessionRequest::NewPty(
NewPty {
term: term.to_string(),
size,
command_line: command_line.map(|s| s.to_string()),
env,
},
reply,
}))
))
.await?;
let (mut ssh_pty, mut child) = rx.recv().await?;
let (mut ssh_pty, mut child) = rx.recv().await??;
ssh_pty.tx.replace(self.tx.clone());
child.tx.replace(self.tx.clone());
Ok((ssh_pty, child))
@ -136,13 +137,15 @@ impl Session {
) -> anyhow::Result<ExecResult> {
let (reply, rx) = bounded(1);
self.tx
.send(SessionRequest::Exec(Exec {
command_line: command_line.to_string(),
env,
.send(SessionRequest::Exec(
Exec {
command_line: command_line.to_string(),
env,
},
reply,
}))
))
.await?;
let mut exec = rx.recv().await?;
let mut exec = rx.recv().await??;
exec.child.tx.replace(self.tx.clone());
Ok(exec)
}

View File

@ -404,23 +404,20 @@ impl SessionInner {
Ok(req) => {
sess.set_blocking(true);
let res = match req {
SessionRequest::NewPty(newpty) => {
if let Err(err) = self.new_pty(sess, &newpty) {
log::error!("{:?} -> error: {:#}", newpty, err);
SessionRequest::NewPty(newpty, reply) => {
dispatch(reply, || self.new_pty(sess, newpty), "NewPty")
}
SessionRequest::ResizePty(resize, Some(reply)) => {
dispatch(reply, || self.resize_pty(resize), "resize_pty")
}
SessionRequest::ResizePty(resize, None) => {
if let Err(err) = self.resize_pty(resize) {
log::error!("error in resize_pty: {:#}", err);
}
Ok(true)
}
SessionRequest::ResizePty(resize) => {
if let Err(err) = self.resize_pty(&resize) {
log::error!("{:?} -> error: {:#}", resize, err);
}
Ok(true)
}
SessionRequest::Exec(exec) => {
if let Err(err) = self.exec(sess, &exec) {
log::error!("{:?} -> error: {:#}", exec, err);
}
Ok(true)
SessionRequest::Exec(exec, reply) => {
dispatch(reply, || self.exec(sess, exec), "exec")
}
SessionRequest::SignalChannel(info) => {
if let Err(err) = self.signal_channel(&info) {
@ -625,7 +622,7 @@ impl SessionInner {
Ok(())
}
pub fn exec(&mut self, sess: &mut SessionWrap, exec: &Exec) -> anyhow::Result<()> {
pub fn exec(&mut self, sess: &mut SessionWrap, exec: Exec) -> anyhow::Result<ExecResult> {
let mut channel = sess.open_session()?;
if let Some(env) = &exec.env {
@ -694,10 +691,9 @@ impl SessionInner {
],
};
exec.reply.try_send(result)?;
self.channels.insert(channel_id, info);
Ok(())
Ok(result)
}
/// Open a handle to a file.