| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
use std::io; |
| 8 |
|
| 9 |
use tokio::sync::mpsc; |
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
pub struct TerminalHandle { |
| 14 |
sink: Vec<u8>, |
| 15 |
tx: mpsc::Sender<Vec<u8>>, |
| 16 |
} |
| 17 |
|
| 18 |
impl TerminalHandle { |
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
pub fn new( |
| 24 |
session_handle: russh::server::Handle, |
| 25 |
channel_id: russh::ChannelId, |
| 26 |
) -> Self { |
| 27 |
let (tx, mut rx) = mpsc::channel::<Vec<u8>>(64); |
| 28 |
|
| 29 |
tokio::spawn(async move { |
| 30 |
while let Some(data) = rx.recv().await { |
| 31 |
|
| 32 |
if session_handle.data(channel_id, data).await.is_err() { |
| 33 |
break; |
| 34 |
} |
| 35 |
} |
| 36 |
}); |
| 37 |
|
| 38 |
Self { |
| 39 |
sink: Vec::with_capacity(4096), |
| 40 |
tx, |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
impl io::Write for TerminalHandle { |
| 46 |
fn write(&mut self, buf: &[u8]) -> io::Result<usize> { |
| 47 |
self.sink.extend_from_slice(buf); |
| 48 |
Ok(buf.len()) |
| 49 |
} |
| 50 |
|
| 51 |
fn flush(&mut self) -> io::Result<()> { |
| 52 |
if self.sink.is_empty() { |
| 53 |
return Ok(()); |
| 54 |
} |
| 55 |
let data = std::mem::take(&mut self.sink); |
| 56 |
self.tx |
| 57 |
.try_send(data) |
| 58 |
.map_err(|e| io::Error::new(io::ErrorKind::BrokenPipe, e))?; |
| 59 |
Ok(()) |
| 60 |
} |
| 61 |
} |
| 62 |
|