async_ssh2_lite/
listener.rs

1use core::time::Duration;
2use std::sync::Arc;
3
4use ssh2::{BlockDirections, Listener, Session};
5
6use crate::{channel::AsyncChannel, error::Error, session_stream::AsyncSessionStream};
7
8//
9pub struct AsyncListener<S> {
10    inner: Listener,
11    sess: Session,
12    stream: Arc<S>,
13}
14
15impl<S> AsyncListener<S> {
16    pub(crate) fn from_parts(inner: Listener, sess: Session, stream: Arc<S>) -> Self {
17        Self {
18            inner,
19            sess,
20            stream,
21        }
22    }
23}
24
25impl<S> AsyncListener<S>
26where
27    S: AsyncSessionStream + Send + Sync + 'static,
28{
29    pub async fn accept(&mut self) -> Result<AsyncChannel<S>, Error> {
30        let channel = self
31            .stream
32            .x_with(
33                || self.inner.accept(),
34                &self.sess,
35                BlockDirections::Both,
36                Some(Duration::from_millis(10)),
37            )
38            .await?;
39
40        Ok(AsyncChannel::from_parts(
41            channel,
42            self.sess.clone(),
43            self.stream.clone(),
44        ))
45    }
46}