From 1772198a4aa42c06519f62bff8f304d51955740b Mon Sep 17 00:00:00 2001 From: Joe Grund Date: Tue, 18 Oct 2022 16:41:38 -0400 Subject: [PATCH] Implement detached signal sender Allow signals to be sent detached from the channel struct. This is done using a higher-order function and by cloning the sender so it can be used independently of the `Channel`. This is useful to be able to spawn a cancelation handler separately from the main `Channel` recieve loop. Signed-off-by: Joe Grund --- russh/src/channels.rs | 32 ++++++++++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/russh/src/channels.rs b/russh/src/channels.rs index 227d0e1c..4dff63a9 100644 --- a/russh/src/channels.rs +++ b/russh/src/channels.rs @@ -174,11 +174,39 @@ impl> Channel { } /// Signal a remote process. - pub async fn signal(&mut self, signal: Sig) -> Result<(), Error> { - self.send_msg(ChannelMsg::Signal { signal }).await?; + pub async fn signal(&self, signal: Sig) -> Result<(), Error> { + self.sender + .sender + .send(Msg::Signal { + id: self.sender.id, + signal, + }) + .await + .map_err(|_| Error::SendError)?; Ok(()) } + /// Get a `FnOnce` that can be used to send a signal through this channel + pub fn get_signal_sender( + &self, + ) -> impl FnOnce(Sig) -> Pin> + Send>> { + let sender = self.sender.clone(); + let id = self.sender.id; + + move |signal| { + async move { + sender + .sender + .send(Msg::Signal { id, signal }) + .await + .map_err(|_| Error::SendError)?; + + Ok(()) + } + .boxed() + } + } + /// Request the start of a subsystem with the given name. pub async fn request_subsystem>( &mut self,