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(_) => 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
81#[cfg(feature = "alloc")]
82impl SSHServer<'static> {
83 pub fn new_owned() -> Self {
84 let runner = Runner::new_server_owned();
85 let sunset = AsyncSunset::new(runner);
86 Self { sunset }
87 }
88}
89
90#[cfg(feature = "futures-io")]
91impl SSHServer<'_> {
92 pub async fn run_futures_io(
93 &self,
94 rsock: &mut (impl futures_io::AsyncRead + Unpin),
95 wsock: &mut (impl futures_io::AsyncWrite + Unpin),
96 ) -> Result<()> {
97 let mut rsock = embedded_io_adapters::futures_03::FromFutures::new(rsock);
98 let mut wsock = embedded_io_adapters::futures_03::FromFutures::new(wsock);
99 self.sunset.run(&mut rsock, &mut wsock).await
100 }
101}
102
103#[cfg(feature = "tokio")]
104impl SSHServer<'_> {
105 pub async fn run_tokio(
106 &self,
107 rsock: &mut (impl tokio::io::AsyncRead + Unpin),
108 wsock: &mut (impl tokio::io::AsyncWrite + Unpin),
109 ) -> Result<()> {
110 let mut rsock = embedded_io_adapters::tokio_1::FromTokio::new(rsock);
111 let mut wsock = embedded_io_adapters::tokio_1::FromTokio::new(wsock);
112 self.sunset.run(&mut rsock, &mut wsock).await
113 }
114}