use embedded_io_async::{Read, Write};
use sunset::*;
use crate::*;
use async_sunset::{AsyncSunset, ProgressHolder};
#[derive(Debug)]
pub struct SSHServer<'a> {
sunset: AsyncSunset<'a, sunset::Server>,
}
impl<'a> SSHServer<'a> {
pub fn new(inbuf: &'a mut [u8], outbuf: &'a mut [u8]) -> Self {
let runner = Runner::new_server(inbuf, outbuf);
let sunset = AsyncSunset::new(runner);
Self { sunset }
}
pub async fn run(
&self,
rsock: &mut impl Read,
wsock: &mut impl Write,
) -> Result<()> {
self.sunset.run(rsock, wsock).await
}
pub async fn progress<'g, 'f>(
&'g self,
ph: &'f mut ProgressHolder<'g, 'a, sunset::Server>,
) -> Result<ServEvent<'f, 'a>> {
match self.sunset.progress(ph).await? {
Event::Serv(x) => Ok(x),
Event::None => Ok(ServEvent::PollAgain),
Event::Progressed => Ok(ServEvent::PollAgain),
Event::Cli(_) => Err(Error::bug()),
}
}
pub async fn stdio(&self, ch: ChanHandle) -> Result<ChanInOut<'_>> {
Ok(ChanInOut::new(self.sunset.add_channel(ch).await?))
}
pub async fn stdio_stderr(
&self,
ch: ChanHandle,
) -> Result<(ChanInOut<'_>, ChanOut<'_>)> {
let io_normal = self.sunset.add_channel(ch).await?;
let e = ChanOut::new(io_normal.clone_stderr());
let i = ChanInOut::new(io_normal);
Ok((i, e))
}
}