1use embedded_io_async::{Read, Write};
2
3use sunset::*;
4
5use crate::*;
6use async_sunset::{AsyncSunset, ProgressHolder};
7
8#[derive(Debug)]
18pub struct SSHServer<'a> {
19 sunset: AsyncSunset<'a, sunset::Server>,
20}
21
22impl<'a> SSHServer<'a> {
23 pub fn new(inbuf: &'a mut [u8], outbuf: &'a mut [u8]) -> Self {
25 let runner = Runner::new_server(inbuf, outbuf);
26 let sunset = AsyncSunset::new(runner);
27 Self { sunset }
28 }
29
30 pub async fn run(
34 &self,
35 rsock: &mut impl Read,
36 wsock: &mut impl Write,
37 ) -> Result<()> {
38 self.sunset.run(rsock, wsock).await
39 }
40
41 pub async fn progress<'g, 'f>(
47 &'g self,
48 ph: &'f mut ProgressHolder<'g, 'a, sunset::Server>,
49 ) -> Result<ServEvent<'f, 'a>> {
50 match self.sunset.progress(ph).await? {
52 Event::Serv(x) => Ok(x),
53 Event::None => Ok(ServEvent::PollAgain),
54 Event::Progressed => Ok(ServEvent::PollAgain),
55 Event::Cli(_) => Err(Error::bug()),
56 }
57 }
58
59 pub async fn stdio(&self, ch: ChanHandle) -> Result<ChanInOut<'_>> {
64 Ok(ChanInOut::new(self.sunset.add_channel(ch).await?))
65 }
66
67 pub async fn stdio_stderr(
71 &self,
72 ch: ChanHandle,
73 ) -> Result<(ChanInOut<'_>, ChanOut<'_>)> {
74 let io_normal = self.sunset.add_channel(ch).await?;
75 let e = ChanOut::new(io_normal.clone_stderr());
76 let i = ChanInOut::new(io_normal);
77 Ok((i, e))
78 }
79}
80#[cfg(feature = "alloc")]
81impl SSHServer<'static> {
82 pub fn new_owned() -> Self {
83 let runner = Runner::new_server_owned();
84 let sunset = AsyncSunset::new(runner);
85 Self { sunset }
86 }
87}